From 5bd0de7b5a81d168df9b18bd727480f8dd526bf8 Mon Sep 17 00:00:00 2001 From: tombuildsstuff Date: Tue, 23 Jan 2018 15:31:18 +0100 Subject: [PATCH] Migrating over to the new Disks SDK --- azurerm/config.go | 30 ++--- azurerm/data_source_managed_disk.go | 19 ++- azurerm/data_source_snapshot.go | 5 +- azurerm/encryption_settings.go | 16 +-- azurerm/resource_arm_image_test.go | 7 +- azurerm/resource_arm_managed_disk.go | 124 ++++++++++-------- azurerm/resource_arm_managed_disk_test.go | 33 ++--- azurerm/resource_arm_snapshot.go | 55 ++++---- azurerm/resource_arm_snapshot_test.go | 6 +- azurerm/resource_arm_virtual_machine.go | 11 +- ..._arm_virtual_machine_managed_disks_test.go | 8 +- 11 files changed, 172 insertions(+), 142 deletions(-) diff --git a/azurerm/config.go b/azurerm/config.go index 4a53e7850e5b..465a29b8aa11 100644 --- a/azurerm/config.go +++ b/azurerm/config.go @@ -12,7 +12,6 @@ import ( "github.com/Azure/azure-sdk-for-go/arm/automation" "github.com/Azure/azure-sdk-for-go/arm/cosmos-db" - "github.com/Azure/azure-sdk-for-go/arm/disk" "github.com/Azure/azure-sdk-for-go/arm/keyvault" keyVault "github.com/Azure/azure-sdk-for-go/dataplane/keyvault" appinsights "github.com/Azure/azure-sdk-for-go/services/appinsights/mgmt/2015-05-01/insights" @@ -61,8 +60,6 @@ type ArmClient struct { StopContext context.Context - diskClient disk.DisksClient - snapshotsClient disk.SnapshotsClient cosmosDBClient cosmosdb.DatabaseAccountsClient automationAccountClient automation.AccountClient automationRunbookClient automation.RunbookClient @@ -104,7 +101,9 @@ type ArmClient struct { // Compute availSetClient compute.AvailabilitySetsClient + diskClient compute.DisksClient imageClient compute.ImagesClient + snapshotsClient compute.SnapshotsClient usageOpsClient compute.UsageClient vmExtensionImageClient compute.VirtualMachineExtensionImagesClient vmExtensionClient compute.VirtualMachineExtensionsClient @@ -437,7 +436,6 @@ func getArmClient(c *authentication.Config) (*ArmClient, error) { client.registerContainerInstanceClients(endpoint, c.SubscriptionID, auth, sender) client.registerContainerRegistryClients(endpoint, c.SubscriptionID, auth, sender) client.registerDatabases(endpoint, c.SubscriptionID, auth, sender) - client.registerDisks(endpoint, c.SubscriptionID, auth, sender) client.registerDNSClients(endpoint, c.SubscriptionID, auth, sender) client.registerEventGridClients(endpoint, c.SubscriptionID, auth, sender) client.registerEventHubClients(endpoint, c.SubscriptionID, auth, sender) @@ -538,10 +536,18 @@ func (c *ArmClient) registerComputeClients(endpoint, subscriptionId string, auth c.configureClient(&availabilitySetsClient.Client, auth) c.availSetClient = availabilitySetsClient + diskClient := compute.NewDisksClientWithBaseURI(endpoint, subscriptionId) + c.configureClient(&diskClient.Client, auth) + c.diskClient = diskClient + imagesClient := compute.NewImagesClientWithBaseURI(endpoint, subscriptionId) c.configureClient(&imagesClient.Client, auth) c.imageClient = imagesClient + snapshotsClient := compute.NewSnapshotsClientWithBaseURI(endpoint, subscriptionId) + c.configureClient(&snapshotsClient.Client, auth) + c.snapshotsClient = snapshotsClient + usageClient := compute.NewUsageClientWithBaseURI(endpoint, subscriptionId) c.configureClient(&usageClient.Client, auth) c.usageOpsClient = usageClient @@ -656,22 +662,6 @@ func (c *ArmClient) registerDatabases(endpoint, subscriptionId string, auth auto c.sqlServersClient = sqlSrvClient } -func (c *ArmClient) registerDisks(endpoint, subscriptionId string, auth autorest.Authorizer, sender autorest.Sender) { - diskClient := disk.NewDisksClientWithBaseURI(endpoint, subscriptionId) - setUserAgent(&diskClient.Client) - diskClient.Authorizer = auth - diskClient.Sender = sender - diskClient.SkipResourceProviderRegistration = c.skipProviderRegistration - c.diskClient = diskClient - - snapshotsClient := disk.NewSnapshotsClientWithBaseURI(endpoint, subscriptionId) - setUserAgent(&snapshotsClient.Client) - snapshotsClient.Authorizer = auth - snapshotsClient.Sender = sender - snapshotsClient.SkipResourceProviderRegistration = c.skipProviderRegistration - c.snapshotsClient = snapshotsClient -} - func (c *ArmClient) registerDNSClients(endpoint, subscriptionId string, auth autorest.Authorizer, sender autorest.Sender) { dn := dns.NewRecordSetsClientWithBaseURI(endpoint, subscriptionId) c.configureClient(&dn.Client, auth) diff --git a/azurerm/data_source_managed_disk.go b/azurerm/data_source_managed_disk.go index 92472178a50c..76a5493012e2 100644 --- a/azurerm/data_source_managed_disk.go +++ b/azurerm/data_source_managed_disk.go @@ -50,12 +50,13 @@ func dataSourceArmManagedDisk() *schema.Resource { } func dataSourceArmManagedDiskRead(d *schema.ResourceData, meta interface{}) error { - diskClient := meta.(*ArmClient).diskClient + client := meta.(*ArmClient).diskClient + ctx := meta.(*ArmClient).StopContext resGroup := d.Get("resource_group_name").(string) name := d.Get("name").(string) - resp, err := diskClient.Get(resGroup, name) + resp, err := client.Get(ctx, resGroup, name) if err != nil { if resp.StatusCode == http.StatusNotFound { d.SetId("") @@ -65,8 +66,18 @@ func dataSourceArmManagedDiskRead(d *schema.ResourceData, meta interface{}) erro } d.SetId(*resp.ID) - if resp.Properties != nil { - flattenAzureRmManagedDiskProperties(d, resp.Properties) + + if sku := resp.Sku; sku != nil { + d.Set("storage_account_type", string(sku.Name)) + } + + if props := resp.DiskProperties; props != nil { + if diskSize := props.DiskSizeGB; diskSize != nil { + d.Set("disk_size_gb", *diskSize) + } + if osType := props.OsType; osType != "" { + d.Set("os_type", string(osType)) + } } if resp.CreationData != nil { diff --git a/azurerm/data_source_snapshot.go b/azurerm/data_source_snapshot.go index 8b098df13140..292ea62bd232 100644 --- a/azurerm/data_source_snapshot.go +++ b/azurerm/data_source_snapshot.go @@ -101,18 +101,19 @@ func dataSourceArmSnapshot() *schema.Resource { func dataSourceArmSnapshotRead(d *schema.ResourceData, meta interface{}) error { client := meta.(*ArmClient).snapshotsClient + ctx := meta.(*ArmClient).StopContext resourceGroup := d.Get("resource_group_name").(string) name := d.Get("name").(string) - resp, err := client.Get(resourceGroup, name) + resp, err := client.Get(ctx, resourceGroup, name) if err != nil { return fmt.Errorf("Error loading Snapshot %q (Resource Group %q): %+v", name, resourceGroup, err) } d.SetId(*resp.ID) - if props := resp.Properties; props != nil { + if props := resp.DiskProperties; props != nil { d.Set("os_type", string(props.OsType)) d.Set("time_created", props.TimeCreated.String()) diff --git a/azurerm/encryption_settings.go b/azurerm/encryption_settings.go index aba8c66274ad..9179f04e0234 100644 --- a/azurerm/encryption_settings.go +++ b/azurerm/encryption_settings.go @@ -1,7 +1,7 @@ package azurerm import ( - "github.com/Azure/azure-sdk-for-go/arm/disk" + "github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2017-03-30/compute" "github.com/hashicorp/terraform/helper/schema" "github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils" ) @@ -63,9 +63,9 @@ func encryptionSettingsSchema() *schema.Schema { } } -func expandManagedDiskEncryptionSettings(settings map[string]interface{}) *disk.EncryptionSettings { +func expandManagedDiskEncryptionSettings(settings map[string]interface{}) *compute.EncryptionSettings { enabled := settings["enabled"].(bool) - config := &disk.EncryptionSettings{ + config := &compute.EncryptionSettings{ Enabled: utils.Bool(enabled), } @@ -74,9 +74,9 @@ func expandManagedDiskEncryptionSettings(settings map[string]interface{}) *disk. secretURL := dek["secret_url"].(string) sourceVaultId := dek["source_vault_id"].(string) - config.DiskEncryptionKey = &disk.KeyVaultAndSecretReference{ + config.DiskEncryptionKey = &compute.KeyVaultAndSecretReference{ SecretURL: utils.String(secretURL), - SourceVault: &disk.SourceVault{ + SourceVault: &compute.SourceVault{ ID: utils.String(sourceVaultId), }, } @@ -87,9 +87,9 @@ func expandManagedDiskEncryptionSettings(settings map[string]interface{}) *disk. secretURL := kek["key_url"].(string) sourceVaultId := kek["source_vault_id"].(string) - config.KeyEncryptionKey = &disk.KeyVaultAndKeyReference{ + config.KeyEncryptionKey = &compute.KeyVaultAndKeyReference{ KeyURL: utils.String(secretURL), - SourceVault: &disk.SourceVault{ + SourceVault: &compute.SourceVault{ ID: utils.String(sourceVaultId), }, } @@ -98,7 +98,7 @@ func expandManagedDiskEncryptionSettings(settings map[string]interface{}) *disk. return config } -func flattenManagedDiskEncryptionSettings(encryptionSettings *disk.EncryptionSettings) []interface{} { +func flattenManagedDiskEncryptionSettings(encryptionSettings *compute.EncryptionSettings) []interface{} { value := map[string]interface{}{ "enabled": *encryptionSettings.Enabled, } diff --git a/azurerm/resource_arm_image_test.go b/azurerm/resource_arm_image_test.go index 8d4edf8ab81e..f5a2bf12b622 100644 --- a/azurerm/resource_arm_image_test.go +++ b/azurerm/resource_arm_image_test.go @@ -327,7 +327,8 @@ func testCheckAzureVMSSExists(sourceVMSS string, shouldExist bool) resource.Test } func testCheckAzureRMImageDestroy(s *terraform.State) error { - conn := testAccProvider.Meta().(*ArmClient).diskClient + client := testAccProvider.Meta().(*ArmClient).diskClient + ctx := testAccProvider.Meta().(*ArmClient).StopContext for _, rs := range s.RootModule().Resources { if rs.Type != "azurerm_image" { @@ -337,14 +338,14 @@ func testCheckAzureRMImageDestroy(s *terraform.State) error { name := rs.Primary.Attributes["name"] resourceGroup := rs.Primary.Attributes["resource_group_name"] - resp, err := conn.Get(resourceGroup, name) + resp, err := client.Get(ctx, resourceGroup, name) if err != nil { return nil } if resp.StatusCode != http.StatusNotFound { - return fmt.Errorf("Managed Image still exists: \n%#v", resp.Properties) + return fmt.Errorf("Managed Image still exists: \n%#v", resp.DiskProperties) } } diff --git a/azurerm/resource_arm_managed_disk.go b/azurerm/resource_arm_managed_disk.go index c0063ad8f416..e66c44316b8b 100644 --- a/azurerm/resource_arm_managed_disk.go +++ b/azurerm/resource_arm_managed_disk.go @@ -5,9 +5,10 @@ import ( "log" "strings" - "github.com/Azure/azure-sdk-for-go/arm/disk" + "github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2017-03-30/compute" "github.com/hashicorp/terraform/helper/schema" "github.com/hashicorp/terraform/helper/validation" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/response" "github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils" ) @@ -36,8 +37,8 @@ func resourceArmManagedDisk() *schema.Resource { Type: schema.TypeString, Required: true, ValidateFunc: validation.StringInSlice([]string{ - string(disk.PremiumLRS), - string(disk.StandardLRS), + string(compute.PremiumLRS), + string(compute.StandardLRS), }, true), DiffSuppressFunc: ignoreCaseDiffSuppressFunc, }, @@ -47,10 +48,10 @@ func resourceArmManagedDisk() *schema.Resource { Required: true, ForceNew: true, ValidateFunc: validation.StringInSlice([]string{ - string(disk.Copy), - string(disk.Empty), - string(disk.FromImage), - string(disk.Import), + string(compute.Copy), + string(compute.Empty), + string(compute.FromImage), + string(compute.Import), }, true), }, @@ -77,8 +78,8 @@ func resourceArmManagedDisk() *schema.Resource { Type: schema.TypeString, Optional: true, ValidateFunc: validation.StringInSlice([]string{ - string(disk.Windows), - string(disk.Linux), + string(compute.Windows), + string(compute.Linux), }, true), }, @@ -105,60 +106,60 @@ func validateDiskSizeGB(v interface{}, k string) (ws []string, errors []error) { } func resourceArmManagedDiskCreate(d *schema.ResourceData, meta interface{}) error { - client := meta.(*ArmClient) - diskClient := client.diskClient + client := meta.(*ArmClient).diskClient + ctx := meta.(*ArmClient).StopContext log.Printf("[INFO] preparing arguments for Azure ARM Managed Disk creation.") name := d.Get("name").(string) location := d.Get("location").(string) resGroup := d.Get("resource_group_name").(string) + storageAccountType := d.Get("storage_account_type").(string) + osType := d.Get("os_type").(string) tags := d.Get("tags").(map[string]interface{}) expandedTags := expandTags(tags) - createDisk := disk.Model{ + createDisk := compute.Disk{ Name: &name, Location: &location, - Tags: expandedTags, - } - - storageAccountType := d.Get("storage_account_type").(string) - osType := d.Get("os_type").(string) - - createDisk.Properties = &disk.Properties{ - AccountType: disk.StorageAccountTypes(storageAccountType), - OsType: disk.OperatingSystemTypes(osType), + DiskProperties: &compute.DiskProperties{ + OsType: compute.OperatingSystemTypes(osType), + }, + Sku: &compute.DiskSku{ + Name: compute.StorageAccountTypes(storageAccountType), + }, + Tags: expandedTags, } if v := d.Get("disk_size_gb"); v != 0 { diskSize := int32(v.(int)) - createDisk.Properties.DiskSizeGB = &diskSize + createDisk.DiskProperties.DiskSizeGB = &diskSize } createOption := d.Get("create_option").(string) - createDisk.CreationData = &disk.CreationData{ - CreateOption: disk.CreateOption(createOption), + createDisk.CreationData = &compute.CreationData{ + CreateOption: compute.DiskCreateOption(createOption), } - if strings.EqualFold(createOption, string(disk.Import)) { + if strings.EqualFold(createOption, string(compute.Import)) { if sourceUri := d.Get("source_uri").(string); sourceUri != "" { createDisk.CreationData.SourceURI = &sourceUri } else { - return fmt.Errorf("[ERROR] source_uri must be specified when create_option is `%s`", disk.Import) + return fmt.Errorf("[ERROR] source_uri must be specified when create_option is `%s`", compute.Import) } - } else if strings.EqualFold(createOption, string(disk.Copy)) { + } else if strings.EqualFold(createOption, string(compute.Copy)) { if sourceResourceId := d.Get("source_resource_id").(string); sourceResourceId != "" { createDisk.CreationData.SourceResourceID = &sourceResourceId } else { - return fmt.Errorf("[ERROR] source_resource_id must be specified when create_option is `%s`", disk.Copy) + return fmt.Errorf("[ERROR] source_resource_id must be specified when create_option is `%s`", compute.Copy) } - } else if strings.EqualFold(createOption, string(disk.FromImage)) { + } else if strings.EqualFold(createOption, string(compute.FromImage)) { if imageReferenceId := d.Get("image_reference_id").(string); imageReferenceId != "" { - createDisk.CreationData.ImageReference = &disk.ImageDiskReference{ + createDisk.CreationData.ImageReference = &compute.ImageDiskReference{ ID: utils.String(imageReferenceId), } } else { - return fmt.Errorf("[ERROR] image_reference_id must be specified when create_option is `%s`", disk.FromImage) + return fmt.Errorf("[ERROR] image_reference_id must be specified when create_option is `%s`", compute.FromImage) } } @@ -168,13 +169,17 @@ func resourceArmManagedDiskCreate(d *schema.ResourceData, meta interface{}) erro createDisk.EncryptionSettings = expandManagedDiskEncryptionSettings(settings) } - _, diskErr := diskClient.CreateOrUpdate(resGroup, name, createDisk, make(chan struct{})) - err := <-diskErr + future, err := client.CreateOrUpdate(ctx, resGroup, name, createDisk) if err != nil { return err } - read, err := diskClient.Get(resGroup, name) + err = future.WaitForCompletion(ctx, client.Client) + if err != nil { + return err + } + + read, err := client.Get(ctx, resGroup, name) if err != nil { return err } @@ -188,7 +193,8 @@ func resourceArmManagedDiskCreate(d *schema.ResourceData, meta interface{}) erro } func resourceArmManagedDiskRead(d *schema.ResourceData, meta interface{}) error { - diskClient := meta.(*ArmClient).diskClient + client := meta.(*ArmClient).diskClient + ctx := meta.(*ArmClient).StopContext id, err := parseAzureResourceID(d.Id()) if err != nil { @@ -197,7 +203,7 @@ func resourceArmManagedDiskRead(d *schema.ResourceData, meta interface{}) error resGroup := id.ResourceGroup name := id.Path["disks"] - resp, err := diskClient.Get(resGroup, name) + resp, err := client.Get(ctx, resGroup, name) if err != nil { if utils.ResponseWasNotFound(resp.Response) { d.SetId("") @@ -208,10 +214,22 @@ func resourceArmManagedDiskRead(d *schema.ResourceData, meta interface{}) error d.Set("name", resp.Name) d.Set("resource_group_name", resGroup) - d.Set("location", azureRMNormalizeLocation(*resp.Location)) - if resp.Properties != nil { - flattenAzureRmManagedDiskProperties(d, resp.Properties) + if location := resp.Location; location != nil { + d.Set("location", azureRMNormalizeLocation(*location)) + } + + if sku := resp.Sku; sku != nil { + d.Set("storage_account_type", string(sku.Name)) + } + + if props := resp.DiskProperties; props != nil { + if diskSize := props.DiskSizeGB; diskSize != nil { + d.Set("disk_size_gb", *diskSize) + } + if osType := props.OsType; osType != "" { + d.Set("os_type", string(osType)) + } } if resp.CreationData != nil { @@ -231,7 +249,8 @@ func resourceArmManagedDiskRead(d *schema.ResourceData, meta interface{}) error } func resourceArmManagedDiskDelete(d *schema.ResourceData, meta interface{}) error { - diskClient := meta.(*ArmClient).diskClient + client := meta.(*ArmClient).diskClient + ctx := meta.(*ArmClient).StopContext id, err := parseAzureResourceID(d.Id()) if err != nil { @@ -240,29 +259,24 @@ func resourceArmManagedDiskDelete(d *schema.ResourceData, meta interface{}) erro resGroup := id.ResourceGroup name := id.Path["disks"] - deleteResp, deleteErr := diskClient.Delete(resGroup, name, make(chan struct{})) - resp := <-deleteResp - err = <-deleteErr + future, err := client.Delete(ctx, resGroup, name) if err != nil { - if !utils.ResponseWasNotFound(resp.Response) { + if !response.WasNotFound(future.Response()) { return err } } - return nil -} - -func flattenAzureRmManagedDiskProperties(d *schema.ResourceData, properties *disk.Properties) { - d.Set("storage_account_type", string(properties.AccountType)) - if properties.DiskSizeGB != nil { - d.Set("disk_size_gb", *properties.DiskSizeGB) - } - if properties.OsType != "" { - d.Set("os_type", string(properties.OsType)) + err = future.WaitForCompletion(ctx, client.Client) + if err != nil { + if !response.WasNotFound(future.Response()) { + return err + } } + + return nil } -func flattenAzureRmManagedDiskCreationData(d *schema.ResourceData, creationData *disk.CreationData) { +func flattenAzureRmManagedDiskCreationData(d *schema.ResourceData, creationData *compute.CreationData) { d.Set("create_option", string(creationData.CreateOption)) if ref := creationData.ImageReference; ref != nil { d.Set("image_reference_id", *ref.ID) diff --git a/azurerm/resource_arm_managed_disk_test.go b/azurerm/resource_arm_managed_disk_test.go index 8e9255ee2cda..40b03696da3c 100644 --- a/azurerm/resource_arm_managed_disk_test.go +++ b/azurerm/resource_arm_managed_disk_test.go @@ -5,7 +5,6 @@ import ( "net/http" "testing" - "github.com/Azure/azure-sdk-for-go/arm/disk" "github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2017-03-30/compute" "github.com/hashicorp/terraform/helper/acctest" "github.com/hashicorp/terraform/helper/resource" @@ -13,7 +12,7 @@ import ( ) func TestAccAzureRMManagedDisk_empty(t *testing.T) { - var d disk.Model + var d compute.Disk ri := acctest.RandInt() config := testAccAzureRMManagedDisk_empty(ri, testLocation()) resource.Test(t, resource.TestCase{ @@ -32,7 +31,7 @@ func TestAccAzureRMManagedDisk_empty(t *testing.T) { } func TestAccAzureRMManagedDisk_import(t *testing.T) { - var d disk.Model + var d compute.Disk var vm compute.VirtualMachine ri := acctest.RandInt() location := testLocation() @@ -64,7 +63,7 @@ func TestAccAzureRMManagedDisk_import(t *testing.T) { } func TestAccAzureRMManagedDisk_copy(t *testing.T) { - var d disk.Model + var d compute.Disk ri := acctest.RandInt() config := testAccAzureRMManagedDisk_copy(ri, testLocation()) resource.Test(t, resource.TestCase{ @@ -83,7 +82,7 @@ func TestAccAzureRMManagedDisk_copy(t *testing.T) { } func TestAccAzureRMManagedDisk_fromPlatformImage(t *testing.T) { - var d disk.Model + var d compute.Disk ri := acctest.RandInt() config := testAccAzureRMManagedDisk_platformImage(ri, testLocation()) resource.Test(t, resource.TestCase{ @@ -102,7 +101,7 @@ func TestAccAzureRMManagedDisk_fromPlatformImage(t *testing.T) { } func TestAccAzureRMManagedDisk_update(t *testing.T) { - var d disk.Model + var d compute.Disk resourceName := "azurerm_managed_disk.test" ri := acctest.RandInt() @@ -121,7 +120,7 @@ func TestAccAzureRMManagedDisk_update(t *testing.T) { resource.TestCheckResourceAttr(resourceName, "tags.environment", "acctest"), resource.TestCheckResourceAttr(resourceName, "tags.cost-center", "ops"), resource.TestCheckResourceAttr(resourceName, "disk_size_gb", "1"), - resource.TestCheckResourceAttr(resourceName, "storage_account_type", string(disk.StandardLRS)), + resource.TestCheckResourceAttr(resourceName, "storage_account_type", string(compute.StandardLRS)), ), }, { @@ -131,7 +130,7 @@ func TestAccAzureRMManagedDisk_update(t *testing.T) { resource.TestCheckResourceAttr(resourceName, "tags.%", "1"), resource.TestCheckResourceAttr(resourceName, "tags.environment", "acctest"), resource.TestCheckResourceAttr(resourceName, "disk_size_gb", "2"), - resource.TestCheckResourceAttr(resourceName, "storage_account_type", string(disk.PremiumLRS)), + resource.TestCheckResourceAttr(resourceName, "storage_account_type", string(compute.PremiumLRS)), ), }, }, @@ -139,7 +138,7 @@ func TestAccAzureRMManagedDisk_update(t *testing.T) { } func TestAccAzureRMManagedDisk_encryption(t *testing.T) { - var d disk.Model + var d compute.Disk resourceName := "azurerm_managed_disk.test" ri := acctest.RandInt() @@ -169,7 +168,7 @@ func TestAccAzureRMManagedDisk_encryption(t *testing.T) { } func TestAccAzureRMManagedDisk_NonStandardCasing(t *testing.T) { - var d disk.Model + var d compute.Disk ri := acctest.RandInt() config := testAccAzureRMManagedDiskNonStandardCasing(ri, testLocation()) resource.Test(t, resource.TestCase{ @@ -192,7 +191,7 @@ func TestAccAzureRMManagedDisk_NonStandardCasing(t *testing.T) { }) } -func testCheckAzureRMManagedDiskExists(name string, d *disk.Model, shouldExist bool) resource.TestCheckFunc { +func testCheckAzureRMManagedDiskExists(name string, d *compute.Disk, shouldExist bool) resource.TestCheckFunc { return func(s *terraform.State) error { rs, ok := s.RootModule().Resources[name] if !ok { @@ -205,9 +204,10 @@ func testCheckAzureRMManagedDiskExists(name string, d *disk.Model, shouldExist b return fmt.Errorf("Bad: no resource group found in state for disk: %s", dName) } - conn := testAccProvider.Meta().(*ArmClient).diskClient + client := testAccProvider.Meta().(*ArmClient).diskClient + ctx := testAccProvider.Meta().(*ArmClient).StopContext - resp, err := conn.Get(resourceGroup, dName) + resp, err := client.Get(ctx, resourceGroup, dName) if err != nil { return fmt.Errorf("Bad: Get on diskClient: %+v", err) } @@ -226,7 +226,8 @@ func testCheckAzureRMManagedDiskExists(name string, d *disk.Model, shouldExist b } func testCheckAzureRMManagedDiskDestroy(s *terraform.State) error { - conn := testAccProvider.Meta().(*ArmClient).diskClient + client := testAccProvider.Meta().(*ArmClient).diskClient + ctx := testAccProvider.Meta().(*ArmClient).StopContext for _, rs := range s.RootModule().Resources { if rs.Type != "azurerm_managed_disk" { @@ -236,14 +237,14 @@ func testCheckAzureRMManagedDiskDestroy(s *terraform.State) error { name := rs.Primary.Attributes["name"] resourceGroup := rs.Primary.Attributes["resource_group_name"] - resp, err := conn.Get(resourceGroup, name) + resp, err := client.Get(ctx, resourceGroup, name) if err != nil { return nil } if resp.StatusCode != http.StatusNotFound { - return fmt.Errorf("Managed Disk still exists: \n%#v", resp.Properties) + return fmt.Errorf("Managed Disk still exists: \n%#v", resp.DiskProperties) } } diff --git a/azurerm/resource_arm_snapshot.go b/azurerm/resource_arm_snapshot.go index 7bad5d517221..0bcca2c96837 100644 --- a/azurerm/resource_arm_snapshot.go +++ b/azurerm/resource_arm_snapshot.go @@ -5,7 +5,7 @@ import ( "log" "regexp" - "github.com/Azure/azure-sdk-for-go/arm/disk" + "github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2017-03-30/compute" "github.com/hashicorp/terraform/helper/schema" "github.com/hashicorp/terraform/helper/validation" "github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils" @@ -37,8 +37,8 @@ func resourceArmSnapshot() *schema.Resource { Type: schema.TypeString, Required: true, ValidateFunc: validation.StringInSlice([]string{ - string(disk.Copy), - string(disk.Import), + string(compute.Copy), + string(compute.Import), }, true), DiffSuppressFunc: ignoreCaseDiffSuppressFunc, }, @@ -76,6 +76,7 @@ func resourceArmSnapshot() *schema.Resource { func resourceArmSnapshotCreateUpdate(d *schema.ResourceData, meta interface{}) error { client := meta.(*ArmClient).snapshotsClient + ctx := meta.(*ArmClient).StopContext name := d.Get("name").(string) resourceGroup := d.Get("resource_group_name").(string) @@ -83,31 +84,31 @@ func resourceArmSnapshotCreateUpdate(d *schema.ResourceData, meta interface{}) e createOption := d.Get("create_option").(string) tags := d.Get("tags").(map[string]interface{}) - properties := disk.Snapshot{ + properties := compute.Snapshot{ Location: utils.String(location), - Properties: &disk.Properties{ - CreationData: &disk.CreationData{ - CreateOption: disk.CreateOption(createOption), + DiskProperties: &compute.DiskProperties{ + CreationData: &compute.CreationData{ + CreateOption: compute.DiskCreateOption(createOption), }, }, Tags: expandTags(tags), } if v, ok := d.GetOk("source_uri"); ok { - properties.Properties.CreationData.SourceURI = utils.String(v.(string)) + properties.DiskProperties.CreationData.SourceURI = utils.String(v.(string)) } if v, ok := d.GetOk("source_resource_id"); ok { - properties.Properties.CreationData.SourceResourceID = utils.String(v.(string)) + properties.DiskProperties.CreationData.SourceResourceID = utils.String(v.(string)) } if v, ok := d.GetOk("storage_account_id"); ok { - properties.Properties.CreationData.StorageAccountID = utils.String(v.(string)) + properties.DiskProperties.CreationData.StorageAccountID = utils.String(v.(string)) } diskSizeGB := d.Get("disk_size_gb").(int) if diskSizeGB > 0 { - properties.Properties.DiskSizeGB = utils.Int32(int32(diskSizeGB)) + properties.DiskProperties.DiskSizeGB = utils.Int32(int32(diskSizeGB)) } if v, ok := d.GetOk("encryption_settings"); ok { @@ -116,13 +117,17 @@ func resourceArmSnapshotCreateUpdate(d *schema.ResourceData, meta interface{}) e properties.EncryptionSettings = expandManagedDiskEncryptionSettings(settings) } - _, createErr := client.CreateOrUpdate(resourceGroup, name, properties, make(chan struct{})) - err := <-createErr + future, err := client.CreateOrUpdate(ctx, resourceGroup, name, properties) if err != nil { return err } - resp, err := client.Get(resourceGroup, name) + err = future.WaitForCompletion(ctx, client.Client) + if err != nil { + return err + } + + resp, err := client.Get(ctx, resourceGroup, name) if err != nil { return err } @@ -134,6 +139,7 @@ func resourceArmSnapshotCreateUpdate(d *schema.ResourceData, meta interface{}) e func resourceArmSnapshotRead(d *schema.ResourceData, meta interface{}) error { client := meta.(*ArmClient).snapshotsClient + ctx := meta.(*ArmClient).StopContext id, err := parseAzureResourceID(d.Id()) if err != nil { @@ -143,7 +149,7 @@ func resourceArmSnapshotRead(d *schema.ResourceData, meta interface{}) error { resourceGroup := id.ResourceGroup name := id.Path["snapshots"] - resp, err := client.Get(resourceGroup, name) + resp, err := client.Get(ctx, resourceGroup, name) if err != nil { if utils.ResponseWasNotFound(resp.Response) { log.Printf("[INFO] Error reading Snapshot %q - removing from state", d.Id()) @@ -155,10 +161,13 @@ func resourceArmSnapshotRead(d *schema.ResourceData, meta interface{}) error { } d.Set("name", resp.Name) - d.Set("location", azureRMNormalizeLocation(*resp.Location)) d.Set("resource_group_name", resourceGroup) - if props := resp.Properties; props != nil { + if location := resp.Location; location != nil { + d.Set("location", azureRMNormalizeLocation(*location)) + } + + if props := resp.DiskProperties; props != nil { if data := props.CreationData; data != nil { d.Set("create_option", string(data.CreateOption)) @@ -192,6 +201,7 @@ func resourceArmSnapshotRead(d *schema.ResourceData, meta interface{}) error { func resourceArmSnapshotDelete(d *schema.ResourceData, meta interface{}) error { client := meta.(*ArmClient).snapshotsClient + ctx := meta.(*ArmClient).StopContext id, err := parseAzureResourceID(d.Id()) if err != nil { @@ -201,17 +211,12 @@ func resourceArmSnapshotDelete(d *schema.ResourceData, meta interface{}) error { resourceGroup := id.ResourceGroup name := id.Path["snapshots"] - deleteResp, deleteErr := client.Delete(resourceGroup, name, make(chan struct{})) - resp := <-deleteResp - err = <-deleteErr + future, err := client.Delete(ctx, resourceGroup, name) if err != nil { - if utils.ResponseWasNotFound(resp.Response) { - return nil - } - - return fmt.Errorf("Error making Read request on Snapshot %q: %+v", name, err) + return fmt.Errorf("Error deleting Snapshot: %+v", err) } + err = future.WaitForCompletion(ctx, client.Client) if err != nil { return fmt.Errorf("Error deleting Snapshot: %+v", err) } diff --git a/azurerm/resource_arm_snapshot_test.go b/azurerm/resource_arm_snapshot_test.go index 40edc3c1f84a..47bc96711cde 100644 --- a/azurerm/resource_arm_snapshot_test.go +++ b/azurerm/resource_arm_snapshot_test.go @@ -194,7 +194,8 @@ func testCheckAzureRMSnapshotDestroy(s *terraform.State) error { resourceGroup := rs.Primary.Attributes["resource_group_name"] client := testAccProvider.Meta().(*ArmClient).snapshotsClient - resp, err := client.Get(resourceGroup, name) + ctx := testAccProvider.Meta().(*ArmClient).StopContext + resp, err := client.Get(ctx, resourceGroup, name) if err != nil { if utils.ResponseWasNotFound(resp.Response) { return nil @@ -223,8 +224,9 @@ func testCheckAzureRMSnapshotExists(name string) resource.TestCheckFunc { } client := testAccProvider.Meta().(*ArmClient).snapshotsClient + ctx := testAccProvider.Meta().(*ArmClient).StopContext - resp, err := client.Get(resourceGroup, name) + resp, err := client.Get(ctx, resourceGroup, name) if err != nil { if utils.ResponseWasNotFound(resp.Response) { return fmt.Errorf("Bad: Topic %q (resource group: %q) does not exist", name, resourceGroup) diff --git a/azurerm/resource_arm_virtual_machine.go b/azurerm/resource_arm_virtual_machine.go index 7ed4fa3d4ad0..1b6fad38ced0 100644 --- a/azurerm/resource_arm_virtual_machine.go +++ b/azurerm/resource_arm_virtual_machine.go @@ -834,7 +834,8 @@ func resourceArmVirtualMachineDeleteVhd(uri string, meta interface{}) error { } func resourceArmVirtualMachineDeleteManagedDisk(managedDiskID string, meta interface{}) error { - diskClient := meta.(*ArmClient).diskClient + client := meta.(*ArmClient).diskClient + ctx := meta.(*ArmClient).StopContext id, err := parseAzureResourceID(managedDiskID) if err != nil { @@ -843,8 +844,12 @@ func resourceArmVirtualMachineDeleteManagedDisk(managedDiskID string, meta inter resGroup := id.ResourceGroup name := id.Path["disks"] - _, error := diskClient.Delete(resGroup, name, make(chan struct{})) - err = <-error + future, err := client.Delete(ctx, resGroup, name) + if err != nil { + return fmt.Errorf("Error deleting Managed Disk (%s %s) %+v", name, resGroup, err) + } + + err = future.WaitForCompletion(ctx, client.Client) if err != nil { return fmt.Errorf("Error deleting Managed Disk (%s %s) %+v", name, resGroup, err) } diff --git a/azurerm/resource_arm_virtual_machine_managed_disks_test.go b/azurerm/resource_arm_virtual_machine_managed_disks_test.go index cf259c971e55..2761685f9f4f 100644 --- a/azurerm/resource_arm_virtual_machine_managed_disks_test.go +++ b/azurerm/resource_arm_virtual_machine_managed_disks_test.go @@ -8,7 +8,6 @@ import ( "strings" "testing" - "github.com/Azure/azure-sdk-for-go/arm/disk" "github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2017-03-30/compute" "github.com/hashicorp/terraform/helper/acctest" "github.com/hashicorp/terraform/helper/resource" @@ -1373,15 +1372,16 @@ func findAzureRMVirtualMachineManagedDiskID(md *compute.ManagedDiskParameters) ( return *md.ID, nil } -func testGetAzureRMVirtualMachineManagedDisk(managedDiskID *string) (*disk.Model, error) { +func testGetAzureRMVirtualMachineManagedDisk(managedDiskID *string) (*compute.Disk, error) { armID, err := parseAzureResourceID(*managedDiskID) if err != nil { return nil, fmt.Errorf("Unable to parse Managed Disk ID %s, %+v", *managedDiskID, err) } name := armID.Path["disks"] resourceGroup := armID.ResourceGroup - conn := testAccProvider.Meta().(*ArmClient).diskClient - d, err := conn.Get(resourceGroup, name) + client := testAccProvider.Meta().(*ArmClient).diskClient + ctx := testAccProvider.Meta().(*ArmClient).StopContext + d, err := client.Get(ctx, resourceGroup, name) //check status first since sdk client returns error if not 200 if d.Response.StatusCode == http.StatusNotFound { return &d, nil