Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/stackit_public-ip.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,6 @@ stackit public-ip [flags]
* [stackit public-ip describe](./stackit_public-ip_describe.md) - Shows details of a Public IP
* [stackit public-ip disassociate](./stackit_public-ip_disassociate.md) - Disassociates a Public IP from a network interface or a virtual IP
* [stackit public-ip list](./stackit_public-ip_list.md) - Lists all Public IPs of a project
* [stackit public-ip ranges](./stackit_public-ip_ranges.md) - Provides functionality for STACKIT public-ip ranges
* [stackit public-ip update](./stackit_public-ip_update.md) - Updates a Public IP

34 changes: 34 additions & 0 deletions docs/stackit_public-ip_ranges.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
## stackit public-ip ranges

Provides functionality for STACKIT public-ip ranges

### Synopsis

Provides functionality for STACKIT public-ip ranges

```
stackit public-ip ranges [flags]
```

### Options

```
-h, --help Help for "stackit public-ip ranges"
```

### Options inherited from parent commands

```
-y, --assume-yes If set, skips all confirmation prompts
--async If set, runs the command asynchronously
-o, --output-format string Output format, one of ["json" "pretty" "none" "yaml"]
-p, --project-id string Project ID
--region string Target region for region-specific requests
--verbosity string Verbosity of the CLI, one of ["debug" "info" "warning" "error"] (default "info")
```

### SEE ALSO

* [stackit public-ip](./stackit_public-ip.md) - Provides functionality for public IPs
* [stackit public-ip ranges list](./stackit_public-ip_ranges_list.md) - Lists all STACKIT public-ip ranges

47 changes: 47 additions & 0 deletions docs/stackit_public-ip_ranges_list.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
## stackit public-ip ranges list

Lists all STACKIT public-ip ranges

### Synopsis

Lists all STACKIT public-ip ranges.

```
stackit public-ip ranges list [flags]
```

### Examples

```
Lists all STACKIT public-ip ranges
$ stackit public-ip ranges list

Lists all STACKIT public-ip ranges, piping to a tool like fzf for interactive selection
$ stackit public-ip ranges list -o pretty | fzf

Lists up to 10 STACKIT public-ip ranges
$ stackit public-ip ranges list --limit 10
```

### Options

```
-h, --help Help for "stackit public-ip ranges list"
--limit int Maximum number of entries to list
```

### Options inherited from parent commands

```
-y, --assume-yes If set, skips all confirmation prompts
--async If set, runs the command asynchronously
-o, --output-format string Output format, one of ["json" "pretty" "none" "yaml"]
-p, --project-id string Project ID
--region string Target region for region-specific requests
--verbosity string Verbosity of the CLI, one of ["debug" "info" "warning" "error"] (default "info")
```

### SEE ALSO

* [stackit public-ip ranges](./stackit_public-ip_ranges.md) - Provides functionality for STACKIT public-ip ranges

2 changes: 2 additions & 0 deletions internal/cmd/public-ip/public-ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/stackitcloud/stackit-cli/internal/cmd/public-ip/describe"
"github.com/stackitcloud/stackit-cli/internal/cmd/public-ip/disassociate"
"github.com/stackitcloud/stackit-cli/internal/cmd/public-ip/list"
"github.com/stackitcloud/stackit-cli/internal/cmd/public-ip/ranges"
"github.com/stackitcloud/stackit-cli/internal/cmd/public-ip/update"
"github.com/stackitcloud/stackit-cli/internal/pkg/args"
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"
Expand Down Expand Up @@ -35,4 +36,5 @@ func addSubcommands(cmd *cobra.Command, params *params.CmdParams) {
cmd.AddCommand(update.NewCmd(params))
cmd.AddCommand(associate.NewCmd(params))
cmd.AddCommand(disassociate.NewCmd(params))
cmd.AddCommand(ranges.NewCmd(params))
}
149 changes: 149 additions & 0 deletions internal/cmd/public-ip/ranges/list/list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
package list

import (
"context"
"encoding/json"
"fmt"

"github.com/goccy/go-yaml"
"github.com/spf13/cobra"
"github.com/stackitcloud/stackit-cli/internal/cmd/params"
"github.com/stackitcloud/stackit-cli/internal/pkg/args"
"github.com/stackitcloud/stackit-cli/internal/pkg/errors"
"github.com/stackitcloud/stackit-cli/internal/pkg/examples"
"github.com/stackitcloud/stackit-cli/internal/pkg/flags"
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
"github.com/stackitcloud/stackit-cli/internal/pkg/services/iaas/client"
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"
"github.com/stackitcloud/stackit-sdk-go/services/iaas"
)

const (
limitFlag = "limit"
)

type inputModel struct {
*globalflags.GlobalFlagModel
Limit *int64
}

func NewCmd(params *params.CmdParams) *cobra.Command {
cmd := &cobra.Command{
Use: "list",
Short: "Lists all STACKIT public-ip ranges",
Long: "Lists all STACKIT public-ip ranges.",
Args: args.NoArgs,
Example: examples.Build(
examples.NewExample(
`Lists all STACKIT public-ip ranges`,
"$ stackit public-ip ranges list",
),
examples.NewExample(
`Lists all STACKIT public-ip ranges, piping to a tool like fzf for interactive selection`,
"$ stackit public-ip ranges list -o pretty | fzf",
),
examples.NewExample(
`Lists up to 10 STACKIT public-ip ranges`,
"$ stackit public-ip ranges list --limit 10",
),
),
RunE: func(cmd *cobra.Command, _ []string) error {
ctx := context.Background()
model, err := parseInput(params.Printer, cmd)
if err != nil {
return err
}

// Configure API client
apiClient, err := client.ConfigureClient(params.Printer, params.CliVersion)
if err != nil {
return err
}

// Call API
req := apiClient.ListPublicIPRanges(ctx)
resp, err := req.Execute()
if err != nil {
return fmt.Errorf("list public IP ranges: %w", err)
}
publicIpRanges := utils.GetSliceFromPointer(resp.Items)

// Truncate output
if model.Limit != nil && len(publicIpRanges) > int(*model.Limit) {
publicIpRanges = publicIpRanges[:*model.Limit]
}

return outputResult(params.Printer, model.OutputFormat, publicIpRanges)
},
}

configureFlags(cmd)
return cmd
}

func configureFlags(cmd *cobra.Command) {
cmd.Flags().Int64(limitFlag, 0, "Maximum number of entries to list")
}

func parseInput(p *print.Printer, cmd *cobra.Command) (*inputModel, error) {
globalFlags := globalflags.Parse(p, cmd)

limit := flags.FlagToInt64Pointer(p, cmd, limitFlag)
if limit != nil && *limit < 1 {
return nil, &errors.FlagValidationError{
Flag: limitFlag,
Details: "must be greater than 0",
}
}

model := inputModel{
GlobalFlagModel: globalFlags,
Limit: limit,
}

if p.IsVerbosityDebug() {
modelStr, err := print.BuildDebugStrFromInputModel(model)
if err != nil {
p.Debug(print.ErrorLevel, "convert model to string for debugging: %v", err)
} else {
p.Debug(print.DebugLevel, "parsed input values: %s", modelStr)
}
}

return &model, nil
}

func outputResult(p *print.Printer, outputFormat string, publicIpRanges []iaas.PublicNetwork) error {
switch outputFormat {
case print.JSONOutputFormat:
details, err := json.MarshalIndent(publicIpRanges, "", " ")
if err != nil {
return fmt.Errorf("marshal public IP ranges: %w", err)
}
p.Outputln(string(details))

return nil
case print.YAMLOutputFormat:
details, err := yaml.MarshalWithOptions(publicIpRanges, yaml.IndentSequence(true), yaml.UseJSONMarshaler())
if err != nil {
return fmt.Errorf("marshal public IP ranges: %w", err)
}
p.Outputln(string(details))

return nil
default:
if len(publicIpRanges) == 0 {
p.Outputln("No public IP ranges found")
return nil
}

for _, item := range publicIpRanges {
if item.Cidr != nil && *item.Cidr != "" {
p.Outputln(*item.Cidr)
}
}

return nil
}
}
Loading
Loading