Skip to content

Commit b494e0d

Browse files
committed
Extend network-area route create command to support labels as well
Labels are optional and can now be provided directly during creation of the route. Signed-off-by: Alexander Dahmen <[email protected]>
1 parent cb2f7d9 commit b494e0d

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

docs/stackit_beta_network-area_route_create.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,16 @@ stackit beta network-area route create [flags]
1717
```
1818
Create a static route with prefix "1.1.1.0/24" and next hop "1.1.1.1" in a STACKIT Network Area with ID "xxx" in organization with ID "yyy"
1919
$ stackit beta network-area route create --organization-id yyy --network-area-id xxx --prefix 1.1.1.0/24 --next-hop 1.1.1.1
20+
21+
Create a static route with labels "key:value" and "foo:bar" with prefix "1.1.1.0/24" and next hop "1.1.1.1" in a STACKIT Network Area with ID "xxx" in organization with ID "yyy"
22+
$ stackit beta network-area route create --labels key=value,foo=bar --organization-id yyy --network-area-id xxx --prefix 1.1.1.0/24 --next-hop 1.1.1.1
2023
```
2124

2225
### Options
2326

2427
```
2528
-h, --help Help for "stackit beta network-area route create"
29+
--labels stringToString Labels are key-value string pairs which can be attached to a route. A label can be provided with the format key=value and the flag can be used multiple times to provide a list of labels (default [])
2630
--network-area-id string STACKIT Network Area ID
2731
--next-hop string Next hop IP address. Must be a valid IPv4
2832
--organization-id string Organization ID

internal/cmd/beta/network-area/route/create/create.go

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ import (
1212
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
1313
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
1414
"github.com/stackitcloud/stackit-cli/internal/pkg/services/iaas/client"
15-
"github.com/stackitcloud/stackit-cli/internal/pkg/services/iaas/utils"
15+
iaasUtils "github.com/stackitcloud/stackit-cli/internal/pkg/services/iaas/utils"
16+
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"
1617
"github.com/stackitcloud/stackit-sdk-go/services/iaas"
1718

1819
"github.com/spf13/cobra"
@@ -23,6 +24,7 @@ const (
2324
networkAreaIdFlag = "network-area-id"
2425
prefixFlag = "prefix"
2526
nexthopFlag = "next-hop"
27+
labelFlag = "labels"
2628
)
2729

2830
type inputModel struct {
@@ -31,6 +33,7 @@ type inputModel struct {
3133
NetworkAreaId *string
3234
Prefix *string
3335
Nexthop *string
36+
Labels *map[string]string
3437
}
3538

3639
func NewCmd(p *print.Printer) *cobra.Command {
@@ -47,6 +50,10 @@ func NewCmd(p *print.Printer) *cobra.Command {
4750
`Create a static route with prefix "1.1.1.0/24" and next hop "1.1.1.1" in a STACKIT Network Area with ID "xxx" in organization with ID "yyy"`,
4851
"$ stackit beta network-area route create --organization-id yyy --network-area-id xxx --prefix 1.1.1.0/24 --next-hop 1.1.1.1",
4952
),
53+
examples.NewExample(
54+
`Create a static route with labels "key:value" and "foo:bar" with prefix "1.1.1.0/24" and next hop "1.1.1.1" in a STACKIT Network Area with ID "xxx" in organization with ID "yyy"`,
55+
"$ stackit beta network-area route create --labels key=value,foo=bar --organization-id yyy --network-area-id xxx --prefix 1.1.1.0/24 --next-hop 1.1.1.1",
56+
),
5057
),
5158
RunE: func(cmd *cobra.Command, args []string) error {
5259
ctx := context.Background()
@@ -62,7 +69,7 @@ func NewCmd(p *print.Printer) *cobra.Command {
6269
}
6370

6471
// Get network area label
65-
networkAreaLabel, err := utils.GetNetworkAreaName(ctx, apiClient, *model.OrganizationId, *model.NetworkAreaId)
72+
networkAreaLabel, err := iaasUtils.GetNetworkAreaName(ctx, apiClient, *model.OrganizationId, *model.NetworkAreaId)
6673
if err != nil {
6774
p.Debug(print.ErrorLevel, "get network area name: %v", err)
6875
networkAreaLabel = *model.NetworkAreaId
@@ -87,7 +94,7 @@ func NewCmd(p *print.Printer) *cobra.Command {
8794
return fmt.Errorf("empty response from API")
8895
}
8996

90-
route, err := utils.GetRouteFromAPIResponse(*model.Prefix, *model.Nexthop, resp.Items)
97+
route, err := iaasUtils.GetRouteFromAPIResponse(*model.Prefix, *model.Nexthop, resp.Items)
9198
if err != nil {
9299
return err
93100
}
@@ -104,6 +111,7 @@ func configureFlags(cmd *cobra.Command) {
104111
cmd.Flags().Var(flags.UUIDFlag(), networkAreaIdFlag, "STACKIT Network Area ID")
105112
cmd.Flags().Var(flags.CIDRFlag(), prefixFlag, "Static route prefix")
106113
cmd.Flags().String(nexthopFlag, "", "Next hop IP address. Must be a valid IPv4")
114+
cmd.Flags().StringToString(labelFlag, nil, "Labels are key-value string pairs which can be attached to a route. A label can be provided with the format key=value and the flag can be used multiple times to provide a list of labels")
107115

108116
err := flags.MarkFlagsRequired(cmd, organizationIdFlag, networkAreaIdFlag, prefixFlag, nexthopFlag)
109117
cobra.CheckErr(err)
@@ -118,6 +126,7 @@ func parseInput(p *print.Printer, cmd *cobra.Command) (*inputModel, error) {
118126
NetworkAreaId: flags.FlagToStringPointer(p, cmd, networkAreaIdFlag),
119127
Prefix: flags.FlagToStringPointer(p, cmd, prefixFlag),
120128
Nexthop: flags.FlagToStringPointer(p, cmd, nexthopFlag),
129+
Labels: flags.FlagToStringToStringPointer(p, cmd, labelFlag),
121130
}
122131

123132
if p.IsVerbosityDebug() {
@@ -134,11 +143,22 @@ func parseInput(p *print.Printer, cmd *cobra.Command) (*inputModel, error) {
134143

135144
func buildRequest(ctx context.Context, model *inputModel, apiClient *iaas.APIClient) iaas.ApiCreateNetworkAreaRouteRequest {
136145
req := apiClient.CreateNetworkAreaRoute(ctx, *model.OrganizationId, *model.NetworkAreaId)
146+
147+
var labelsMap *map[string]interface{}
148+
if model.Labels != nil && len(*model.Labels) > 0 {
149+
// convert map[string]string to map[string]interface{}
150+
labelsMap = utils.Ptr(map[string]interface{}{})
151+
for k, v := range *model.Labels {
152+
(*labelsMap)[k] = v
153+
}
154+
}
155+
137156
payload := iaas.CreateNetworkAreaRoutePayload{
138157
Ipv4: &[]iaas.Route{
139158
{
140159
Prefix: model.Prefix,
141160
Nexthop: model.Nexthop,
161+
Labels: labelsMap,
142162
},
143163
},
144164
}

0 commit comments

Comments
 (0)