|
| 1 | +package describe |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "github.com/spf13/cobra" |
| 8 | + sfs "github.com/stackitcloud/stackit-sdk-go/services/sfs/v1api" |
| 9 | + |
| 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/sfs/client" |
| 17 | + "github.com/stackitcloud/stackit-cli/internal/pkg/tables" |
| 18 | + "github.com/stackitcloud/stackit-cli/internal/pkg/types" |
| 19 | + "github.com/stackitcloud/stackit-cli/internal/pkg/utils" |
| 20 | +) |
| 21 | + |
| 22 | +const snapshotPolicyIdArg = "SNAPSHOT_POLICY_ID" |
| 23 | + |
| 24 | +type inputModel struct { |
| 25 | + *globalflags.GlobalFlagModel |
| 26 | + SnapshotPolicyId string |
| 27 | +} |
| 28 | + |
| 29 | +func NewCmd(params *types.CmdParams) *cobra.Command { |
| 30 | + cmd := &cobra.Command{ |
| 31 | + Use: fmt.Sprintf("describe %s", snapshotPolicyIdArg), |
| 32 | + Short: "Shows details of a snapshot policy", |
| 33 | + Long: "Shows details of a snapshot policy.", |
| 34 | + Args: args.SingleArg(snapshotPolicyIdArg, utils.ValidateUUID), |
| 35 | + Example: examples.Build( |
| 36 | + examples.NewExample( |
| 37 | + `Describe a snapshot policy with ID "xxx"`, |
| 38 | + "$ stackit beta sfs snapshot-policy describe xxx", |
| 39 | + ), |
| 40 | + ), |
| 41 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 42 | + ctx := context.Background() |
| 43 | + model, err := parseInput(params.Printer, cmd, args) |
| 44 | + if err != nil { |
| 45 | + return fmt.Errorf("unable to parse input: %w", err) |
| 46 | + } |
| 47 | + |
| 48 | + // Configure API client |
| 49 | + apiClient, err := client.ConfigureClient(params.Printer, params.CliVersion) |
| 50 | + if err != nil { |
| 51 | + return err |
| 52 | + } |
| 53 | + |
| 54 | + // Call API |
| 55 | + req := buildRequest(ctx, model, apiClient) |
| 56 | + resp, err := req.Execute() |
| 57 | + if err != nil { |
| 58 | + return fmt.Errorf("read snapshot policy: %w", err) |
| 59 | + } |
| 60 | + |
| 61 | + // Get projectLabel |
| 62 | + projectLabel, err := projectname.GetProjectName(ctx, params.Printer, params.CliVersion, cmd) |
| 63 | + if err != nil { |
| 64 | + params.Printer.Debug(print.ErrorLevel, "get project name: %v", err) |
| 65 | + projectLabel = model.ProjectId |
| 66 | + } else if projectLabel == "" { |
| 67 | + projectLabel = model.ProjectId |
| 68 | + } |
| 69 | + |
| 70 | + return outputResult(params.Printer, model.OutputFormat, model.SnapshotPolicyId, projectLabel, resp) |
| 71 | + }, |
| 72 | + } |
| 73 | + return cmd |
| 74 | +} |
| 75 | + |
| 76 | +func parseInput(p *print.Printer, cmd *cobra.Command, inputArgs []string) (*inputModel, error) { |
| 77 | + snapshotPolicyId := inputArgs[0] |
| 78 | + |
| 79 | + globalFlags := globalflags.Parse(p, cmd) |
| 80 | + if globalFlags.ProjectId == "" { |
| 81 | + return nil, &errors.ProjectIdError{} |
| 82 | + } |
| 83 | + |
| 84 | + model := inputModel{ |
| 85 | + GlobalFlagModel: globalFlags, |
| 86 | + SnapshotPolicyId: snapshotPolicyId, |
| 87 | + } |
| 88 | + |
| 89 | + p.DebugInputModel(model) |
| 90 | + return &model, nil |
| 91 | +} |
| 92 | + |
| 93 | +func buildRequest(ctx context.Context, model *inputModel, apiClient *sfs.APIClient) sfs.ApiGetSnapshotPolicyRequest { |
| 94 | + return apiClient.DefaultAPI.GetSnapshotPolicy(ctx, model.ProjectId, model.SnapshotPolicyId) |
| 95 | +} |
| 96 | + |
| 97 | +func outputResult(p *print.Printer, outputFormat, snapshotPolicyId, projectLabel string, snapshotPolicy *sfs.GetSnapshotPolicyResponse) error { |
| 98 | + return p.OutputResult(outputFormat, snapshotPolicy, func() error { |
| 99 | + if snapshotPolicy == nil || snapshotPolicy.SnapshotPolicy == nil { |
| 100 | + p.Outputf("Snapshot policy %q not found in project %q", snapshotPolicyId, projectLabel) |
| 101 | + return nil |
| 102 | + } |
| 103 | + |
| 104 | + var content []tables.Table |
| 105 | + |
| 106 | + table := tables.NewTable() |
| 107 | + table.SetTitle("Snapshot Policy") |
| 108 | + policy := snapshotPolicy.SnapshotPolicy |
| 109 | + |
| 110 | + table.AddRow("ID", utils.PtrString(policy.Id)) |
| 111 | + table.AddSeparator() |
| 112 | + table.AddRow("NAME", utils.PtrString(policy.Name)) |
| 113 | + table.AddSeparator() |
| 114 | + table.AddRow("ENABLED", utils.PtrString(policy.Enabled)) |
| 115 | + table.AddSeparator() |
| 116 | + table.AddRow("COMMENT", utils.PtrString(policy.Comment)) |
| 117 | + table.AddSeparator() |
| 118 | + table.AddRow("CREATED AT", utils.ConvertTimePToDateTimeString(policy.CreatedAt)) |
| 119 | + |
| 120 | + content = append(content, table) |
| 121 | + |
| 122 | + if len(policy.SnapshotSchedules) > 0 { |
| 123 | + snapshotSchedulesTable := tables.NewTable() |
| 124 | + snapshotSchedulesTable.SetTitle("Snapshot Schedules") |
| 125 | + |
| 126 | + snapshotSchedulesTable.SetHeader("ID", "NAME", "INTERVAL", "PREFIX", "RETENTION COUNT", "RETENTION PERIOD", "CREATED AT") |
| 127 | + |
| 128 | + for _, snapshotSchedule := range policy.SnapshotSchedules { |
| 129 | + snapshotSchedulesTable.AddRow( |
| 130 | + utils.PtrString(snapshotSchedule.Id), |
| 131 | + utils.PtrString(snapshotSchedule.Name), |
| 132 | + utils.PtrString(snapshotSchedule.Interval), |
| 133 | + utils.PtrString(snapshotSchedule.Prefix), |
| 134 | + utils.PtrString(snapshotSchedule.RetentionCount), |
| 135 | + utils.PtrString(snapshotSchedule.RetentionPeriod), |
| 136 | + utils.ConvertTimePToDateTimeString(snapshotSchedule.CreatedAt), |
| 137 | + ) |
| 138 | + snapshotSchedulesTable.AddSeparator() |
| 139 | + } |
| 140 | + |
| 141 | + content = append(content, snapshotSchedulesTable) |
| 142 | + } |
| 143 | + |
| 144 | + if err := tables.DisplayTables(p, content); err != nil { |
| 145 | + return fmt.Errorf("render tables: %w", err) |
| 146 | + } |
| 147 | + return nil |
| 148 | + }) |
| 149 | +} |
0 commit comments