Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ ARGS:

FLAGS:
-h, --help help for terminate
-w, --wait wait until the server and its resources are deleted

GLOBAL FLAGS:
-c, --config string The path to the config file
Expand Down
6 changes: 5 additions & 1 deletion internal/core/cobra_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,11 @@ func (b *cobraBuilder) hydrateCobra(cobraCmd *cobra.Command, cmd *Command, group
}

if cmd.WaitFunc != nil {
cobraCmd.PersistentFlags().BoolP("wait", "w", false, "wait until the "+cmd.Resource+" is ready")
waitUsage := "wait until the " + cmd.Resource + " is ready"
if cmd.WaitUsage != "" {
waitUsage = cmd.WaitUsage
}
cobraCmd.PersistentFlags().BoolP("wait", "w", false, waitUsage)
}
}

Expand Down
3 changes: 3 additions & 0 deletions internal/core/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ type Command struct {
// WaitFunc will be called if non-nil when the -w (--wait) flag is passed.
WaitFunc WaitFunc

// WaitUsage override the usage for the -w (--wait) flag
WaitUsage string

// Aliases contains a list of aliases for a command
Aliases []string
// cache command path
Expand Down
2 changes: 2 additions & 0 deletions internal/core/result.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ type SuccessResult struct {
Resource string
Verb string
Empty bool
// Used to pass resource to an AfterFunc on success
TargetResource any
}

// This type can be return by a command that need to output specific content on stdout directly.
Expand Down
47 changes: 46 additions & 1 deletion internal/namespaces/instance/v1/custom_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package instance
import (
"bytes"
"context"
"errors"
"fmt"
"net"
"reflect"
Expand Down Expand Up @@ -1127,6 +1128,48 @@ func serverTerminateCommand() *core.Command {
Short: "Stop a running server",
},
},
WaitUsage: "wait until the server and its resources are deleted",
WaitFunc: func(ctx context.Context, argsI, respI interface{}) (interface{}, error) {
terminateServerArgs := argsI.(*customTerminateServerRequest)
server := respI.(*core.SuccessResult).TargetResource.(*instance.Server)
client := core.ExtractClient(ctx)
api := instance.NewAPI(client)

notFoundErr := &scw.ResourceNotFoundError{}

_, err := api.WaitForServer(&instance.WaitForServerRequest{
Zone: server.Zone,
ServerID: server.ID,
Timeout: scw.TimeDurationPtr(serverActionTimeout),
RetryInterval: core.DefaultRetryInterval,
})
if err != nil {
err = errors.Unwrap(err)
if !errors.As(err, &notFoundErr) {
return nil, err
}
}

if terminateServerArgs.WithBlock == withBlockTrue {
for _, volume := range server.Volumes {
if volume.VolumeType != instance.VolumeServerVolumeTypeBSSD {
continue
}
_, err := api.WaitForVolume(&instance.WaitForVolumeRequest{
VolumeID: volume.ID,
Zone: volume.Zone,
})
if err != nil {
if errors.As(err, &notFoundErr) {
continue
}
return nil, err
}
}
}

return respI, nil
},
Run: func(ctx context.Context, argsI interface{}) (interface{}, error) {
terminateServerArgs := argsI.(*customTerminateServerRequest)

Expand Down Expand Up @@ -1183,7 +1226,9 @@ func serverTerminateCommand() *core.Command {
_, _ = interactive.Printf("successfully deleted ip %s\n", server.Server.PublicIP.Address.String())
}

return &core.SuccessResult{}, err
return &core.SuccessResult{
TargetResource: server.Server,
}, err
},
}
}
Expand Down
11 changes: 10 additions & 1 deletion internal/namespaces/instance/v1/custom_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -357,10 +357,19 @@ func Test_ServerTerminate(t *testing.T) {
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`,
Cmd: `scw instance server terminate {{ .Server.ID }} with-ip=true with-block=true -w`,
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.GetVolume(&instance.GetVolumeRequest{
VolumeID: server.Volumes["0"].ID,
Zone: server.Zone,
})
require.IsType(t, &scw.ResourceNotFoundError{}, err)
},
),
DisableParallel: true,
}))
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
🟩🟩🟩 STDOUT️ 🟩🟩🟩️
✅ Success.
🟥🟥🟥 STDERR️️ 🟥🟥🟥️
successfully deleted ip 51.158.108.20
successfully deleted ip 51.158.115.98
🟩🟩🟩 JSON STDOUT 🟩🟩🟩
{
"message": "Success",
Expand Down