|
1 | 1 | package describe |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "strings" |
| 8 | + |
| 9 | + "github.com/goccy/go-yaml" |
4 | 10 | "github.com/spf13/cobra" |
5 | 11 | "github.com/stackitcloud/stackit-cli/internal/pkg/args" |
| 12 | + "github.com/stackitcloud/stackit-cli/internal/pkg/errors" |
6 | 13 | "github.com/stackitcloud/stackit-cli/internal/pkg/examples" |
| 14 | + "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" |
7 | 15 | "github.com/stackitcloud/stackit-cli/internal/pkg/print" |
| 16 | + "github.com/stackitcloud/stackit-cli/internal/pkg/services/iaas/client" |
| 17 | + "github.com/stackitcloud/stackit-cli/internal/pkg/tables" |
| 18 | + "github.com/stackitcloud/stackit-cli/internal/pkg/utils" |
| 19 | + "github.com/stackitcloud/stackit-sdk-go/services/iaas" |
8 | 20 | ) |
9 | 21 |
|
| 22 | +type inputModel struct { |
| 23 | + *globalflags.GlobalFlagModel |
| 24 | + SecurityGroupId string |
| 25 | +} |
| 26 | + |
| 27 | +const argNameGroupId = "argGroupId" |
| 28 | + |
10 | 29 | func NewCmd(p *print.Printer) *cobra.Command { |
11 | 30 | cmd := &cobra.Command{ |
12 | 31 | Use: "describe", |
13 | 32 | Short: "describe security groups", |
14 | 33 | Long: "describe security groups", |
15 | | - Args: args.NoArgs, |
| 34 | + Args: args.SingleArg(argNameGroupId, utils.ValidateUUID), |
16 | 35 | Example: examples.Build( |
17 | | - examples.NewExample(`example 1`, `foo bar baz`), |
18 | | - examples.NewExample(`example 2`, `foo bar baz`), |
| 36 | + examples.NewExample(`describe an existing group`, `$ stackit beta security-group describe 9e9c44fe-eb9a-4d45-bf08-365e961845d1`), |
19 | 37 | ), |
20 | 38 | RunE: func(cmd *cobra.Command, args []string) error { |
21 | 39 | return executeDescribe(cmd, p, args) |
22 | 40 | }, |
23 | 41 | } |
24 | | - cmd.Flags().String("dummy", "foo", "fooify") |
| 42 | + configureFlags(cmd) |
| 43 | + |
25 | 44 | return cmd |
26 | 45 | } |
27 | 46 |
|
28 | 47 | func executeDescribe(cmd *cobra.Command, p *print.Printer, args []string) error { |
29 | 48 | p.Info("executing describe command") |
| 49 | + ctx := context.Background() |
| 50 | + model, err := parseInput(p, cmd, args) |
| 51 | + if err != nil { |
| 52 | + return err |
| 53 | + } |
| 54 | + |
| 55 | + // Configure API client |
| 56 | + apiClient, err := client.ConfigureClient(p) |
| 57 | + if err != nil { |
| 58 | + return err |
| 59 | + } |
| 60 | + |
| 61 | + // Call API |
| 62 | + request := buildRequest(ctx, model, apiClient) |
| 63 | + |
| 64 | + p.Info("security group %q for %q\n", model.SecurityGroupId, model.ProjectId) |
| 65 | + |
| 66 | + group, err := request.Execute() |
| 67 | + if err != nil { |
| 68 | + return fmt.Errorf("get security group: %w", err) |
| 69 | + } |
| 70 | + if err := outputResult(p, model, group); err != nil { |
| 71 | + return err |
| 72 | + } |
| 73 | + |
30 | 74 | return nil |
31 | 75 | } |
| 76 | + |
| 77 | +func configureFlags(cmd *cobra.Command) { |
| 78 | + globalflags.Configure(cmd.Flags()) |
| 79 | +} |
| 80 | + |
| 81 | +func buildRequest(ctx context.Context, model *inputModel, apiClient *iaas.APIClient) iaas.ApiGetSecurityGroupRequest { |
| 82 | + request := apiClient.GetSecurityGroup(ctx, model.ProjectId, model.SecurityGroupId) |
| 83 | + return request |
| 84 | + |
| 85 | +} |
| 86 | + |
| 87 | +func parseInput(p *print.Printer, cmd *cobra.Command, args []string) (*inputModel, error) { |
| 88 | + globalFlags := globalflags.Parse(p, cmd) |
| 89 | + if globalFlags.ProjectId == "" { |
| 90 | + return nil, &errors.ProjectIdError{} |
| 91 | + } |
| 92 | + if err := cmd.ValidateArgs(args); err != nil { |
| 93 | + return nil, &errors.ArgValidationError{ |
| 94 | + Arg: argNameGroupId, |
| 95 | + Details: fmt.Sprintf("argument validation failed: %v", err), |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + model := inputModel{ |
| 100 | + GlobalFlagModel: globalFlags, |
| 101 | + SecurityGroupId: args[0], |
| 102 | + } |
| 103 | + |
| 104 | + if p.IsVerbosityDebug() { |
| 105 | + modelStr, err := print.BuildDebugStrFromInputModel(model) |
| 106 | + if err != nil { |
| 107 | + p.Debug(print.ErrorLevel, "convert model to string for debugging: %v", err) |
| 108 | + } else { |
| 109 | + p.Debug(print.DebugLevel, "parsed input values: %s", modelStr) |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + return &model, nil |
| 114 | +} |
| 115 | + |
| 116 | +func outputResult(p *print.Printer, model *inputModel, resp *iaas.SecurityGroup) error { |
| 117 | + switch model.OutputFormat { |
| 118 | + case print.JSONOutputFormat: |
| 119 | + details, err := json.MarshalIndent(resp, "", " ") |
| 120 | + if err != nil { |
| 121 | + return fmt.Errorf("marshal security group: %w", err) |
| 122 | + } |
| 123 | + p.Outputln(string(details)) |
| 124 | + |
| 125 | + return nil |
| 126 | + case print.YAMLOutputFormat: |
| 127 | + details, err := yaml.MarshalWithOptions(resp, yaml.IndentSequence(true)) |
| 128 | + if err != nil { |
| 129 | + return fmt.Errorf("marshal security group: %w", err) |
| 130 | + } |
| 131 | + p.Outputln(string(details)) |
| 132 | + |
| 133 | + return nil |
| 134 | + default: |
| 135 | + table := tables.NewTable() |
| 136 | + if id := resp.Id; id != nil { |
| 137 | + table.AddRow("ID", *id) |
| 138 | + } |
| 139 | + table.AddSeparator() |
| 140 | + |
| 141 | + if name := resp.Name; name != nil { |
| 142 | + table.AddRow("NAME", name) |
| 143 | + table.AddSeparator() |
| 144 | + } |
| 145 | + |
| 146 | + if description := resp.Description; description != nil { |
| 147 | + table.AddRow("DESCRIPTION", description) |
| 148 | + table.AddSeparator() |
| 149 | + } |
| 150 | + |
| 151 | + if labels := resp.Labels; labels != nil { |
| 152 | + var builder strings.Builder |
| 153 | + for k, v := range *labels { |
| 154 | + builder.WriteString(fmt.Sprintf("%s=%s ", k, v)) |
| 155 | + } |
| 156 | + table.AddRow("LABELS", builder.String()) |
| 157 | + table.AddSeparator() |
| 158 | + } |
| 159 | + |
| 160 | + if stateful := resp.Stateful; stateful != nil { |
| 161 | + table.AddRow("STATEFUL", stateful) |
| 162 | + table.AddSeparator() |
| 163 | + } |
| 164 | + |
| 165 | + if err := table.Display(p); err != nil { |
| 166 | + return fmt.Errorf("render table: %w", err) |
| 167 | + } |
| 168 | + |
| 169 | + return nil |
| 170 | + } |
| 171 | +} |
0 commit comments