|
| 1 | +package describe |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "github.com/stackitcloud/stackit-cli/internal/pkg/types" |
| 8 | + |
| 9 | + "github.com/spf13/cobra" |
| 10 | + "github.com/stackitcloud/stackit-cli/internal/pkg/args" |
| 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/services/resourcemanager/client" |
| 15 | + "github.com/stackitcloud/stackit-cli/internal/pkg/tables" |
| 16 | + "github.com/stackitcloud/stackit-cli/internal/pkg/utils" |
| 17 | + "github.com/stackitcloud/stackit-sdk-go/services/resourcemanager" |
| 18 | +) |
| 19 | + |
| 20 | +const ( |
| 21 | + organizationIdArg = "ORGANIZATION_ID" |
| 22 | +) |
| 23 | + |
| 24 | +type inputModel struct { |
| 25 | + *globalflags.GlobalFlagModel |
| 26 | + OrganizationId string |
| 27 | +} |
| 28 | + |
| 29 | +func NewCmd(params *types.CmdParams) *cobra.Command { |
| 30 | + cmd := &cobra.Command{ |
| 31 | + Use: "describe", |
| 32 | + Short: "Show an organization", |
| 33 | + Long: "Show an organization.", |
| 34 | + // the arg can be the organization uuid or the container id, which is not a uuid, so no validation needed |
| 35 | + Args: args.SingleArg(organizationIdArg, nil), |
| 36 | + Example: examples.Build( |
| 37 | + examples.NewExample( |
| 38 | + `Describe the organization with the organization uuid "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"`, |
| 39 | + "$ stackit organization describe xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx", |
| 40 | + ), |
| 41 | + examples.NewExample( |
| 42 | + `Describe the organization with the container id "foo-bar-organization"`, |
| 43 | + "$ stackit organization describe foo-bar-organization", |
| 44 | + ), |
| 45 | + ), |
| 46 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 47 | + ctx := context.Background() |
| 48 | + model, err := parseInput(params.Printer, cmd, args) |
| 49 | + if err != nil { |
| 50 | + return err |
| 51 | + } |
| 52 | + |
| 53 | + // Configure API client |
| 54 | + apiClient, err := client.ConfigureClient(params.Printer, params.CliVersion) |
| 55 | + if err != nil { |
| 56 | + return err |
| 57 | + } |
| 58 | + |
| 59 | + // Call API |
| 60 | + req := buildRequest(ctx, model, apiClient) |
| 61 | + resp, err := req.Execute() |
| 62 | + if err != nil { |
| 63 | + return err |
| 64 | + } |
| 65 | + |
| 66 | + return outputResult(params.Printer, model.OutputFormat, resp) |
| 67 | + }, |
| 68 | + } |
| 69 | + return cmd |
| 70 | +} |
| 71 | + |
| 72 | +func parseInput(p *print.Printer, cmd *cobra.Command, inputArgs []string) (*inputModel, error) { |
| 73 | + organizationId := inputArgs[0] |
| 74 | + globalFlags := globalflags.Parse(p, cmd) |
| 75 | + |
| 76 | + model := inputModel{ |
| 77 | + GlobalFlagModel: globalFlags, |
| 78 | + OrganizationId: organizationId, |
| 79 | + } |
| 80 | + |
| 81 | + p.DebugInputModel(model) |
| 82 | + return &model, nil |
| 83 | +} |
| 84 | + |
| 85 | +func buildRequest(ctx context.Context, model *inputModel, apiClient *resourcemanager.APIClient) resourcemanager.ApiGetOrganizationRequest { |
| 86 | + req := apiClient.GetOrganization(ctx, model.OrganizationId) |
| 87 | + return req |
| 88 | +} |
| 89 | + |
| 90 | +func outputResult(p *print.Printer, outputFormat string, organization *resourcemanager.OrganizationResponse) error { |
| 91 | + return p.OutputResult(outputFormat, organization, func() error { |
| 92 | + if organization == nil { |
| 93 | + p.Outputln("show organization: empty response") |
| 94 | + return nil |
| 95 | + } |
| 96 | + |
| 97 | + table := tables.NewTable() |
| 98 | + |
| 99 | + table.AddRow("ORGANIZATION ID", utils.PtrString(organization.OrganizationId)) |
| 100 | + table.AddSeparator() |
| 101 | + table.AddRow("NAME", utils.PtrString(organization.Name)) |
| 102 | + table.AddSeparator() |
| 103 | + table.AddRow("CONTAINER ID", utils.PtrString(organization.ContainerId)) |
| 104 | + table.AddSeparator() |
| 105 | + table.AddRow("STATUS", utils.PtrString(organization.LifecycleState)) |
| 106 | + table.AddSeparator() |
| 107 | + table.AddRow("CREATION TIME", utils.PtrString(organization.CreationTime)) |
| 108 | + table.AddSeparator() |
| 109 | + table.AddRow("UPDATE TIME", utils.PtrString(organization.UpdateTime)) |
| 110 | + table.AddSeparator() |
| 111 | + table.AddRow("LABELS", utils.JoinStringMap(utils.PtrValue(organization.Labels), ": ", ", ")) |
| 112 | + |
| 113 | + err := table.Display(p) |
| 114 | + if err != nil { |
| 115 | + return fmt.Errorf("render table: %w", err) |
| 116 | + } |
| 117 | + return nil |
| 118 | + }) |
| 119 | +} |
0 commit comments