From 0d427939bcb5cc7eac3c6841b111a1f0daadcf71 Mon Sep 17 00:00:00 2001 From: Christopher Boumenot Date: Thu, 22 Jun 2017 16:36:40 -0700 Subject: [PATCH] azure: Fix custom managed images I missed a change in my SDK update, and when I integrated this missing change I broke custom images. --- builder/azure/arm/config.go | 4 +-- builder/azure/arm/resource_resolver.go | 4 +-- builder/azure/arm/template_factory.go | 2 +- ...stVirtualMachineDeployment08.approved.json | 24 ++++------------- .../azure/common/template/template_builder.go | 27 ++++--------------- 5 files changed, 13 insertions(+), 48 deletions(-) diff --git a/builder/azure/arm/config.go b/builder/azure/arm/config.go index ee3d124f1..1d0c98d14 100644 --- a/builder/azure/arm/config.go +++ b/builder/azure/arm/config.go @@ -68,9 +68,7 @@ type Config struct { CustomManagedImageResourceGroupName string `mapstructure:"custom_managed_image_resource_group_name"` CustomManagedImageName string `mapstructure:"custom_managed_image_name"` - customManagedImageLocation string - customManagedImageBlobUri string - customManagedImageOSState compute.OperatingSystemStateTypes + customManagedImageID string Location string `mapstructure:"location"` VMSize string `mapstructure:"vm_size"` diff --git a/builder/azure/arm/resource_resolver.go b/builder/azure/arm/resource_resolver.go index ceaa7d746..993540f13 100644 --- a/builder/azure/arm/resource_resolver.go +++ b/builder/azure/arm/resource_resolver.go @@ -50,9 +50,7 @@ func (s *resourceResolver) Resolve(c *Config) error { return err } - c.customManagedImageBlobUri = *image.ImageProperties.StorageProfile.OsDisk.BlobURI - c.customManagedImageLocation = *image.Location - c.customManagedImageOSState = image.ImageProperties.StorageProfile.OsDisk.OsState + c.customManagedImageID = *image.ID } return nil diff --git a/builder/azure/arm/template_factory.go b/builder/azure/arm/template_factory.go index 034f41dfd..3cd00845f 100644 --- a/builder/azure/arm/template_factory.go +++ b/builder/azure/arm/template_factory.go @@ -53,7 +53,7 @@ func GetVirtualMachineDeployment(config *Config) (*resources.Deployment, error) if config.ImageUrl != "" { builder.SetImageUrl(config.ImageUrl, osType) } else if config.CustomManagedImageName != "" { - builder.SetManagedDiskUrl(config.CustomManagedImageName, config.customManagedImageLocation, config.customManagedImageBlobUri, config.customManagedImageOSState) + builder.SetManagedDiskUrl(config.customManagedImageID) } else if config.ManagedImageName != "" && config.ImagePublisher != "" { imageID := fmt.Sprintf("/subscriptions/%s/providers/Microsoft.Compute/locations/%s/publishers/%s/ArtifactTypes/vmimage/offers/%s/skus/%s/versions/%s", config.SubscriptionID, diff --git a/builder/azure/arm/template_factory_test.TestVirtualMachineDeployment08.approved.json b/builder/azure/arm/template_factory_test.TestVirtualMachineDeployment08.approved.json index 8fa181797..7089ed59c 100644 --- a/builder/azure/arm/template_factory_test.TestVirtualMachineDeployment08.approved.json +++ b/builder/azure/arm/template_factory_test.TestVirtualMachineDeployment08.approved.json @@ -87,8 +87,7 @@ { "apiVersion": "[variables('apiVersion')]", "dependsOn": [ - "[concat('Microsoft.Network/networkInterfaces/', variables('nicName'))]", - "[concat('Microsoft.Compute/images/', 'CustomManagedImageName')]" + "[concat('Microsoft.Network/networkInterfaces/', variables('nicName'))]" ], "location": "[variables('location')]", "name": "[parameters('vmName')]", @@ -125,30 +124,17 @@ }, "storageProfile": { "imageReference": { - "id": "[resourceId(resourceGroup().name, 'Microsoft.Compute/images', 'CustomManagedImageName')]" + "id": "" }, "osDisk": { "caching": "ReadWrite", - "createOption": "FromImage", - "name": "osdisk" - } - } - }, - "type": "Microsoft.Compute/virtualMachines" - }, - { - "apiVersion": "[variables('managedDiskApiVersion')]", - "location": "", - "name": "CustomManagedImageName", - "properties": { - "storageProfile": { - "osDisk": { - "blobUri": "", + "createOption": "fromImage", + "name": "osdisk", "osType": "Linux" } } }, - "type": "Microsoft.Compute/images" + "type": "Microsoft.Compute/virtualMachines" } ], "variables": { diff --git a/builder/azure/common/template/template_builder.go b/builder/azure/common/template/template_builder.go index d93094ff4..898bf77e9 100644 --- a/builder/azure/common/template/template_builder.go +++ b/builder/azure/common/template/template_builder.go @@ -101,38 +101,21 @@ func (s *TemplateBuilder) BuildWindows(keyVaultName, winRMCertificateUrl string) return nil } -func (s *TemplateBuilder) SetManagedDiskUrl(managedDiskImageName, location, blobUri string, osState compute.OperatingSystemStateTypes) error { +func (s *TemplateBuilder) SetManagedDiskUrl(managedImageId string) error { resource, err := s.getResourceByType(resourceVirtualMachine) if err != nil { return err } - resourceId := s.toResourceID(resourceManagedDisk, managedDiskImageName) profile := resource.Properties.StorageProfile profile.ImageReference = &compute.ImageReference{ - ID: to.StringPtr(resourceId), + ID: &managedImageId, } + profile.OsDisk.Name = to.StringPtr("osdisk") + profile.OsDisk.OsType = s.osType + profile.OsDisk.CreateOption = compute.FromImage profile.OsDisk.Vhd = nil - *resource.DependsOn = append(*resource.DependsOn, fmt.Sprintf("[concat('%s/', '%s')]", resourceManagedDisk, managedDiskImageName)) - - managedDiskResource := &Resource{ - Type: to.StringPtr(resourceManagedDisk), - ApiVersion: to.StringPtr(s.toVariable("managedDiskApiVersion")), - Name: to.StringPtr(managedDiskImageName), - Location: to.StringPtr(location), - Properties: &Properties{ - StorageProfile: &StorageProfileUnion{ - OsDisk: &OSDiskUnion{ - OsType: s.osType, - OsState: osState, - BlobURI: to.StringPtr(blobUri), - }, - }, - }, - } - - *s.template.Resources = append(*s.template.Resources, *managedDiskResource) return nil }