Skip to content

Commit 405441d

Browse files
committed
feat(routing-table): provide rt and routes functionality
1 parent 9896dba commit 405441d

13 files changed

Lines changed: 938 additions & 0 deletions

File tree

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ require (
2121
github.com/stackitcloud/stackit-sdk-go/services/dns v0.17.1
2222
github.com/stackitcloud/stackit-sdk-go/services/git v0.7.1
2323
github.com/stackitcloud/stackit-sdk-go/services/iaas v0.30.0
24+
github.com/stackitcloud/stackit-sdk-go/services/iaasalpha v0.1.21-alpha
2425
github.com/stackitcloud/stackit-sdk-go/services/mongodbflex v1.5.2
2526
github.com/stackitcloud/stackit-sdk-go/services/opensearch v0.24.1
2627
github.com/stackitcloud/stackit-sdk-go/services/postgresflex v1.2.1

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -573,6 +573,8 @@ github.com/stackitcloud/stackit-sdk-go/services/git v0.7.1 h1:hkFixFnBcQzU4BSIZF
573573
github.com/stackitcloud/stackit-sdk-go/services/git v0.7.1/go.mod h1:Ng1EzrRndG3iGXGH90AZJz//wfK+2YOyDwTnTLwX3a4=
574574
github.com/stackitcloud/stackit-sdk-go/services/iaas v0.30.0 h1:01+noyCSadNH3ALHufcVXxNs0hBsetzJkOMN1Fe0VLc=
575575
github.com/stackitcloud/stackit-sdk-go/services/iaas v0.30.0/go.mod h1:854gnLR92NvAbJAA1xZEumrtNh1DoBP1FXTMvhwYA6w=
576+
github.com/stackitcloud/stackit-sdk-go/services/iaasalpha v0.1.21-alpha h1:m1jq6a8dbUe+suFuUNdHmM/cSehpGLUtDbK1CqLqydg=
577+
github.com/stackitcloud/stackit-sdk-go/services/iaasalpha v0.1.21-alpha/go.mod h1:Nu1b5Phsv8plgZ51+fkxPVsU91ZJ5Ayz+cthilxdmQ8=
576578
github.com/stackitcloud/stackit-sdk-go/services/loadbalancer v1.5.1 h1:OdJEs8eOfrzn9tCBDLxIyP8hX50zPfcXNYnRoQX+chs=
577579
github.com/stackitcloud/stackit-sdk-go/services/loadbalancer v1.5.1/go.mod h1:11uzaOPCF9SeDHXEGOPMlHDD3J5r2TnvCGUwW9Igq9c=
578580
github.com/stackitcloud/stackit-sdk-go/services/logme v0.25.1 h1:hv5WrRU9rN6Jx4OwdOGJRyaQrfA9p1tzEoQK6/CDyoA=

internal/cmd/beta/beta.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55

66
"github.com/stackitcloud/stackit-cli/internal/cmd/beta/alb"
7+
"github.com/stackitcloud/stackit-cli/internal/cmd/beta/routingtable"
78
"github.com/stackitcloud/stackit-cli/internal/cmd/beta/sqlserverflex"
89
"github.com/stackitcloud/stackit-cli/internal/cmd/params"
910
"github.com/stackitcloud/stackit-cli/internal/pkg/args"
@@ -38,4 +39,5 @@ func NewCmd(params *params.CmdParams) *cobra.Command {
3839
func addSubcommands(cmd *cobra.Command, params *params.CmdParams) {
3940
cmd.AddCommand(sqlserverflex.NewCmd(params))
4041
cmd.AddCommand(alb.NewCmd(params))
42+
cmd.AddCommand(routingtable.NewCmd(params))
4143
}
Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
package describe
2+
3+
import (
4+
"context"
5+
"encoding/json"
6+
"fmt"
7+
"strings"
8+
9+
"github.com/goccy/go-yaml"
10+
"github.com/spf13/cobra"
11+
"github.com/stackitcloud/stackit-cli/internal/cmd/params"
12+
"github.com/stackitcloud/stackit-cli/internal/pkg/args"
13+
"github.com/stackitcloud/stackit-cli/internal/pkg/errors"
14+
"github.com/stackitcloud/stackit-cli/internal/pkg/examples"
15+
"github.com/stackitcloud/stackit-cli/internal/pkg/flags"
16+
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
17+
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
18+
"github.com/stackitcloud/stackit-cli/internal/pkg/services/iaas/client"
19+
"github.com/stackitcloud/stackit-cli/internal/pkg/tables"
20+
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"
21+
"github.com/stackitcloud/stackit-sdk-go/services/iaasalpha"
22+
)
23+
24+
const (
25+
organizationIdFlag = "organization-id"
26+
networkAreaIdFlag = "network-area-id"
27+
routingTableArg = "ROUTING_TABLE_ID_ARG"
28+
)
29+
30+
type inputModel struct {
31+
*globalflags.GlobalFlagModel
32+
OrganizationId *string
33+
NetworkAreaId *string
34+
RoutingTableId *string
35+
}
36+
37+
func NewCmd(params *params.CmdParams) *cobra.Command {
38+
cmd := &cobra.Command{
39+
Use: fmt.Sprintf("describe %s", routingTableArg),
40+
Short: "Describe a routing-table",
41+
Long: "Describe a routing-table",
42+
Args: args.SingleArg(routingTableArg, nil),
43+
Example: examples.Build(
44+
examples.NewExample(
45+
`Describe a routing-table`,
46+
`$ stackit beta routing-table describe xxxx-xxxx-xxxx-xxxx`,
47+
),
48+
),
49+
RunE: func(cmd *cobra.Command, args []string) error {
50+
ctx := context.Background()
51+
model, err := parseInput(params.Printer, cmd, args)
52+
if err != nil {
53+
return err
54+
}
55+
56+
// Configure API client
57+
apiClient, err := client.ConfigureAlphaClient(params.Printer, params.CliVersion)
58+
if err != nil {
59+
return err
60+
}
61+
62+
// Call API
63+
request := apiClient.GetRoutingTableOfArea(
64+
ctx,
65+
*model.OrganizationId,
66+
*model.NetworkAreaId,
67+
model.Region,
68+
*model.RoutingTableId,
69+
)
70+
71+
response, err := request.Execute()
72+
if err != nil {
73+
return fmt.Errorf("describe routing-tables: %w", err)
74+
}
75+
76+
return outputResult(params.Printer, model.OutputFormat, response)
77+
},
78+
}
79+
80+
configureFlags(cmd)
81+
return cmd
82+
}
83+
84+
func configureFlags(cmd *cobra.Command) {
85+
cmd.Flags().Var(flags.UUIDFlag(), organizationIdFlag, "Organization ID")
86+
cmd.Flags().Var(flags.UUIDFlag(), networkAreaIdFlag, "Network-Area ID")
87+
88+
err := flags.MarkFlagsRequired(cmd, organizationIdFlag, networkAreaIdFlag)
89+
cobra.CheckErr(err)
90+
}
91+
92+
func parseInput(p *print.Printer, cmd *cobra.Command, args []string) (*inputModel, error) {
93+
globalFlags := globalflags.Parse(p, cmd)
94+
if globalFlags.ProjectId == "" {
95+
return nil, &errors.ProjectIdError{}
96+
}
97+
98+
if len(args) <= 0 {
99+
return nil, fmt.Errorf("at least one argument is required")
100+
}
101+
routingTableId := args[0]
102+
103+
model := inputModel{
104+
GlobalFlagModel: globalFlags,
105+
NetworkAreaId: flags.FlagToStringPointer(p, cmd, networkAreaIdFlag),
106+
OrganizationId: flags.FlagToStringPointer(p, cmd, organizationIdFlag),
107+
RoutingTableId: &routingTableId,
108+
}
109+
110+
if p.IsVerbosityDebug() {
111+
modelStr, err := print.BuildDebugStrFromInputModel(model)
112+
if err != nil {
113+
p.Debug(print.ErrorLevel, "convert model to string for debugging: %v", err)
114+
} else {
115+
p.Debug(print.DebugLevel, "parsed input values: %s", modelStr)
116+
}
117+
}
118+
119+
return &model, nil
120+
}
121+
122+
func outputResult(p *print.Printer, outputFormat string, routingTable *iaasalpha.RoutingTable) error {
123+
switch outputFormat {
124+
case print.JSONOutputFormat:
125+
details, err := json.MarshalIndent(routingTable, "", " ")
126+
if err != nil {
127+
return fmt.Errorf("marshal routing-table describe: %w", err)
128+
}
129+
p.Outputln(string(details))
130+
131+
return nil
132+
case print.YAMLOutputFormat:
133+
details, err := yaml.MarshalWithOptions(routingTable, yaml.IndentSequence(true), yaml.UseJSONMarshaler())
134+
if err != nil {
135+
return fmt.Errorf("marshal routing-table describe: %w", err)
136+
}
137+
p.Outputln(string(details))
138+
139+
return nil
140+
default:
141+
var labels []string
142+
for key, value := range *routingTable.Labels {
143+
labels = append(labels, fmt.Sprintf("%s: %s", key, value))
144+
}
145+
146+
table := tables.NewTable()
147+
table.SetHeader("ID", "NAME", "DESCRIPTION", "CREATED_AT", "UPDATED_AT", "DEFAULT", "LABELS", "SYSTEM_ROUTES")
148+
table.AddRow(
149+
utils.PtrString(routingTable.Id),
150+
utils.PtrString(routingTable.Name),
151+
utils.PtrString(routingTable.Description),
152+
routingTable.CreatedAt.String(),
153+
routingTable.UpdatedAt.String(),
154+
utils.PtrString(routingTable.Default),
155+
strings.Join(labels, "\n"),
156+
utils.PtrString(routingTable.SystemRoutes),
157+
)
158+
159+
err := table.Display(p)
160+
if err != nil {
161+
return fmt.Errorf("render table: %w", err)
162+
}
163+
164+
return nil
165+
}
166+
}

0 commit comments

Comments
 (0)