|
| 1 | +package create |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + |
| 8 | + "github.com/goccy/go-yaml" |
| 9 | + "github.com/spf13/cobra" |
| 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/flags" |
| 14 | + "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" |
| 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/utils" |
| 18 | + "github.com/stackitcloud/stackit-sdk-go/services/iaas" |
| 19 | +) |
| 20 | + |
| 21 | +const ( |
| 22 | + nameFlag = "name" |
| 23 | + policyFlag = "policy" |
| 24 | +) |
| 25 | + |
| 26 | +type inputModel struct { |
| 27 | + *globalflags.GlobalFlagModel |
| 28 | + Name string |
| 29 | + Policy string |
| 30 | +} |
| 31 | + |
| 32 | +func NewCmd(p *print.Printer) *cobra.Command { |
| 33 | + cmd := &cobra.Command{ |
| 34 | + Use: "create", |
| 35 | + Short: "Creates an affinity groups", |
| 36 | + Long: `Creates an affinity groups.`, |
| 37 | + Args: args.NoArgs, |
| 38 | + Example: examples.Build( |
| 39 | + examples.NewExample( |
| 40 | + `Create an affinity group with name "AFFINITY_GROUP_NAME" and policy "soft-affinity"`, |
| 41 | + "$ stackit beta affinity-group create --name AFFINITY_GROUP_NAME --policy soft-affinity", |
| 42 | + ), |
| 43 | + ), |
| 44 | + RunE: func(cmd *cobra.Command, _ []string) error { |
| 45 | + ctx := context.Background() |
| 46 | + model, err := parseInput(p, cmd) |
| 47 | + if err != nil { |
| 48 | + return err |
| 49 | + } |
| 50 | + |
| 51 | + // Configure API client |
| 52 | + apiClient, err := client.ConfigureClient(p) |
| 53 | + if err != nil { |
| 54 | + return err |
| 55 | + } |
| 56 | + |
| 57 | + if !model.AssumeYes { |
| 58 | + prompt := fmt.Sprintf("Are you sure you want to create the affinity group %q?", model.Name) |
| 59 | + err = p.PromptForConfirmation(prompt) |
| 60 | + if err != nil { |
| 61 | + return err |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + // Call API |
| 66 | + request := buildRequest(ctx, *model, apiClient) |
| 67 | + |
| 68 | + result, err := request.Execute() |
| 69 | + if err != nil { |
| 70 | + return fmt.Errorf("create affinity group: %w", err) |
| 71 | + } |
| 72 | + if resp := result; resp != nil { |
| 73 | + return outputResult(p, *model, *resp) |
| 74 | + } |
| 75 | + return fmt.Errorf("create affinity group: nil result") |
| 76 | + }, |
| 77 | + } |
| 78 | + configureFlags(cmd) |
| 79 | + return cmd |
| 80 | +} |
| 81 | + |
| 82 | +func configureFlags(cmd *cobra.Command) { |
| 83 | + cmd.Flags().String(nameFlag, "", "The name of the affinity group.") |
| 84 | + cmd.Flags().String(policyFlag, "", `The policy for the affinity group. Valid values for the policy are: "hard-affinity", "hard-anti-affinity", "soft-affinity", "soft-anti-affinity"`) |
| 85 | + |
| 86 | + if err := flags.MarkFlagsRequired(cmd, nameFlag, policyFlag); err != nil { |
| 87 | + cobra.CheckErr(err) |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +func buildRequest(ctx context.Context, model inputModel, apiClient *iaas.APIClient) iaas.ApiCreateAffinityGroupRequest { |
| 92 | + req := apiClient.CreateAffinityGroup(ctx, model.ProjectId) |
| 93 | + req = req.CreateAffinityGroupPayload( |
| 94 | + iaas.CreateAffinityGroupPayload{ |
| 95 | + Name: utils.Ptr(model.Name), |
| 96 | + Policy: utils.Ptr(model.Policy), |
| 97 | + }, |
| 98 | + ) |
| 99 | + return req |
| 100 | +} |
| 101 | + |
| 102 | +func parseInput(p *print.Printer, cmd *cobra.Command) (*inputModel, error) { |
| 103 | + globalFlags := globalflags.Parse(p, cmd) |
| 104 | + if globalFlags.ProjectId == "" { |
| 105 | + return nil, &errors.ProjectIdError{} |
| 106 | + } |
| 107 | + |
| 108 | + model := inputModel{ |
| 109 | + GlobalFlagModel: globalFlags, |
| 110 | + Name: flags.FlagToStringValue(p, cmd, nameFlag), |
| 111 | + Policy: flags.FlagToStringValue(p, cmd, policyFlag), |
| 112 | + } |
| 113 | + |
| 114 | + if p.IsVerbosityDebug() { |
| 115 | + modelStr, err := print.BuildDebugStrFromInputModel(model) |
| 116 | + if err != nil { |
| 117 | + p.Debug(print.ErrorLevel, "convert model to string for debugging: %v", err) |
| 118 | + } else { |
| 119 | + p.Debug(print.DebugLevel, "parsed input values: %s", modelStr) |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + return &model, nil |
| 124 | +} |
| 125 | + |
| 126 | +func outputResult(p *print.Printer, model inputModel, resp iaas.AffinityGroup) error { |
| 127 | + outputFormat := "" |
| 128 | + if model.GlobalFlagModel != nil { |
| 129 | + outputFormat = model.GlobalFlagModel.OutputFormat |
| 130 | + } |
| 131 | + switch outputFormat { |
| 132 | + case print.JSONOutputFormat: |
| 133 | + details, err := json.MarshalIndent(resp, "", " ") |
| 134 | + if err != nil { |
| 135 | + return fmt.Errorf("marshal affinity group: %w", err) |
| 136 | + } |
| 137 | + p.Outputln(string(details)) |
| 138 | + case print.YAMLOutputFormat: |
| 139 | + details, err := yaml.MarshalWithOptions(resp, yaml.IndentSequence(true), yaml.UseJSONMarshaler()) |
| 140 | + if err != nil { |
| 141 | + return fmt.Errorf("marshal affinity group: %w", err) |
| 142 | + } |
| 143 | + p.Outputln(string(details)) |
| 144 | + default: |
| 145 | + p.Outputf("Created affinity group %q with id %s\n", model.Name, utils.PtrString(resp.Id)) |
| 146 | + } |
| 147 | + return nil |
| 148 | +} |
0 commit comments