|
| 1 | +package attach |
| 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 | + cliErr "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 | + iaasUtils "github.com/stackitcloud/stackit-cli/internal/pkg/services/iaas/utils" |
| 18 | + "github.com/stackitcloud/stackit-sdk-go/services/iaas" |
| 19 | +) |
| 20 | + |
| 21 | +const ( |
| 22 | + serverIdFlag = "server-id" |
| 23 | + securityGroupIdFlag = "security-group-id" |
| 24 | +) |
| 25 | + |
| 26 | +type inputModel struct { |
| 27 | + *globalflags.GlobalFlagModel |
| 28 | + ServerId string |
| 29 | + SecurityGroupId string |
| 30 | +} |
| 31 | + |
| 32 | +func NewCmd(params *types.CmdParams) *cobra.Command { |
| 33 | + cmd := &cobra.Command{ |
| 34 | + Use: "attach", |
| 35 | + Short: "Attaches a security group to a server", |
| 36 | + Long: "Attaches a security group to a server.", |
| 37 | + Args: args.NoArgs, |
| 38 | + Example: examples.Build( |
| 39 | + examples.NewExample( |
| 40 | + `Attach a security group with ID "xxx" to a server with ID "yyy"`, |
| 41 | + `$ stackit server security-group attach --server-id yyy --security-group-id xxx`, |
| 42 | + ), |
| 43 | + ), |
| 44 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 45 | + ctx := context.Background() |
| 46 | + model, err := parseInput(params.Printer, cmd, args) |
| 47 | + if err != nil { |
| 48 | + return err |
| 49 | + } |
| 50 | + |
| 51 | + // Configure API client |
| 52 | + apiClient, err := client.ConfigureClient(params.Printer, params.CliVersion) |
| 53 | + if err != nil { |
| 54 | + return err |
| 55 | + } |
| 56 | + |
| 57 | + serverLabel, err := iaasUtils.GetServerName(ctx, apiClient, model.ProjectId, model.Region, model.ServerId) |
| 58 | + if err != nil { |
| 59 | + params.Printer.Debug(print.ErrorLevel, "get server name: %v", err) |
| 60 | + serverLabel = model.ServerId |
| 61 | + } else if serverLabel == "" { |
| 62 | + serverLabel = model.ServerId |
| 63 | + } |
| 64 | + |
| 65 | + securityGroupLabel, err := iaasUtils.GetSecurityGroupName(ctx, apiClient, model.ProjectId, model.Region, model.SecurityGroupId) |
| 66 | + if err != nil { |
| 67 | + params.Printer.Debug(print.ErrorLevel, "get security group name: %v", err) |
| 68 | + securityGroupLabel = model.SecurityGroupId |
| 69 | + } |
| 70 | + |
| 71 | + prompt := fmt.Sprintf("Are you sure you want to attach security group %q to server %q?", securityGroupLabel, serverLabel) |
| 72 | + err = params.Printer.PromptForConfirmation(prompt) |
| 73 | + if err != nil { |
| 74 | + return err |
| 75 | + } |
| 76 | + |
| 77 | + // Call API |
| 78 | + req := buildRequest(ctx, model, apiClient) |
| 79 | + if err := req.Execute(); err != nil { |
| 80 | + return fmt.Errorf("attach security group to server: %w", err) |
| 81 | + } |
| 82 | + |
| 83 | + params.Printer.Outputf("Attached security group %q to server %q\n", securityGroupLabel, serverLabel) |
| 84 | + |
| 85 | + return nil |
| 86 | + }, |
| 87 | + } |
| 88 | + configureFlags(cmd) |
| 89 | + return cmd |
| 90 | +} |
| 91 | + |
| 92 | +func configureFlags(cmd *cobra.Command) { |
| 93 | + cmd.Flags().Var(flags.UUIDFlag(), serverIdFlag, "Server ID") |
| 94 | + cmd.Flags().Var(flags.UUIDFlag(), securityGroupIdFlag, "Security Group ID") |
| 95 | + |
| 96 | + err := flags.MarkFlagsRequired(cmd, serverIdFlag, securityGroupIdFlag) |
| 97 | + cobra.CheckErr(err) |
| 98 | +} |
| 99 | + |
| 100 | +func parseInput(p *print.Printer, cmd *cobra.Command, _ []string) (*inputModel, error) { |
| 101 | + globalFlags := globalflags.Parse(p, cmd) |
| 102 | + if globalFlags.ProjectId == "" { |
| 103 | + return nil, &cliErr.ProjectIdError{} |
| 104 | + } |
| 105 | + |
| 106 | + model := inputModel{ |
| 107 | + GlobalFlagModel: globalFlags, |
| 108 | + ServerId: flags.FlagToStringValue(p, cmd, serverIdFlag), |
| 109 | + SecurityGroupId: flags.FlagToStringValue(p, cmd, securityGroupIdFlag), |
| 110 | + } |
| 111 | + |
| 112 | + p.DebugInputModel(model) |
| 113 | + return &model, nil |
| 114 | +} |
| 115 | + |
| 116 | +func buildRequest(ctx context.Context, model *inputModel, apiClient *iaas.APIClient) iaas.ApiAddSecurityGroupToServerRequest { |
| 117 | + req := apiClient.AddSecurityGroupToServer(ctx, model.ProjectId, model.Region, model.ServerId, model.SecurityGroupId) |
| 118 | + return req |
| 119 | +} |
0 commit comments