diff --git a/builder/azure/chroot/builder.go b/builder/azure/chroot/builder.go index 26105ed1a..f1b577352 100644 --- a/builder/azure/chroot/builder.go +++ b/builder/azure/chroot/builder.go @@ -367,7 +367,6 @@ func buildsteps(config Config, info *client.ComputeInfo) []multistep.Step { if config.FromScratch { steps = append(steps, &StepCreateNewDisk{ - SubscriptionID: info.SubscriptionID, ResourceGroup: info.ResourceGroupName, DiskName: config.TemporaryOSDiskName, DiskSizeGB: config.OSDiskSizeGB, @@ -388,7 +387,6 @@ func buildsteps(config Config, info *client.ComputeInfo) []multistep.Step { } steps = append(steps, &StepCreateNewDisk{ - SubscriptionID: info.SubscriptionID, ResourceGroup: info.ResourceGroupName, DiskName: config.TemporaryOSDiskName, DiskSizeGB: config.OSDiskSizeGB, @@ -406,11 +404,9 @@ func buildsteps(config Config, info *client.ComputeInfo) []multistep.Step { steps = append(steps, &StepVerifySourceDisk{ SourceDiskResourceID: config.Source, - SubscriptionID: info.SubscriptionID, Location: info.Location, }, &StepCreateNewDisk{ - SubscriptionID: info.SubscriptionID, ResourceGroup: info.ResourceGroupName, DiskName: config.TemporaryOSDiskName, DiskSizeGB: config.OSDiskSizeGB, diff --git a/builder/azure/chroot/builder_test.go b/builder/azure/chroot/builder_test.go index 307335768..67e0deae1 100644 --- a/builder/azure/chroot/builder_test.go +++ b/builder/azure/chroot/builder_test.go @@ -154,8 +154,7 @@ func Test_buildsteps(t *testing.T) { for _, s := range steps { if s, ok := s.(*StepVerifySourceDisk); ok { if s.SourceDiskResourceID == "diskresourceid" && - s.Location == info.Location && - s.SubscriptionID == info.SubscriptionID { + s.Location == info.Location { return } t.Errorf("found misconfigured StepVerifySourceDisk: %+v", s) diff --git a/builder/azure/chroot/step_create_new_disk.go b/builder/azure/chroot/step_create_new_disk.go index 6a041bc53..0cf808058 100644 --- a/builder/azure/chroot/step_create_new_disk.go +++ b/builder/azure/chroot/step_create_new_disk.go @@ -15,10 +15,10 @@ import ( var _ multistep.Step = &StepCreateNewDisk{} type StepCreateNewDisk struct { - SubscriptionID, ResourceGroup, DiskName string - DiskSizeGB int32 // optional, ignored if 0 - DiskStorageAccountType string // from compute.DiskStorageAccountTypes - HyperVGeneration string + ResourceGroup, DiskName string + DiskSizeGB int32 // optional, ignored if 0 + DiskStorageAccountType string // from compute.DiskStorageAccountTypes + HyperVGeneration string Location string PlatformImage *client.PlatformImage @@ -33,7 +33,7 @@ func (s StepCreateNewDisk) Run(ctx context.Context, state multistep.StateBag) mu ui := state.Get("ui").(packer.Ui) diskResourceID := fmt.Sprintf("/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Compute/disks/%s", - s.SubscriptionID, + azcli.SubscriptionID(), s.ResourceGroup, s.DiskName) state.Put("os_disk_resource_id", diskResourceID) @@ -67,7 +67,7 @@ func (s StepCreateNewDisk) Run(ctx context.Context, state multistep.StateBag) mu disk.CreationData.ImageReference = &compute.ImageDiskReference{ ID: to.StringPtr(fmt.Sprintf( "/subscriptions/%s/providers/Microsoft.Compute/locations/%s/publishers/%s/artifacttypes/vmimage/offers/%s/skus/%s/versions/%s", - s.SubscriptionID, s.Location, s.PlatformImage.Publisher, s.PlatformImage.Offer, s.PlatformImage.Sku, s.PlatformImage.Version)), + azcli.SubscriptionID(), s.Location, s.PlatformImage.Publisher, s.PlatformImage.Offer, s.PlatformImage.Sku, s.PlatformImage.Version)), } } diff --git a/builder/azure/chroot/step_create_new_disk_test.go b/builder/azure/chroot/step_create_new_disk_test.go index 249ba814a..b9e8537c8 100644 --- a/builder/azure/chroot/step_create_new_disk_test.go +++ b/builder/azure/chroot/step_create_new_disk_test.go @@ -17,7 +17,6 @@ import ( func TestStepCreateNewDisk_Run(t *testing.T) { type fields struct { - SubscriptionID string ResourceGroup string DiskName string DiskSizeGB int32 @@ -37,7 +36,6 @@ func TestStepCreateNewDisk_Run(t *testing.T) { { name: "HappyPathDiskSource", fields: fields{ - SubscriptionID: "SubscriptionID", ResourceGroup: "ResourceGroupName", DiskName: "TemporaryOSDiskName", DiskSizeGB: 42, @@ -68,7 +66,6 @@ func TestStepCreateNewDisk_Run(t *testing.T) { { name: "HappyPathDiskSource", fields: fields{ - SubscriptionID: "SubscriptionID", ResourceGroup: "ResourceGroupName", DiskName: "TemporaryOSDiskName", DiskStorageAccountType: string(compute.StandardLRS), @@ -105,7 +102,6 @@ func TestStepCreateNewDisk_Run(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { s := StepCreateNewDisk{ - SubscriptionID: tt.fields.SubscriptionID, ResourceGroup: tt.fields.ResourceGroup, DiskName: tt.fields.DiskName, DiskSizeGB: tt.fields.DiskSizeGB, @@ -135,7 +131,8 @@ func TestStepCreateNewDisk_Run(t *testing.T) { state := new(multistep.BasicStateBag) state.Put("azureclient", &client.AzureClientSetMock{ - DisksClientMock: m, + DisksClientMock: m, + SubscriptionIDMock: "SubscriptionID", }) state.Put("ui", packer.TestUi(t)) diff --git a/builder/azure/chroot/step_verify_source_disk.go b/builder/azure/chroot/step_verify_source_disk.go index 04879abd9..43a0343ef 100644 --- a/builder/azure/chroot/step_verify_source_disk.go +++ b/builder/azure/chroot/step_verify_source_disk.go @@ -15,7 +15,6 @@ import ( ) type StepVerifySourceDisk struct { - SubscriptionID string SourceDiskResourceID string Location string } @@ -34,10 +33,10 @@ func (s StepVerifySourceDisk) Run(ctx context.Context, state multistep.StateBag) return multistep.ActionHalt } - if !strings.EqualFold(resource.SubscriptionID, s.SubscriptionID) { + if !strings.EqualFold(resource.SubscriptionID, azcli.SubscriptionID()) { err := fmt.Errorf("Source disk resource %q is in a different subscription than this VM (%q). "+ "Packer does not know how to handle that.", - s.SourceDiskResourceID, s.SubscriptionID) + s.SourceDiskResourceID, azcli.SubscriptionID()) log.Printf("StepVerifySourceDisk.Run: error: %+v", err) state.Put("error", err) ui.Error(err.Error()) diff --git a/builder/azure/chroot/step_verify_source_disk_test.go b/builder/azure/chroot/step_verify_source_disk_test.go index e8096d39c..77fefd890 100644 --- a/builder/azure/chroot/step_verify_source_disk_test.go +++ b/builder/azure/chroot/step_verify_source_disk_test.go @@ -18,7 +18,6 @@ import ( func Test_StepVerifySourceDisk_Run(t *testing.T) { type fields struct { - SubscriptionID string SourceDiskResourceID string Location string @@ -38,7 +37,6 @@ func Test_StepVerifySourceDisk_Run(t *testing.T) { { name: "HappyPath", fields: fields{ - SubscriptionID: "subid1", SourceDiskResourceID: "/subscriptions/subid1/resourcegroups/rg1/providers/Microsoft.Compute/disks/disk1", Location: "westus2", @@ -50,7 +48,6 @@ func Test_StepVerifySourceDisk_Run(t *testing.T) { { name: "NotAResourceID", fields: fields{ - SubscriptionID: "subid1", SourceDiskResourceID: "/other", Location: "westus2", @@ -63,7 +60,6 @@ func Test_StepVerifySourceDisk_Run(t *testing.T) { { name: "DiskNotFound", fields: fields{ - SubscriptionID: "subid1", SourceDiskResourceID: "/subscriptions/subid1/resourcegroups/rg1/providers/Microsoft.Compute/disks/disk1", Location: "westus2", @@ -76,7 +72,6 @@ func Test_StepVerifySourceDisk_Run(t *testing.T) { { name: "NotADisk", fields: fields{ - SubscriptionID: "subid1", SourceDiskResourceID: "/subscriptions/subid1/resourcegroups/rg1/providers/Microsoft.Compute/images/image1", Location: "westus2", @@ -88,7 +83,6 @@ func Test_StepVerifySourceDisk_Run(t *testing.T) { { name: "OtherSubscription", fields: fields{ - SubscriptionID: "subid1", SourceDiskResourceID: "/subscriptions/subid2/resourcegroups/rg1/providers/Microsoft.Compute/disks/disk1", Location: "westus2", @@ -101,7 +95,6 @@ func Test_StepVerifySourceDisk_Run(t *testing.T) { { name: "OtherLocation", fields: fields{ - SubscriptionID: "subid1", SourceDiskResourceID: "/subscriptions/subid1/resourcegroups/rg1/providers/Microsoft.Compute/disks/disk1", Location: "eastus", @@ -115,7 +108,6 @@ func Test_StepVerifySourceDisk_Run(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { s := StepVerifySourceDisk{ - SubscriptionID: tt.fields.SubscriptionID, SourceDiskResourceID: tt.fields.SourceDiskResourceID, Location: tt.fields.Location, } @@ -133,7 +125,8 @@ func Test_StepVerifySourceDisk_Run(t *testing.T) { state := new(multistep.BasicStateBag) state.Put("azureclient", &client.AzureClientSetMock{ - DisksClientMock: m, + DisksClientMock: m, + SubscriptionIDMock: "subid1", }) state.Put("ui", ui) diff --git a/builder/azure/common/client/azure_client_set.go b/builder/azure/common/client/azure_client_set.go index 8e0231a7b..bcbbd60a4 100644 --- a/builder/azure/common/client/azure_client_set.go +++ b/builder/azure/common/client/azure_client_set.go @@ -20,6 +20,9 @@ type AzureClientSet interface { VirtualMachineImagesClient() VirtualMachineImagesClientAPI PollClient() autorest.Client + + // SubscriptionID returns the subscription ID that this client set was created for + SubscriptionID() string } var subscriptionPathRegex = regexp.MustCompile(`/subscriptions/([[:xdigit:]]{8}(-[[:xdigit:]]{4}){3}-[[:xdigit:]]{12})`) @@ -50,6 +53,10 @@ func new(c Config, say func(string)) (*azureClientSet, error) { }, nil } +func (s azureClientSet) SubscriptionID() string { + return s.subscriptionID +} + func (s azureClientSet) configureAutorestClient(c *autorest.Client) { c.AddToUserAgent(useragent.String()) c.Authorizer = s.authorizer diff --git a/builder/azure/common/client/azure_client_set_mock.go b/builder/azure/common/client/azure_client_set_mock.go index 8932ed767..3363ca3bf 100644 --- a/builder/azure/common/client/azure_client_set_mock.go +++ b/builder/azure/common/client/azure_client_set_mock.go @@ -5,6 +5,8 @@ import ( "github.com/Azure/go-autorest/autorest" ) +var _ AzureClientSet = &AzureClientSetMock{} + // AzureClientSetMock provides a generic mock for AzureClientSet type AzureClientSetMock struct { DisksClientMock computeapi.DisksClientAPI @@ -13,6 +15,7 @@ type AzureClientSetMock struct { VirtualMachinesClientMock computeapi.VirtualMachinesClientAPI PollClientMock autorest.Client MetadataClientMock MetadataClientAPI + SubscriptionIDMock string } // DisksClient returns a DisksClientAPI @@ -44,3 +47,8 @@ func (m *AzureClientSetMock) PollClient() autorest.Client { func (m *AzureClientSetMock) MetadataClient() MetadataClientAPI { return m.MetadataClientMock } + +// SubscriptionID returns SubscriptionIDMock +func (m *AzureClientSetMock) SubscriptionID() string { + return m.SubscriptionIDMock +}