Use all snapshots when creating a shared image

This commit is contained in:
Paul Meyer 2020-04-29 19:14:15 +00:00
parent c6b995a34d
commit de4839b66e
2 changed files with 45 additions and 7 deletions

View File

@ -4,6 +4,7 @@ import (
"context" "context"
"fmt" "fmt"
"log" "log"
"sort"
"time" "time"
"github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2019-12-01/compute" "github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2019-12-01/compute"
@ -22,11 +23,11 @@ type StepCreateSharedImageVersion struct {
func (s *StepCreateSharedImageVersion) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction { func (s *StepCreateSharedImageVersion) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
azcli := state.Get("azureclient").(client.AzureClientSet) azcli := state.Get("azureclient").(client.AzureClientSet)
ui := state.Get("ui").(packer.Ui) ui := state.Get("ui").(packer.Ui)
osDiskSnapshotResourceID := state.Get(stateBagKey_Snapshotset).(Diskset)[-1].String() snapshotset := state.Get(stateBagKey_Snapshotset).(Diskset)
ui.Say(fmt.Sprintf("Creating image version %s\n using %s for os disk.", ui.Say(fmt.Sprintf("Creating image version %s\n using %q for os disk.",
s.Destination.ResourceID(azcli.SubscriptionID()), s.Destination.ResourceID(azcli.SubscriptionID()),
osDiskSnapshotResourceID)) snapshotset.OS()))
var targetRegions []compute.TargetRegion var targetRegions []compute.TargetRegion
// transform target regions to API objects // transform target regions to API objects
@ -44,7 +45,7 @@ func (s *StepCreateSharedImageVersion) Run(ctx context.Context, state multistep.
GalleryImageVersionProperties: &compute.GalleryImageVersionProperties{ GalleryImageVersionProperties: &compute.GalleryImageVersionProperties{
StorageProfile: &compute.GalleryImageVersionStorageProfile{ StorageProfile: &compute.GalleryImageVersionStorageProfile{
OsDiskImage: &compute.GalleryOSDiskImage{ OsDiskImage: &compute.GalleryOSDiskImage{
Source: &compute.GalleryArtifactVersionSource{ID: &osDiskSnapshotResourceID}, Source: &compute.GalleryArtifactVersionSource{ID: to.StringPtr(snapshotset.OS().String())},
HostCaching: compute.HostCaching(s.OSDiskCacheType), HostCaching: compute.HostCaching(s.OSDiskCacheType),
}, },
}, },
@ -55,6 +56,23 @@ func (s *StepCreateSharedImageVersion) Run(ctx context.Context, state multistep.
}, },
} }
var datadisks []compute.GalleryDataDiskImage
for lun, resource := range snapshotset {
if lun != -1 {
datadisks = append(datadisks, compute.GalleryDataDiskImage{
Lun: to.Int32Ptr(lun),
Source: &compute.GalleryArtifactVersionSource{ID: to.StringPtr(resource.String())},
})
}
}
if datadisks != nil {
// sort by lun
sort.Slice(datadisks, func(i, j int) bool {
return *datadisks[i].Lun < *datadisks[j].Lun
})
imageVersion.GalleryImageVersionProperties.StorageProfile.DataDiskImages = &datadisks
}
f, err := azcli.GalleryImageVersionsClient().CreateOrUpdate( f, err := azcli.GalleryImageVersionsClient().CreateOrUpdate(
ctx, ctx,
s.Destination.ResourceGroup, s.Destination.ResourceGroup,

View File

@ -25,6 +25,7 @@ func TestStepCreateSharedImageVersion_Run(t *testing.T) {
tests := []struct { tests := []struct {
name string name string
fields fields fields fields
snapshotset Diskset
want multistep.StepAction want multistep.StepAction
expectedPutBody string expectedPutBody string
}{ }{
@ -47,6 +48,11 @@ func TestStepCreateSharedImageVersion_Run(t *testing.T) {
}, },
Location: "region2", Location: "region2",
}, },
snapshotset: diskset(
"/subscriptions/12345/resourceGroups/group1/providers/Microsoft.Compute/snapshots/osdisksnapshot",
"/subscriptions/12345/resourceGroups/group1/providers/Microsoft.Compute/snapshots/datadisksnapshot0",
"/subscriptions/12345/resourceGroups/group1/providers/Microsoft.Compute/snapshots/datadisksnapshot1",
"/subscriptions/12345/resourceGroups/group1/providers/Microsoft.Compute/snapshots/datadisksnapshot2"),
expectedPutBody: `{ expectedPutBody: `{
"location": "region2", "location": "region2",
"properties": { "properties": {
@ -63,9 +69,23 @@ func TestStepCreateSharedImageVersion_Run(t *testing.T) {
"storageProfile": { "storageProfile": {
"osDiskImage": { "osDiskImage": {
"source": { "source": {
"id": "/subscriptions/12345/resourceGroups/group1/providers/Microsoft.Compute/snapshots/snapshot1" "id": "/subscriptions/12345/resourceGroups/group1/providers/Microsoft.Compute/snapshots/osdisksnapshot"
} }
} },
"dataDiskImages": [
{
"lun": 0,
"source": { "id": "/subscriptions/12345/resourceGroups/group1/providers/Microsoft.Compute/snapshots/datadisksnapshot0" }
},
{
"lun": 1,
"source": { "id": "/subscriptions/12345/resourceGroups/group1/providers/Microsoft.Compute/snapshots/datadisksnapshot1" }
},
{
"lun": 2,
"source": { "id": "/subscriptions/12345/resourceGroups/group1/providers/Microsoft.Compute/snapshots/datadisksnapshot2" }
}
]
} }
} }
}`, }`,
@ -94,7 +114,7 @@ func TestStepCreateSharedImageVersion_Run(t *testing.T) {
GalleryImageVersionsClientMock: m, GalleryImageVersionsClientMock: m,
}) })
state.Put("ui", packer.TestUi(t)) state.Put("ui", packer.TestUi(t))
state.Put(stateBagKey_Snapshotset, diskset("/subscriptions/12345/resourceGroups/group1/providers/Microsoft.Compute/snapshots/snapshot1")) state.Put(stateBagKey_Snapshotset, tt.snapshotset)
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
s := &StepCreateSharedImageVersion{ s := &StepCreateSharedImageVersion{