Skip to content

Commit

Permalink
Update version of gallery image API and pass in VM source ID when sou…
Browse files Browse the repository at this point in the history
…rce is a VM (#404)
  • Loading branch information
JenGoldstrich authored Apr 16, 2024
1 parent 317e465 commit e82be53
Show file tree
Hide file tree
Showing 25 changed files with 109 additions and 53 deletions.
4 changes: 2 additions & 2 deletions builder/azure/arm/azure_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ import (
"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/compute/2022-03-02/snapshots"
"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/compute/2023-07-03/galleryimages"
"github.com/hashicorp/go-azure-sdk/resource-manager/compute/2023-07-03/galleryimageversions"
"github.com/hashicorp/go-azure-sdk/resource-manager/keyvault/2023-07-01/secrets"
"github.com/hashicorp/go-azure-sdk/resource-manager/keyvault/2023-07-01/vaults"
networks "github.com/hashicorp/go-azure-sdk/resource-manager/network/2023-09-01"
Expand Down
4 changes: 2 additions & 2 deletions builder/azure/arm/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import (
"github.com/hashicorp/packer-plugin-azure/builder/azure/common/log"

"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/compute/2023-07-03/galleryimages"
"github.com/hashicorp/go-azure-sdk/resource-manager/compute/2023-07-03/galleryimageversions"
"github.com/hashicorp/go-azure-sdk/resource-manager/storage/2023-01-01/storageaccounts"

"github.com/hashicorp/go-azure-helpers/resourcemanager/commonids"
Expand Down
2 changes: 1 addition & 1 deletion builder/azure/arm/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,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-01/virtualmachines"
"github.com/hashicorp/go-azure-sdk/resource-manager/compute/2022-03-03/galleryimageversions"
"github.com/hashicorp/go-azure-sdk/resource-manager/compute/2023-07-03/galleryimageversions"
"github.com/masterzen/winrm"

azcommon "github.com/hashicorp/packer-plugin-azure/builder/azure/common"
Expand Down
2 changes: 1 addition & 1 deletion builder/azure/arm/step_get_source_image_name.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

"github.com/hashicorp/packer-plugin-azure/builder/azure/common/log"

"github.com/hashicorp/go-azure-sdk/resource-manager/compute/2022-03-03/galleryimageversions"
"github.com/hashicorp/go-azure-sdk/resource-manager/compute/2023-07-03/galleryimageversions"
"github.com/hashicorp/packer-plugin-sdk/multistep"
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
"github.com/hashicorp/packer-plugin-sdk/packerbuilderdata"
Expand Down
2 changes: 1 addition & 1 deletion builder/azure/arm/step_get_source_image_name_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"context"
"testing"

galleryimageversions "github.com/hashicorp/go-azure-sdk/resource-manager/compute/2022-03-03/galleryimageversions"
galleryimageversions "github.com/hashicorp/go-azure-sdk/resource-manager/compute/2023-07-03/galleryimageversions"
"github.com/hashicorp/packer-plugin-azure/builder/azure/common/client"
"github.com/hashicorp/packer-plugin-sdk/multistep"
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
Expand Down
13 changes: 7 additions & 6 deletions builder/azure/arm/step_publish_to_shared_image_gallery.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"fmt"

"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/galleryimageversions"
"github.com/hashicorp/go-azure-sdk/resource-manager/compute/2023-07-03/galleryimageversions"
"github.com/hashicorp/packer-plugin-azure/builder/azure/common"
"github.com/hashicorp/packer-plugin-azure/builder/azure/common/constants"
"github.com/hashicorp/packer-plugin-sdk/multistep"
Expand All @@ -25,14 +25,14 @@ type StepPublishToSharedImageGallery struct {

type PublishArgs struct {
SubscriptionID string
SourceID string
SharedImageGallery SharedImageGalleryDestination
EndOfLifeDate string
ExcludeFromLatest bool
ReplicaCount int64
Location string
ReplicationMode galleryimageversions.ReplicationMode
Tags map[string]string
GallerySource galleryimageversions.GalleryArtifactVersionFullSource
}

func NewStepPublishToSharedImageGallery(client *AzureClient, ui packersdk.Ui, config *Config) *StepPublishToSharedImageGallery {
Expand Down Expand Up @@ -183,9 +183,7 @@ func (s *StepPublishToSharedImageGallery) publishToSig(ctx context.Context, args
Tags: &args.Tags,
Properties: &galleryimageversions.GalleryImageVersionProperties{
StorageProfile: galleryimageversions.GalleryImageVersionStorageProfile{
Source: &galleryimageversions.GalleryArtifactVersionFullSource{
Id: &args.SourceID,
},
Source: &args.GallerySource,
},
PublishingProfile: &galleryimageversions.GalleryArtifactPublishingProfileBase{
TargetRegions: &imageVersionRegions,
Expand Down Expand Up @@ -233,16 +231,19 @@ func (s *StepPublishToSharedImageGallery) Run(ctx context.Context, stateBag mult
sharedImageGallery := getSigDestination(stateBag)
var sourceID string

gallerySource := galleryimageversions.GalleryArtifactVersionFullSource{}
var isManagedImage = stateBag.Get(constants.ArmIsManagedImage).(bool)
if isManagedImage {
targetManagedImageResourceGroupName := stateBag.Get(constants.ArmManagedImageResourceGroupName).(string)
targetManagedImageName := stateBag.Get(constants.ArmManagedImageName).(string)

managedImageSubscription := stateBag.Get(constants.ArmManagedImageSubscription).(string)
sourceID = fmt.Sprintf("/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Compute/images/%s", managedImageSubscription, targetManagedImageResourceGroupName, targetManagedImageName)
gallerySource.Id = &sourceID
} else {
var imageParameters = stateBag.Get(constants.ArmImageParameters).(*images.Image)
sourceID = *imageParameters.Properties.SourceVirtualMachine.Id
gallerySource.VirtualMachineId = &sourceID
}

miSGImageVersionEndOfLifeDate, _ := stateBag.Get(constants.ArmManagedImageSharedGalleryImageVersionEndOfLifeDate).(string)
Expand Down Expand Up @@ -290,14 +291,14 @@ func (s *StepPublishToSharedImageGallery) Run(ctx context.Context, stateBag mult
ctx,
PublishArgs{
SubscriptionID: subscriptionID,
SourceID: sourceID,
SharedImageGallery: sharedImageGallery,
EndOfLifeDate: miSGImageVersionEndOfLifeDate,
ExcludeFromLatest: miSGImageVersionExcludeFromLatest,
Location: location,
ReplicationMode: replicationMode,
Tags: tags,
ReplicaCount: miSigReplicaCount,
GallerySource: gallerySource,
},
)

Expand Down
81 changes: 66 additions & 15 deletions builder/azure/arm/step_publish_to_shared_image_gallery_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/hashicorp/go-azure-sdk/resource-manager/compute/2022-03-01/images"
"github.com/hashicorp/go-azure-sdk/resource-manager/compute/2023-07-03/galleryimageversions"

"github.com/hashicorp/packer-plugin-azure/builder/azure/common"
"github.com/hashicorp/packer-plugin-azure/builder/azure/common/constants"
Expand Down Expand Up @@ -38,10 +39,40 @@ func TestStepPublishToSharedImageGalleryShouldNotPublishForVhd(t *testing.T) {
}

func TestStepPublishToSharedImageGalleryShouldPublishForManagedImageWithSig(t *testing.T) {
var actualPublishArgs PublishArgs
expectedSource := "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/my-group/providers/Microsoft.Compute/images/packer-test"
expectedPublishArgs := PublishArgs{
SubscriptionID: "Unit Test: ManagedImageSubscription",
ReplicaCount: 1,
ReplicationMode: "Full",
Location: "Unit Test: Location",
GallerySource: galleryimageversions.GalleryArtifactVersionFullSource{
Id: &expectedSource,
},
SharedImageGallery: SharedImageGalleryDestination{
SigDestinationGalleryName: "Unit Test: ManagedImageSharedGalleryName",
SigDestinationImageName: "Unit Test: ManagedImageSharedGalleryImageName",
SigDestinationSubscription: "00000000-0000-0000-0000-000000000000",
SigDestinationImageVersion: "Unit Test: ManagedImageSharedGalleryImageVersion",
SigDestinationResourceGroup: "Unit Test: ManagedImageSigPublishResourceGroup",
SigDestinationReplicationRegions: []string{
"ManagedImageSharedGalleryReplicationRegionA",
"ManagedImageSharedGalleryReplicationRegionB",
},
SigDestinationTargetRegions: []TargetRegion{
{Name: "ManagedImageSharedGalleryReplicationRegionA"},
{Name: "ManagedImageSharedGalleryReplicationRegionB"},
},
SigDestinationStorageAccountType: "Standard_LRS",
},
Tags: map[string]string{"tag01": "Unit Test: Tags"},
}
var testSubject = &StepPublishToSharedImageGallery{
publish: func(context.Context, PublishArgs) (string, error) {
publish: func(ctx context.Context, args PublishArgs) (string, error) {
actualPublishArgs = args
return "", nil
},

say: func(message string) {},
error: func(e error) {},
toSIG: func() bool { return true },
Expand All @@ -56,20 +87,28 @@ func TestStepPublishToSharedImageGalleryShouldPublishForManagedImageWithSig(t *t
if _, ok := stateBag.GetOk(constants.Error); ok == true {
t.Fatalf("Expected the step to not set stateBag['%s'], but it was.", constants.Error)
}
if diff := cmp.Diff(actualPublishArgs, expectedPublishArgs, []cmp.Option{
cmpopts.IgnoreUnexported(PublishArgs{}),
}...); diff != "" {
t.Fatalf("Unexpected diff %s", diff)
}
}

func TestStepPublishToSharedImageGalleryShouldPublishForNonManagedImageWithSig(t *testing.T) {
var actualPublishArgs PublishArgs
expectedSource := "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/my-group/providers/Microsoft.Compute/virtualMachines/packer-test"
expectedPublishArgs := PublishArgs{
SubscriptionID: "Unit Test: ManagedImageSubscription",
ReplicaCount: 1,
ReplicationMode: "Full",
Location: "Unit Test: Location",
SourceID: "Unit Test: VM ID",
GallerySource: galleryimageversions.GalleryArtifactVersionFullSource{
VirtualMachineId: &expectedSource,
},
SharedImageGallery: SharedImageGalleryDestination{
SigDestinationGalleryName: "Unit Test: ManagedImageSharedGalleryName",
SigDestinationImageName: "Unit Test: ManagedImageSharedGalleryImageName",
SigDestinationSubscription: "Unit Test: ManagedImageSubscription",
SigDestinationSubscription: "00000000-0000-0000-0000-000000000000",
SigDestinationImageVersion: "Unit Test: ManagedImageSharedGalleryImageVersion",
SigDestinationResourceGroup: "Unit Test: ManagedImageSigPublishResourceGroup",
SigDestinationReplicationRegions: []string{
Expand Down Expand Up @@ -112,17 +151,20 @@ func TestStepPublishToSharedImageGalleryShouldPublishForNonManagedImageWithSig(t
}

func TestStepPublishToSharedImageGalleryShouldPublishWithShallowReplication(t *testing.T) {
expectedSource := "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/my-group/providers/Microsoft.Compute/virtualMachines/packer-test"
var actualPublishArgs PublishArgs
expectedPublishArgs := PublishArgs{
SubscriptionID: "Unit Test: ManagedImageSubscription",
ReplicaCount: 1,
ReplicationMode: "Shallow",
Location: "Unit Test: Location",
SourceID: "Unit Test: VM ID",
GallerySource: galleryimageversions.GalleryArtifactVersionFullSource{
VirtualMachineId: &expectedSource,
},
SharedImageGallery: SharedImageGalleryDestination{
SigDestinationGalleryName: "Unit Test: ManagedImageSharedGalleryName",
SigDestinationImageName: "Unit Test: ManagedImageSharedGalleryImageName",
SigDestinationSubscription: "Unit Test: ManagedImageSubscription",
SigDestinationSubscription: "00000000-0000-0000-0000-000000000000",
SigDestinationImageVersion: "Unit Test: ManagedImageSharedGalleryImageVersion",
SigDestinationResourceGroup: "Unit Test: ManagedImageSigPublishResourceGroup",
SigDestinationReplicationRegions: []string{
Expand Down Expand Up @@ -166,17 +208,20 @@ func TestStepPublishToSharedImageGalleryShouldPublishWithShallowReplication(t *t
}

func TestStepPublishToSharedImageGalleryShouldPublishWithReplicationCount(t *testing.T) {
expectedSource := "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/my-group/providers/Microsoft.Compute/virtualMachines/packer-test"
var actualPublishArgs PublishArgs
expectedPublishArgs := PublishArgs{
SubscriptionID: "Unit Test: ManagedImageSubscription",
ReplicaCount: 5,
ReplicationMode: "Full",
Location: "Unit Test: Location",
SourceID: "Unit Test: VM ID",
GallerySource: galleryimageversions.GalleryArtifactVersionFullSource{
VirtualMachineId: &expectedSource,
},
SharedImageGallery: SharedImageGalleryDestination{
SigDestinationGalleryName: "Unit Test: ManagedImageSharedGalleryName",
SigDestinationImageName: "Unit Test: ManagedImageSharedGalleryImageName",
SigDestinationSubscription: "Unit Test: ManagedImageSubscription",
SigDestinationSubscription: "00000000-0000-0000-0000-000000000000",
SigDestinationImageVersion: "Unit Test: ManagedImageSharedGalleryImageVersion",
SigDestinationResourceGroup: "Unit Test: ManagedImageSigPublishResourceGroup",
SigDestinationReplicationRegions: []string{
Expand Down Expand Up @@ -220,17 +265,20 @@ func TestStepPublishToSharedImageGalleryShouldPublishWithReplicationCount(t *tes
}

func TestStepPublishToSharedImageGalleryShouldPublishTargetRegions(t *testing.T) {
expectedSource := "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/my-group/providers/Microsoft.Compute/virtualMachines/packer-test"
var actualPublishArgs PublishArgs
expectedPublishArgs := PublishArgs{
SubscriptionID: "Unit Test: ManagedImageSubscription",
ReplicaCount: 1,
ReplicationMode: "Full",
Location: "Unit Test: Location",
SourceID: "Unit Test: VM ID",
GallerySource: galleryimageversions.GalleryArtifactVersionFullSource{
VirtualMachineId: &expectedSource,
},
SharedImageGallery: SharedImageGalleryDestination{
SigDestinationGalleryName: "Unit Test: ManagedImageSharedGalleryName",
SigDestinationImageName: "Unit Test: ManagedImageSharedGalleryImageName",
SigDestinationSubscription: "Unit Test: ManagedImageSubscription",
SigDestinationSubscription: "00000000-0000-0000-0000-000000000000",
SigDestinationImageVersion: "Unit Test: ManagedImageSharedGalleryImageVersion",
SigDestinationResourceGroup: "Unit Test: ManagedImageSigPublishResourceGroup",
SigDestinationReplicationRegions: []string{
Expand Down Expand Up @@ -387,16 +435,19 @@ func TestPublishToSharedImageGalleryBuildAzureImageTargetRegions(t *testing.T) {

func TestStepPublishToSharedImageGalleryShouldPublishForConfidentialVMImageWithSig(t *testing.T) {
var actualPublishArgs PublishArgs
expectedSource := "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/my-group/providers/Microsoft.Compute/virtualMachines/packer-test"
expectedPublishArgs := PublishArgs{
SubscriptionID: "Unit Test: ManagedImageSubscription",
ReplicaCount: 1,
ReplicationMode: "Full",
Location: "Unit Test: Location",
SourceID: "Unit Test: VM ID",
GallerySource: galleryimageversions.GalleryArtifactVersionFullSource{
VirtualMachineId: &expectedSource,
},
SharedImageGallery: SharedImageGalleryDestination{
SigDestinationGalleryName: "Unit Test: ManagedImageSharedGalleryName",
SigDestinationImageName: "Unit Test: ManagedImageSharedGalleryImageName",
SigDestinationSubscription: "Unit Test: ManagedImageSubscription",
SigDestinationSubscription: "00000000-0000-0000-0000-000000000000",
SigDestinationImageVersion: "Unit Test: ManagedImageSharedGalleryImageVersion",
SigDestinationResourceGroup: "Unit Test: ManagedImageSigPublishResourceGroup",
SigDestinationReplicationRegions: []string{
Expand Down Expand Up @@ -459,14 +510,14 @@ func createTestStateBagStepPublishToSharedImageGallery(managed bool) multistep.S
})
stateBag.Put(constants.ArmManagedImageSharedGalleryImageVersionStorageAccountType, "Standard_LRS")
if managed {
stateBag.Put(constants.ArmManagedImageResourceGroupName, "Unit Test: ManagedImageResourceGroupName")
stateBag.Put(constants.ArmManagedImageName, "Unit Test: ManagedImageName")
stateBag.Put(constants.ArmManagedImageResourceGroupName, "my-group")
stateBag.Put(constants.ArmManagedImageName, "packer-test")
} else {
stateBag.Put(constants.ArmImageParameters, &images.Image{Properties: &images.ImageProperties{
SourceVirtualMachine: &images.SubResource{Id: common.StringPtr("Unit Test: VM ID")},
SourceVirtualMachine: &images.SubResource{Id: common.StringPtr("/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/my-group/providers/Microsoft.Compute/virtualMachines/packer-test")},
}})
}
stateBag.Put(constants.ArmManagedImageSubscription, "Unit Test: ManagedImageSubscription")
stateBag.Put(constants.ArmManagedImageSubscription, "00000000-0000-0000-0000-000000000000")
stateBag.Put(constants.ArmSharedImageGalleryDestinationSubscription, "Unit Test: ManagedImageSubscription")
stateBag.Put(constants.ArmIsManagedImage, managed)
stateBag.Put(constants.ArmIsSIGImage, true)
Expand Down
2 changes: 1 addition & 1 deletion builder/azure/chroot/step_create_new_diskset.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"

"github.com/hashicorp/go-azure-sdk/resource-manager/compute/2022-03-02/disks"
"github.com/hashicorp/go-azure-sdk/resource-manager/compute/2022-03-03/galleryimageversions"
"github.com/hashicorp/go-azure-sdk/resource-manager/compute/2023-07-03/galleryimageversions"
)

var _ multistep.Step = &StepCreateNewDiskset{}
Expand Down
2 changes: 1 addition & 1 deletion builder/azure/chroot/step_create_new_diskset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/google/go-cmp/cmp"
"github.com/hashicorp/go-azure-helpers/resourcemanager/commonids"
"github.com/hashicorp/go-azure-sdk/resource-manager/compute/2022-03-02/disks"
"github.com/hashicorp/go-azure-sdk/resource-manager/compute/2022-03-03/galleryimageversions"
"github.com/hashicorp/go-azure-sdk/resource-manager/compute/2023-07-03/galleryimageversions"
"github.com/hashicorp/packer-plugin-azure/builder/azure/common"
"github.com/hashicorp/packer-plugin-azure/builder/azure/common/client"
"github.com/hashicorp/packer-plugin-sdk/multistep"
Expand Down
2 changes: 1 addition & 1 deletion builder/azure/chroot/step_create_shared_image_version.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

"github.com/hashicorp/packer-plugin-azure/builder/azure/common/log"

"github.com/hashicorp/go-azure-sdk/resource-manager/compute/2022-03-03/galleryimageversions"
"github.com/hashicorp/go-azure-sdk/resource-manager/compute/2023-07-03/galleryimageversions"
"github.com/hashicorp/packer-plugin-azure/builder/azure/common"
"github.com/hashicorp/packer-plugin-azure/builder/azure/common/client"
"github.com/hashicorp/packer-plugin-sdk/multistep"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"testing"

"github.com/google/go-cmp/cmp"
"github.com/hashicorp/go-azure-sdk/resource-manager/compute/2022-03-03/galleryimageversions"
"github.com/hashicorp/go-azure-sdk/resource-manager/compute/2023-07-03/galleryimageversions"
"github.com/hashicorp/packer-plugin-azure/builder/azure/common"
"github.com/hashicorp/packer-plugin-azure/builder/azure/common/client"
"github.com/hashicorp/packer-plugin-sdk/multistep"
Expand Down
2 changes: 1 addition & 1 deletion builder/azure/chroot/step_get_source_image_name.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

"github.com/hashicorp/packer-plugin-azure/builder/azure/common/log"

"github.com/hashicorp/go-azure-sdk/resource-manager/compute/2022-03-03/galleryimageversions"
"github.com/hashicorp/go-azure-sdk/resource-manager/compute/2023-07-03/galleryimageversions"
"github.com/hashicorp/packer-plugin-azure/builder/azure/common/client"
"github.com/hashicorp/packer-plugin-sdk/multistep"
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
Expand Down
2 changes: 1 addition & 1 deletion builder/azure/chroot/step_get_source_image_name_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"fmt"
"testing"

"github.com/hashicorp/go-azure-sdk/resource-manager/compute/2022-03-03/galleryimageversions"
"github.com/hashicorp/go-azure-sdk/resource-manager/compute/2023-07-03/galleryimageversions"
"github.com/hashicorp/packer-plugin-azure/builder/azure/common"
"github.com/hashicorp/packer-plugin-azure/builder/azure/common/client"
"github.com/hashicorp/packer-plugin-sdk/multistep"
Expand Down
Loading

0 comments on commit e82be53

Please sign in to comment.