Skip to content

Commit 9bfa88a

Browse files
committed
add status endpoint
1 parent f0fe08f commit 9bfa88a

9 files changed

Lines changed: 440 additions & 4 deletions

File tree

docs/stackit_beta_vpn_gateway.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,5 @@ stackit beta vpn gateway [flags]
3434
* [stackit beta vpn gateway delete](./stackit_beta_vpn_gateway_delete.md) - Deletes a vpn gateway
3535
* [stackit beta vpn gateway describe](./stackit_beta_vpn_gateway_describe.md) - Shows details of a gateway
3636
* [stackit beta vpn gateway list](./stackit_beta_vpn_gateway_list.md) - Lists all vpn gateways
37+
* [stackit beta vpn gateway status](./stackit_beta_vpn_gateway_status.md) - Shows the status of a gateway
3738

docs/stackit_beta_vpn_gateway_delete.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ stackit beta vpn gateway delete GATEWAY_ID [flags]
1313
### Examples
1414

1515
```
16-
Delete a vpn gateway with the ID "xxx"
16+
Delete the vpn gateway with the ID "xxx"
1717
$ stackit beta vpn gateway delete xxx
1818
```
1919

docs/stackit_beta_vpn_gateway_describe.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ stackit beta vpn gateway describe GATEWAY_ID [flags]
1313
### Examples
1414

1515
```
16-
Describe a gateway with the ID "xxx"
16+
Describe the gateway with the ID "xxx"
1717
$ stackit beta vpn gateway describe xxx
1818
```
1919

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
## stackit beta vpn gateway status
2+
3+
Shows the status of a gateway
4+
5+
### Synopsis
6+
7+
Shows the status of a gateway.
8+
9+
```
10+
stackit beta vpn gateway status GATEWAY_ID [flags]
11+
```
12+
13+
### Examples
14+
15+
```
16+
Show the status of the gateway with the ID "xxx"
17+
$ stackit beta vpn gateway describe xxx
18+
```
19+
20+
### Options
21+
22+
```
23+
-h, --help Help for "stackit beta vpn gateway status"
24+
```
25+
26+
### Options inherited from parent commands
27+
28+
```
29+
-y, --assume-yes If set, skips all confirmation prompts
30+
--async If set, runs the command asynchronously
31+
-o, --output-format string Output format, (one of: [json, pretty, none, yaml])
32+
-p, --project-id string Project ID
33+
--region string Target region for region-specific requests
34+
--verbosity string Verbosity of the CLI, (one of: [debug, info, warning, error]) (default "info")
35+
```
36+
37+
### SEE ALSO
38+
39+
* [stackit beta vpn gateway](./stackit_beta_vpn_gateway.md) - Provides functionality for VPN gateway
40+

internal/cmd/beta/vpn/gateway/delete/delete.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func NewCmd(params *types.CmdParams) *cobra.Command {
3737
Args: args.SingleArg(gatewayIdArg, utils.ValidateUUID),
3838
Example: examples.Build(
3939
examples.NewExample(
40-
`Delete a vpn gateway with the ID "xxx"`,
40+
`Delete the vpn gateway with the ID "xxx"`,
4141
"$ stackit beta vpn gateway delete xxx",
4242
),
4343
),

internal/cmd/beta/vpn/gateway/describe/describe.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func NewCmd(params *types.CmdParams) *cobra.Command {
3636
Args: args.SingleArg(gatewayIdArg, utils.ValidateUUID),
3737
Example: examples.Build(
3838
examples.NewExample(
39-
`Describe a gateway with the ID "xxx"`,
39+
`Describe the gateway with the ID "xxx"`,
4040
"$ stackit beta vpn gateway describe xxx",
4141
),
4242
),

internal/cmd/beta/vpn/gateway/gateway.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"github.com/stackitcloud/stackit-cli/internal/cmd/beta/vpn/gateway/delete"
66
"github.com/stackitcloud/stackit-cli/internal/cmd/beta/vpn/gateway/describe"
77
"github.com/stackitcloud/stackit-cli/internal/cmd/beta/vpn/gateway/list"
8+
"github.com/stackitcloud/stackit-cli/internal/cmd/beta/vpn/gateway/status"
89
"github.com/stackitcloud/stackit-cli/internal/pkg/args"
910
"github.com/stackitcloud/stackit-cli/internal/pkg/types"
1011
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"
@@ -29,4 +30,5 @@ func addSubcommands(cmd *cobra.Command, params *types.CmdParams) {
2930
cmd.AddCommand(delete.NewCmd(params))
3031
cmd.AddCommand(describe.NewCmd(params))
3132
cmd.AddCommand(list.NewCmd(params))
33+
cmd.AddCommand(status.NewCmd(params))
3234
}
Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
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

Comments
 (0)