Skip to content

Commit c382cf8

Browse files
committed
feat(public-ip): add command to list stackit ip-ranges
1 parent 9896dba commit c382cf8

File tree

7 files changed

+394
-0
lines changed

7 files changed

+394
-0
lines changed

docs/stackit_public-ip.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ stackit public-ip [flags]
3535
* [stackit public-ip delete](./stackit_public-ip_delete.md) - Deletes a Public IP
3636
* [stackit public-ip describe](./stackit_public-ip_describe.md) - Shows details of a Public IP
3737
* [stackit public-ip disassociate](./stackit_public-ip_disassociate.md) - Disassociates a Public IP from a network interface or a virtual IP
38+
* [stackit public-ip ip-ranges](./stackit_public-ip_ip-ranges.md) - Provides functionality for STACKIT Cloud public ip-ranges
3839
* [stackit public-ip list](./stackit_public-ip_list.md) - Lists all Public IPs of a project
3940
* [stackit public-ip update](./stackit_public-ip_update.md) - Updates a Public IP
4041

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
## stackit public-ip ip-ranges
2+
3+
Provides functionality for STACKIT Cloud public ip-ranges
4+
5+
### Synopsis
6+
7+
Provides functionality for STACKIT Cloud public ip-ranges
8+
9+
```
10+
stackit public-ip ip-ranges [flags]
11+
```
12+
13+
### Options
14+
15+
```
16+
-h, --help Help for "stackit public-ip ip-ranges"
17+
```
18+
19+
### Options inherited from parent commands
20+
21+
```
22+
-y, --assume-yes If set, skips all confirmation prompts
23+
--async If set, runs the command asynchronously
24+
-o, --output-format string Output format, one of ["json" "pretty" "none" "yaml"]
25+
-p, --project-id string Project ID
26+
--region string Target region for region-specific requests
27+
--verbosity string Verbosity of the CLI, one of ["debug" "info" "warning" "error"] (default "info")
28+
```
29+
30+
### SEE ALSO
31+
32+
* [stackit public-ip](./stackit_public-ip.md) - Provides functionality for public IPs
33+
* [stackit public-ip ip-ranges list](./stackit_public-ip_ip-ranges_list.md) - Lists all STACKIT cloud public IP ranges.
34+
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
## stackit public-ip ip-ranges list
2+
3+
Lists all STACKIT cloud public IP ranges.
4+
5+
### Synopsis
6+
7+
Lists all STACKIT cloud public IP ranges.
8+
9+
```
10+
stackit public-ip ip-ranges list [flags]
11+
```
12+
13+
### Examples
14+
15+
```
16+
Lists all STACKIT cloud public IP ranges
17+
$ stackit public-ip ranges list
18+
19+
Lists all STACKIT cloud public IP ranges, piping to a tool like fzf for interactive selection
20+
$ stackit public-ip ip-ranges list -o pretty | fzf
21+
```
22+
23+
### Options
24+
25+
```
26+
-h, --help Help for "stackit public-ip ip-ranges list"
27+
```
28+
29+
### Options inherited from parent commands
30+
31+
```
32+
-y, --assume-yes If set, skips all confirmation prompts
33+
--async If set, runs the command asynchronously
34+
-o, --output-format string Output format, one of ["json" "pretty" "none" "yaml"]
35+
-p, --project-id string Project ID
36+
--region string Target region for region-specific requests
37+
--verbosity string Verbosity of the CLI, one of ["debug" "info" "warning" "error"] (default "info")
38+
```
39+
40+
### SEE ALSO
41+
42+
* [stackit public-ip ip-ranges](./stackit_public-ip_ip-ranges.md) - Provides functionality for STACKIT Cloud public ip-ranges
43+
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package ipranges
2+
3+
import (
4+
"github.com/stackitcloud/stackit-cli/internal/cmd/params"
5+
"github.com/stackitcloud/stackit-cli/internal/cmd/public-ip/ip-ranges/list"
6+
"github.com/stackitcloud/stackit-cli/internal/pkg/args"
7+
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"
8+
9+
"github.com/spf13/cobra"
10+
)
11+
12+
func NewCmd(params *params.CmdParams) *cobra.Command {
13+
cmd := &cobra.Command{
14+
Use: "ip-ranges",
15+
Short: "Provides functionality for STACKIT Cloud public ip-ranges",
16+
Long: "Provides functionality for STACKIT Cloud public ip-ranges",
17+
Args: args.NoArgs,
18+
Run: utils.CmdHelp,
19+
}
20+
addSubcommands(cmd, params)
21+
return cmd
22+
}
23+
24+
func addSubcommands(cmd *cobra.Command, params *params.CmdParams) {
25+
cmd.AddCommand(list.NewCmd(params))
26+
}
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
package list
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/globalflags"
16+
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
17+
"github.com/stackitcloud/stackit-cli/internal/pkg/services/iaas/client"
18+
"github.com/stackitcloud/stackit-sdk-go/services/iaas"
19+
)
20+
21+
type inputModel struct {
22+
*globalflags.GlobalFlagModel
23+
}
24+
25+
func NewCmd(params *params.CmdParams) *cobra.Command {
26+
cmd := &cobra.Command{
27+
Use: "list",
28+
Short: "Lists all STACKIT cloud public IP ranges.",
29+
Long: "Lists all STACKIT cloud public IP ranges.",
30+
Args: args.NoArgs,
31+
Example: examples.Build(
32+
examples.NewExample(
33+
`Lists all STACKIT cloud public IP ranges`,
34+
"$ stackit public-ip ranges list",
35+
),
36+
examples.NewExample(
37+
`Lists all STACKIT cloud public IP ranges, piping to a tool like fzf for interactive selection`,
38+
"$ stackit public-ip ip-ranges list -o pretty | fzf",
39+
),
40+
),
41+
RunE: func(cmd *cobra.Command, _ []string) error {
42+
ctx := context.Background()
43+
model, err := parseInput(params.Printer, cmd)
44+
if err != nil {
45+
return err
46+
}
47+
48+
// Configure API client
49+
apiClient, err := client.ConfigureClient(params.Printer, params.CliVersion)
50+
if err != nil {
51+
return err
52+
}
53+
54+
// Call API
55+
req := apiClient.ListPublicIPRanges(ctx)
56+
resp, err := req.Execute()
57+
58+
if err != nil {
59+
return fmt.Errorf("list public ip ranges s: %w", err)
60+
}
61+
62+
if resp.Items == nil || len(*resp.Items) == 0 {
63+
params.Printer.Info("No public IP ranges found\n")
64+
return nil
65+
}
66+
67+
return outputResult(params.Printer, model.OutputFormat, *resp)
68+
},
69+
}
70+
return cmd
71+
}
72+
73+
func parseInput(p *print.Printer, cmd *cobra.Command) (*inputModel, error) {
74+
globalFlags := globalflags.Parse(p, cmd)
75+
if globalFlags.ProjectId == "" {
76+
return nil, &errors.ProjectIdError{}
77+
}
78+
79+
model := inputModel{GlobalFlagModel: globalFlags}
80+
81+
if p.IsVerbosityDebug() {
82+
modelStr, err := print.BuildDebugStrFromInputModel(model)
83+
if err != nil {
84+
p.Debug(print.ErrorLevel, "convert model to string for debugging: %v", err)
85+
} else {
86+
p.Debug(print.DebugLevel, "parsed input values: %s", modelStr)
87+
}
88+
}
89+
90+
return &model, nil
91+
}
92+
93+
func outputResult(p *print.Printer, outputFormat string, networkListResponse iaas.PublicNetworkListResponse) error {
94+
switch outputFormat {
95+
case print.JSONOutputFormat:
96+
details, err := json.MarshalIndent(networkListResponse, "", " ")
97+
if err != nil {
98+
return fmt.Errorf("marshal public IP: %w", err)
99+
}
100+
p.Outputln(string(details))
101+
102+
return nil
103+
case print.YAMLOutputFormat:
104+
details, err := yaml.MarshalWithOptions(networkListResponse, yaml.IndentSequence(true), yaml.UseJSONMarshaler())
105+
if err != nil {
106+
return fmt.Errorf("marshal public IP: %w", err)
107+
}
108+
p.Outputln(string(details))
109+
110+
return nil
111+
default:
112+
var publicIps []string
113+
for _, item := range *networkListResponse.Items {
114+
if item.Cidr != nil || *item.Cidr != "" {
115+
publicIps = append(publicIps, *item.Cidr)
116+
}
117+
}
118+
p.Outputln(strings.Join(publicIps, "\n"))
119+
120+
return nil
121+
}
122+
}

0 commit comments

Comments
 (0)