Skip to content

Commit

Permalink
lxc: return the GetImageAliasType error in dereferenceAlias to fa…
Browse files Browse the repository at this point in the history
…il early if the remote index does not exist

Signed-off-by: Gabriel Mougard <gabriel.mougard@canonical.com>
  • Loading branch information
gabrielmougard committed Mar 18, 2024
1 parent 4f3af9a commit 7b48493
Showing 1 changed file with 56 additions and 14 deletions.
70 changes: 56 additions & 14 deletions lxc/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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 := "."
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 7b48493

Please sign in to comment.