|
| 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