Skip to content

Commit

Permalink
prompt user by default when terminating an instance with at least 1 b…
Browse files Browse the repository at this point in the history
…lock volume
  • Loading branch information
jawher committed May 5, 2020
1 parent 91d776d commit b033f53
Show file tree
Hide file tree
Showing 11 changed files with 1,041 additions and 957 deletions.
51 changes: 46 additions & 5 deletions internal/namespaces/instance/v1/custom_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -831,9 +831,17 @@ type customTerminateServerRequest struct {
Zone scw.Zone
ServerID string
WithIP bool
WithBlock bool
WithBlock withBlock
}

type withBlock string

const (
withBlockPrompt = withBlock("prompt")
withBlockTrue = withBlock("true")
withBlockFalse = withBlock("false")
)

func serverTerminateCommand() *core.Command {
return &core.Command{
Short: `Terminate server`,
Expand All @@ -855,7 +863,12 @@ func serverTerminateCommand() *core.Command {
{
Name: "with-block",
Short: "Delete the Block Storage volumes attached to the server",
Default: core.DefaultValueSetter("true"),
Default: core.DefaultValueSetter("prompt"),
EnumValues: []string{
string(withBlockPrompt),
string(withBlockTrue),
string(withBlockFalse),
},
},
core.ZoneArgSpec(),
},
Expand Down Expand Up @@ -897,11 +910,15 @@ func serverTerminateCommand() *core.Command {
return nil, err
}

if !terminateServerArgs.WithBlock {
// detach block storage volumes before terminating the isntance to preserve them
preserveBlockVolumes, err := shouldPreserveBlockVolumes(server, terminateServerArgs.WithBlock)
if err != nil {
return nil, err
}

if preserveBlockVolumes {
// detach block storage volumes before terminating the instance to preserve them
var multiErr error
for _, volume := range server.Server.Volumes {

if volume.VolumeType != instance.VolumeTypeBSSD {
continue
}
Expand Down Expand Up @@ -947,3 +964,27 @@ func serverTerminateCommand() *core.Command {
},
}
}

func shouldPreserveBlockVolumes(server *instance.GetServerResponse, terminateWithBlock withBlock) (bool, error) {
switch terminateWithBlock {
case withBlockTrue:
return false, nil
case withBlockFalse:
return true, nil
case withBlockPrompt:
for _, volume := range server.Server.Volumes {
if volume.VolumeType != instance.VolumeTypeBSSD {
continue
}

deleteVolumes, err := interactive.PromptBoolWithConfig(&interactive.PromptBoolConfig{
Prompt: "Do you want to also delete the block volumes attached to this instance ?",
DefaultValue: false,
})
return !deleteVolumes, err
}
return false, nil
default:
return false, fmt.Errorf("unsupported with-block value %v", terminateWithBlock)
}
}

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
successfully deleted ip 51.15.138.78
successfully deleted volume cli-srv-intelligent-euclid-1 (10 GB b_ssd)
successfully deleted ip 51.158.108.20
successfully deleted volume cli-srv-cool-chaplygin-1 (10 GB b_ssd)

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
successfully deleted ip 51.15.138.78
successfully deleted ip 51.158.108.20
successfully deleted volume snapshot-de728daa-0bf6-4c64-abf5-a9477e791c83-2019-03-05_10:13 (20 GB l_ssd)

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
successfully deleted ip 163.172.146.186
successfully deleted ip 51.158.108.20
successfully deleted volume snapshot-de728daa-0bf6-4c64-abf5-a9477e791c83-2019-03-05_10:13 (20 GB l_ssd)
successfully deleted volume cli-srv-eager-driscoll-1 (10 GB b_ssd)
successfully deleted volume cli-srv-unruffled-sanderson-1 (10 GB b_ssd)

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
successfully detached volume cli-srv-zen-pascal-1
successfully detached volume cli-srv-priceless-rosalind-1
successfully deleted ip 51.158.108.20

Large diffs are not rendered by default.

0 comments on commit b033f53

Please sign in to comment.