|
| 1 | +package list |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "github.com/stackitcloud/stackit-cli/internal/pkg/types" |
| 8 | + |
| 9 | + "github.com/stackitcloud/stackit-cli/internal/pkg/args" |
| 10 | + "github.com/stackitcloud/stackit-cli/internal/pkg/errors" |
| 11 | + "github.com/stackitcloud/stackit-cli/internal/pkg/examples" |
| 12 | + "github.com/stackitcloud/stackit-cli/internal/pkg/flags" |
| 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/services/observability/client" |
| 16 | + "github.com/stackitcloud/stackit-cli/internal/pkg/tables" |
| 17 | + |
| 18 | + observabilityUtils "github.com/stackitcloud/stackit-cli/internal/pkg/services/observability/utils" |
| 19 | + |
| 20 | + "github.com/spf13/cobra" |
| 21 | + observability "github.com/stackitcloud/stackit-sdk-go/services/observability/v1api" |
| 22 | +) |
| 23 | + |
| 24 | +const ( |
| 25 | + limitFlag = "limit" |
| 26 | + instanceIdFlag = "instance-id" |
| 27 | +) |
| 28 | + |
| 29 | +type inputModel struct { |
| 30 | + *globalflags.GlobalFlagModel |
| 31 | + Limit *int64 |
| 32 | + InstanceId string |
| 33 | +} |
| 34 | + |
| 35 | +func NewCmd(params *types.CmdParams) *cobra.Command { |
| 36 | + cmd := &cobra.Command{ |
| 37 | + Use: "list", |
| 38 | + Short: "Lists all ACLs of an Observability instance", |
| 39 | + Long: "Lists all IP networks in CIDR notation which are allowed to access an Observability instance.", |
| 40 | + Args: args.NoArgs, |
| 41 | + Example: examples.Build( |
| 42 | + examples.NewExample( |
| 43 | + `List all ACLs of Observability instance "xxx"`, |
| 44 | + "$ stackit observability acl list --instance-id xxx"), |
| 45 | + examples.NewExample( |
| 46 | + `List all ACLs of Observability instance "xxx" in JSON format`, |
| 47 | + "$ stackit observability acl list --instance-id xxx --output-format json"), |
| 48 | + examples.NewExample( |
| 49 | + `List up to 10 ACLs of Observability instance "xxx"`, |
| 50 | + "$ stackit observability acl list --instance-id xxx --limit 10"), |
| 51 | + ), |
| 52 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 53 | + ctx := context.Background() |
| 54 | + model, err := parseInput(params.Printer, cmd, args) |
| 55 | + if err != nil { |
| 56 | + return err |
| 57 | + } |
| 58 | + |
| 59 | + // Configure API client |
| 60 | + apiClient, err := client.ConfigureClient(params.Printer, params.CliVersion) |
| 61 | + if err != nil { |
| 62 | + return err |
| 63 | + } |
| 64 | + |
| 65 | + // Call API |
| 66 | + req := buildRequest(ctx, model, apiClient.DefaultAPI) |
| 67 | + resp, err := req.Execute() |
| 68 | + if err != nil { |
| 69 | + return fmt.Errorf("get Observability instance ACLs: %w", err) |
| 70 | + } |
| 71 | + |
| 72 | + acls := resp.GetAcl() |
| 73 | + |
| 74 | + instanceLabel, err := observabilityUtils.GetInstanceName(ctx, apiClient.DefaultAPI, model.InstanceId, model.ProjectId) |
| 75 | + if err != nil || instanceLabel == "" { |
| 76 | + params.Printer.Debug(print.ErrorLevel, "get instance name: %v", err) |
| 77 | + instanceLabel = model.InstanceId |
| 78 | + } |
| 79 | + |
| 80 | + // Truncate output |
| 81 | + if model.Limit != nil && len(acls) > int(*model.Limit) { |
| 82 | + acls = acls[:*model.Limit] |
| 83 | + } |
| 84 | + |
| 85 | + return outputResult(params.Printer, model.OutputFormat, instanceLabel, acls) |
| 86 | + }, |
| 87 | + } |
| 88 | + |
| 89 | + configureFlags(cmd) |
| 90 | + return cmd |
| 91 | +} |
| 92 | + |
| 93 | +func configureFlags(cmd *cobra.Command) { |
| 94 | + cmd.Flags().Int64(limitFlag, 0, "Maximum number of entries to list") |
| 95 | + cmd.Flags().Var(flags.UUIDFlag(), instanceIdFlag, "Instance ID") |
| 96 | + |
| 97 | + err := flags.MarkFlagsRequired(cmd, instanceIdFlag) |
| 98 | + cobra.CheckErr(err) |
| 99 | +} |
| 100 | + |
| 101 | +func parseInput(p *print.Printer, cmd *cobra.Command, _ []string) (*inputModel, error) { |
| 102 | + globalFlags := globalflags.Parse(p, cmd) |
| 103 | + if globalFlags.ProjectId == "" { |
| 104 | + return nil, &errors.ProjectIdError{} |
| 105 | + } |
| 106 | + |
| 107 | + limit := flags.FlagToInt64Pointer(p, cmd, limitFlag) |
| 108 | + if limit != nil && *limit < 1 { |
| 109 | + return nil, &errors.FlagValidationError{ |
| 110 | + Flag: limitFlag, |
| 111 | + Details: "must be greater than 0", |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + model := inputModel{ |
| 116 | + GlobalFlagModel: globalFlags, |
| 117 | + Limit: limit, |
| 118 | + InstanceId: flags.FlagToStringValue(p, cmd, instanceIdFlag), |
| 119 | + } |
| 120 | + |
| 121 | + p.DebugInputModel(model) |
| 122 | + return &model, nil |
| 123 | +} |
| 124 | + |
| 125 | +func buildRequest(ctx context.Context, model *inputModel, apiClient observability.DefaultAPI) observability.ApiListACLRequest { |
| 126 | + req := apiClient.ListACL(ctx, model.InstanceId, model.ProjectId) |
| 127 | + return req |
| 128 | +} |
| 129 | + |
| 130 | +func outputResult(p *print.Printer, outputFormat, instanceLabel string, acls []string) error { |
| 131 | + return p.OutputResult(outputFormat, acls, func() error { |
| 132 | + if len(acls) == 0 { |
| 133 | + p.Outputf("No ACLs found for instance %q\n", instanceLabel) |
| 134 | + return nil |
| 135 | + } |
| 136 | + table := tables.NewTable() |
| 137 | + table.SetHeader("CIDR") |
| 138 | + for _, acl := range acls { |
| 139 | + table.AddRow(acl) |
| 140 | + } |
| 141 | + err := table.Display(p) |
| 142 | + if err != nil { |
| 143 | + return fmt.Errorf("render table: %w", err) |
| 144 | + } |
| 145 | + |
| 146 | + return nil |
| 147 | + }) |
| 148 | +} |
0 commit comments