|
| 1 | +package list |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "github.com/stackitcloud/stackit-cli/internal/pkg/types" |
| 8 | + "github.com/stackitcloud/stackit-cli/internal/pkg/utils" |
| 9 | + |
| 10 | + "github.com/spf13/cobra" |
| 11 | + mongodbflex "github.com/stackitcloud/stackit-sdk-go/services/mongodbflex/v2api" |
| 12 | + |
| 13 | + "github.com/stackitcloud/stackit-cli/internal/pkg/args" |
| 14 | + cliErr "github.com/stackitcloud/stackit-cli/internal/pkg/errors" |
| 15 | + "github.com/stackitcloud/stackit-cli/internal/pkg/examples" |
| 16 | + "github.com/stackitcloud/stackit-cli/internal/pkg/flags" |
| 17 | + "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" |
| 18 | + "github.com/stackitcloud/stackit-cli/internal/pkg/print" |
| 19 | + "github.com/stackitcloud/stackit-cli/internal/pkg/services/mongodbflex/client" |
| 20 | + "github.com/stackitcloud/stackit-cli/internal/pkg/tables" |
| 21 | +) |
| 22 | + |
| 23 | +const ( |
| 24 | + limitFlag = "limit" |
| 25 | +) |
| 26 | + |
| 27 | +type inputModel struct { |
| 28 | + *globalflags.GlobalFlagModel |
| 29 | + Limit *int64 |
| 30 | +} |
| 31 | + |
| 32 | +func NewCmd(params *types.CmdParams) *cobra.Command { |
| 33 | + cmd := &cobra.Command{ |
| 34 | + Use: "list", |
| 35 | + Short: "Lists MongoDB Flex flavors", |
| 36 | + Long: "Lists MongoDB Flex flavors.", |
| 37 | + Args: args.NoArgs, |
| 38 | + Example: examples.Build( |
| 39 | + examples.NewExample( |
| 40 | + `List MongoDB Flex flavor`, |
| 41 | + "$ stackit mongodbflex flavor list"), |
| 42 | + ), |
| 43 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 44 | + ctx := context.Background() |
| 45 | + model, err := parseInput(params.Printer, cmd, args) |
| 46 | + if err != nil { |
| 47 | + return err |
| 48 | + } |
| 49 | + |
| 50 | + // Configure API client |
| 51 | + apiClient, err := client.ConfigureClient(params.Printer, params.CliVersion) |
| 52 | + if err != nil { |
| 53 | + return err |
| 54 | + } |
| 55 | + |
| 56 | + // Call API |
| 57 | + flavors, err := buildRequest(ctx, model, apiClient.DefaultAPI).Execute() |
| 58 | + if err != nil { |
| 59 | + return fmt.Errorf("get MongoDB Flex flavors: %w", err) |
| 60 | + } |
| 61 | + |
| 62 | + return outputResult(params.Printer, model.OutputFormat, flavors.Flavors) |
| 63 | + }, |
| 64 | + } |
| 65 | + |
| 66 | + configureFlags(cmd) |
| 67 | + return cmd |
| 68 | +} |
| 69 | + |
| 70 | +func configureFlags(cmd *cobra.Command) { |
| 71 | + cmd.Flags().Int64(limitFlag, 0, "Maximum number of entries to list") |
| 72 | +} |
| 73 | + |
| 74 | +func parseInput(p *print.Printer, cmd *cobra.Command, _ []string) (*inputModel, error) { |
| 75 | + globalFlags := globalflags.Parse(p, cmd) |
| 76 | + if globalFlags.ProjectId == "" { |
| 77 | + return nil, &cliErr.ProjectIdError{} |
| 78 | + } |
| 79 | + |
| 80 | + limit := flags.FlagToInt64Pointer(p, cmd, limitFlag) |
| 81 | + if limit != nil && *limit < 1 { |
| 82 | + return nil, &cliErr.FlagValidationError{ |
| 83 | + Flag: limitFlag, |
| 84 | + Details: "must be greater than 0", |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + model := inputModel{ |
| 89 | + GlobalFlagModel: globalFlags, |
| 90 | + Limit: limit, |
| 91 | + } |
| 92 | + |
| 93 | + p.DebugInputModel(model) |
| 94 | + return &model, nil |
| 95 | +} |
| 96 | + |
| 97 | +func buildRequest(ctx context.Context, model *inputModel, apiClient mongodbflex.DefaultAPI) mongodbflex.ApiListFlavorsRequest { |
| 98 | + return apiClient.ListFlavors(ctx, model.ProjectId, model.Region) |
| 99 | +} |
| 100 | + |
| 101 | +func outputResult(p *print.Printer, outputFormat string, flavors []mongodbflex.InstanceFlavor) error { |
| 102 | + return p.OutputResult(outputFormat, flavors, func() error { |
| 103 | + if len(flavors) == 0 { |
| 104 | + p.Outputf("No MongoDB flavors found.") |
| 105 | + return nil |
| 106 | + } |
| 107 | + |
| 108 | + table := tables.NewTable() |
| 109 | + table.SetTitle("Flavors") |
| 110 | + table.SetHeader("ID", "CPU", "MEMORY", "DESCRIPTION", "VALID INSTANCE TYPES") |
| 111 | + for _, f := range flavors { |
| 112 | + table.AddRow( |
| 113 | + utils.PtrString(f.Id), |
| 114 | + utils.PtrString(f.Cpu), |
| 115 | + utils.PtrString(f.Memory), |
| 116 | + utils.PtrString(f.Description), |
| 117 | + f.Categories, |
| 118 | + ) |
| 119 | + table.AddSeparator() |
| 120 | + } |
| 121 | + |
| 122 | + return table.Display(p) |
| 123 | + }) |
| 124 | +} |
0 commit comments