Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to the newest version of hashicorp/go-azure-sdk #396

Merged
merged 2 commits into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
269 changes: 141 additions & 128 deletions builder/azure/arm/azure_client.go

Large diffs are not rendered by default.

28 changes: 15 additions & 13 deletions builder/azure/arm/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
"github.com/hashicorp/go-azure-sdk/resource-manager/compute/2022-03-01/images"
"github.com/hashicorp/go-azure-sdk/resource-manager/compute/2022-03-03/galleryimages"
"github.com/hashicorp/go-azure-sdk/resource-manager/compute/2022-03-03/galleryimageversions"
"github.com/hashicorp/go-azure-sdk/resource-manager/storage/2022-09-01/storageaccounts"
"github.com/hashicorp/go-azure-sdk/resource-manager/storage/2023-01-01/storageaccounts"

"github.com/hashicorp/go-azure-helpers/resourcemanager/commonids"
"github.com/hashicorp/hcl/v2/hcldec"
Expand Down Expand Up @@ -64,7 +64,7 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, []string, error) {
}

func (b *Builder) Run(ctx context.Context, ui packersdk.Ui, hook packersdk.Hook) (packersdk.Artifact, error) {

// All requests on the new (non auto rest) base layer of the azure SDK require a context with a timeout for polling purposes
ui.Say("Running builder ...")

// FillParameters function captures authType and sets defaults.
Expand Down Expand Up @@ -111,6 +111,7 @@ func (b *Builder) Run(ctx context.Context, ui packersdk.Ui, hook packersdk.Hook)
authOptions,
)

ui.Message("ARM Client successfully created")
if err != nil {
return nil, err
}
Expand All @@ -119,6 +120,9 @@ func (b *Builder) Run(ctx context.Context, ui packersdk.Ui, hook packersdk.Hook)
if err := resolver.Resolve(&b.config); err != nil {
return nil, err
}
// All requests against go-azure-sdk require a polling duration
builderPollingContext, builderCancel := context.WithTimeout(ctx, azureClient.PollingDuration)
defer builderCancel()
objectID := azureClient.ObjectID
if b.config.ClientConfig.ObjectID == "" {
b.config.ClientConfig.ObjectID = objectID
Expand All @@ -132,20 +136,18 @@ func (b *Builder) Run(ctx context.Context, ui packersdk.Ui, hook packersdk.Hook)

if b.config.isManagedImage() {
groupId := commonids.NewResourceGroupID(b.config.ClientConfig.SubscriptionID, b.config.ManagedImageResourceGroupName)
_, err := azureClient.ResourceGroupsClient.Get(ctx, groupId)
_, err := azureClient.ResourceGroupsClient.Get(builderPollingContext, groupId)
if err != nil {
return nil, fmt.Errorf("Cannot locate the managed image resource group %s.", b.config.ManagedImageResourceGroupName)
}

// If a managed image already exists it cannot be overwritten.
imageId := images.NewImageID(b.config.ClientConfig.SubscriptionID, b.config.ManagedImageResourceGroupName, b.config.ManagedImageName)
_, err = azureClient.ImagesClient.Get(ctx, imageId, images.DefaultGetOperationOptions())
_, err = azureClient.ImagesClient.Get(builderPollingContext, imageId, images.DefaultGetOperationOptions())
if err == nil {
if b.config.PackerForce {
ui.Say(fmt.Sprintf("the managed image named %s already exists, but deleting it due to -force flag", b.config.ManagedImageName))
deleteImageContext, cancel := context.WithTimeout(ctx, azureClient.PollingDuration)
defer cancel()
err := azureClient.ImagesClient.DeleteThenPoll(deleteImageContext, imageId)
err := azureClient.ImagesClient.DeleteThenPoll(builderPollingContext, imageId)
if err != nil {
return nil, fmt.Errorf("failed to delete the managed image named %s : %s", b.config.ManagedImageName, azureClient.LastError.Error())
}
Expand All @@ -157,7 +159,7 @@ func (b *Builder) Run(ctx context.Context, ui packersdk.Ui, hook packersdk.Hook)

if b.config.BuildResourceGroupName != "" {
buildGroupId := commonids.NewResourceGroupID(b.config.ClientConfig.SubscriptionID, b.config.BuildResourceGroupName)
group, err := azureClient.ResourceGroupsClient.Get(ctx, buildGroupId)
group, err := azureClient.ResourceGroupsClient.Get(builderPollingContext, buildGroupId)
if err != nil {
return nil, fmt.Errorf("Cannot locate the existing build resource resource group %s.", b.config.BuildResourceGroupName)
}
Expand All @@ -168,7 +170,7 @@ func (b *Builder) Run(ctx context.Context, ui packersdk.Ui, hook packersdk.Hook)
b.config.validateLocationZoneResiliency(ui.Say)

if b.config.StorageAccount != "" {
account, err := b.getBlobAccount(ctx, azureClient, b.config.ClientConfig.SubscriptionID, b.config.ResourceGroupName, b.config.StorageAccount)
account, err := b.getBlobAccount(builderPollingContext, azureClient, b.config.ClientConfig.SubscriptionID, b.config.ResourceGroupName, b.config.StorageAccount)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -202,13 +204,13 @@ func (b *Builder) Run(ctx context.Context, ui packersdk.Ui, hook packersdk.Hook)
}
b.stateBag.Put(constants.ArmSharedImageGalleryDestinationSubscription, sigSubscriptionID)
galleryId := galleryimages.NewGalleryImageID(sigSubscriptionID, b.config.SharedGalleryDestination.SigDestinationResourceGroup, b.config.SharedGalleryDestination.SigDestinationGalleryName, b.config.SharedGalleryDestination.SigDestinationImageName)
_, err = azureClient.GalleryImagesClient.Get(ctx, galleryId)
_, 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)
}
// 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(ctx, galleryImageVersionId, galleryimageversions.DefaultGetOperationOptions())
_, err := azureClient.GalleryImageVersionsClient.Get(builderPollingContext, galleryImageVersionId, galleryimageversions.DefaultGetOperationOptions())
if err == nil {
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))
Expand Down Expand Up @@ -268,7 +270,7 @@ func (b *Builder) Run(ctx context.Context, ui packersdk.Ui, hook packersdk.Hook)
if b.config.SharedGallery.GalleryName != "" {
client := azureClient.GalleryImagesClient
id := galleryimages.NewGalleryImageID(b.config.SharedGallery.Subscription, b.config.SharedGallery.ResourceGroup, b.config.SharedGallery.GalleryName, b.config.SharedGallery.ImageName)
galleryImage, err := client.Get(ctx, id)
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)
}
Expand Down Expand Up @@ -505,7 +507,7 @@ func canonicalizeLocation(location string) string {
}

func (b *Builder) getBlobAccount(ctx context.Context, client *AzureClient, subscriptionId string, resourceGroupName string, storageAccountName string) (*storageaccounts.StorageAccount, error) {
id := storageaccounts.NewStorageAccountID(subscriptionId, resourceGroupName, storageAccountName)
id := commonids.NewStorageAccountID(subscriptionId, resourceGroupName, storageAccountName)
account, err := client.StorageAccountsClient.GetProperties(ctx, id, storageaccounts.DefaultGetPropertiesOperationOptions())
if err != nil {
return nil, err
Expand Down
118 changes: 0 additions & 118 deletions builder/azure/arm/inspector.go

This file was deleted.

3 changes: 1 addition & 2 deletions builder/azure/arm/resource_resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (

"github.com/hashicorp/go-azure-helpers/resourcemanager/commonids"
"github.com/hashicorp/go-azure-sdk/resource-manager/compute/2022-03-01/images"
"github.com/hashicorp/go-azure-sdk/resource-manager/network/2022-09-01/subnets"
)

type resourceResolver struct {
Expand Down Expand Up @@ -123,7 +122,7 @@ func findVirtualNetworkSubnet(client *AzureClient, subscriptionId string, resour

subnetListContext, cancel := context.WithTimeout(context.TODO(), client.PollingDuration)
defer cancel()
subnets, err := client.NetworkMetaClient.Subnets.List(subnetListContext, subnets.NewVirtualNetworkID(subscriptionId, resourceGroupName, name))
subnets, err := client.NetworkMetaClient.Subnets.List(subnetListContext, commonids.NewVirtualNetworkID(subscriptionId, resourceGroupName, name))
if err != nil {
return "", err
}
Expand Down
8 changes: 6 additions & 2 deletions builder/azure/arm/step_capture_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ func NewStepCaptureImage(client *AzureClient, ui packersdk.Ui) *StepCaptureImage
}

func (s *StepCaptureImage) generalize(ctx context.Context, vmId virtualmachines.VirtualMachineId) error {
_, err := s.client.Generalize(ctx, vmId)
pollingContext, cancel := context.WithTimeout(ctx, s.client.PollingDuration)
defer cancel()
_, err := s.client.Generalize(pollingContext, vmId)
if err != nil {
s.say(s.client.LastError.Error())
}
Expand Down Expand Up @@ -73,7 +75,9 @@ func (s *StepCaptureImage) captureImage(ctx context.Context, vmId virtualmachine
}

func (s *StepCaptureImage) getVMID(ctx context.Context, vmId virtualmachines.VirtualMachineId) (string, error) {
vmResponse, err := s.client.VirtualMachinesClient.Get(ctx, vmId, virtualmachines.DefaultGetOperationOptions())
pollingContext, cancel := context.WithTimeout(ctx, s.client.PollingDuration)
defer cancel()
vmResponse, err := s.client.VirtualMachinesClient.Get(pollingContext, vmId, virtualmachines.DefaultGetOperationOptions())
if err != nil {
return "", err
}
Expand Down
7 changes: 5 additions & 2 deletions builder/azure/arm/step_certificate_in_keyvault.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"fmt"
"time"

"github.com/hashicorp/go-azure-sdk/resource-manager/keyvault/2023-02-01/secrets"
"github.com/hashicorp/go-azure-sdk/resource-manager/keyvault/2023-07-01/secrets"
"github.com/hashicorp/packer-plugin-azure/builder/azure/common/constants"
"github.com/hashicorp/packer-plugin-sdk/multistep"
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
Expand Down Expand Up @@ -52,7 +52,10 @@ func (s *StepCertificateInKeyVault) setCertificate(ctx context.Context, id secre
Exp: &expirationTimeUnix,
}
}
_, err := s.client.SecretsClient.CreateOrUpdate(ctx, id, secret)
pollingContext, cancel := context.WithTimeout(ctx, s.client.PollingDuration)
defer cancel()

_, err := s.client.SecretsClient.CreateOrUpdate(pollingContext, id, secret)

return err
}
Expand Down
2 changes: 1 addition & 1 deletion builder/azure/arm/step_certificate_in_keyvault_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"fmt"
"testing"

"github.com/hashicorp/go-azure-sdk/resource-manager/keyvault/2023-02-01/secrets"
"github.com/hashicorp/go-azure-sdk/resource-manager/keyvault/2023-07-01/secrets"
"github.com/hashicorp/packer-plugin-azure/builder/azure/common/constants"
"github.com/hashicorp/packer-plugin-sdk/multistep"
)
Expand Down
13 changes: 10 additions & 3 deletions builder/azure/arm/step_create_resource_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ func NewStepCreateResourceGroup(client *AzureClient, ui packersdk.Ui) *StepCreat

func (s *StepCreateResourceGroup) createResourceGroup(ctx context.Context, subscriptionId string, resourceGroupName string, location string, tags map[string]string) error {
id := commonids.NewResourceGroupID(subscriptionId, resourceGroupName)
_, err := s.client.ResourceGroupsClient.CreateOrUpdate(ctx, id, resourcegroups.ResourceGroup{
pollingContext, cancel := context.WithTimeout(ctx, s.client.PollingDuration)
defer cancel()
_, err := s.client.ResourceGroupsClient.CreateOrUpdate(pollingContext, id, resourcegroups.ResourceGroup{
Location: location,
Tags: &tags,
})
Expand All @@ -51,7 +53,10 @@ func (s *StepCreateResourceGroup) createResourceGroup(ctx context.Context, subsc

func (s *StepCreateResourceGroup) doesResourceGroupExist(ctx context.Context, subscriptionId string, resourceGroupName string) (bool, error) {
id := commonids.NewResourceGroupID(subscriptionId, resourceGroupName)
exists, err := s.client.ResourceGroupsClient.Get(ctx, id)
pollingContext, cancel := context.WithTimeout(ctx, s.client.PollingDuration)
defer cancel()

exists, err := s.client.ResourceGroupsClient.Get(pollingContext, id)
if err != nil {
if exists.HttpResponse.StatusCode == 404 {
return false, nil
Expand Down Expand Up @@ -142,7 +147,9 @@ func (s *StepCreateResourceGroup) Cleanup(state multistep.StateBag) {
ui.Say("\nCleanup requested, deleting resource group ...")
id := commonids.NewResourceGroupID(subscriptionId, resourceGroupName)
if state.Get(constants.ArmAsyncResourceGroupDelete).(bool) {
_, deleteErr := s.client.ResourceGroupsClient.Delete(ctx, id, resourcegroups.DefaultDeleteOperationOptions())
pollingContext, cancel := context.WithTimeout(ctx, s.client.PollingDuration)
defer cancel()
_, deleteErr := s.client.ResourceGroupsClient.Delete(pollingContext, id, resourcegroups.DefaultDeleteOperationOptions())
if deleteErr != nil {
ui.Error(fmt.Sprintf("Error deleting resource group. Please delete it manually.\n\n"+
"Name: %s\n"+
Expand Down
19 changes: 11 additions & 8 deletions builder/azure/arm/step_deploy_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ import (

"github.com/hashicorp/go-azure-helpers/resourcemanager/commonids"
"github.com/hashicorp/go-azure-sdk/resource-manager/compute/2022-03-01/virtualmachines"
"github.com/hashicorp/go-azure-sdk/resource-manager/compute/2022-03-02/disks"
"github.com/hashicorp/go-azure-sdk/resource-manager/network/2022-09-01/networksecuritygroups"
"github.com/hashicorp/go-azure-sdk/resource-manager/network/2022-09-01/virtualnetworks"
"github.com/hashicorp/go-azure-sdk/resource-manager/network/2023-09-01/networksecuritygroups"
"github.com/hashicorp/go-azure-sdk/resource-manager/resources/2022-09-01/deploymentoperations"
"github.com/hashicorp/go-azure-sdk/resource-manager/resources/2022-09-01/deployments"
"github.com/hashicorp/packer-plugin-azure/builder/azure/common/client"
Expand Down Expand Up @@ -189,8 +187,10 @@ func (s *StepDeployTemplate) getImageDetails(ctx context.Context, subscriptionId
//TODO is this still true?
//We can't depend on constants.ArmOSDiskVhd being set
var imageName, imageType string
pollingContext, cancel := context.WithTimeout(ctx, s.client.PollingDuration)
defer cancel()
vmID := virtualmachines.NewVirtualMachineID(subscriptionId, resourceGroupName, computeName)
vm, err := s.client.VirtualMachinesClient.Get(ctx, vmID, virtualmachines.DefaultGetOperationOptions())
vm, err := s.client.VirtualMachinesClient.Get(pollingContext, vmID, virtualmachines.DefaultGetOperationOptions())
if err != nil {
return imageName, imageType, err
}
Expand Down Expand Up @@ -237,7 +237,7 @@ func deleteResource(ctx context.Context, client *AzureClient, subscriptionId str
err := client.NetworkMetaClient.NetworkInterfaces.DeleteThenPoll(pollingContext, interfaceID)
return err
case "Microsoft.Network/virtualNetworks":
vnetID := virtualnetworks.NewVirtualNetworkID(subscriptionId, resourceGroupName, resourceName)
vnetID := commonids.NewVirtualNetworkID(subscriptionId, resourceGroupName, resourceName)
err := client.NetworkMetaClient.VirtualNetworks.DeleteThenPoll(pollingContext, vnetID)
return err
case "Microsoft.Network/networkSecurityGroups":
Expand All @@ -261,7 +261,7 @@ func (s *StepDeployTemplate) deleteImage(ctx context.Context, imageName string,
if isManagedDisk {
xs := strings.Split(imageName, "/")
diskName := xs[len(xs)-1]
diskId := disks.NewDiskID(subscriptionId, resourceGroupName, diskName)
diskId := commonids.NewManagedDiskID(subscriptionId, resourceGroupName, diskName)

if err := s.client.DisksClient.DeleteThenPoll(pollingContext, diskId); err != nil {
return err
Expand All @@ -279,7 +279,7 @@ func (s *StepDeployTemplate) deleteImage(ctx context.Context, imageName string,
if len(xs) < 3 {
return errors.New("Unable to parse path of image " + imageName)
}
_, err = s.client.GiovanniBlobClient.Delete(pollingContext, storageAccountName, "images", blobName, giovanniBlobStorageSDK.DeleteInput{})
_, err = s.client.GiovanniBlobClient.Delete(pollingContext, storageAccountName, blobName, giovanniBlobStorageSDK.DeleteInput{})
return err
}

Expand All @@ -288,7 +288,10 @@ func (s *StepDeployTemplate) deleteDeploymentResources(ctx context.Context, subs
options := deploymentoperations.DefaultListOperationOptions()
options.Top = &maxResources
id := deploymentoperations.NewResourceGroupDeploymentID(subscriptionId, resourceGroupName, deploymentName)
deploymentOperations, err := s.client.DeploymentOperationsClient.ListComplete(ctx, id, options)
pollingContext, cancel := context.WithTimeout(ctx, s.client.PollingDuration)
defer cancel()

deploymentOperations, err := s.client.DeploymentOperationsClient.ListComplete(pollingContext, id, options)
if err != nil {
s.reportIfError(err, resourceGroupName)
return err
Expand Down
Loading
Loading