- 
                Notifications
    You must be signed in to change notification settings 
- Fork 26
feat: provide functionality to list public ip-ranges #993
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Merged
      
      
            Benjosh95
  merged 12 commits into
  stackitcloud:main
from
h3adex:feat/list-stackit-ip-ranges
  
      
      
   
  Oct 2, 2025 
      
    
  
     Merged
                    Changes from all commits
      Commits
    
    
            Show all changes
          
          
            12 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      a281e2f
              
                feat(public-ip): add command to list stackit ip-ranges
              
              
                h3adex a354a79
              
                review changes
              
              
                h3adex fcea9d4
              
                add tests, limit flag, renaming
              
              
                Benjosh95 a400038
              
                update docs
              
              
                Benjosh95 2dc0617
              
                remove unnecessary slice
              
              
                Benjosh95 1176ed8
              
                fix invalid json/yaml
              
              
                Benjosh95 2ec9ddc
              
                add n use new util func
              
              
                Benjosh95 5eb1159
              
                add tests for TestGetSliceFromPointer
              
              
                Benjosh95 8071950
              
                fix utils tests
              
              
                Benjosh95 bc9f629
              
                fix utils
              
              
                Benjosh95 953575a
              
                fix util test
              
              
                Benjosh95 9aa6683
              
                fix util test
              
              
                Benjosh95 File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | 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 | ||
|  | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | 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 | ||
|  | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | 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 | ||
| } | ||
| } | ||
      
      Oops, something went wrong.
        
    
  
      
      Oops, something went wrong.
        
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
Uh oh!
There was an error while loading. Please reload this page.