|
| 1 | +package describe |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "strings" |
| 7 | + |
| 8 | + "github.com/spf13/cobra" |
| 9 | + "github.com/stackitcloud/stackit-cli/internal/pkg/args" |
| 10 | + cliErr "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/globalflags" |
| 13 | + "github.com/stackitcloud/stackit-cli/internal/pkg/print" |
| 14 | + "github.com/stackitcloud/stackit-cli/internal/pkg/projectname" |
| 15 | + "github.com/stackitcloud/stackit-cli/internal/pkg/services/automation/client" |
| 16 | + "github.com/stackitcloud/stackit-cli/internal/pkg/tables" |
| 17 | + "github.com/stackitcloud/stackit-cli/internal/pkg/types" |
| 18 | + "github.com/stackitcloud/stackit-cli/internal/pkg/utils" |
| 19 | + automation "github.com/stackitcloud/stackit-sdk-go/services/automation/v1betaapi" |
| 20 | +) |
| 21 | + |
| 22 | +const ( |
| 23 | + templateIdArg = "TEMPLATE_ID" |
| 24 | +) |
| 25 | + |
| 26 | +type inputModel struct { |
| 27 | + *globalflags.GlobalFlagModel |
| 28 | + TemplateId string |
| 29 | +} |
| 30 | + |
| 31 | +func NewCmd(params *types.CmdParams) *cobra.Command { |
| 32 | + cmd := &cobra.Command{ |
| 33 | + Use: "describe", |
| 34 | + Short: "Shows details of a Volume Automation Template", |
| 35 | + Long: "Shows details of a Volume Automation Template.", |
| 36 | + Args: args.SingleArg(templateIdArg, utils.ValidateUUID), |
| 37 | + Example: examples.Build( |
| 38 | + examples.NewExample( |
| 39 | + `Describe the Volume Automation Template with ID "xxx"`, |
| 40 | + "$ stackit beta volume automation template describe xxx"), |
| 41 | + ), |
| 42 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 43 | + ctx := context.Background() |
| 44 | + |
| 45 | + model, err := parseInput(params.Printer, cmd, args) |
| 46 | + if err != nil { |
| 47 | + return err |
| 48 | + } |
| 49 | + |
| 50 | + // Configure API client |
| 51 | + apiClient, err := client.ConfigureClient(params.Printer, params.CliVersion) |
| 52 | + if err != nil { |
| 53 | + return err |
| 54 | + } |
| 55 | + |
| 56 | + // Call API |
| 57 | + resp, err := buildRequest(ctx, model, apiClient).Execute() |
| 58 | + if err != nil { |
| 59 | + return fmt.Errorf("describe volume automation template: %w", err) |
| 60 | + } |
| 61 | + |
| 62 | + // Get projectLabel |
| 63 | + projectLabel, err := projectname.GetProjectName(ctx, params.Printer, params.CliVersion, cmd) |
| 64 | + if err != nil { |
| 65 | + params.Printer.Debug(print.ErrorLevel, "get project name: %v", err) |
| 66 | + projectLabel = model.ProjectId |
| 67 | + } else if projectLabel == "" { |
| 68 | + projectLabel = model.ProjectId |
| 69 | + } |
| 70 | + |
| 71 | + return outputResult(params.Printer, model.OutputFormat, model.TemplateId, projectLabel, resp) |
| 72 | + }, |
| 73 | + } |
| 74 | + return cmd |
| 75 | +} |
| 76 | + |
| 77 | +func buildRequest(ctx context.Context, model *inputModel, apiClient *automation.APIClient) automation.ApiGetVolumeTemplateRequest { |
| 78 | + req := apiClient.DefaultAPI.GetVolumeTemplate(ctx, model.ProjectId, model.Region, model.TemplateId) |
| 79 | + return req |
| 80 | +} |
| 81 | + |
| 82 | +func parseInput(p *print.Printer, cmd *cobra.Command, inputArgs []string) (*inputModel, error) { |
| 83 | + templateId := inputArgs[0] |
| 84 | + |
| 85 | + globalFlags := globalflags.Parse(p, cmd) |
| 86 | + if globalFlags.ProjectId == "" { |
| 87 | + return nil, &cliErr.ProjectIdError{} |
| 88 | + } |
| 89 | + |
| 90 | + model := inputModel{ |
| 91 | + GlobalFlagModel: globalFlags, |
| 92 | + TemplateId: templateId, |
| 93 | + } |
| 94 | + |
| 95 | + p.DebugInputModel(model) |
| 96 | + return &model, nil |
| 97 | +} |
| 98 | + |
| 99 | +func outputResult(p *print.Printer, outputFormat, templateId, projectLabel string, template *automation.GetVolumeTemplateResponse) error { |
| 100 | + return p.OutputResult(outputFormat, template, func() error { |
| 101 | + if template == nil { |
| 102 | + p.Outputf("Volume automation template %q not found in project %q\n", templateId, projectLabel) |
| 103 | + return nil |
| 104 | + } |
| 105 | + |
| 106 | + var content []tables.Table |
| 107 | + |
| 108 | + table := tables.NewTable() |
| 109 | + |
| 110 | + table.SetTitle("Volume Automation Template") |
| 111 | + table.AddRow("ID", template.Id) |
| 112 | + table.AddSeparator() |
| 113 | + table.AddRow("NAME", template.Name) |
| 114 | + table.AddSeparator() |
| 115 | + table.AddRow("DESCRIPTION", template.Description) |
| 116 | + table.AddSeparator() |
| 117 | + if template.Input != nil { |
| 118 | + table.AddRow("INPUT KIND", template.Input.Kind) |
| 119 | + table.AddSeparator() |
| 120 | + } |
| 121 | + |
| 122 | + content = append(content, table) |
| 123 | + |
| 124 | + if template.Output != nil && len(template.Output.Steps) > 0 { |
| 125 | + outputStepsTable := tables.NewTable() |
| 126 | + outputStepsTable.SetTitle("OUTPUT STEPS") |
| 127 | + for _, step := range template.Output.Steps { |
| 128 | + outputStepsTable.AddRow("NAME", step.Name) |
| 129 | + outputStepsTable.AddSeparator() |
| 130 | + if step.Result != nil { |
| 131 | + if step.Result.GetVolumeIDsResult != nil { |
| 132 | + volumeIds := "" |
| 133 | + if len(step.Result.GetVolumeIDsResult.VolumeIDs) > 0 { |
| 134 | + volumeIds = strings.Join(step.Result.GetVolumeIDsResult.VolumeIDs, "\n") |
| 135 | + } |
| 136 | + outputStepsTable.AddRow("VOLUME IDS", volumeIds) |
| 137 | + outputStepsTable.AddSeparator() |
| 138 | + } |
| 139 | + if step.Result.CreateSnapshotsResult != nil { |
| 140 | + createdSnapshotIds := "" |
| 141 | + if len(step.Result.CreateSnapshotsResult.CreatedSnapshotIDs) > 0 { |
| 142 | + createdSnapshotIds = strings.Join(step.Result.CreateSnapshotsResult.CreatedSnapshotIDs, "\n") |
| 143 | + } |
| 144 | + outputStepsTable.AddRow("CREATED SNAPSHOT IDS", createdSnapshotIds) |
| 145 | + outputStepsTable.AddSeparator() |
| 146 | + } |
| 147 | + } |
| 148 | + } |
| 149 | + content = append(content, outputStepsTable) |
| 150 | + } |
| 151 | + |
| 152 | + err := tables.DisplayTables(p, content) |
| 153 | + if err != nil { |
| 154 | + return fmt.Errorf("render tables: %w", err) |
| 155 | + } |
| 156 | + return nil |
| 157 | + }) |
| 158 | +} |
0 commit comments