|
| 1 | +package deletemovecode |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "github.com/stackitcloud/stackit-cli/internal/pkg/types" |
| 8 | + |
| 9 | + "github.com/stackitcloud/stackit-cli/internal/pkg/args" |
| 10 | + "github.com/stackitcloud/stackit-cli/internal/pkg/errors" |
| 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/dns/client" |
| 15 | + dnsUtils "github.com/stackitcloud/stackit-cli/internal/pkg/services/dns/utils" |
| 16 | + "github.com/stackitcloud/stackit-cli/internal/pkg/utils" |
| 17 | + |
| 18 | + "github.com/spf13/cobra" |
| 19 | + dns "github.com/stackitcloud/stackit-sdk-go/services/dns/v1api" |
| 20 | +) |
| 21 | + |
| 22 | +const ( |
| 23 | + zoneIdArg = "ZONE_ID" |
| 24 | +) |
| 25 | + |
| 26 | +type inputModel struct { |
| 27 | + *globalflags.GlobalFlagModel |
| 28 | + ZoneId string |
| 29 | +} |
| 30 | + |
| 31 | +func NewCmd(params *types.CmdParams) *cobra.Command { |
| 32 | + cmd := &cobra.Command{ |
| 33 | + Use: fmt.Sprintf("delete-move-code %s", zoneIdArg), |
| 34 | + Short: "Deletes the move code of a DNS zone", |
| 35 | + Long: "Deletes (invalidates) the move code of a DNS zone, so it can no longer be used to move the zone to another project.", |
| 36 | + Args: args.SingleArg(zoneIdArg, utils.ValidateUUID), |
| 37 | + Example: examples.Build( |
| 38 | + examples.NewExample( |
| 39 | + `Delete the move code of a DNS zone with ID "xxx"`, |
| 40 | + "$ stackit dns zone delete-move-code xxx"), |
| 41 | + ), |
| 42 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 43 | + ctx := context.Background() |
| 44 | + model, err := parseInput(params.Printer, cmd, args) |
| 45 | + if err != nil { |
| 46 | + return err |
| 47 | + } |
| 48 | + |
| 49 | + // Configure API client |
| 50 | + apiClient, err := client.ConfigureClient(params.Printer, params.CliVersion) |
| 51 | + if err != nil { |
| 52 | + return err |
| 53 | + } |
| 54 | + |
| 55 | + zoneLabel, err := dnsUtils.GetZoneName(ctx, apiClient.DefaultAPI, model.ProjectId, model.ZoneId) |
| 56 | + if err != nil { |
| 57 | + params.Printer.Debug(print.ErrorLevel, "get zone name: %v", err) |
| 58 | + zoneLabel = model.ZoneId |
| 59 | + } |
| 60 | + prompt := fmt.Sprintf("Are you sure you want to delete the move code of zone %q? (This cannot be undone)", zoneLabel) |
| 61 | + err = params.Printer.PromptForConfirmation(prompt) |
| 62 | + if err != nil { |
| 63 | + return err |
| 64 | + } |
| 65 | + |
| 66 | + // Call API |
| 67 | + req := buildRequest(ctx, model, apiClient) |
| 68 | + resp, err := req.Execute() |
| 69 | + if err != nil { |
| 70 | + return fmt.Errorf("delete DNS zone move code: %w", err) |
| 71 | + } |
| 72 | + |
| 73 | + return outputResult(params.Printer, model, zoneLabel, resp) |
| 74 | + }, |
| 75 | + } |
| 76 | + return cmd |
| 77 | +} |
| 78 | + |
| 79 | +func parseInput(p *print.Printer, cmd *cobra.Command, inputArgs []string) (*inputModel, error) { |
| 80 | + zoneId := inputArgs[0] |
| 81 | + globalFlags := globalflags.Parse(p, cmd) |
| 82 | + if globalFlags.ProjectId == "" { |
| 83 | + return nil, &errors.ProjectIdError{} |
| 84 | + } |
| 85 | + |
| 86 | + model := inputModel{ |
| 87 | + GlobalFlagModel: globalFlags, |
| 88 | + ZoneId: zoneId, |
| 89 | + } |
| 90 | + |
| 91 | + p.DebugInputModel(model) |
| 92 | + return &model, nil |
| 93 | +} |
| 94 | + |
| 95 | +func buildRequest(ctx context.Context, model *inputModel, apiClient *dns.APIClient) dns.ApiDeleteMoveCodeRequest { |
| 96 | + req := apiClient.DefaultAPI.DeleteMoveCode(ctx, model.ProjectId, model.ZoneId) |
| 97 | + return req |
| 98 | +} |
| 99 | + |
| 100 | +func outputResult(p *print.Printer, model *inputModel, zoneLabel string, resp *dns.Message) error { |
| 101 | + if resp == nil { |
| 102 | + return fmt.Errorf("move code deletion response is empty") |
| 103 | + } |
| 104 | + |
| 105 | + return p.OutputResult(model.OutputFormat, resp, func() error { |
| 106 | + p.Outputf("Deleted move code of zone %s\n", zoneLabel) |
| 107 | + return nil |
| 108 | + }) |
| 109 | +} |
0 commit comments