|
| 1 | +package list |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "strconv" |
| 8 | + |
| 9 | + "github.com/goccy/go-yaml" |
| 10 | + "github.com/spf13/cobra" |
| 11 | + "github.com/stackitcloud/stackit-cli/internal/pkg/args" |
| 12 | + "github.com/stackitcloud/stackit-cli/internal/pkg/errors" |
| 13 | + "github.com/stackitcloud/stackit-cli/internal/pkg/examples" |
| 14 | + "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" |
| 15 | + "github.com/stackitcloud/stackit-cli/internal/pkg/print" |
| 16 | + "github.com/stackitcloud/stackit-cli/internal/pkg/projectname" |
| 17 | + "github.com/stackitcloud/stackit-cli/internal/pkg/services/iaas/client" |
| 18 | + "github.com/stackitcloud/stackit-cli/internal/pkg/tables" |
| 19 | + "github.com/stackitcloud/stackit-sdk-go/services/iaas" |
| 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: "list", |
| 29 | + Short: "Lists quotas", |
| 30 | + Long: "Lists project quotas.", |
| 31 | + Args: args.NoArgs, |
| 32 | + Example: examples.Build( |
| 33 | + examples.NewExample( |
| 34 | + `List available quotas`, |
| 35 | + `$ stackit beta quota list`, |
| 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 | + } |
| 56 | + |
| 57 | + // Call API |
| 58 | + request := buildRequest(ctx, model, apiClient) |
| 59 | + |
| 60 | + response, err := request.Execute() |
| 61 | + if err != nil { |
| 62 | + return fmt.Errorf("list quotas: %w", err) |
| 63 | + } |
| 64 | + |
| 65 | + if items := response.Quotas; items == nil { |
| 66 | + p.Info("No quotas found for project %q", projectLabel) |
| 67 | + } else { |
| 68 | + if err := outputResult(p, model.OutputFormat, items); err != nil { |
| 69 | + return fmt.Errorf("output quotas: %w", err) |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + return nil |
| 74 | + }, |
| 75 | + } |
| 76 | + |
| 77 | + return cmd |
| 78 | +} |
| 79 | + |
| 80 | +func parseInput(p *print.Printer, cmd *cobra.Command) (*inputModel, error) { |
| 81 | + globalFlags := globalflags.Parse(p, cmd) |
| 82 | + if globalFlags.ProjectId == "" { |
| 83 | + return nil, &errors.ProjectIdError{} |
| 84 | + } |
| 85 | + |
| 86 | + model := inputModel{ |
| 87 | + GlobalFlagModel: globalFlags, |
| 88 | + } |
| 89 | + |
| 90 | + if p.IsVerbosityDebug() { |
| 91 | + modelStr, err := print.BuildDebugStrFromInputModel(model) |
| 92 | + if err != nil { |
| 93 | + p.Debug(print.ErrorLevel, "convert model to string for debugging: %v", err) |
| 94 | + } else { |
| 95 | + p.Debug(print.DebugLevel, "parsed input values: %s", modelStr) |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + return &model, nil |
| 100 | +} |
| 101 | + |
| 102 | +func buildRequest(ctx context.Context, model *inputModel, apiClient *iaas.APIClient) iaas.ApiListQuotasRequest { |
| 103 | + request := apiClient.ListQuotas(ctx, model.ProjectId) |
| 104 | + |
| 105 | + return request |
| 106 | +} |
| 107 | + |
| 108 | +func outputResult(p *print.Printer, outputFormat string, quotas *iaas.QuotaList) error { |
| 109 | + switch outputFormat { |
| 110 | + case print.JSONOutputFormat: |
| 111 | + details, err := json.MarshalIndent(quotas, "", " ") |
| 112 | + if err != nil { |
| 113 | + return fmt.Errorf("marshal quota list: %w", err) |
| 114 | + } |
| 115 | + p.Outputln(string(details)) |
| 116 | + |
| 117 | + return nil |
| 118 | + case print.YAMLOutputFormat: |
| 119 | + details, err := yaml.MarshalWithOptions(quotas, yaml.IndentSequence(true), yaml.UseJSONMarshaler()) |
| 120 | + if err != nil { |
| 121 | + return fmt.Errorf("marshal quota list: %w", err) |
| 122 | + } |
| 123 | + p.Outputln(string(details)) |
| 124 | + |
| 125 | + return nil |
| 126 | + |
| 127 | + default: |
| 128 | + table := tables.NewTable() |
| 129 | + table.SetHeader("NAME", "LIMIT", "CURRENT USAGE", "PERCENT") |
| 130 | + if val := quotas.BackupGigabytes; val != nil { |
| 131 | + table.AddRow("Total size in GiB of backups [GiB]", conv(val.GetLimit()), conv(val.GetUsage()), percentage(val)) |
| 132 | + } |
| 133 | + if val := quotas.Backups; val != nil { |
| 134 | + table.AddRow("Number of backups [Count]", conv(val.GetLimit()), conv(val.GetUsage()), percentage(val)) |
| 135 | + } |
| 136 | + if val := quotas.Gigabytes; val != nil { |
| 137 | + table.AddRow("Total size in GiB of volumes and snapshots [GiB]", conv(val.GetLimit()), conv(val.GetUsage()), percentage(val)) |
| 138 | + } |
| 139 | + if val := quotas.Networks; val != nil { |
| 140 | + table.AddRow("Number of networks [Count]", conv(val.GetLimit()), conv(val.GetUsage()), percentage(val)) |
| 141 | + } |
| 142 | + if val := quotas.Nics; val != nil { |
| 143 | + table.AddRow("Number of network interfaces (nics) [Count]", conv(val.GetLimit()), conv(val.GetUsage()), percentage(val)) |
| 144 | + } |
| 145 | + if val := quotas.PublicIps; val != nil { |
| 146 | + table.AddRow("Number of public IP addresses [Count]", conv(val.GetLimit()), conv(val.GetUsage()), percentage(val)) |
| 147 | + } |
| 148 | + if val := quotas.Ram; val != nil { |
| 149 | + table.AddRow("Amount of server RAM in MiB [MiB]", conv(val.GetLimit()), conv(val.GetUsage()), percentage(val)) |
| 150 | + } |
| 151 | + if val := quotas.SecurityGroupRules; val != nil { |
| 152 | + table.AddRow("Number of security group rules [Count]", conv(val.GetLimit()), conv(val.GetUsage()), percentage(val)) |
| 153 | + } |
| 154 | + if val := quotas.SecurityGroups; val != nil { |
| 155 | + table.AddRow("Number of security groups [Count]", conv(val.GetLimit()), conv(val.GetUsage()), percentage(val)) |
| 156 | + } |
| 157 | + if val := quotas.Snapshots; val != nil { |
| 158 | + table.AddRow("Number of snapshots [Count]", conv(val.GetLimit()), conv(val.GetUsage()), percentage(val)) |
| 159 | + } |
| 160 | + if val := quotas.Vcpu; val != nil { |
| 161 | + table.AddRow("Number of server cores (vcpu) [Count]", conv(val.GetLimit()), conv(val.GetUsage()), percentage(val)) |
| 162 | + } |
| 163 | + if val := quotas.Volumes; val != nil { |
| 164 | + table.AddRow("Number of volumes [Count]", conv(val.GetLimit()), conv(val.GetUsage()), percentage(val)) |
| 165 | + } |
| 166 | + err := table.Display(p) |
| 167 | + if err != nil { |
| 168 | + return fmt.Errorf("render table: %w", err) |
| 169 | + } |
| 170 | + |
| 171 | + return nil |
| 172 | + } |
| 173 | +} |
| 174 | + |
| 175 | +func conv(n *int64) string { |
| 176 | + if n != nil { |
| 177 | + return strconv.FormatInt(*n, 10) |
| 178 | + } |
| 179 | + return "n/a" |
| 180 | +} |
| 181 | + |
| 182 | +func percentage(val interface { |
| 183 | + GetLimit() *int64 |
| 184 | + GetUsage() *int64 |
| 185 | +}) string { |
| 186 | + if a, b := val.GetLimit(), val.GetUsage(); a != nil && b != nil { |
| 187 | + return fmt.Sprintf("%3.1f%%", 100.0/float64(*a)*float64(*b)) |
| 188 | + } |
| 189 | + return "n/a" |
| 190 | +} |
0 commit comments