Skip to content

Commit

Permalink
Migrate to newest SDK, fully remove go-autorest, fix chroot builder b…
Browse files Browse the repository at this point in the history
…ug, move metadata client off auto-rest, and handle unathenticated subscription call in custom http client
  • Loading branch information
JenGoldstrich committed Mar 28, 2024
1 parent 8bee379 commit 47c605c
Show file tree
Hide file tree
Showing 44 changed files with 781 additions and 838 deletions.
7 changes: 4 additions & 3 deletions GNUmakefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
NAME=azure
BINARY=packer-plugin-${NAME}
PLUGIN_FQN="$(shell grep -E '^module' <go.mod | sed -E 's/module *//')"

COUNT?=1
TEST?=$(shell go list ./...)
Expand All @@ -10,9 +11,9 @@ HASHICORP_PACKER_PLUGIN_SDK_VERSION?=$(shell go list -m github.com/hashicorp/pac
build:
@go build -o ${BINARY}

dev: build
@mkdir -p ~/.packer.d/plugins/
@mv ${BINARY} ~/.packer.d/plugins/${BINARY}
dev:
@go build -ldflags="-X '${PLUGIN_FQN}/version.VersionPrerelease=dev'" -o '${BINARY}'
packer plugins install --path ${BINARY} "$(shell echo "${PLUGIN_FQN}" | sed 's/packer-plugin-//')"

test:
@go test -race -count $(COUNT) $(TEST) -timeout=3m
Expand Down
265 changes: 139 additions & 126 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,14 +204,14 @@ 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 {
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)
}
Expand Down Expand Up @@ -258,7 +260,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 @@ -495,7 +497,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
Loading

0 comments on commit 47c605c

Please sign in to comment.