|
| 1 | +package attach |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + |
| 8 | + "github.com/stackitcloud/stackit-cli/internal/pkg/args" |
| 9 | + "github.com/stackitcloud/stackit-cli/internal/pkg/errors" |
| 10 | + "github.com/stackitcloud/stackit-cli/internal/pkg/examples" |
| 11 | + "github.com/stackitcloud/stackit-cli/internal/pkg/flags" |
| 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/iaas/client" |
| 15 | + iaasUtils "github.com/stackitcloud/stackit-cli/internal/pkg/services/iaas/utils" |
| 16 | + |
| 17 | + "github.com/goccy/go-yaml" |
| 18 | + "github.com/spf13/cobra" |
| 19 | + "github.com/stackitcloud/stackit-sdk-go/services/iaas" |
| 20 | +) |
| 21 | + |
| 22 | +const ( |
| 23 | + serviceAccMailArg = "SERVICE_ACCOUNT_EMAIL" |
| 24 | + |
| 25 | + serverIdFlag = "server-id" |
| 26 | +) |
| 27 | + |
| 28 | +type inputModel struct { |
| 29 | + *globalflags.GlobalFlagModel |
| 30 | + ServerId *string |
| 31 | + ServiceAccMail string |
| 32 | +} |
| 33 | + |
| 34 | +func NewCmd(p *print.Printer) *cobra.Command { |
| 35 | + cmd := &cobra.Command{ |
| 36 | + Use: "attach", |
| 37 | + Short: "Attach a service account to a server", |
| 38 | + Long: "Attach a service account to a server", |
| 39 | + Args: args.SingleArg(serviceAccMailArg, nil), |
| 40 | + Example: examples.Build( |
| 41 | + examples.NewExample( |
| 42 | + `Attach a service account with mail "[email protected]" to a server with ID "yyy"`, |
| 43 | + "$ stackit beta server service-account attach [email protected] --server-id yyy", |
| 44 | + ), |
| 45 | + ), |
| 46 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 47 | + ctx := context.Background() |
| 48 | + model, err := parseInput(p, cmd, args) |
| 49 | + if err != nil { |
| 50 | + return err |
| 51 | + } |
| 52 | + |
| 53 | + // Configure API client |
| 54 | + apiClient, err := client.ConfigureClient(p) |
| 55 | + if err != nil { |
| 56 | + return err |
| 57 | + } |
| 58 | + serverLabel, err := iaasUtils.GetServerName(ctx, apiClient, model.ProjectId, *model.ServerId) |
| 59 | + if err != nil { |
| 60 | + p.Debug(print.ErrorLevel, "get server name: %v", err) |
| 61 | + serverLabel = *model.ServerId |
| 62 | + } |
| 63 | + |
| 64 | + if !model.AssumeYes { |
| 65 | + prompt := fmt.Sprintf("Are you sure you want to attach service account %q to server %q?", model.ServiceAccMail, serverLabel) |
| 66 | + err = p.PromptForConfirmation(prompt) |
| 67 | + if err != nil { |
| 68 | + return err |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + // Call API |
| 73 | + req := buildRequest(ctx, model, apiClient) |
| 74 | + resp, err := req.Execute() |
| 75 | + if err != nil { |
| 76 | + return fmt.Errorf("attach service account to server: %w", err) |
| 77 | + } |
| 78 | + |
| 79 | + return outputResult(p, model.OutputFormat, model.ServiceAccMail, serverLabel, resp) |
| 80 | + }, |
| 81 | + } |
| 82 | + configureFlags(cmd) |
| 83 | + return cmd |
| 84 | +} |
| 85 | + |
| 86 | +func configureFlags(cmd *cobra.Command) { |
| 87 | + cmd.Flags().VarP(flags.UUIDFlag(), serverIdFlag, "s", "Server ID") |
| 88 | + |
| 89 | + err := flags.MarkFlagsRequired(cmd, serverIdFlag) |
| 90 | + cobra.CheckErr(err) |
| 91 | +} |
| 92 | + |
| 93 | +func parseInput(p *print.Printer, cmd *cobra.Command, inputArgs []string) (*inputModel, error) { |
| 94 | + serviceAccMail := inputArgs[0] |
| 95 | + globalFlags := globalflags.Parse(p, cmd) |
| 96 | + if globalFlags.ProjectId == "" { |
| 97 | + return nil, &errors.ProjectIdError{} |
| 98 | + } |
| 99 | + |
| 100 | + model := inputModel{ |
| 101 | + GlobalFlagModel: globalFlags, |
| 102 | + ServerId: flags.FlagToStringPointer(p, cmd, serverIdFlag), |
| 103 | + ServiceAccMail: serviceAccMail, |
| 104 | + } |
| 105 | + |
| 106 | + if p.IsVerbosityDebug() { |
| 107 | + modelStr, err := print.BuildDebugStrFromInputModel(model) |
| 108 | + if err != nil { |
| 109 | + p.Debug(print.ErrorLevel, "convert model to string for debugging: %v", err) |
| 110 | + } else { |
| 111 | + p.Debug(print.DebugLevel, "parsed input values: %s", modelStr) |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + return &model, nil |
| 116 | +} |
| 117 | + |
| 118 | +func buildRequest(ctx context.Context, model *inputModel, apiClient *iaas.APIClient) iaas.ApiAddServiceAccountToServerRequest { |
| 119 | + req := apiClient.AddServiceAccountToServer(ctx, model.ProjectId, *model.ServerId, model.ServiceAccMail) |
| 120 | + return req |
| 121 | +} |
| 122 | + |
| 123 | +func outputResult(p *print.Printer, outputFormat, serviceAccMail, serverLabel string, serviceAccounts *iaas.ServiceAccountMailListResponse) error { |
| 124 | + switch outputFormat { |
| 125 | + case print.JSONOutputFormat: |
| 126 | + details, err := json.MarshalIndent(serviceAccounts, "", " ") |
| 127 | + if err != nil { |
| 128 | + return fmt.Errorf("marshal service account: %w", err) |
| 129 | + } |
| 130 | + p.Outputln(string(details)) |
| 131 | + |
| 132 | + return nil |
| 133 | + case print.YAMLOutputFormat: |
| 134 | + details, err := yaml.MarshalWithOptions(serviceAccounts, yaml.IndentSequence(true)) |
| 135 | + if err != nil { |
| 136 | + return fmt.Errorf("marshal service account: %w", err) |
| 137 | + } |
| 138 | + p.Outputln(string(details)) |
| 139 | + |
| 140 | + return nil |
| 141 | + default: |
| 142 | + p.Outputf("Attached service account %q to server %q\n", serviceAccMail, serverLabel) |
| 143 | + return nil |
| 144 | + } |
| 145 | +} |
0 commit comments