|
| 1 | +package plans |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + |
| 8 | + "github.com/goccy/go-yaml" |
| 9 | + "github.com/spf13/cobra" |
| 10 | + "github.com/stackitcloud/stackit-cli/internal/pkg/args" |
| 11 | + "github.com/stackitcloud/stackit-cli/internal/pkg/errors" |
| 12 | + "github.com/stackitcloud/stackit-cli/internal/pkg/examples" |
| 13 | + "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" |
| 14 | + "github.com/stackitcloud/stackit-cli/internal/pkg/print" |
| 15 | + "github.com/stackitcloud/stackit-cli/internal/pkg/projectname" |
| 16 | + "github.com/stackitcloud/stackit-cli/internal/pkg/services/alb/client" |
| 17 | + "github.com/stackitcloud/stackit-cli/internal/pkg/tables" |
| 18 | + "github.com/stackitcloud/stackit-cli/internal/pkg/utils" |
| 19 | + "github.com/stackitcloud/stackit-sdk-go/services/alb" |
| 20 | +) |
| 21 | + |
| 22 | +type inputModel struct { |
| 23 | + *globalflags.GlobalFlagModel |
| 24 | +} |
| 25 | + |
| 26 | +func NewCmd(p *print.Printer) *cobra.Command { |
| 27 | + cmd := &cobra.Command{ |
| 28 | + Use: "plans", |
| 29 | + Short: "Lists the application load balancer plans", |
| 30 | + Long: "Lists the available application load balancer plans.", |
| 31 | + Args: args.NoArgs, |
| 32 | + Example: examples.Build( |
| 33 | + examples.NewExample( |
| 34 | + `List all application load balancer plans`, |
| 35 | + `$ stackit beta alb plans`, |
| 36 | + ), |
| 37 | + ), |
| 38 | + RunE: func(cmd *cobra.Command, _ []string) error { |
| 39 | + ctx := context.Background() |
| 40 | + model, err := parseInput(p, cmd) |
| 41 | + if err != nil { |
| 42 | + return err |
| 43 | + } |
| 44 | + |
| 45 | + // Configure API client |
| 46 | + apiClient, err := client.ConfigureClient(p) |
| 47 | + if err != nil { |
| 48 | + return err |
| 49 | + } |
| 50 | + |
| 51 | + projectLabel, err := projectname.GetProjectName(ctx, p, cmd) |
| 52 | + if err != nil { |
| 53 | + p.Debug(print.ErrorLevel, "get project name: %v", err) |
| 54 | + projectLabel = model.ProjectId |
| 55 | + } else if projectLabel == "" { |
| 56 | + projectLabel = model.ProjectId |
| 57 | + } |
| 58 | + |
| 59 | + // Call API |
| 60 | + request := buildRequest(ctx, model, apiClient) |
| 61 | + |
| 62 | + response, err := request.Execute() |
| 63 | + if err != nil { |
| 64 | + return fmt.Errorf("list plans: %w", err) |
| 65 | + } |
| 66 | + |
| 67 | + if items := response.ValidPlans; items == nil || len(*items) == 0 { |
| 68 | + p.Info("No plans found for project %q", projectLabel) |
| 69 | + } else { |
| 70 | + if err := outputResult(p, model.OutputFormat, *items); err != nil { |
| 71 | + return fmt.Errorf("output plans: %w", err) |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + return nil |
| 76 | + }, |
| 77 | + } |
| 78 | + |
| 79 | + return cmd |
| 80 | +} |
| 81 | + |
| 82 | +func parseInput(p *print.Printer, cmd *cobra.Command) (*inputModel, error) { |
| 83 | + globalFlags := globalflags.Parse(p, cmd) |
| 84 | + if globalFlags.ProjectId == "" { |
| 85 | + return nil, &errors.ProjectIdError{} |
| 86 | + } |
| 87 | + |
| 88 | + model := inputModel{ |
| 89 | + GlobalFlagModel: globalFlags, |
| 90 | + } |
| 91 | + |
| 92 | + if p.IsVerbosityDebug() { |
| 93 | + modelStr, err := print.BuildDebugStrFromInputModel(model) |
| 94 | + if err != nil { |
| 95 | + p.Debug(print.ErrorLevel, "convert model to string for debugging: %v", err) |
| 96 | + } else { |
| 97 | + p.Debug(print.DebugLevel, "parsed input values: %s", modelStr) |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + return &model, nil |
| 102 | +} |
| 103 | + |
| 104 | +func buildRequest(ctx context.Context, model *inputModel, apiClient *alb.APIClient) alb.ApiListPlansRequest { |
| 105 | + request := apiClient.ListPlans(ctx, model.Region) |
| 106 | + |
| 107 | + return request |
| 108 | +} |
| 109 | + |
| 110 | +func outputResult(p *print.Printer, outputFormat string, items []alb.PlanDetails) error { |
| 111 | + switch outputFormat { |
| 112 | + case print.JSONOutputFormat: |
| 113 | + details, err := json.MarshalIndent(items, "", " ") |
| 114 | + if err != nil { |
| 115 | + return fmt.Errorf("marshal plans: %w", err) |
| 116 | + } |
| 117 | + p.Outputln(string(details)) |
| 118 | + |
| 119 | + return nil |
| 120 | + case print.YAMLOutputFormat: |
| 121 | + details, err := yaml.MarshalWithOptions(items, yaml.IndentSequence(true), yaml.UseJSONMarshaler()) |
| 122 | + if err != nil { |
| 123 | + return fmt.Errorf("marshal plans: %w", err) |
| 124 | + } |
| 125 | + p.Outputln(string(details)) |
| 126 | + |
| 127 | + return nil |
| 128 | + default: |
| 129 | + table := tables.NewTable() |
| 130 | + table.SetHeader("PLAN ID", "NAME", "FLAVOR", "MAX CONNS", "DESCRIPTION") |
| 131 | + for _, item := range items { |
| 132 | + table.AddRow(utils.PtrString(item.PlanId), |
| 133 | + utils.PtrString(item.Name), |
| 134 | + utils.PtrString(item.FlavorName), |
| 135 | + utils.PtrString(item.MaxConnections), |
| 136 | + utils.Truncate(item.Description, 70), |
| 137 | + ) |
| 138 | + } |
| 139 | + err := table.Display(p) |
| 140 | + if err != nil { |
| 141 | + return fmt.Errorf("render table: %w", err) |
| 142 | + } |
| 143 | + |
| 144 | + return nil |
| 145 | + } |
| 146 | +} |
0 commit comments