Skip to content

Commit bd71f0e

Browse files
authored
chore(instance): extract server delete command from custom_server.go (scaleway#4366)
1 parent b7f2a90 commit bd71f0e

File tree

2 files changed

+237
-222
lines changed

2 files changed

+237
-222
lines changed

internal/namespaces/instance/v1/custom_server.go

Lines changed: 0 additions & 222 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ import (
1515
"github.com/fatih/color"
1616
"github.com/scaleway/scaleway-cli/v2/core"
1717
"github.com/scaleway/scaleway-cli/v2/core/human"
18-
"github.com/scaleway/scaleway-cli/v2/internal/interactive"
19-
block "github.com/scaleway/scaleway-sdk-go/api/block/v1alpha1"
2018
"github.com/scaleway/scaleway-sdk-go/api/instance/v1"
2119
"github.com/scaleway/scaleway-sdk-go/api/vpc/v2"
2220
"github.com/scaleway/scaleway-sdk-go/logger"
@@ -681,223 +679,3 @@ func serverWaitCommand() *core.Command {
681679
},
682680
}
683681
}
684-
685-
type customDeleteServerRequest struct {
686-
Zone scw.Zone
687-
ServerID string
688-
WithVolumes withVolumes
689-
WithIP bool
690-
ForceShutdown bool
691-
}
692-
693-
type withVolumes string
694-
695-
const (
696-
withVolumesNone = withVolumes("none")
697-
withVolumesLocal = withVolumes("local")
698-
withVolumesBlock = withVolumes("block")
699-
withVolumesRoot = withVolumes("root")
700-
withVolumesAll = withVolumes("all")
701-
)
702-
703-
func serverDeleteCommand() *core.Command {
704-
return &core.Command{
705-
Short: `Delete server`,
706-
Long: `Delete a server with the given ID.`,
707-
Namespace: "instance",
708-
Verb: "delete",
709-
Resource: "server",
710-
ArgsType: reflect.TypeOf(customDeleteServerRequest{}),
711-
ArgSpecs: core.ArgSpecs{
712-
{
713-
Name: "server-id",
714-
Required: true,
715-
Positional: true,
716-
},
717-
{
718-
Name: "with-volumes",
719-
Short: "Delete the volumes attached to the server",
720-
Default: core.DefaultValueSetter("all"),
721-
EnumValues: []string{
722-
string(withVolumesNone),
723-
string(withVolumesLocal),
724-
string(withVolumesBlock),
725-
string(withVolumesRoot),
726-
string(withVolumesAll),
727-
},
728-
},
729-
{
730-
Name: "with-ip",
731-
Short: "Delete the IP attached to the server",
732-
},
733-
{
734-
Name: "force-shutdown",
735-
Short: "Force shutdown of the instance server before deleting it",
736-
},
737-
core.ZoneArgSpec((*instance.API)(nil).Zones()...),
738-
},
739-
Examples: []*core.Example{
740-
{
741-
Short: "Delete a server in the default zone with a given id",
742-
ArgsJSON: `{"server_id": "11111111-1111-1111-1111-111111111111"}`,
743-
},
744-
{
745-
Short: "Delete a server in fr-par-1 zone with a given id",
746-
ArgsJSON: `{"zone":"fr-par-1", "server_id": "11111111-1111-1111-1111-111111111111"}`,
747-
},
748-
},
749-
SeeAlsos: []*core.SeeAlso{
750-
{
751-
Command: "scw instance server terminate",
752-
Short: "Terminate a running server",
753-
},
754-
{
755-
Command: "scw instance server stop",
756-
Short: "Stop a running server",
757-
},
758-
},
759-
WaitUsage: "wait until the server and its resources are deleted",
760-
WaitFunc: func(ctx context.Context, _, respI interface{}) (interface{}, error) {
761-
server := respI.(*core.SuccessResult).TargetResource.(*instance.Server)
762-
client := core.ExtractClient(ctx)
763-
api := instance.NewAPI(client)
764-
765-
notFoundErr := &scw.ResourceNotFoundError{}
766-
767-
_, err := api.WaitForServer(&instance.WaitForServerRequest{
768-
Zone: server.Zone,
769-
ServerID: server.ID,
770-
Timeout: scw.TimeDurationPtr(serverActionTimeout),
771-
RetryInterval: core.DefaultRetryInterval,
772-
})
773-
if err != nil {
774-
err = errors.Unwrap(err)
775-
if !errors.As(err, &notFoundErr) {
776-
return nil, err
777-
}
778-
}
779-
return respI, nil
780-
},
781-
Run: func(ctx context.Context, argsI interface{}) (interface{}, error) {
782-
deleteServerArgs := argsI.(*customDeleteServerRequest)
783-
784-
client := core.ExtractClient(ctx)
785-
api := instance.NewAPI(client)
786-
787-
server, err := api.GetServer(&instance.GetServerRequest{
788-
Zone: deleteServerArgs.Zone,
789-
ServerID: deleteServerArgs.ServerID,
790-
})
791-
if err != nil {
792-
return nil, err
793-
}
794-
795-
if deleteServerArgs.ForceShutdown {
796-
finalStateServer, err := api.WaitForServer(&instance.WaitForServerRequest{
797-
Zone: deleteServerArgs.Zone,
798-
ServerID: deleteServerArgs.ServerID,
799-
Timeout: scw.TimeDurationPtr(serverActionTimeout),
800-
RetryInterval: core.DefaultRetryInterval,
801-
})
802-
if err != nil {
803-
return nil, err
804-
}
805-
806-
if finalStateServer.State != instance.ServerStateStopped {
807-
err = api.ServerActionAndWait(&instance.ServerActionAndWaitRequest{
808-
Zone: deleteServerArgs.Zone,
809-
ServerID: deleteServerArgs.ServerID,
810-
Action: instance.ServerActionPoweroff,
811-
Timeout: scw.TimeDurationPtr(serverActionTimeout),
812-
RetryInterval: core.DefaultRetryInterval,
813-
})
814-
if err != nil {
815-
return nil, err
816-
}
817-
}
818-
}
819-
820-
err = api.DeleteServer(&instance.DeleteServerRequest{
821-
Zone: deleteServerArgs.Zone,
822-
ServerID: deleteServerArgs.ServerID,
823-
})
824-
if err != nil {
825-
return nil, err
826-
}
827-
828-
if deleteServerArgs.WithIP && server.Server.PublicIPs != nil {
829-
for _, ip := range server.Server.PublicIPs {
830-
if ip.Dynamic {
831-
continue
832-
}
833-
err = api.DeleteIP(&instance.DeleteIPRequest{
834-
Zone: deleteServerArgs.Zone,
835-
IP: ip.ID,
836-
})
837-
if err != nil {
838-
return nil, err
839-
}
840-
_, _ = interactive.Printf("successfully deleted ip %s\n", ip.Address.String())
841-
}
842-
}
843-
844-
deletedVolumeMessages := [][2]string(nil)
845-
volumeDelete:
846-
for index, volume := range server.Server.Volumes {
847-
switch {
848-
case deleteServerArgs.WithVolumes == withVolumesNone:
849-
break volumeDelete
850-
case deleteServerArgs.WithVolumes == withVolumesRoot && index != "0":
851-
continue
852-
case deleteServerArgs.WithVolumes == withVolumesLocal && volume.VolumeType != instance.VolumeServerVolumeTypeLSSD:
853-
continue
854-
case deleteServerArgs.WithVolumes == withVolumesBlock && volume.VolumeType != instance.VolumeServerVolumeTypeBSSD && volume.VolumeType != instance.VolumeServerVolumeTypeSbsVolume:
855-
continue
856-
case volume.VolumeType == instance.VolumeServerVolumeTypeScratch:
857-
continue
858-
}
859-
if volume.VolumeType == instance.VolumeServerVolumeTypeSbsVolume {
860-
err = block.NewAPI(client).DeleteVolume(&block.DeleteVolumeRequest{
861-
Zone: deleteServerArgs.Zone,
862-
VolumeID: volume.ID,
863-
})
864-
} else {
865-
err = api.DeleteVolume(&instance.DeleteVolumeRequest{
866-
Zone: deleteServerArgs.Zone,
867-
VolumeID: volume.ID,
868-
})
869-
}
870-
if err != nil {
871-
return nil, &core.CliError{
872-
Err: err,
873-
Hint: "Make sure this resource have been deleted or try to delete it manually.",
874-
}
875-
}
876-
humanSize, err := human.Marshal(volume.Size, nil)
877-
if err != nil {
878-
logger.Debugf("cannot marshal human size %v", volume.Size)
879-
}
880-
volumeName := ""
881-
if volume.Name != nil {
882-
volumeName = *volume.Name
883-
}
884-
deletedVolumeMessages = append(deletedVolumeMessages, [2]string{
885-
index,
886-
fmt.Sprintf("successfully deleted volume %s (%s %s)", volumeName, humanSize, volume.VolumeType),
887-
})
888-
}
889-
890-
// Sort and print deleted volume messages
891-
sort.Slice(deletedVolumeMessages, func(i, j int) bool {
892-
return deletedVolumeMessages[i][0] < deletedVolumeMessages[j][0]
893-
})
894-
for _, message := range deletedVolumeMessages {
895-
_, _ = interactive.Println(message[1])
896-
}
897-
898-
return &core.SuccessResult{
899-
TargetResource: server.Server,
900-
}, nil
901-
},
902-
}
903-
}

0 commit comments

Comments
 (0)