|
| 1 | +package describe |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "github.com/stackitcloud/stackit-cli/internal/pkg/args" |
| 8 | + "github.com/stackitcloud/stackit-cli/internal/pkg/errors" |
| 9 | + "github.com/stackitcloud/stackit-cli/internal/pkg/examples" |
| 10 | + "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" |
| 11 | + "github.com/stackitcloud/stackit-cli/internal/pkg/print" |
| 12 | + "github.com/stackitcloud/stackit-cli/internal/pkg/services/object-storage/client" |
| 13 | + objectStorageUtils "github.com/stackitcloud/stackit-cli/internal/pkg/services/object-storage/utils" |
| 14 | + "github.com/stackitcloud/stackit-cli/internal/pkg/tables" |
| 15 | + "github.com/stackitcloud/stackit-cli/internal/pkg/types" |
| 16 | + |
| 17 | + "github.com/spf13/cobra" |
| 18 | + objectstorage "github.com/stackitcloud/stackit-sdk-go/services/objectstorage/v2api" |
| 19 | +) |
| 20 | + |
| 21 | +type inputModel struct { |
| 22 | + *globalflags.GlobalFlagModel |
| 23 | +} |
| 24 | + |
| 25 | +func NewCmd(params *types.CmdParams) *cobra.Command { |
| 26 | + cmd := &cobra.Command{ |
| 27 | + Use: "describe", |
| 28 | + Short: "Describe object storage compliance lock", |
| 29 | + Long: "Describe object storage compliance lock.", |
| 30 | + Args: args.NoArgs, |
| 31 | + Example: examples.Build( |
| 32 | + examples.NewExample( |
| 33 | + `Describe object storage compliance lock`, |
| 34 | + "$ stackit object-storage compliance-lock describe"), |
| 35 | + ), |
| 36 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 37 | + ctx := context.Background() |
| 38 | + model, err := parseInput(params.Printer, cmd, args) |
| 39 | + if err != nil { |
| 40 | + return err |
| 41 | + } |
| 42 | + |
| 43 | + // Configure API client |
| 44 | + apiClient, err := client.ConfigureClient(params.Printer, params.CliVersion) |
| 45 | + if err != nil { |
| 46 | + return err |
| 47 | + } |
| 48 | + |
| 49 | + // Check if the project is enabled before trying to describe |
| 50 | + enabled, err := objectStorageUtils.ProjectEnabled(ctx, apiClient.DefaultAPI, model.ProjectId, model.Region) |
| 51 | + if err != nil { |
| 52 | + return fmt.Errorf("check if Object Storage is enabled: %w", err) |
| 53 | + } |
| 54 | + if !enabled { |
| 55 | + return &errors.ServiceDisabledError{ |
| 56 | + Service: "object-storage", |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + // Call API |
| 61 | + req := buildRequest(ctx, model, apiClient) |
| 62 | + resp, err := req.Execute() |
| 63 | + if err != nil { |
| 64 | + return fmt.Errorf("get object storage compliance lock: %w", err) |
| 65 | + } |
| 66 | + |
| 67 | + return outputResult(params.Printer, model.OutputFormat, resp) |
| 68 | + }, |
| 69 | + } |
| 70 | + return cmd |
| 71 | +} |
| 72 | + |
| 73 | +func parseInput(p *print.Printer, cmd *cobra.Command, _ []string) (*inputModel, error) { |
| 74 | + globalFlags := globalflags.Parse(p, cmd) |
| 75 | + if globalFlags.ProjectId == "" { |
| 76 | + return nil, &errors.ProjectIdError{} |
| 77 | + } |
| 78 | + |
| 79 | + model := inputModel{ |
| 80 | + GlobalFlagModel: globalFlags, |
| 81 | + } |
| 82 | + |
| 83 | + p.DebugInputModel(model) |
| 84 | + return &model, nil |
| 85 | +} |
| 86 | + |
| 87 | +func buildRequest(ctx context.Context, model *inputModel, apiClient *objectstorage.APIClient) objectstorage.ApiGetComplianceLockRequest { |
| 88 | + req := apiClient.DefaultAPI.GetComplianceLock(ctx, model.ProjectId, model.Region) |
| 89 | + return req |
| 90 | +} |
| 91 | + |
| 92 | +func outputResult(p *print.Printer, outputFormat string, resp *objectstorage.ComplianceLockResponse) error { |
| 93 | + return p.OutputResult(outputFormat, resp, func() error { |
| 94 | + if resp == nil { |
| 95 | + return fmt.Errorf("response is empty") |
| 96 | + } |
| 97 | + |
| 98 | + table := tables.NewTable() |
| 99 | + table.AddRow("PROJECT ID", resp.Project) |
| 100 | + table.AddSeparator() |
| 101 | + table.AddRow("MAX RETENTION DAYS", resp.MaxRetentionDays) |
| 102 | + table.AddSeparator() |
| 103 | + |
| 104 | + err := table.Display(p) |
| 105 | + if err != nil { |
| 106 | + return fmt.Errorf("render table: %w", err) |
| 107 | + } |
| 108 | + |
| 109 | + return nil |
| 110 | + }) |
| 111 | +} |
0 commit comments