From 9b97e326f4e9daa289d952c0ccdef03393581da3 Mon Sep 17 00:00:00 2001 From: aznashwan Date: Tue, 2 Jun 2015 11:38:09 +0300 Subject: [PATCH] Saving progress before adaptation. --- .../azure/resource_azure_dns_server.go | 39 +++------ .../azure/resource_azure_hosted_service.go | 29 +++---- .../azure/resource_azure_instance.go | 9 ++- .../resource_azure_security_group_rule.go | 74 +++++++---------- .../azure/resource_azure_storage_service.go | 42 +++++----- .../azure/resource_azure_virtual_network.go | 81 ++++++++++--------- builtin/providers/azure/utils.go | 10 +++ builtin/providers/azure/validation.go | 1 - 8 files changed, 129 insertions(+), 156 deletions(-) delete mode 100644 builtin/providers/azure/validation.go diff --git a/builtin/providers/azure/resource_azure_dns_server.go b/builtin/providers/azure/resource_azure_dns_server.go index f66903e07315..453a51667f37 100644 --- a/builtin/providers/azure/resource_azure_dns_server.go +++ b/builtin/providers/azure/resource_azure_dns_server.go @@ -38,10 +38,7 @@ func resourceAzureDnsServer() *schema.Resource { // resourceAzureDnsServerCreate does all the necessary API calls // to create a new DNS server definition on Azure. func resourceAzureDnsServerCreate(d *schema.ResourceData, meta interface{}) error { - azureClient, ok := meta.(*AzureClient) - if !ok { - return fmt.Errorf("Failed to convert to *AzureClient, got: %T", meta) - } + azureClient := meta.(*AzureClient) managementClient := azureClient.managementClient networkClient := virtualnetwork.NewClient(managementClient) @@ -81,10 +78,7 @@ func resourceAzureDnsServerCreate(d *schema.ResourceData, meta interface{}) erro // resourceAzureDnsServerRead does all the necessary API calls to read // the state of the DNS server off Azure. func resourceAzureDnsServerRead(d *schema.ResourceData, meta interface{}) error { - azureClient, ok := meta.(*AzureClient) - if !ok { - return fmt.Errorf("Failed to convert to *AzureClient, got: %T", meta) - } + azureClient := meta.(*AzureClient) managementClient := azureClient.managementClient networkClient := virtualnetwork.NewClient(managementClient) @@ -102,6 +96,7 @@ func resourceAzureDnsServerRead(d *schema.ResourceData, meta interface{}) error if dns.Name == name { found = true d.Set("dns_address", dns.IPAddress) + break } } @@ -116,39 +111,35 @@ func resourceAzureDnsServerRead(d *schema.ResourceData, meta interface{}) error // resourceAzureDnsServerUpdate does all the necessary API calls // to update the DNS definition on Azure. func resourceAzureDnsServerUpdate(d *schema.ResourceData, meta interface{}) error { - azureClient, ok := meta.(*AzureClient) - if !ok { - return fmt.Errorf("Failed to convert to *AzureClient, got: %T", meta) - } + azureClient := meta.(*AzureClient) managementClient := azureClient.managementClient networkClient := virtualnetwork.NewClient(managementClient) - var err error var found bool name := d.Get("name").(string) caddress := d.HasChange("dns_address") - var netConf virtualnetwork.NetworkConfiguration if caddress { log.Println("[DEBUG] DNS server address has changes; updating it on Azure.") log.Println("[INFO] Fetching current network configuration from Azure.") azureClient.mutex.Lock() defer azureClient.mutex.Unlock() - netConf, err = networkClient.GetVirtualNetworkConfiguration() + netConf, err := networkClient.GetVirtualNetworkConfiguration() if err != nil { return fmt.Errorf("Failed to get the current network configuration from Azure: %s", err) } // search for our DNS and update its address value: for i, dns := range netConf.Configuration.DNS.DNSServers { - found = true if dns.Name == name { + found = true netConf.Configuration.DNS.DNSServers[i].IPAddress = d.Get("dns_address").(string) + break } } // if the config has changes, send the configuration back to Azure: - if found && caddress { + if found { log.Println("[INFO] Sending updated network configuration back to Azure.") reqID, err := networkClient.SetVirtualNetworkConfiguration(netConf) if err != nil { @@ -172,10 +163,7 @@ func resourceAzureDnsServerUpdate(d *schema.ResourceData, meta interface{}) erro // resourceAzureDnsServerExists does all the necessary API calls to // check if the DNS server definition alredy exists on Azure. func resourceAzureDnsServerExists(d *schema.ResourceData, meta interface{}) (bool, error) { - azureClient, ok := meta.(*AzureClient) - if !ok { - return false, fmt.Errorf("Failed to convert to *AzureClient, got: %T", meta) - } + azureClient := meta.(*AzureClient) managementClient := azureClient.managementClient networkClient := virtualnetwork.NewClient(managementClient) @@ -194,16 +182,15 @@ func resourceAzureDnsServerExists(d *schema.ResourceData, meta interface{}) (boo } } + // if we reached this point; the resource must have been deleted; and we must untrack it: + d.SetId("") return false, nil } // resourceAzureDnsServerDelete does all the necessary API calls // to delete the DNS server definition from Azure. func resourceAzureDnsServerDelete(d *schema.ResourceData, meta interface{}) error { - azureClient, ok := meta.(*AzureClient) - if !ok { - return fmt.Errorf("Failed to convert to *AzureClient, got: %T", meta) - } + azureClient := meta.(*AzureClient) managementClient := azureClient.managementClient networkClient := virtualnetwork.NewClient(managementClient) @@ -224,6 +211,7 @@ func resourceAzureDnsServerDelete(d *schema.ResourceData, meta interface{}) erro netConf.Configuration.DNS.DNSServers[:i], netConf.Configuration.DNS.DNSServers[i+1:]..., ) + break } } @@ -238,6 +226,5 @@ func resourceAzureDnsServerDelete(d *schema.ResourceData, meta interface{}) erro return fmt.Errorf("Error setting network configuration: %s", err) } - d.SetId("") return nil } diff --git a/builtin/providers/azure/resource_azure_hosted_service.go b/builtin/providers/azure/resource_azure_hosted_service.go index 1eb54e3d3937..c19b8bd58efc 100644 --- a/builtin/providers/azure/resource_azure_hosted_service.go +++ b/builtin/providers/azure/resource_azure_hosted_service.go @@ -74,10 +74,7 @@ func resourceAzureHostedService() *schema.Resource { // resourceAzureHostedServiceCreate does all the necessary API calls // to create a hosted service on Azure. func resourceAzureHostedServiceCreate(d *schema.ResourceData, meta interface{}) error { - azureClient, ok := meta.(*AzureClient) - if !ok { - return fmt.Errorf("Failed to convert to *AzureClient, got: %T", meta) - } + azureClient := meta.(*AzureClient) managementClient := azureClient.managementClient hostedServiceClient := hostedservice.NewClient(managementClient) @@ -109,10 +106,7 @@ func resourceAzureHostedServiceCreate(d *schema.ResourceData, meta interface{}) // resourceAzureHostedServiceExists checks whether a hosted service with the // given service_name already exists on Azure. func resourceAzureHostedServiceExists(d *schema.ResourceData, meta interface{}) (bool, error) { - azureClient, ok := meta.(*AzureClient) - if !ok { - return false, fmt.Errorf("Failed to convert to *AzureClient, got: %T", meta) - } + azureClient := meta.(*AzureClient) hostedServiceClient := hostedservice.NewClient(azureClient.managementClient) log.Println("[INFO] Querying for hosted service existence.") @@ -134,17 +128,21 @@ func resourceAzureHostedServiceExists(d *schema.ResourceData, meta interface{}) // resourceAzureHostedServiceRead does all the necessary API calls // to read the state of a hosted service from Azure. func resourceAzureHostedServiceRead(d *schema.ResourceData, meta interface{}) error { - azureClient, ok := meta.(*AzureClient) - if !ok { - return fmt.Errorf("Failed to convert to *AzureClient, got: %T", meta) - } + azureClient := meta.(*AzureClient) hostedServiceClient := hostedservice.NewClient(azureClient.managementClient) log.Println("[INFO] Querying for hosted service info.") serviceName := d.Get("service_name").(string) hostedService, err := hostedServiceClient.GetHostedService(serviceName) if err != nil { - return fmt.Errorf("Failed to get hosted service: %s", err) + if management.IsResourceNotFoundError(err) { + // it means the hosted service was deleted in the meantime, + // so we must remove it here: + d.SetId("") + return nil + } else { + return fmt.Errorf("Failed to get hosted service: %s", err) + } } log.Println("[DEBUG] Reading hosted service query result data.") @@ -169,10 +167,7 @@ func resourceAzureHostedServiceUpdate(d *schema.ResourceData, meta interface{}) // resourceAzureHostedServiceDelete does all the necessary API calls to // delete a hosted service from Azure. func resourceAzureHostedServiceDelete(d *schema.ResourceData, meta interface{}) error { - azureClient, ok := meta.(*AzureClient) - if !ok { - return fmt.Errorf("Failed to convert to *AzureClient, got: %T", meta) - } + azureClient := meta.(*AzureClient) managementClient := azureClient.managementClient hostedServiceClient := hostedservice.NewClient(managementClient) diff --git a/builtin/providers/azure/resource_azure_instance.go b/builtin/providers/azure/resource_azure_instance.go index 1969946293f6..319f1c5a8dc1 100644 --- a/builtin/providers/azure/resource_azure_instance.go +++ b/builtin/providers/azure/resource_azure_instance.go @@ -47,6 +47,10 @@ func resourceAzureInstance() *schema.Resource { ForceNew: true, Description: parameterDescriptions["image"], }, + "deployment_slot": &schema.Schema{ + Type: schema.TypeString, + Optional: true, + }, // TODO(aznashwan): make this used: // - seperate config for Linux and Windows // - Windows to join domain. @@ -203,10 +207,13 @@ func resourceAzureInstanceCreate(d *schema.ResourceData, meta interface{}) error vmutils.ConfigureWithPublicSSH(&role) // deploy the VM: + slot := d.Get("deployment_slot").(string) reqID, err := virtualmachine.NewClient(managementClient).CreateDeployment( role, serviceName, - virtualmachine.CreateDeploymentOptions{}, + virtualmachine.CreateDeploymentOptions{ + DeploymentSlot: slot, + }, ) if err != nil { return fmt.Errorf("Failed to initiate deployment creation: %s", err) diff --git a/builtin/providers/azure/resource_azure_security_group_rule.go b/builtin/providers/azure/resource_azure_security_group_rule.go index 65c3351fb4b0..749f96b1a118 100644 --- a/builtin/providers/azure/resource_azure_security_group_rule.go +++ b/builtin/providers/azure/resource_azure_security_group_rule.go @@ -81,15 +81,13 @@ func resourceAzureSecurityGroupRule() *schema.Resource { // resourceAzureSecurityGroupRuleCreate does all the necessary API calls to // create a new network security group rule on Azure. func resourceAzureSecurityGroupRuleCreate(d *schema.ResourceData, meta interface{}) error { - azureClient, ok := meta.(*AzureClient) - if !ok { - return fmt.Errorf("Failed to convert to *AzureClient, got: %T", meta) - } + azureClient := meta.(*AzureClient) managementClient := azureClient.managementClient netSecClient := netsecgroup.NewClient(managementClient) // create and configure the RuleResponse: rule := netsecgroup.RuleRequest{ + // TODO(aznashwan): security checks here: Name: d.Get("name").(string), Type: netsecgroup.RuleType(d.Get("type").(string)), Priority: d.Get("priority").(int), @@ -122,10 +120,7 @@ func resourceAzureSecurityGroupRuleCreate(d *schema.ResourceData, meta interface // resourceAzureSecurityGroupRuleRead does all the necessary API calls to // read the state of a network security group ruke off Azure. func resourceAzureSecurityGroupRuleRead(d *schema.ResourceData, meta interface{}) error { - azureClient, ok := meta.(*AzureClient) - if !ok { - return fmt.Errorf("Failed to convert to *AzureClient, got: %T", meta) - } + azureClient := meta.(*AzureClient) managementClient := azureClient.managementClient netSecClient := netsecgroup.NewClient(managementClient) @@ -161,6 +156,8 @@ func resourceAzureSecurityGroupRuleRead(d *schema.ResourceData, meta interface{} d.Set("destination_address_prefix", rule.DestinationAddressPrefix) d.Set("destination_port_range", rule.DestinationPortRange) d.Set("protocol", rule.Protocol) + + break } } @@ -174,10 +171,7 @@ func resourceAzureSecurityGroupRuleRead(d *schema.ResourceData, meta interface{} // resourceAzureSecurityGroupRuleUpdate does all the necessary API calls to // update the state of a network security group ruke off Azure. func resourceAzureSecurityGroupRuleUpdate(d *schema.ResourceData, meta interface{}) error { - azureClient, ok := meta.(*AzureClient) - if !ok { - return fmt.Errorf("Failed to convert to *AzureClient, got: %T", meta) - } + azureClient := meta.(*AzureClient) managementClient := azureClient.managementClient netSecClient := netsecgroup.NewClient(managementClient) @@ -214,6 +208,7 @@ func resourceAzureSecurityGroupRuleUpdate(d *schema.ResourceData, meta interface // else, start building up the rule request struct: newRule := netsecgroup.RuleRequest{ + // TODO(azhnashwan): Parameter check here: Name: d.Get("name").(string), Type: netsecgroup.RuleType(d.Get("type").(string)), Priority: d.Get("priority").(int), @@ -245,10 +240,7 @@ func resourceAzureSecurityGroupRuleUpdate(d *schema.ResourceData, meta interface // resourceAzureSecurityGroupRuleExists does all the necessary API calls to // check for the existence of the network security group rule on Azure. func resourceAzureSecurityGroupRuleExists(d *schema.ResourceData, meta interface{}) (bool, error) { - azureClient, ok := meta.(*AzureClient) - if !ok { - return false, fmt.Errorf("Failed to convert to *AzureClient, got: %T", meta) - } + azureClient := meta.(*AzureClient) managementClient := azureClient.managementClient netSecClient := netsecgroup.NewClient(managementClient) @@ -269,29 +261,24 @@ func resourceAzureSecurityGroupRuleExists(d *schema.ResourceData, meta interface } // try and find our security group rule: - var found bool name := d.Get("name").(string) for _, rule := range secgroup.Rules { if rule.Name == name { - found = true + return true, nil } } - // check is the resource has not been deleted in the meantime: - if !found { - // if not; remove the resource: - d.SetId("") - } - return found, nil + // if here; it means the resource has been deleted in the + // meantime and must be removed from the schema: + d.SetId("") + + return false, nil } // resourceAzureSecurityGroupRuleDelete does all the necessary API calls to // delete a network security group rule off Azure. func resourceAzureSecurityGroupRuleDelete(d *schema.ResourceData, meta interface{}) error { - azureClient, ok := meta.(*AzureClient) - if !ok { - return fmt.Errorf("Failed to convert to *AzureClient, got: %T", meta) - } + azureClient := meta.(*AzureClient) managementClient := azureClient.managementClient netSecClient := netsecgroup.NewClient(managementClient) @@ -301,38 +288,31 @@ func resourceAzureSecurityGroupRuleDelete(d *schema.ResourceData, meta interface log.Println("[INFO] Sending network security group rule query for deletion to Azure.") secgroup, err := netSecClient.GetNetworkSecurityGroup(secGroupName) if err != nil { - if !management.IsResourceNotFoundError(err) { - return fmt.Errorf("Error issuing network security group rules query: %s", err) - } else { + if management.IsResourceNotFoundError(err) { // it meants that the network security group this rule belonged to has // been deleted; so we need do nothing more but stop tracking the resource: d.SetId("") return nil + } else { + return fmt.Errorf("Error issuing network security group rules query: %s", err) } } // check is the resource has not been deleted in the meantime: - var found bool name := d.Get("name").(string) for _, rule := range secgroup.Rules { if rule.Name == name { - found = true + // if not; we shall issue the delete: + reqID, err := netSecClient.DeleteNetworkSecurityGroupRule(secGroupName, name) + if err != nil { + return fmt.Errorf("Error sending network security group rule delete request to Azure: %s", err) + } + err = managementClient.WaitForOperation(reqID, nil) + if err != nil { + return fmt.Errorf("Error deleting network security group rule off Azure: %s", err) + } } } - if !found { - // if not; remove the resource: - d.SetId("") - } - - // if not; we shall issue the delete: - reqID, err := netSecClient.DeleteNetworkSecurityGroupRule(secGroupName, name) - if err != nil { - return fmt.Errorf("Error sending network security group rule delete request to Azure: %s", err) - } - err = managementClient.WaitForOperation(reqID, nil) - if err != nil { - return fmt.Errorf("Error deleting network security group rule off Azure: %s", err) - } return nil } diff --git a/builtin/providers/azure/resource_azure_storage_service.go b/builtin/providers/azure/resource_azure_storage_service.go index b96e67b7ac2f..2b4ac0d63056 100644 --- a/builtin/providers/azure/resource_azure_storage_service.go +++ b/builtin/providers/azure/resource_azure_storage_service.go @@ -22,6 +22,7 @@ func resourceAzureStorageService() *schema.Resource { SchemaVersion: 1, Schema: map[string]*schema.Schema{ + // General attributes: "name": &schema.Schema{ Type: schema.TypeString, Required: true, @@ -34,22 +35,20 @@ func resourceAzureStorageService() *schema.Resource { ForceNew: true, Description: parameterDescriptions["location"], }, - "account_type": &schema.Schema{ - Type: schema.TypeString, - Required: true, - // ForceNew: true, - Description: parameterDescriptions["account_type"], - }, - "url": &schema.Schema{ - Type: schema.TypeString, - Computed: true, - }, "description": &schema.Schema{ Type: schema.TypeString, Optional: true, Default: "", Description: parameterDescriptions["description"], }, + // Functional attriutes: + "account_type": &schema.Schema{ + Type: schema.TypeString, + Required: true, + // TODO(aznashwan): ??? + // ForceNew: true, + Description: parameterDescriptions["account_type"], + }, "affinity_group": &schema.Schema{ Type: schema.TypeString, Optional: true, @@ -61,6 +60,11 @@ func resourceAzureStorageService() *schema.Resource { Optional: true, Elem: schema.TypeString, }, + // Computed attributes: + "url": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + }, "primary_key": &schema.Schema{ Type: schema.TypeString, Computed: true, @@ -76,10 +80,7 @@ func resourceAzureStorageService() *schema.Resource { // resourceAzureStorageServiceCreate does all the necessary API calls to // create a new Azure storage service. func resourceAzureStorageServiceCreate(d *schema.ResourceData, meta interface{}) error { - azureClient, ok := meta.(*AzureClient) - if !ok { - return fmt.Errorf("Failed to convert to *AzureClient, got: %T", meta) - } + azureClient := meta.(*AzureClient) managementClient := azureClient.managementClient storageServiceClient := storageservice.NewClient(managementClient) @@ -117,17 +118,13 @@ func resourceAzureStorageServiceCreate(d *schema.ResourceData, meta interface{}) }, }) if err != nil { - return fmt.Errorf("Failed to create Azure storage service: %s", err) + return fmt.Errorf("Failed to create Azure storage service %s: %s", name, err) } err = managementClient.WaitForOperation(reqID, nil) if err != nil { - return fmt.Errorf("Failed creating storage service: %s", err) + return fmt.Errorf("Failed creating storage service %s: %s", name, err) } - // TODO(aznashwan): find work around here: - // get computed values: - // d.Set("url", svc.Url) - d.SetId(label) return resourceAzureStorageServiceRead(d, meta) } @@ -135,10 +132,7 @@ func resourceAzureStorageServiceCreate(d *schema.ResourceData, meta interface{}) // resourceAzureStorageServiceRead does all the necessary API calls to // read the state of the storage service off Azure. func resourceAzureStorageServiceRead(d *schema.ResourceData, meta interface{}) error { - azureClient, ok := meta.(*AzureClient) - if !ok { - return fmt.Errorf("Failed to convert to *AzureClient, got: %T", meta) - } + azureClient := meta.(*AzureClient) managementClient := azureClient.managementClient storageServiceClient := storageservice.NewClient(managementClient) diff --git a/builtin/providers/azure/resource_azure_virtual_network.go b/builtin/providers/azure/resource_azure_virtual_network.go index bd28d751262a..5bb1e0a4b3b9 100644 --- a/builtin/providers/azure/resource_azure_virtual_network.go +++ b/builtin/providers/azure/resource_azure_virtual_network.go @@ -78,10 +78,7 @@ func resourceAzureVirtualNetwork() *schema.Resource { // resourceAzureVirtualNetworkCreate does all the necessary API calls to create // an Azure virtual network. func resourceAzureVirtualNetworkCreate(d *schema.ResourceData, meta interface{}) error { - azureClient, ok := meta.(*AzureClient) - if !ok { - return fmt.Errorf("Failed to convert to *AzureClient, got: %T", meta) - } + azureClient := meta.(*AzureClient) managementClient := azureClient.managementClient networkClient := virtualnetwork.NewClient(managementClient) netSecClient := netsecgroup.NewClient(managementClient) @@ -94,8 +91,15 @@ func resourceAzureVirtualNetworkCreate(d *schema.ResourceData, meta interface{}) return fmt.Errorf("Error while retrieving current network configuration: %s", err) } - // create new virtual network configuration and add it to the config. + // check to ensure the virtual network does not exists already: name := d.Get("name").(string) + for _, vn := range netConf.Configuration.VirtualNetworkSites { + if vn.Name == name { + return fmt.Errorf("A virtual network with the given name already exists!") + } + } + + // create new virtual network configuration and add it to the config. location := d.Get("location").(string) // fetch address spaces: @@ -179,10 +183,7 @@ func resourceAzureVirtualNetworkCreate(d *schema.ResourceData, meta interface{}) // resourceAzureVirtualNetworkRead does all the necessary API calls to read // the state of a virtual network from Azure. func resourceAzureVirtualNetworkRead(d *schema.ResourceData, meta interface{}) error { - azureClient, ok := meta.(*AzureClient) - if !ok { - return fmt.Errorf("Failed to convert to *AzureClient, got: %T", meta) - } + azureClient := meta.(*AzureClient) managementClient := azureClient.managementClient networkClient := virtualnetwork.NewClient(managementClient) secGroupClient := netsecgroup.NewClient(managementClient) @@ -196,8 +197,11 @@ func resourceAzureVirtualNetworkRead(d *schema.ResourceData, meta interface{}) e name := d.Get("name").(string) location := d.Get("location").(string) + // search for our virtual network's configuration: + var found bool for _, vnet := range netConf.Configuration.VirtualNetworkSites { if vnet.Name == name && vnet.Location == location { + found = true d.Set("address_space_prefixes", vnet.AddressSpace.AddressPrefix) // read subnets: @@ -231,16 +235,19 @@ func resourceAzureVirtualNetworkRead(d *schema.ResourceData, meta interface{}) e } } + if !found { + // it means that the resource was deleted in the meantime and + // we must remove it from the schema. + d.SetId("") + } + return nil } // resourceAzureVirtualNetworkUpdate does all the necessary API calls to // update the status of our virtual network. func resourceAzureVirtualNetworkUpdate(d *schema.ResourceData, meta interface{}) error { - azureClient, ok := meta.(*AzureClient) - if !ok { - return fmt.Errorf("Failed to convert to *AzureClient, got: %T", meta) - } + azureClient := meta.(*AzureClient) managementClient := azureClient.managementClient networkClient := virtualnetwork.NewClient(managementClient) secGroupClient := netsecgroup.NewClient(managementClient) @@ -350,6 +357,7 @@ func resourceAzureVirtualNetworkUpdate(d *schema.ResourceData, meta interface{}) vnets[i].Subnets = subnets } } + break } // if the resource was not found; it means it was deleted from outside Terraform @@ -374,10 +382,7 @@ func resourceAzureVirtualNetworkUpdate(d *schema.ResourceData, meta interface{}) // resourceAzureVirtualNetworkExists does all the necessary API calls to // check if the virtual network already exists. func resourceAzureVirtualNetworkExists(d *schema.ResourceData, meta interface{}) (bool, error) { - azureClient, ok := meta.(*AzureClient) - if !ok { - return false, fmt.Errorf("Failed to convert to *AzureClient, got: %T", meta) - } + azureClient := meta.(*AzureClient) managementClient := azureClient.managementClient networkClient := virtualnetwork.NewClient(managementClient) @@ -398,16 +403,16 @@ func resourceAzureVirtualNetworkExists(d *schema.ResourceData, meta interface{}) } } + // if not found; it means the resource was deleted in the + // meantime, and we must remove it from the schema. + d.SetId("") return false, nil } // resourceAzureVirtualNetworkDelete does all the necessary API calls to delete // the virtual network off Azure. func resourceAzureVirtualNetworkDelete(d *schema.ResourceData, meta interface{}) error { - azureClient, ok := meta.(*AzureClient) - if !ok { - return fmt.Errorf("Failed to convert to *AzureClient, got: %T", meta) - } + azureClient := meta.(*AzureClient) managementClient := azureClient.managementClient networkClient := virtualnetwork.NewClient(managementClient) secGroupClient := netsecgroup.NewClient(managementClient) @@ -445,39 +450,35 @@ func resourceAzureVirtualNetworkDelete(d *schema.ResourceData, meta interface{}) if err != nil { return fmt.Errorf("Error removing network security group settings from subnet %d: %s", i+1, err) } + + break } } } // look for our virtual network and remove it: - var found bool for i, vnet := range netConf.Configuration.VirtualNetworkSites { if vnet.Name == name && vnet.Location == location { - found = true netConf.Configuration.VirtualNetworkSites = append( netConf.Configuration.VirtualNetworkSites[:i], netConf.Configuration.VirtualNetworkSites[i+1:]..., ) - } - } - // if not found; it means the resource has been delted in the meantime; - // so we stop keeping track if it: - if !found { - d.SetId("") - } else { - // else; send the updated configuration back: - log.Println("[INFO] Sending virtual network configuration back to Azure.") - reqID, err := networkClient.SetVirtualNetworkConfiguration(netConf) - if err != nil { - return fmt.Errorf("Failed updating network configuration: %s", err) - } - err = managementClient.WaitForOperation(reqID, nil) - if err != nil { - return fmt.Errorf("Failed to set new Azure network configuration: %s", err) + // send the updated configuration back: + log.Printf("[INFO] Starting deletion of virtual network %s off Azure.", name) + log.Println("[INFO] Sending virtual network configuration back to Azure.") + reqID, err := networkClient.SetVirtualNetworkConfiguration(netConf) + if err != nil { + return fmt.Errorf("Failed updating network configuration: %s", err) + } + err = managementClient.WaitForOperation(reqID, nil) + if err != nil { + return fmt.Errorf("Failed to set new Azure network configuration: %s", err) + } + + break } } - d.SetId("") return nil } diff --git a/builtin/providers/azure/utils.go b/builtin/providers/azure/utils.go index d61d1bce658d..c8786d60fc8c 100644 --- a/builtin/providers/azure/utils.go +++ b/builtin/providers/azure/utils.go @@ -25,3 +25,13 @@ func getRandomStringLabel(n int) string { } return string(buf) } + +// sprintfParams is a helper function which takes a string-bool map and returns +// a formatted string with all the keys for displaying in errors. +func sprintfParams(m map[string]bool) string { + s := "" + for k, _ := range m { + s = s + k + ", " + } + return s[:len(s)-2] +} diff --git a/builtin/providers/azure/validation.go b/builtin/providers/azure/validation.go deleted file mode 100644 index 6512f735e019..000000000000 --- a/builtin/providers/azure/validation.go +++ /dev/null @@ -1 +0,0 @@ -package azure