Skip to content

Commit

Permalink
Fix #258
Browse files Browse the repository at this point in the history
  • Loading branch information
Quentin Perez committed Jan 8, 2016
1 parent 52cb149 commit ec98863
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 31 deletions.
87 changes: 74 additions & 13 deletions pkg/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -1255,7 +1255,7 @@ func (s *ScalewayAPI) PutVolume(volumeID string, definition ScalewayVolumePutDef
return err
}

// ResolveServer attempts the find a matching Identifier for the input string
// ResolveServer attempts to find a matching Identifier for the input string
func (s *ScalewayAPI) ResolveServer(needle string) (ScalewayResolverResults, error) {
servers := s.Cache.LookUpServers(needle, true)
if len(servers) == 0 {
Expand All @@ -1267,7 +1267,19 @@ func (s *ScalewayAPI) ResolveServer(needle string) (ScalewayResolverResults, err
return servers, nil
}

// ResolveSnapshot attempts the find a matching Identifier for the input string
// ResolveVolume attempts to find a matching Identifier for the input string
func (s *ScalewayAPI) ResolveVolume(needle string) (ScalewayResolverResults, error) {
volumes := s.Cache.LookUpVolumes(needle, true)
if len(volumes) == 0 {
if _, err := s.GetVolumes(); err != nil {
return nil, err
}
volumes = s.Cache.LookUpVolumes(needle, true)
}
return volumes, nil
}

// ResolveSnapshot attempts to find a matching Identifier for the input string
func (s *ScalewayAPI) ResolveSnapshot(needle string) (ScalewayResolverResults, error) {
snapshots := s.Cache.LookUpSnapshots(needle, true)
if len(snapshots) == 0 {
Expand All @@ -1279,7 +1291,7 @@ func (s *ScalewayAPI) ResolveSnapshot(needle string) (ScalewayResolverResults, e
return snapshots, nil
}

// ResolveImage attempts the find a matching Identifier for the input string
// ResolveImage attempts to find a matching Identifier for the input string
func (s *ScalewayAPI) ResolveImage(needle string) (ScalewayResolverResults, error) {
images := s.Cache.LookUpImages(needle, true)
if len(images) == 0 {
Expand All @@ -1291,7 +1303,7 @@ func (s *ScalewayAPI) ResolveImage(needle string) (ScalewayResolverResults, erro
return images, nil
}

// ResolveBootscript attempts the find a matching Identifier for the input string
// ResolveBootscript attempts to find a matching Identifier for the input string
func (s *ScalewayAPI) ResolveBootscript(needle string) (ScalewayResolverResults, error) {
bootscripts := s.Cache.LookUpBootscripts(needle, true)
if len(bootscripts) == 0 {
Expand Down Expand Up @@ -1372,6 +1384,40 @@ func (s *ScalewayAPI) DeleteImage(imageID string) error {
return nil
}

// DeleteSnapshot deletes a snapshot
func (s *ScalewayAPI) DeleteSnapshot(snapshotID string) error {
defer s.Cache.RemoveSnapshot(snapshotID)
resp, err := s.DeleteResponse(fmt.Sprintf("snapshots/%s", snapshotID))
if resp != nil {
defer resp.Body.Close()
}
if err != nil {
return err
}

if _, err := s.handleHTTPError([]int{204}, resp); err != nil {
return err
}
return nil
}

// DeleteVolume deletes a volume
func (s *ScalewayAPI) DeleteVolume(volumeID string) error {
defer s.Cache.RemoveVolume(volumeID)
resp, err := s.DeleteResponse(fmt.Sprintf("volumes/%s", volumeID))
if resp != nil {
defer resp.Body.Close()
}
if err != nil {
return err
}

if _, err := s.handleHTTPError([]int{204}, resp); err != nil {
return err
}
return nil
}

// GetSnapshots gets the list of snapshots from the ScalewayAPI
func (s *ScalewayAPI) GetSnapshots() (*[]ScalewaySnapshot, error) {
query := url.Values{}
Expand Down Expand Up @@ -1825,7 +1871,7 @@ func (s *ScalewayAPI) GetDashboard() (*ScalewayDashboard, error) {
return &dashboard.Dashboard, nil
}

// GetServerID returns exactly one server matching or dies
// GetServerID returns exactly one server matching
func (s *ScalewayAPI) GetServerID(needle string) (string, error) {
// Parses optional type prefix, i.e: "server:name" -> "name"
_, needle = parseNeedle(needle)
Expand Down Expand Up @@ -1858,7 +1904,25 @@ func showResolverResults(needle string, results ScalewayResolverResults) error {
return fmt.Errorf("Too many candidates for %s (%d)", needle, len(results))
}

// GetSnapshotID returns exactly one snapshot matching or dies
// GetVolumeID returns exactly one volume matching
func (s *ScalewayAPI) GetVolumeID(needle string) (string, error) {
// Parses optional type prefix, i.e: "volume:name" -> "name"
_, needle = parseNeedle(needle)

volumes, err := s.ResolveVolume(needle)
if err != nil {
return "", fmt.Errorf("Unable to resolve volume %s: %s", needle, err)
}
if len(volumes) == 1 {
return volumes[0].Identifier, nil
}
if len(volumes) == 0 {
return "", fmt.Errorf("No such volume: %s", needle)
}
return "", showResolverResults(needle, volumes)
}

// GetSnapshotID returns exactly one snapshot matching
func (s *ScalewayAPI) GetSnapshotID(needle string) (string, error) {
// Parses optional type prefix, i.e: "snapshot:name" -> "name"
_, needle = parseNeedle(needle)
Expand All @@ -1876,8 +1940,8 @@ func (s *ScalewayAPI) GetSnapshotID(needle string) (string, error) {
return "", showResolverResults(needle, snapshots)
}

// GetImageID returns exactly one image matching or dies
func (s *ScalewayAPI) GetImageID(needle string, exitIfMissing bool) (*ScalewayImageIdentifier, error) {
// GetImageID returns exactly one image matching
func (s *ScalewayAPI) GetImageID(needle string) (*ScalewayImageIdentifier, error) {
// Parses optional type prefix, i.e: "image:name" -> "name"
_, needle = parseNeedle(needle)

Expand All @@ -1895,10 +1959,7 @@ func (s *ScalewayAPI) GetImageID(needle string, exitIfMissing bool) (*ScalewayIm
}, nil
}
if len(images) == 0 {
if exitIfMissing {
return nil, fmt.Errorf("No such image: %s", needle)
}
return nil, nil
return nil, fmt.Errorf("No such image: %s", needle)
}
return nil, showResolverResults(needle, images)
}
Expand Down Expand Up @@ -2255,7 +2316,7 @@ func (s *ScalewayAPI) GetQuotas() (*ScalewayGetQuotas, error) {
return &quotas, nil
}

// GetBootscriptID returns exactly one bootscript matching or dies
// GetBootscriptID returns exactly one bootscript matching
func (s *ScalewayAPI) GetBootscriptID(needle, arch string) (string, error) {
// Parses optional type prefix, i.e: "bootscript:name" -> "name"
_, needle = parseNeedle(needle)
Expand Down
2 changes: 1 addition & 1 deletion pkg/api/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ func CreateServer(api *ScalewayAPI, c *ConfigCreateServer) (string, error) {
// Use an existing image
// FIXME: handle snapshots
inheritingVolume = true
imageIdentifier, err = api.GetImageID(c.ImageName, false)
imageIdentifier, err = api.GetImageID(c.ImageName)
if err != nil {
return "", err
}
Expand Down
10 changes: 6 additions & 4 deletions pkg/cli/cmd_rmi.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ import "github.com/scaleway/scaleway-cli/pkg/commands"

var cmdRmi = &Command{
Exec: runRmi,
UsageLine: "rmi [OPTIONS] IMAGE [IMAGE...]",
Description: "Remove one or more images",
Help: "Remove one or more images.",
UsageLine: "rmi [OPTIONS] IDENTIFIER [IDENTIFIER...]",
Description: "Remove one or more image(s)/volume(s)/snapshot(s)",
Help: "Remove one or more image(s)/volume(s)/snapshot(s)",
Examples: `
$ scw rmi myimage
$ scw rmi mysnapshot
$ scw rmi myvolume
$ scw rmi $(scw images -q)
`,
}
Expand All @@ -33,7 +35,7 @@ func runRmi(cmd *Command, rawArgs []string) error {
}

args := commands.RmiArgs{
Images: rawArgs,
Identifier: rawArgs,
}
ctx := cmd.GetContext(rawArgs)
return commands.RunRmi(ctx, args)
Expand Down
2 changes: 1 addition & 1 deletion pkg/commands/history.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type HistoryArgs struct {

// RunHistory is the handler for 'scw history'
func RunHistory(ctx CommandContext, args HistoryArgs) error {
imageID, err := ctx.API.GetImageID(args.Image, true)
imageID, err := ctx.API.GetImageID(args.Image)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/commands/ps.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func RunPs(ctx CommandContext, args PsArgs) error {
goto skipServer
}
case "image":
imageID, err := ctx.API.GetImageID(value, true)
imageID, err := ctx.API.GetImageID(value)
if err != nil {
goto skipServer
}
Expand Down
40 changes: 29 additions & 11 deletions pkg/commands/rmi.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,44 @@ import (

// RmiArgs are flags for the `RunRmi` function
type RmiArgs struct {
Images []string
Identifier []string // images/volumes/snapshots
}

// RunRmi is the handler for 'scw rmi'
func RunRmi(ctx CommandContext, args RmiArgs) error {
hasError := false
for _, needle := range args.Images {
image, err := ctx.API.GetImageID(needle, true)
if err != nil {
return err
for _, needle := range args.Identifier {
if image, err := ctx.API.GetImageID(needle); err == nil {
if err = ctx.API.DeleteImage(image.Identifier); err != nil {
logrus.Errorf("failed to delete image %s: %s", image.Identifier, err)
hasError = true
} else {
fmt.Fprintln(ctx.Stdout, needle)
}
continue
}
if err = ctx.API.DeleteImage(image.Identifier); err != nil {
logrus.Errorf("failed to delete image %s: %s", image.Identifier, err)
hasError = true
} else {
fmt.Fprintln(ctx.Stdout, needle)
if snapshotID, err := ctx.API.GetSnapshotID(needle); err == nil {
if err = ctx.API.DeleteSnapshot(snapshotID); err != nil {
logrus.Errorf("failed to delete snapshot %s: %s", snapshotID, err)
hasError = true
} else {
fmt.Fprintln(ctx.Stdout, needle)
}
continue
}
if volumeID, err := ctx.API.GetVolumeID(needle); err == nil {
if err = ctx.API.DeleteVolume(volumeID); err != nil {
logrus.Errorf("failed to delete volume %s: %s", volumeID, err)
hasError = true
} else {
fmt.Fprintln(ctx.Stdout, needle)
}
continue
}
hasError = true
}
if hasError {
return fmt.Errorf("at least 1 image failed to be removed")
return fmt.Errorf("at least 1 image/snapshot/volume failed to be removed")
}
return nil
}

0 comments on commit ec98863

Please sign in to comment.