Skip to content
Permalink

Comparing changes

This is a direct comparison between two commits made in this repository or its related repositories. View the default comparison for this range or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: hashicorp/packer-plugin-azure
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 7d494b061de29ccc540ec2d307874568481f759c
Choose a base ref
..
head repository: hashicorp/packer-plugin-azure
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 21eb7b2bc2626b899c79fd8ed13721af42571cec
Choose a head ref
Showing with 14 additions and 4 deletions.
  1. +14 −4 builder/azure/arm/builder.go
18 changes: 14 additions & 4 deletions builder/azure/arm/builder.go
Original file line number Diff line number Diff line change
@@ -152,7 +152,7 @@ func (b *Builder) Run(ctx context.Context, ui packersdk.Ui, hook packersdk.Hook)
return nil, fmt.Errorf("failed to delete the managed image named %s : %s", b.config.ManagedImageName, azureClient.LastError.Error())
}
} else {
return nil, fmt.Errorf("the managed image named %s already exists in the resource group %s, use the -force option to automatically delete it.", b.config.ManagedImageName, b.config.ManagedImageResourceGroupName)
return nil, fmt.Errorf("the managed image named %s already exists in the resource group %s, use a different manage image name or use the -force option to automatically delete it.", b.config.ManagedImageName, b.config.ManagedImageResourceGroupName)
}
}
}
@@ -196,8 +196,8 @@ func (b *Builder) Run(ctx context.Context, ui packersdk.Ui, hook packersdk.Hook)
if b.config.DiskEncryptionSetId != "" {
b.stateBag.Put(constants.ArmBuildDiskEncryptionSetId, b.config.DiskEncryptionSetId)
}
// Validate that Shared Gallery Image exists before publishing to SIG
if b.config.isPublishToSIG() {
// Validate that Shared Gallery Image exists before publishing to SIG
sigSubscriptionID := b.config.SharedGalleryDestination.SigDestinationSubscription
if sigSubscriptionID == "" {
sigSubscriptionID = b.stateBag.Get(constants.ArmSubscription).(string)
@@ -208,12 +208,22 @@ func (b *Builder) Run(ctx context.Context, ui packersdk.Ui, hook packersdk.Hook)
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)
}

// 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)
_, err := azureClient.GalleryImageVersionsClient.Get(builderPollingContext, galleryImageVersionId, galleryimageversions.DefaultGetOperationOptions())
if err == nil {
return nil, fmt.Errorf("a gallery image version for image name:version %s:%s already exists in gallery %s", b.config.SharedGalleryDestination.SigDestinationImageName, b.config.SharedGalleryDestination.SigDestinationImageVersion, b.config.SharedGalleryDestination.SigDestinationGalleryName)
if b.config.PackerForce {
ui.Say(fmt.Sprintf("a gallery image version for image name:version %s:%s already exists in gallery %s, but deleting it due to -force flag", b.config.SharedGalleryDestination.SigDestinationGalleryName, b.config.SharedGalleryDestination.SigDestinationImageVersion, b.config.SharedGalleryDestination.SigDestinationImageName))
deleteImageContext, cancel := context.WithTimeout(ctx, azureClient.PollingDuration)
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)
}

} else {
return nil, fmt.Errorf("a gallery image version for image name:version %s:%s already exists in gallery %s, use a different gallery image version or use the -force option to automatically delete it.", b.config.SharedGalleryDestination.SigDestinationImageName, b.config.SharedGalleryDestination.SigDestinationImageVersion, b.config.SharedGalleryDestination.SigDestinationGalleryName)
}
}

if len(b.config.SharedGalleryDestination.SigDestinationTargetRegions) > 0 {