|
| 1 | +package status |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "github.com/spf13/cobra" |
| 8 | + vpn "github.com/stackitcloud/stackit-sdk-go/services/vpn/v1api" |
| 9 | + |
| 10 | + "github.com/stackitcloud/stackit-cli/internal/pkg/args" |
| 11 | + "github.com/stackitcloud/stackit-cli/internal/pkg/errors" |
| 12 | + "github.com/stackitcloud/stackit-cli/internal/pkg/examples" |
| 13 | + "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" |
| 14 | + "github.com/stackitcloud/stackit-cli/internal/pkg/print" |
| 15 | + "github.com/stackitcloud/stackit-cli/internal/pkg/projectname" |
| 16 | + "github.com/stackitcloud/stackit-cli/internal/pkg/services/vpn/client" |
| 17 | + "github.com/stackitcloud/stackit-cli/internal/pkg/tables" |
| 18 | + "github.com/stackitcloud/stackit-cli/internal/pkg/types" |
| 19 | + "github.com/stackitcloud/stackit-cli/internal/pkg/utils" |
| 20 | +) |
| 21 | + |
| 22 | +const ( |
| 23 | + gatewayIdArg = "GATEWAY_ID" |
| 24 | +) |
| 25 | + |
| 26 | +type inputModel struct { |
| 27 | + *globalflags.GlobalFlagModel |
| 28 | + GatewayId string |
| 29 | +} |
| 30 | + |
| 31 | +func NewCmd(params *types.CmdParams) *cobra.Command { |
| 32 | + cmd := &cobra.Command{ |
| 33 | + Use: fmt.Sprintf("status %s", gatewayIdArg), |
| 34 | + Short: "Shows the status of a gateway", |
| 35 | + Long: "Shows the status of a gateway.", |
| 36 | + Args: args.SingleArg(gatewayIdArg, utils.ValidateUUID), |
| 37 | + Example: examples.Build( |
| 38 | + examples.NewExample( |
| 39 | + `Show the status of the gateway with the ID "xxx"`, |
| 40 | + "$ stackit beta vpn gateway describe xxx", |
| 41 | + ), |
| 42 | + ), |
| 43 | + RunE: func(cmd *cobra.Command, inputArgs []string) error { |
| 44 | + ctx := context.Background() |
| 45 | + model, err := parseInput(params.Printer, cmd, inputArgs) |
| 46 | + if err != nil { |
| 47 | + return fmt.Errorf("unable to parse input: %w", err) |
| 48 | + } |
| 49 | + |
| 50 | + // Configure API client |
| 51 | + apiClient, err := client.ConfigureClient(params.Printer, params.CliVersion) |
| 52 | + if err != nil { |
| 53 | + return err |
| 54 | + } |
| 55 | + |
| 56 | + // Call API |
| 57 | + req := buildRequest(ctx, model, apiClient) |
| 58 | + resp, err := req.Execute() |
| 59 | + if err != nil { |
| 60 | + return fmt.Errorf("describe vpn gateway: %w", err) |
| 61 | + } |
| 62 | + |
| 63 | + projectLabel, err := projectname.GetProjectName(ctx, params.Printer, params.CliVersion, cmd) |
| 64 | + if err != nil || projectLabel == "" { |
| 65 | + projectLabel = model.ProjectId |
| 66 | + } |
| 67 | + |
| 68 | + return outputResult(params.Printer, model.OutputFormat, model.GatewayId, projectLabel, resp) |
| 69 | + }, |
| 70 | + } |
| 71 | + return cmd |
| 72 | +} |
| 73 | + |
| 74 | +func parseInput(p *print.Printer, cmd *cobra.Command, inputArgs []string) (*inputModel, error) { |
| 75 | + gatewayId := inputArgs[0] |
| 76 | + globalFlags := globalflags.Parse(p, cmd) |
| 77 | + if globalFlags.ProjectId == "" { |
| 78 | + return nil, &errors.ProjectIdError{} |
| 79 | + } |
| 80 | + |
| 81 | + model := inputModel{ |
| 82 | + GlobalFlagModel: globalFlags, |
| 83 | + GatewayId: gatewayId, |
| 84 | + } |
| 85 | + |
| 86 | + p.DebugInputModel(model) |
| 87 | + return &model, nil |
| 88 | +} |
| 89 | + |
| 90 | +func buildRequest(ctx context.Context, model *inputModel, apiClient *vpn.APIClient) vpn.ApiGetGatewayStatusRequest { |
| 91 | + return apiClient.DefaultAPI.GetGatewayStatus(ctx, model.ProjectId, model.Region, model.GatewayId) |
| 92 | +} |
| 93 | + |
| 94 | +func outputResult(p *print.Printer, outputFormat, gatewayId, projectLabel string, gateway *vpn.GatewayStatusResponse) error { |
| 95 | + return p.OutputResult(outputFormat, gateway, func() error { |
| 96 | + if gateway == nil { |
| 97 | + p.Outputf("gateway %q not found in project %q\n", gatewayId, projectLabel) |
| 98 | + return nil |
| 99 | + } |
| 100 | + |
| 101 | + mainTable := tables.NewTable() |
| 102 | + mainTable.SetTitle("Gateway Status") |
| 103 | + |
| 104 | + mainTable.AddRow("ID", gateway.GetId()) |
| 105 | + mainTable.AddSeparator() |
| 106 | + mainTable.AddRow("NAME", gateway.GetDisplayName()) |
| 107 | + mainTable.AddSeparator() |
| 108 | + mainTable.AddRow("STATUS", gateway.GetGatewayStatus()) |
| 109 | + if gateway.ErrorMessage != nil { |
| 110 | + mainTable.AddSeparator() |
| 111 | + mainTable.AddRow("ERROR MESSAGE", *gateway.ErrorMessage) |
| 112 | + } |
| 113 | + |
| 114 | + ts := []tables.Table{ |
| 115 | + mainTable, |
| 116 | + } |
| 117 | + for _, tunnel := range gateway.Tunnels { |
| 118 | + ts = append(ts, tunnelTable(tunnel)) |
| 119 | + } |
| 120 | + |
| 121 | + return tables.DisplayTables(p, ts) |
| 122 | + }) |
| 123 | +} |
| 124 | + |
| 125 | +func tunnelTable(tunnel vpn.VPNTunnels) tables.Table { |
| 126 | + title := "Tunnel" |
| 127 | + if tunnel.Name != nil { |
| 128 | + title = string(*tunnel.Name) |
| 129 | + } |
| 130 | + |
| 131 | + table := tables.NewTable() |
| 132 | + table.SetTitle(title) |
| 133 | + |
| 134 | + table.AddSeparator() |
| 135 | + table.AddRow("PUBLIC IP", tunnel.GetPublicIP()) |
| 136 | + table.AddSeparator() |
| 137 | + table.AddRow("INTERNAL NEXT HOP IP", tunnel.GetInternalNextHopIP()) |
| 138 | + table.AddSeparator() |
| 139 | + table.AddRow("STATE", tunnel.GetInstanceState()) |
| 140 | + |
| 141 | + if tunnel.BgpStatus.IsSet() { |
| 142 | + table.AddSeparator() |
| 143 | + routeString := "" |
| 144 | + for _, route := range tunnel.BgpStatus.Get().Routes { |
| 145 | + if route.Network != "" { |
| 146 | + routeString += fmt.Sprintf("Network: %s; ", route.Network) |
| 147 | + } |
| 148 | + if route.Origin != "" { |
| 149 | + routeString += fmt.Sprintf("Origin: %s; ", route.Origin) |
| 150 | + } |
| 151 | + if route.Path != "" { |
| 152 | + routeString += fmt.Sprintf("Path: %s; ", route.Path) |
| 153 | + } |
| 154 | + if route.PeerId != "" { |
| 155 | + routeString += fmt.Sprintf("PeerId: %s; ", route.PeerId) |
| 156 | + } |
| 157 | + routeString += fmt.Sprintf("Weight: %d\n", route.Weight) |
| 158 | + } |
| 159 | + table.AddRow("BGP Routes", routeString) |
| 160 | + table.AddSeparator() |
| 161 | + bgpPeers := "" |
| 162 | + for _, peer := range tunnel.BgpStatus.Get().Peers { |
| 163 | + if peer.PeerUptime != "" { |
| 164 | + bgpPeers += fmt.Sprintf("PeerUptime: %s; ", peer.PeerUptime) |
| 165 | + } |
| 166 | + if peer.RemoteIP != "" { |
| 167 | + bgpPeers += fmt.Sprintf("RemoteIP: %s; ", peer.RemoteIP) |
| 168 | + } |
| 169 | + if peer.State != "" { |
| 170 | + bgpPeers += fmt.Sprintf("State: %s; ", peer.State) |
| 171 | + } |
| 172 | + bgpPeers += fmt.Sprintf("LocalAsn: %d; ", peer.LocalAs) |
| 173 | + bgpPeers += fmt.Sprintf("PfxRcd: %d; ", peer.PfxRcd) |
| 174 | + bgpPeers += fmt.Sprintf("PfxSnt: %d; ", peer.PfxSnt) |
| 175 | + bgpPeers += fmt.Sprintf("RemoteAs: %d\n", peer.RemoteAs) |
| 176 | + } |
| 177 | + table.AddRow("BGP Peers", bgpPeers) |
| 178 | + } |
| 179 | + |
| 180 | + return table |
| 181 | +} |
0 commit comments