Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(instance): add terminate command #998

Merged
merged 6 commits into from
May 6, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions internal/namespaces/instance/v1/custom_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -910,12 +910,12 @@ func serverTerminateCommand() *core.Command {
return nil, err
}

preserveBlockVolumes, err := shouldPreserveBlockVolumes(server, terminateServerArgs.WithBlock)
deleteBlockVolumes, err := shouldDeleteBlockVolumes(server, terminateServerArgs.WithBlock)
if err != nil {
return nil, err
}

if preserveBlockVolumes {
if !deleteBlockVolumes {
// detach block storage volumes before terminating the instance to preserve them
var multiErr error
for _, volume := range server.Server.Volumes {
Expand Down Expand Up @@ -949,11 +949,13 @@ func serverTerminateCommand() *core.Command {

var multiErr error
if terminateServerArgs.WithIP && server.Server.PublicIP != nil && !server.Server.PublicIP.Dynamic {
fmt.Printf("delete IP...\n")
err = api.DeleteIP(&instance.DeleteIPRequest{
Zone: terminateServerArgs.Zone,
IP: server.Server.PublicIP.ID,
})
if err != nil {
fmt.Printf("failed to delete iop: %+v\n", err)
multiErr = multierror.Append(multiErr, err)
} else {
_, _ = interactive.Printf("successfully deleted ip %s\n", server.Server.PublicIP.Address.String())
Expand All @@ -965,23 +967,23 @@ func serverTerminateCommand() *core.Command {
}
}

func shouldPreserveBlockVolumes(server *instance.GetServerResponse, terminateWithBlock withBlock) (bool, error) {
func shouldDeleteBlockVolumes(server *instance.GetServerResponse, terminateWithBlock withBlock) (bool, error) {
switch terminateWithBlock {
case withBlockTrue:
return false, nil
case withBlockFalse:
return true, nil
case withBlockFalse:
return false, nil
case withBlockPrompt:
// Only prompt user if at least one block volume is attached to the instance
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 ?",
return interactive.PromptBoolWithConfig(&interactive.PromptBoolConfig{
Prompt: "Do you also want to delete block volumes attached to this instance ?",
DefaultValue: false,
})
return !deleteVolumes, err
}
return false, nil
default:
Expand Down
32 changes: 30 additions & 2 deletions internal/namespaces/instance/v1/custom_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,25 +284,42 @@ func Test_ServerDelete(t *testing.T) {
func Test_ServerTerminate(t *testing.T) {
interactive.IsInteractive = true

t.Run("without IPs", core.Test(&core.TestConfig{
t.Run("without IP", core.Test(&core.TestConfig{
Commands: GetCommands(),
BeforeFunc: core.ExecStoreBeforeCmd("Server", "scw instance server create image=ubuntu-bionic -w"),
Cmd: `scw instance server terminate {{ .Server.ID }}`,
Check: core.TestCheckCombine(
core.TestCheckGolden(),
core.TestCheckExitCode(0),
func(t *testing.T, ctx *core.CheckFuncCtx) {
api := instance.NewAPI(ctx.Client)
server := ctx.Meta["Server"].(*instance.Server)
_, err := api.GetIP(&instance.GetIPRequest{
IP: server.PublicIP.ID,
})
assert.NoError(t, err)
},
),
AfterFunc: core.ExecAfterCmd(`scw instance ip delete {{ index .Server.PublicIP.ID }}`),
DisableParallel: true,
}))

t.Run("with IPs", core.Test(&core.TestConfig{
t.Run("with IP", core.Test(&core.TestConfig{
Commands: GetCommands(),
BeforeFunc: core.ExecStoreBeforeCmd("Server", "scw instance server create image=ubuntu-bionic -w"),
Cmd: `scw instance server terminate {{ .Server.ID }} with-ip=true`,
Check: core.TestCheckCombine(
core.TestCheckGolden(),
core.TestCheckExitCode(0),
func(t *testing.T, ctx *core.CheckFuncCtx) {
api := instance.NewAPI(ctx.Client)
server := ctx.Meta["Server"].(*instance.Server)
_, err := api.GetIP(&instance.GetIPRequest{
IP: server.PublicIP.ID,
})
require.IsType(t, &scw.ResponseError{}, err)
assert.Equal(t, 403, err.(*scw.ResponseError).StatusCode)
},
),
DisableParallel: true,
}))
Expand All @@ -319,6 +336,17 @@ func Test_ServerTerminate(t *testing.T) {
DisableParallel: true,
}))

t.Run("with block", core.Test(&core.TestConfig{
Commands: GetCommands(),
BeforeFunc: core.ExecStoreBeforeCmd("Server", "scw instance server create image=ubuntu-bionic additional-volumes.0=block:10G -w"),
Cmd: `scw instance server terminate {{ .Server.ID }} with-ip=true with-block=true`,
Check: core.TestCheckCombine(
core.TestCheckGolden(),
core.TestCheckExitCode(0),
),
DisableParallel: true,
}))

interactive.IsInteractive = false
}

Expand Down
Loading