|
| 1 | +<<<<<<< HEAD |
1 | 2 | package list |
2 | 3 |
|
3 | 4 | import ( |
@@ -176,3 +177,180 @@ func outputResult(p *print.Printer, outputFormat string, items []alb.LoadBalance |
176 | 177 | return nil |
177 | 178 | } |
178 | 179 | } |
| 180 | +||||||| parent of db5dfa32 (feat(alb): list command) |
| 181 | +======= |
| 182 | +package list |
| 183 | + |
| 184 | +import ( |
| 185 | + "context" |
| 186 | + "encoding/json" |
| 187 | + "fmt" |
| 188 | + |
| 189 | + "github.com/goccy/go-yaml" |
| 190 | + "github.com/spf13/cobra" |
| 191 | + "github.com/stackitcloud/stackit-cli/internal/pkg/args" |
| 192 | + "github.com/stackitcloud/stackit-cli/internal/pkg/errors" |
| 193 | + "github.com/stackitcloud/stackit-cli/internal/pkg/examples" |
| 194 | + "github.com/stackitcloud/stackit-cli/internal/pkg/flags" |
| 195 | + "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" |
| 196 | + "github.com/stackitcloud/stackit-cli/internal/pkg/print" |
| 197 | + "github.com/stackitcloud/stackit-cli/internal/pkg/projectname" |
| 198 | + "github.com/stackitcloud/stackit-cli/internal/pkg/services/alb/client" |
| 199 | + "github.com/stackitcloud/stackit-cli/internal/pkg/tables" |
| 200 | + "github.com/stackitcloud/stackit-cli/internal/pkg/utils" |
| 201 | + "github.com/stackitcloud/stackit-sdk-go/services/alb" |
| 202 | +) |
| 203 | + |
| 204 | +type inputModel struct { |
| 205 | + *globalflags.GlobalFlagModel |
| 206 | + Limit *int64 |
| 207 | +} |
| 208 | + |
| 209 | +const ( |
| 210 | + labelSelectorFlag = "label-selector" |
| 211 | + limitFlag = "limit" |
| 212 | +) |
| 213 | + |
| 214 | +func NewCmd(p *print.Printer) *cobra.Command { |
| 215 | + cmd := &cobra.Command{ |
| 216 | + Use: "list", |
| 217 | + Short: "Lists albs", |
| 218 | + Long: "Lists application load balancers.", |
| 219 | + Args: args.NoArgs, |
| 220 | + Example: examples.Build( |
| 221 | + examples.NewExample( |
| 222 | + `List all load balancers`, |
| 223 | + `$ stackit beta alb list`, |
| 224 | + ), |
| 225 | + examples.NewExample( |
| 226 | + `List the first 10 application load balancers`, |
| 227 | + `$ stackit beta alb list --limit=10`, |
| 228 | + ), |
| 229 | + ), |
| 230 | + RunE: func(cmd *cobra.Command, _ []string) error { |
| 231 | + ctx := context.Background() |
| 232 | + model, err := parseInput(p, cmd) |
| 233 | + if err != nil { |
| 234 | + return err |
| 235 | + } |
| 236 | + |
| 237 | + // Configure API client |
| 238 | + apiClient, err := client.ConfigureClient(p) |
| 239 | + if err != nil { |
| 240 | + return err |
| 241 | + } |
| 242 | + |
| 243 | + projectLabel, err := projectname.GetProjectName(ctx, p, cmd) |
| 244 | + if err != nil { |
| 245 | + p.Debug(print.ErrorLevel, "get project name: %v", err) |
| 246 | + projectLabel = model.ProjectId |
| 247 | + } else if projectLabel == "" { |
| 248 | + projectLabel = model.ProjectId |
| 249 | + } |
| 250 | + |
| 251 | + // Call API |
| 252 | + request := buildRequest(ctx, model, apiClient) |
| 253 | + |
| 254 | + response, err := request.Execute() |
| 255 | + if err != nil { |
| 256 | + return fmt.Errorf("list load balancerse: %w", err) |
| 257 | + } |
| 258 | + |
| 259 | + if items := response.LoadBalancers; items != nil && len(*items) == 0 { |
| 260 | + p.Info("No load balancers found for project %q", projectLabel) |
| 261 | + } else { |
| 262 | + if model.Limit != nil && len(*items) > int(*model.Limit) { |
| 263 | + *items = (*items)[:*model.Limit] |
| 264 | + } |
| 265 | + if err := outputResult(p, model.OutputFormat, *items); err != nil { |
| 266 | + return fmt.Errorf("output loadbalancers: %w", err) |
| 267 | + } |
| 268 | + } |
| 269 | + |
| 270 | + return nil |
| 271 | + }, |
| 272 | + } |
| 273 | + |
| 274 | + configureFlags(cmd) |
| 275 | + return cmd |
| 276 | +} |
| 277 | + |
| 278 | +func configureFlags(cmd *cobra.Command) { |
| 279 | + cmd.Flags().Int64(limitFlag, 0, "Limit the output to the first n elements") |
| 280 | +} |
| 281 | + |
| 282 | +func parseInput(p *print.Printer, cmd *cobra.Command) (*inputModel, error) { |
| 283 | + globalFlags := globalflags.Parse(p, cmd) |
| 284 | + if globalFlags.ProjectId == "" { |
| 285 | + return nil, &errors.ProjectIdError{} |
| 286 | + } |
| 287 | + |
| 288 | + limit := flags.FlagToInt64Pointer(p, cmd, limitFlag) |
| 289 | + if limit != nil && *limit < 1 { |
| 290 | + return nil, &errors.FlagValidationError{ |
| 291 | + Flag: limitFlag, |
| 292 | + Details: "must be greater than 0", |
| 293 | + } |
| 294 | + } |
| 295 | + |
| 296 | + model := inputModel{ |
| 297 | + GlobalFlagModel: globalFlags, |
| 298 | + Limit: limit, |
| 299 | + } |
| 300 | + |
| 301 | + if p.IsVerbosityDebug() { |
| 302 | + modelStr, err := print.BuildDebugStrFromInputModel(model) |
| 303 | + if err != nil { |
| 304 | + p.Debug(print.ErrorLevel, "convert model to string for debugging: %v", err) |
| 305 | + } else { |
| 306 | + p.Debug(print.DebugLevel, "parsed input values: %s", modelStr) |
| 307 | + } |
| 308 | + } |
| 309 | + |
| 310 | + return &model, nil |
| 311 | +} |
| 312 | + |
| 313 | +func buildRequest(ctx context.Context, model *inputModel, apiClient *alb.APIClient) alb.ApiListLoadBalancersRequest { |
| 314 | + request := apiClient.ListLoadBalancers(ctx, model.ProjectId, model.Region) |
| 315 | + |
| 316 | + return request |
| 317 | +} |
| 318 | +func outputResult(p *print.Printer, outputFormat string, items []alb.LoadBalancer) error { |
| 319 | + switch outputFormat { |
| 320 | + case print.JSONOutputFormat: |
| 321 | + details, err := json.MarshalIndent(items, "", " ") |
| 322 | + if err != nil { |
| 323 | + return fmt.Errorf("marshal loadbalancer list: %w", err) |
| 324 | + } |
| 325 | + p.Outputln(string(details)) |
| 326 | + |
| 327 | + return nil |
| 328 | + case print.YAMLOutputFormat: |
| 329 | + details, err := yaml.MarshalWithOptions(items, yaml.IndentSequence(true), yaml.UseJSONMarshaler()) |
| 330 | + if err != nil { |
| 331 | + return fmt.Errorf("marshal loadbalancer list: %w", err) |
| 332 | + } |
| 333 | + p.Outputln(string(details)) |
| 334 | + |
| 335 | + return nil |
| 336 | + default: |
| 337 | + table := tables.NewTable() |
| 338 | + table.SetHeader("NAME", "EXTERNAL ADDRESS", "REGION", "STATUS", "VERSION") |
| 339 | + for _, item := range items { |
| 340 | + table.AddRow(utils.PtrString(item.Name), |
| 341 | + utils.PtrString(item.ExternalAddress), |
| 342 | + utils.PtrString(item.Region), |
| 343 | + utils.PtrString(item.Status), |
| 344 | + utils.PtrString(item.Version), |
| 345 | + utils.PtrString(item.ExternalAddress), |
| 346 | + ) |
| 347 | + } |
| 348 | + err := table.Display(p) |
| 349 | + if err != nil { |
| 350 | + return fmt.Errorf("render table: %w", err) |
| 351 | + } |
| 352 | + |
| 353 | + return nil |
| 354 | + } |
| 355 | +} |
| 356 | +>>>>>>> db5dfa32 (feat(alb): list command) |
0 commit comments