Skip to content

Commit

Permalink
ARM: Show API errors when failing to validate existing resources and …
Browse files Browse the repository at this point in the history
…when deleting them. (#423)

* Return returns from API/SDK for the following events that happen in the builder

- Retrieving the resource group for managed images
- Retrieving the SIG image definition destination
- Deleting the existing SIG Image version
- Retrieving the Parent Shared Image Gallery

* Update builder/azure/arm/builder.go

Co-authored-by: Lucas Bajolet <105649352+lbajolet-hashicorp@users.noreply.github.com>

* Update builder/azure/arm/builder.go

Co-authored-by: Lucas Bajolet <105649352+lbajolet-hashicorp@users.noreply.github.com>

* Update builder/azure/arm/builder.go

Co-authored-by: Lucas Bajolet <105649352+lbajolet-hashicorp@users.noreply.github.com>

---------

Co-authored-by: Lucas Bajolet <105649352+lbajolet-hashicorp@users.noreply.github.com>
  • Loading branch information
JenGoldstrich and lbajolet-hashicorp authored Jun 13, 2024
1 parent 9309d6c commit 2b89b16
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions builder/azure/arm/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ func (b *Builder) Run(ctx context.Context, ui packersdk.Ui, hook packersdk.Hook)
groupId := commonids.NewResourceGroupID(b.config.ClientConfig.SubscriptionID, b.config.ManagedImageResourceGroupName)
_, err := azureClient.ResourceGroupsClient.Get(builderPollingContext, groupId)
if err != nil {
return nil, fmt.Errorf("Cannot locate the managed image resource group %s.", b.config.ManagedImageResourceGroupName)
return nil, fmt.Errorf("Cannot locate the managed image resource group %s, received error %s", b.config.ManagedImageResourceGroupName, err)
}

// If a managed image already exists it cannot be overwritten.
Expand Down Expand Up @@ -214,7 +214,11 @@ func (b *Builder) Run(ctx context.Context, ui packersdk.Ui, hook packersdk.Hook)
galleryId := galleryimages.NewGalleryImageID(sigSubscriptionID, b.config.SharedGalleryDestination.SigDestinationResourceGroup, b.config.SharedGalleryDestination.SigDestinationGalleryName, b.config.SharedGalleryDestination.SigDestinationImageName)
_, err = azureClient.GalleryImagesClient.Get(builderPollingContext, galleryId)
if err != nil {
return nil, fmt.Errorf("the Shared Gallery Image '%s' to which to publish the managed image version to does not exist in the resource group '%s' or does not contain managed image '%s'", b.config.SharedGalleryDestination.SigDestinationGalleryName, b.config.SharedGalleryDestination.SigDestinationResourceGroup, b.config.SharedGalleryDestination.SigDestinationImageName)
return nil, fmt.Errorf("failed to get image %q from image gallery %q in resource group %q: %s",
b.config.SharedGalleryDestination.SigDestinationImageName,
b.config.SharedGalleryDestination.SigDestinationGalleryName,
b.config.SharedGalleryDestination.SigDestinationResourceGroup,
err)
}
// Check if a Image Version already exists for our target destination
galleryImageVersionId := galleryimageversions.NewImageVersionID(sigSubscriptionID, b.config.SharedGalleryDestination.SigDestinationResourceGroup, b.config.SharedGalleryDestination.SigDestinationGalleryName, b.config.SharedGalleryDestination.SigDestinationImageName, b.config.SharedGalleryDestination.SigDestinationImageVersion)
Expand All @@ -226,7 +230,7 @@ func (b *Builder) Run(ctx context.Context, ui packersdk.Ui, hook packersdk.Hook)
defer cancel()
err := azureClient.GalleryImageVersionsClient.DeleteThenPoll(deleteImageContext, galleryImageVersionId)
if err != nil {
return nil, fmt.Errorf("failed to delete gallery image version for image name:version %s:%s in gallery %s", b.config.SharedGalleryDestination.SigDestinationImageName, b.config.SharedGalleryDestination.SigDestinationImageVersion, b.config.SharedGalleryDestination.SigDestinationGalleryName)
return nil, fmt.Errorf("failed to delete gallery image version for image name:version %s:%s in gallery %s, received error: %s", b.config.SharedGalleryDestination.SigDestinationImageName, b.config.SharedGalleryDestination.SigDestinationImageVersion, b.config.SharedGalleryDestination.SigDestinationGalleryName, err.Error())
}

} else {
Expand Down Expand Up @@ -298,7 +302,7 @@ func (b *Builder) Run(ctx context.Context, ui packersdk.Ui, hook packersdk.Hook)
id := galleryimages.NewGalleryImageID(b.config.SharedGallery.Subscription, b.config.SharedGallery.ResourceGroup, b.config.SharedGallery.GalleryName, b.config.SharedGallery.ImageName)
galleryImage, err := client.Get(builderPollingContext, id)
if err != nil {
return nil, fmt.Errorf("the parent Shared Gallery Image '%s' from which to source the managed image version to does not exist in the resource group '%s' or does not contain managed image '%s'", b.config.SharedGallery.GalleryName, b.config.SharedGallery.ResourceGroup, b.config.SharedGallery.ImageName)
return nil, fmt.Errorf("failed to get parent Shared Gallery Image %s in gallery %s in the resource group %s, received error: %s.", b.config.SharedGallery.GalleryName, b.config.SharedGallery.ImageName, b.config.SharedGallery.ResourceGroup, err.Error())
}
if galleryImage.Model == nil {
return nil, commonclient.NullModelSDKErr
Expand Down

0 comments on commit 2b89b16

Please sign in to comment.