diff --git a/lxc/image.go b/lxc/image.go index e526f579c129..e16ebd1cf200 100644 --- a/lxc/image.go +++ b/lxc/image.go @@ -109,17 +109,24 @@ hash or alias name (if one is set).`)) return cmd } -func (c *cmdImage) dereferenceAlias(d lxd.ImageServer, imageType string, inName string) string { +func (c *cmdImage) dereferenceAlias(d lxd.ImageServer, imageType string, inName string) (string, error) { if inName == "" { inName = "default" } - result, _, _ := d.GetImageAliasType(imageType, inName) - if result == nil { - return inName + result, _, err := d.GetImageAliasType(imageType, inName) + if err != nil { + // Maybe that inName is a fingerprint and can't be found as an alias + i, _, errImage := d.GetImage(inName) + if errImage != nil { + return "", fmt.Errorf(i18n.G("Fingerprint %q not found"), inName) + } + + return i.Fingerprint, nil } - return result.Target + // If inName is an alias and could be resolved, return its fingerprint + return result.Target, nil } // Copy. @@ -229,7 +236,11 @@ func (c *cmdImageCopy) Run(cmd *cobra.Command, args []string) error { imgInfo.Public = true } else { // Resolve any alias and then grab the image information from the source - image := c.image.dereferenceAlias(sourceServer, imageType, name) + image, err := c.image.dereferenceAlias(sourceServer, imageType, name) + if err != nil { + return err + } + imgInfo, _, err = sourceServer.GetImage(image) if err != nil { return err @@ -335,7 +346,11 @@ func (c *cmdImageDelete) Run(cmd *cobra.Command, args []string) error { return fmt.Errorf(i18n.G("Image identifier missing")) } - image := c.image.dereferenceAlias(resource.server, "", resource.name) + image, err := c.image.dereferenceAlias(resource.server, "", resource.name) + if err != nil { + return err + } + op, err := resource.server.DeleteImage(image) if err != nil { return err @@ -404,7 +419,11 @@ func (c *cmdImageEdit) Run(cmd *cobra.Command, args []string) error { } // Resolve any aliases - image := c.image.dereferenceAlias(resource.server, "", resource.name) + image, err := c.image.dereferenceAlias(resource.server, "", resource.name) + if err != nil { + return err + } + if image == "" { image = resource.name } @@ -522,7 +541,10 @@ func (c *cmdImageExport) Run(cmd *cobra.Command, args []string) error { imageType = "virtual-machine" } - fingerprint := c.image.dereferenceAlias(remoteServer, imageType, name) + fingerprint, err := c.image.dereferenceAlias(remoteServer, imageType, name) + if err != nil { + return err + } // Default target is current directory target := "." @@ -914,7 +936,11 @@ func (c *cmdImageInfo) Run(cmd *cobra.Command, args []string) error { imageType = "virtual-machine" } - image := c.image.dereferenceAlias(remoteServer, imageType, name) + image, err := c.image.dereferenceAlias(remoteServer, imageType, name) + if err != nil { + return err + } + info, _, err := remoteServer.GetImage(image) if err != nil { return err @@ -1361,7 +1387,11 @@ func (c *cmdImageRefresh) Run(cmd *cobra.Command, args []string) error { return fmt.Errorf(i18n.G("Image identifier missing")) } - image := c.image.dereferenceAlias(resource.server, "", resource.name) + image, err := c.image.dereferenceAlias(resource.server, "", resource.name) + if err != nil { + return err + } + progress := cli.ProgressRenderer{ Format: i18n.G("Refreshing the image: %s"), Quiet: c.global.flagQuiet, @@ -1448,7 +1478,11 @@ func (c *cmdImageShow) Run(cmd *cobra.Command, args []string) error { imageType = "virtual-machine" } - image := c.image.dereferenceAlias(remoteServer, imageType, name) + image, err := c.image.dereferenceAlias(remoteServer, imageType, name) + if err != nil { + return err + } + info, _, err := remoteServer.GetImage(image) if err != nil { return err @@ -1501,7 +1535,11 @@ func (c *cmdImageGetProp) Run(cmd *cobra.Command, args []string) error { } // Get the corresponding property - image := c.image.dereferenceAlias(remoteServer, "", name) + image, err := c.image.dereferenceAlias(remoteServer, "", name) + if err != nil { + return err + } + info, _, err := remoteServer.GetImage(image) if err != nil { return err @@ -1554,7 +1592,11 @@ func (c *cmdImageSetProp) Run(cmd *cobra.Command, args []string) error { } // Show properties - image := c.image.dereferenceAlias(resource.server, "", resource.name) + image, err := c.image.dereferenceAlias(resource.server, "", resource.name) + if err != nil { + return err + } + info, etag, err := resource.server.GetImage(image) if err != nil { return err