From 58dd568da988a3a9a07c4d556906e5850e0873ea Mon Sep 17 00:00:00 2001 From: aznashwan Date: Tue, 16 Jun 2015 20:54:52 +0300 Subject: [PATCH] Cleaned up client creation and handling. --- builtin/providers/azure/config.go | 95 +++++++++++++++++-- .../azure/resource_azure_data_disk.go | 26 ++--- .../azure/resource_azure_data_disk_test.go | 8 +- .../azure/resource_azure_dns_server.go | 38 +++----- .../azure/resource_azure_dns_server_test.go | 10 +- .../azure/resource_azure_hosted_service.go | 9 +- .../resource_azure_hosted_service_test.go | 9 +- .../azure/resource_azure_instance.go | 54 +++++++---- .../azure/resource_azure_instance_test.go | 9 +- .../azure/resource_azure_local_network.go | 29 +++--- .../resource_azure_local_network_test.go | 9 +- .../azure/resource_azure_security_group.go | 17 ++-- .../resource_azure_security_group_rule.go | 27 +++--- ...resource_azure_security_group_rule_test.go | 7 +- .../resource_azure_security_group_test.go | 8 +- .../azure/resource_azure_storage_blob.go | 17 ++-- .../azure/resource_azure_storage_blob_test.go | 8 +- .../azure/resource_azure_storage_container.go | 16 ++-- .../resource_azure_storage_container_test.go | 8 +- .../azure/resource_azure_storage_queue.go | 12 +-- .../resource_azure_storage_queue_test.go | 8 +- .../azure/resource_azure_storage_service.go | 22 ++--- .../resource_azure_storage_service_helpers.go | 50 ---------- .../resource_azure_storage_service_test.go | 9 +- .../azure/resource_azure_virtual_network.go | 36 +++---- .../resource_azure_virtual_network_test.go | 8 +- 26 files changed, 282 insertions(+), 267 deletions(-) delete mode 100644 builtin/providers/azure/resource_azure_storage_service_helpers.go diff --git a/builtin/providers/azure/config.go b/builtin/providers/azure/config.go index 5ff46084e..cf02bf557 100644 --- a/builtin/providers/azure/config.go +++ b/builtin/providers/azure/config.go @@ -6,6 +6,15 @@ import ( "sync" "github.com/Azure/azure-sdk-for-go/management" + "github.com/Azure/azure-sdk-for-go/management/hostedservice" + "github.com/Azure/azure-sdk-for-go/management/networksecuritygroup" + "github.com/Azure/azure-sdk-for-go/management/osimage" + "github.com/Azure/azure-sdk-for-go/management/storageservice" + "github.com/Azure/azure-sdk-for-go/management/virtualmachine" + "github.com/Azure/azure-sdk-for-go/management/virtualmachinedisk" + "github.com/Azure/azure-sdk-for-go/management/virtualmachineimage" + "github.com/Azure/azure-sdk-for-go/management/virtualnetwork" + "github.com/Azure/azure-sdk-for-go/storage" ) // Config is the configuration structure used to instantiate a @@ -19,10 +28,66 @@ type Config struct { // Client contains all the handles required for managing Azure services. type Client struct { - // unfortunately; because of how Azure's network API works; doing networking operations - // concurrently is very hazardous, and we need a mutex to guard the management.Client. - mutex *sync.Mutex mgmtClient management.Client + + hostedServiceClient hostedservice.HostedServiceClient + + secGroupClient networksecuritygroup.SecurityGroupClient + + osImageClient osimage.OSImageClient + + storageServiceClient storageservice.StorageServiceClient + + vmClient virtualmachine.VirtualMachineClient + + vmDiskClient virtualmachinedisk.DiskClient + + vmImageClient virtualmachineimage.Client + + // unfortunately; because of how Azure's network API works; doing networking operations + // concurrently is very hazardous, and we need a mutex to guard the VirtualNetworkClient. + vnetClient virtualnetwork.VirtualNetworkClient + mutex *sync.Mutex +} + +// getStorageClientForStorageService is helper method which returns the +// storage.Client associated to the given storage service name. +func (c Client) getStorageClientForStorageService(serviceName string) (storage.Client, error) { + var storageClient storage.Client + + keys, err := c.storageServiceClient.GetStorageServiceKeys(serviceName) + if err != nil { + return storageClient, fmt.Errorf("Failed getting Storage Service keys for %s: %s", serviceName, err) + } + + storageClient, err = storage.NewBasicClient(serviceName, keys.PrimaryKey) + if err != nil { + return storageClient, fmt.Errorf("Failed creating Storage Service client for %s: %s", serviceName, err) + } + + return storageClient, err +} + +// getStorageServiceBlobClient is a helper method which returns the +// storage.BlobStorageClient associated to the given storage service name. +func (c Client) getStorageServiceBlobClient(serviceName string) (storage.BlobStorageClient, error) { + storageClient, err := c.getStorageClientForStorageService(serviceName) + if err != nil { + return storage.BlobStorageClient{}, err + } + + return storageClient.GetBlobService(), nil +} + +// getStorageServiceQueueClient is a helper method which returns the +// storage.QueueServiceClient associated to the given storage service name. +func (c Client) getStorageServiceQueueClient(serviceName string) (storage.QueueServiceClient, error) { + storageClient, err := c.getStorageClientForStorageService(serviceName) + if err != nil { + return storage.QueueServiceClient{}, err + } + + return storageClient.GetQueueService(), err } // NewClientFromSettingsFile returns a new Azure management @@ -38,8 +103,16 @@ func (c *Config) NewClientFromSettingsFile() (*Client, error) { } return &Client{ - mutex: &sync.Mutex{}, - mgmtClient: mc, + mgmtClient: mc, + hostedServiceClient: hostedservice.NewClient(mc), + secGroupClient: networksecuritygroup.NewClient(mc), + osImageClient: osimage.NewClient(mc), + storageServiceClient: storageservice.NewClient(mc), + vmClient: virtualmachine.NewClient(mc), + vmDiskClient: virtualmachinedisk.NewClient(mc), + vmImageClient: virtualmachineimage.NewClient(mc), + vnetClient: virtualnetwork.NewClient(mc), + mutex: &sync.Mutex{}, }, nil } @@ -52,7 +125,15 @@ func (c *Config) NewClient() (*Client, error) { } return &Client{ - mutex: &sync.Mutex{}, - mgmtClient: mc, + mgmtClient: mc, + hostedServiceClient: hostedservice.NewClient(mc), + secGroupClient: networksecuritygroup.NewClient(mc), + osImageClient: osimage.NewClient(mc), + storageServiceClient: storageservice.NewClient(mc), + vmClient: virtualmachine.NewClient(mc), + vmDiskClient: virtualmachinedisk.NewClient(mc), + vmImageClient: virtualmachineimage.NewClient(mc), + vnetClient: virtualnetwork.NewClient(mc), + mutex: &sync.Mutex{}, }, nil } diff --git a/builtin/providers/azure/resource_azure_data_disk.go b/builtin/providers/azure/resource_azure_data_disk.go index eb8a6af00..6e48b976a 100644 --- a/builtin/providers/azure/resource_azure_data_disk.go +++ b/builtin/providers/azure/resource_azure_data_disk.go @@ -79,6 +79,7 @@ func resourceAzureDataDisk() *schema.Resource { func resourceAzureDataDiskCreate(d *schema.ResourceData, meta interface{}) error { mc := meta.(*Client).mgmtClient + vmDiskClient := meta.(*Client).vmDiskClient if err := verifyDataDiskParameters(d); err != nil { return err @@ -106,7 +107,7 @@ func resourceAzureDataDiskCreate(d *schema.ResourceData, meta interface{}) error } log.Printf("[DEBUG] Adding data disk %d to instance: %s", lun, vm) - req, err := virtualmachinedisk.NewClient(mc).AddDataDisk(vm, vm, vm, p) + req, err := vmDiskClient.AddDataDisk(vm, vm, vm, p) if err != nil { return fmt.Errorf("Error adding data disk %d to instance %s: %s", lun, vm, err) } @@ -118,7 +119,7 @@ func resourceAzureDataDiskCreate(d *schema.ResourceData, meta interface{}) error } log.Printf("[DEBUG] Retrieving data disk %d from instance %s", lun, vm) - disk, err := virtualmachinedisk.NewClient(mc).GetDataDisk(vm, vm, vm, lun) + disk, err := vmDiskClient.GetDataDisk(vm, vm, vm, lun) if err != nil { return fmt.Errorf("Error retrieving data disk %d from instance %s: %s", lun, vm, err) } @@ -129,13 +130,13 @@ func resourceAzureDataDiskCreate(d *schema.ResourceData, meta interface{}) error } func resourceAzureDataDiskRead(d *schema.ResourceData, meta interface{}) error { - mc := meta.(*Client).mgmtClient + vmDiskClient := meta.(*Client).vmDiskClient lun := d.Get("lun").(int) vm := d.Get("virtual_machine").(string) log.Printf("[DEBUG] Retrieving data disk: %s", d.Id()) - datadisk, err := virtualmachinedisk.NewClient(mc).GetDataDisk(vm, vm, vm, lun) + datadisk, err := vmDiskClient.GetDataDisk(vm, vm, vm, lun) if err != nil { if management.IsResourceNotFoundError(err) { d.SetId("") @@ -152,7 +153,7 @@ func resourceAzureDataDiskRead(d *schema.ResourceData, meta interface{}) error { d.Set("media_link", datadisk.MediaLink) log.Printf("[DEBUG] Retrieving disk: %s", d.Id()) - disk, err := virtualmachinedisk.NewClient(mc).GetDisk(d.Id()) + disk, err := vmDiskClient.GetDisk(d.Id()) if err != nil { return fmt.Errorf("Error retrieving disk %s: %s", d.Id(), err) } @@ -164,7 +165,7 @@ func resourceAzureDataDiskRead(d *schema.ResourceData, meta interface{}) error { func resourceAzureDataDiskUpdate(d *schema.ResourceData, meta interface{}) error { mc := meta.(*Client).mgmtClient - diskClient := virtualmachinedisk.NewClient(mc) + vmDiskClient := meta.(*Client).vmDiskClient lun := d.Get("lun").(int) vm := d.Get("virtual_machine").(string) @@ -174,7 +175,7 @@ func resourceAzureDataDiskUpdate(d *schema.ResourceData, meta interface{}) error ovm, _ := d.GetChange("virtual_machine") log.Printf("[DEBUG] Detaching data disk: %s", d.Id()) - req, err := diskClient. + req, err := vmDiskClient. DeleteDataDisk(ovm.(string), ovm.(string), ovm.(string), olun.(int), false) if err != nil { return fmt.Errorf("Error detaching data disk %s: %s", d.Id(), err) @@ -188,7 +189,7 @@ func resourceAzureDataDiskUpdate(d *schema.ResourceData, meta interface{}) error log.Printf("[DEBUG] Verifying data disk %s is properly detached...", d.Id()) for i := 0; i < 6; i++ { - disk, err := diskClient.GetDisk(d.Id()) + disk, err := vmDiskClient.GetDisk(d.Id()) if err != nil { return fmt.Errorf("Error retrieving disk %s: %s", d.Id(), err) } @@ -210,7 +211,7 @@ func resourceAzureDataDiskUpdate(d *schema.ResourceData, meta interface{}) error } log.Printf("[DEBUG] Updating disk: %s", d.Id()) - req, err := diskClient.UpdateDisk(d.Id(), p) + req, err := vmDiskClient.UpdateDisk(d.Id(), p) if err != nil { return fmt.Errorf("Error updating disk %s: %s", d.Id(), err) } @@ -230,7 +231,7 @@ func resourceAzureDataDiskUpdate(d *schema.ResourceData, meta interface{}) error } log.Printf("[DEBUG] Attaching data disk: %s", d.Id()) - req, err = diskClient.AddDataDisk(vm, vm, vm, p) + req, err = vmDiskClient.AddDataDisk(vm, vm, vm, p) if err != nil { return fmt.Errorf("Error attaching data disk %s to instance %s: %s", d.Id(), vm, err) } @@ -255,7 +256,7 @@ func resourceAzureDataDiskUpdate(d *schema.ResourceData, meta interface{}) error } log.Printf("[DEBUG] Updating data disk: %s", d.Id()) - req, err := diskClient.UpdateDataDisk(vm, vm, vm, lun, p) + req, err := vmDiskClient.UpdateDataDisk(vm, vm, vm, lun, p) if err != nil { return fmt.Errorf("Error updating data disk %s: %s", d.Id(), err) } @@ -272,6 +273,7 @@ func resourceAzureDataDiskUpdate(d *schema.ResourceData, meta interface{}) error func resourceAzureDataDiskDelete(d *schema.ResourceData, meta interface{}) error { mc := meta.(*Client).mgmtClient + vmDiskClient := meta.(*Client).vmDiskClient lun := d.Get("lun").(int) vm := d.Get("virtual_machine").(string) @@ -281,7 +283,7 @@ func resourceAzureDataDiskDelete(d *schema.ResourceData, meta interface{}) error _, removeBlob := d.GetOk("name") log.Printf("[DEBUG] Detaching data disk %s with removeBlob = %t", d.Id(), removeBlob) - req, err := virtualmachinedisk.NewClient(mc).DeleteDataDisk(vm, vm, vm, lun, removeBlob) + req, err := vmDiskClient.DeleteDataDisk(vm, vm, vm, lun, removeBlob) if err != nil { return fmt.Errorf( "Error detaching data disk %s with removeBlob = %t: %s", d.Id(), removeBlob, err) diff --git a/builtin/providers/azure/resource_azure_data_disk_test.go b/builtin/providers/azure/resource_azure_data_disk_test.go index c2719aa9d..dfad26b5e 100644 --- a/builtin/providers/azure/resource_azure_data_disk_test.go +++ b/builtin/providers/azure/resource_azure_data_disk_test.go @@ -101,8 +101,8 @@ func testAccCheckAzureDataDiskExists( return err } - mc := testAccProvider.Meta().(*Client).mgmtClient - d, err := virtualmachinedisk.NewClient(mc).GetDataDisk(vm, vm, vm, lun) + vmDiskClient := testAccProvider.Meta().(*Client).vmDiskClient + d, err := vmDiskClient.GetDataDisk(vm, vm, vm, lun) if err != nil { return err } @@ -138,7 +138,7 @@ func testAccCheckAzureDataDiskAttributes( } func testAccCheckAzureDataDiskDestroy(s *terraform.State) error { - mc := testAccProvider.Meta().(*Client).mgmtClient + vmDiskClient := testAccProvider.Meta().(*Client).vmDiskClient for _, rs := range s.RootModule().Resources { if rs.Type != "azure_data_disk" { @@ -155,7 +155,7 @@ func testAccCheckAzureDataDiskDestroy(s *terraform.State) error { return err } - _, err = virtualmachinedisk.NewClient(mc).GetDataDisk(vm, vm, vm, lun) + _, err = vmDiskClient.GetDataDisk(vm, vm, vm, lun) if err == nil { return fmt.Errorf("Data disk %s still exists", rs.Primary.ID) } diff --git a/builtin/providers/azure/resource_azure_dns_server.go b/builtin/providers/azure/resource_azure_dns_server.go index 30da0843a..0f9db9eec 100644 --- a/builtin/providers/azure/resource_azure_dns_server.go +++ b/builtin/providers/azure/resource_azure_dns_server.go @@ -37,23 +37,14 @@ 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 { - // first; check for the existence of the resource: - exists, err := resourceAzureDnsServerExists(d, meta) - if err != nil { - return err - } - if exists { - return fmt.Errorf("Azure DNS server definition already exists.") - } - azureClient := meta.(*Client) mgmtClient := azureClient.mgmtClient - networkClient := virtualnetwork.NewClient(mgmtClient) + vnetClient := azureClient.vnetClient log.Println("[INFO] Fetching current network configuration from Azure.") azureClient.mutex.Lock() defer azureClient.mutex.Unlock() - netConf, err := networkClient.GetVirtualNetworkConfiguration() + netConf, err := vnetClient.GetVirtualNetworkConfiguration() if err != nil { return fmt.Errorf("Failed to get the current network configuration from Azure: %s", err) } @@ -70,7 +61,7 @@ func resourceAzureDnsServerCreate(d *schema.ResourceData, meta interface{}) erro // send the configuration back to Azure: log.Println("[INFO] Sending updated network configuration back to Azure.") - reqID, err := networkClient.SetVirtualNetworkConfiguration(netConf) + reqID, err := vnetClient.SetVirtualNetworkConfiguration(netConf) if err != nil { return fmt.Errorf("Failed issuing update to network configuration: %s", err) } @@ -86,12 +77,10 @@ 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 := meta.(*Client) - mgmtClient := azureClient.mgmtClient - networkClient := virtualnetwork.NewClient(mgmtClient) + vnetClient := meta.(*Client).vnetClient log.Println("[INFO] Fetching current network configuration from Azure.") - netConf, err := networkClient.GetVirtualNetworkConfiguration() + netConf, err := vnetClient.GetVirtualNetworkConfiguration() if err != nil { return fmt.Errorf("Failed to get the current network configuration from Azure: %s", err) } @@ -121,7 +110,7 @@ func resourceAzureDnsServerRead(d *schema.ResourceData, meta interface{}) error func resourceAzureDnsServerUpdate(d *schema.ResourceData, meta interface{}) error { azureClient := meta.(*Client) mgmtClient := azureClient.mgmtClient - networkClient := virtualnetwork.NewClient(mgmtClient) + vnetClient := azureClient.vnetClient var found bool name := d.Get("name").(string) @@ -131,7 +120,7 @@ func resourceAzureDnsServerUpdate(d *schema.ResourceData, meta interface{}) erro log.Println("[INFO] Fetching current network configuration from Azure.") azureClient.mutex.Lock() defer azureClient.mutex.Unlock() - netConf, err := networkClient.GetVirtualNetworkConfiguration() + netConf, err := vnetClient.GetVirtualNetworkConfiguration() if err != nil { return fmt.Errorf("Failed to get the current network configuration from Azure: %s", err) } @@ -148,7 +137,7 @@ func resourceAzureDnsServerUpdate(d *schema.ResourceData, meta interface{}) erro // if the config has changes, send the configuration back to Azure: if found { log.Println("[INFO] Sending updated network configuration back to Azure.") - reqID, err := networkClient.SetVirtualNetworkConfiguration(netConf) + reqID, err := vnetClient.SetVirtualNetworkConfiguration(netConf) if err != nil { return fmt.Errorf("Failed issuing update to network configuration: %s", err) } @@ -173,11 +162,10 @@ func resourceAzureDnsServerUpdate(d *schema.ResourceData, meta interface{}) erro // check if the DNS server definition alredy exists on Azure. func resourceAzureDnsServerExists(d *schema.ResourceData, meta interface{}) (bool, error) { azureClient := meta.(*Client) - mgmtClient := azureClient.mgmtClient - networkClient := virtualnetwork.NewClient(mgmtClient) + vnetClient := azureClient.vnetClient log.Println("[INFO] Fetching current network configuration from Azure.") - netConf, err := networkClient.GetVirtualNetworkConfiguration() + netConf, err := vnetClient.GetVirtualNetworkConfiguration() if err != nil { return false, fmt.Errorf("Failed to get the current network configuration from Azure: %s", err) } @@ -201,12 +189,12 @@ func resourceAzureDnsServerExists(d *schema.ResourceData, meta interface{}) (boo func resourceAzureDnsServerDelete(d *schema.ResourceData, meta interface{}) error { azureClient := meta.(*Client) mgmtClient := azureClient.mgmtClient - networkClient := virtualnetwork.NewClient(mgmtClient) + vnetClient := azureClient.vnetClient log.Println("[INFO] Fetching current network configuration from Azure.") azureClient.mutex.Lock() defer azureClient.mutex.Unlock() - netConf, err := networkClient.GetVirtualNetworkConfiguration() + netConf, err := vnetClient.GetVirtualNetworkConfiguration() if err != nil { return fmt.Errorf("Failed to get the current network configuration from Azure: %s", err) } @@ -233,7 +221,7 @@ func resourceAzureDnsServerDelete(d *schema.ResourceData, meta interface{}) erro // send the configuration back to Azure: log.Println("[INFO] Sending updated network configuration back to Azure.") - reqID, err := networkClient.SetVirtualNetworkConfiguration(netConf) + reqID, err := vnetClient.SetVirtualNetworkConfiguration(netConf) if err != nil { return fmt.Errorf("Failed issuing update to network configuration: %s", err) } diff --git a/builtin/providers/azure/resource_azure_dns_server_test.go b/builtin/providers/azure/resource_azure_dns_server_test.go index 1654f7ee8..8b8e335b4 100644 --- a/builtin/providers/azure/resource_azure_dns_server_test.go +++ b/builtin/providers/azure/resource_azure_dns_server_test.go @@ -4,7 +4,6 @@ import ( "fmt" "testing" - "github.com/Azure/azure-sdk-for-go/management/virtualnetwork" "github.com/hashicorp/terraform/helper/resource" "github.com/hashicorp/terraform/terraform" ) @@ -69,8 +68,8 @@ func testAccCheckAzureDnsServerExists(name string) resource.TestCheckFunc { return fmt.Errorf("No DNS Server ID set.") } - mgmtClient := testAccProvider.Meta().(*Client).mgmtClient - netConf, err := virtualnetwork.NewClient(mgmtClient).GetVirtualNetworkConfiguration() + vnetClient := testAccProvider.Meta().(*Client).vnetClient + netConf, err := vnetClient.GetVirtualNetworkConfiguration() if err != nil { return fmt.Errorf("Failed fetching networking configuration: %s", err) } @@ -86,7 +85,7 @@ func testAccCheckAzureDnsServerExists(name string) resource.TestCheckFunc { } func testAccCheckAzureDnsServerDestroy(s *terraform.State) error { - mgmtClient := testAccProvider.Meta().(*Client).mgmtClient + vnetClient := testAccProvider.Meta().(*Client).vnetClient for _, resource := range s.RootModule().Resources { if resource.Type != "azure_dns_server" { @@ -97,8 +96,7 @@ func testAccCheckAzureDnsServerDestroy(s *terraform.State) error { return fmt.Errorf("No DNS Server ID is set.") } - networkClient := virtualnetwork.NewClient(mgmtClient) - netConf, err := networkClient.GetVirtualNetworkConfiguration() + netConf, err := vnetClient.GetVirtualNetworkConfiguration() if err != nil { return fmt.Errorf("Error retrieving networking configuration from Azure: %s", err) } diff --git a/builtin/providers/azure/resource_azure_hosted_service.go b/builtin/providers/azure/resource_azure_hosted_service.go index 6fb2d956d..3d7a638a6 100644 --- a/builtin/providers/azure/resource_azure_hosted_service.go +++ b/builtin/providers/azure/resource_azure_hosted_service.go @@ -76,9 +76,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 := meta.(*Client) - mgmtClient := azureClient.mgmtClient - hostedServiceClient := hostedservice.NewClient(mgmtClient) + hostedServiceClient := meta.(*Client).hostedServiceClient serviceName := d.Get("name").(string) location := d.Get("location").(string) @@ -106,8 +104,7 @@ func resourceAzureHostedServiceCreate(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 := meta.(*Client) - hostedServiceClient := hostedservice.NewClient(azureClient.mgmtClient) + hostedServiceClient := meta.(*Client).hostedServiceClient log.Println("[INFO] Querying for hosted service info.") serviceName := d.Get("name").(string) @@ -151,7 +148,7 @@ func resourceAzureHostedServiceUpdate(d *schema.ResourceData, meta interface{}) func resourceAzureHostedServiceDelete(d *schema.ResourceData, meta interface{}) error { azureClient := meta.(*Client) mgmtClient := azureClient.mgmtClient - hostedServiceClient := hostedservice.NewClient(mgmtClient) + hostedServiceClient := azureClient.hostedServiceClient log.Println("[INFO] Issuing hosted service deletion.") serviceName := d.Get("name").(string) diff --git a/builtin/providers/azure/resource_azure_hosted_service_test.go b/builtin/providers/azure/resource_azure_hosted_service_test.go index f0ef50e89..bbad19632 100644 --- a/builtin/providers/azure/resource_azure_hosted_service_test.go +++ b/builtin/providers/azure/resource_azure_hosted_service_test.go @@ -4,7 +4,6 @@ import ( "fmt" "testing" - "github.com/Azure/azure-sdk-for-go/management/hostedservice" "github.com/hashicorp/terraform/helper/resource" "github.com/hashicorp/terraform/terraform" ) @@ -78,14 +77,14 @@ func testAccCheckAzureHostedServiceExists(name string) resource.TestCheckFunc { return fmt.Errorf("Resource's ID is not set.") } - mgmtClient := testAccProvider.Meta().(*Client).mgmtClient - _, err := hostedservice.NewClient(mgmtClient).GetHostedService(resource.Primary.ID) + hostedServiceClient := testAccProvider.Meta().(*Client).hostedServiceClient + _, err := hostedServiceClient.GetHostedService(resource.Primary.ID) return err } } func testAccCheckAzureHostedServiceDestroyed(s *terraform.State) error { - mgmtClient := testAccProvider.Meta().(*Client).mgmtClient + hostedServiceClient := testAccProvider.Meta().(*Client).hostedServiceClient for _, resource := range s.RootModule().Resources { if resource.Type != "azure_hosted_service" { @@ -96,7 +95,7 @@ func testAccCheckAzureHostedServiceDestroyed(s *terraform.State) error { return fmt.Errorf("No Azure Hosted Service Resource found.") } - _, err := hostedservice.NewClient(mgmtClient).GetHostedService(resource.Primary.ID) + _, err := hostedServiceClient.GetHostedService(resource.Primary.ID) return testAccResourceDestroyedErrorFilter("Hosted Service", err) } diff --git a/builtin/providers/azure/resource_azure_instance.go b/builtin/providers/azure/resource_azure_instance.go index 967e49dce..db5db9d38 100644 --- a/builtin/providers/azure/resource_azure_instance.go +++ b/builtin/providers/azure/resource_azure_instance.go @@ -168,7 +168,10 @@ func resourceAzureInstance() *schema.Resource { } func resourceAzureInstanceCreate(d *schema.ResourceData, meta interface{}) (err error) { - mc := meta.(*Client).mgmtClient + azureClient := meta.(*Client) + mc := azureClient.mgmtClient + hostedServiceClient := azureClient.hostedServiceClient + vmClient := azureClient.vmClient name := d.Get("name").(string) @@ -180,7 +183,7 @@ func resourceAzureInstanceCreate(d *schema.ResourceData, meta interface{}) (err // Retrieve the needed details of the image configureForImage, osType, err := retrieveImageDetails( - mc, + meta, d.Get("image").(string), name, d.Get("storage_service_name").(string), @@ -203,7 +206,7 @@ func resourceAzureInstanceCreate(d *schema.ResourceData, meta interface{}) (err } log.Printf("[DEBUG] Creating Cloud Service for instance: %s", name) - err = hostedservice.NewClient(mc).CreateHostedService(p) + err = hostedServiceClient.CreateHostedService(p) if err != nil { return fmt.Errorf("Error creating Cloud Service for instance %s: %s", name, err) } @@ -212,7 +215,7 @@ func resourceAzureInstanceCreate(d *schema.ResourceData, meta interface{}) (err // when we exit with an error defer func(mc management.Client) { if err != nil { - req, err := hostedservice.NewClient(mc).DeleteHostedService(name, true) + req, err := hostedServiceClient.DeleteHostedService(name, true) if err != nil { log.Printf("[DEBUG] Error cleaning up Cloud Service of instance %s: %s", name, err) } @@ -309,7 +312,7 @@ func resourceAzureInstanceCreate(d *schema.ResourceData, meta interface{}) (err } log.Printf("[DEBUG] Creating the new instance...") - req, err := virtualmachine.NewClient(mc).CreateDeployment(role, name, options) + req, err := vmClient.CreateDeployment(role, name, options) if err != nil { return fmt.Errorf("Error creating instance %s: %s", name, err) } @@ -326,10 +329,12 @@ func resourceAzureInstanceCreate(d *schema.ResourceData, meta interface{}) (err } func resourceAzureInstanceRead(d *schema.ResourceData, meta interface{}) error { - mc := meta.(*Client).mgmtClient + azureClient := meta.(*Client) + hostedServiceClient := azureClient.hostedServiceClient + vmClient := azureClient.vmClient log.Printf("[DEBUG] Retrieving Cloud Service for instance: %s", d.Id()) - cs, err := hostedservice.NewClient(mc).GetHostedService(d.Id()) + cs, err := hostedServiceClient.GetHostedService(d.Id()) if err != nil { return fmt.Errorf("Error retrieving Cloud Service of instance %s: %s", d.Id(), err) } @@ -338,7 +343,7 @@ func resourceAzureInstanceRead(d *schema.ResourceData, meta interface{}) error { d.Set("location", cs.Location) log.Printf("[DEBUG] Retrieving instance: %s", d.Id()) - dpmt, err := virtualmachine.NewClient(mc).GetDeployment(d.Id(), d.Id()) + dpmt, err := vmClient.GetDeployment(d.Id(), d.Id()) if err != nil { if management.IsResourceNotFoundError(err) { d.SetId("") @@ -420,7 +425,9 @@ func resourceAzureInstanceRead(d *schema.ResourceData, meta interface{}) error { } func resourceAzureInstanceUpdate(d *schema.ResourceData, meta interface{}) error { - mc := meta.(*Client).mgmtClient + azureClient := meta.(*Client) + mc := azureClient.mgmtClient + vmClient := azureClient.vmClient // First check if anything we can update changed, and if not just return if !d.HasChange("size") && !d.HasChange("endpoint") && !d.HasChange("security_group") { @@ -428,7 +435,7 @@ func resourceAzureInstanceUpdate(d *schema.ResourceData, meta interface{}) error } // Get the current role - role, err := virtualmachine.NewClient(mc).GetRole(d.Id(), d.Id(), d.Id()) + role, err := vmClient.GetRole(d.Id(), d.Id(), d.Id()) if err != nil { return fmt.Errorf("Error retrieving role of instance %s: %s", d.Id(), err) } @@ -482,7 +489,7 @@ func resourceAzureInstanceUpdate(d *schema.ResourceData, meta interface{}) error } // Update the adjusted role - req, err := virtualmachine.NewClient(mc).UpdateRole(d.Id(), d.Id(), d.Id(), *role) + req, err := vmClient.UpdateRole(d.Id(), d.Id(), d.Id(), *role) if err != nil { return fmt.Errorf("Error updating role of instance %s: %s", d.Id(), err) } @@ -496,10 +503,12 @@ func resourceAzureInstanceUpdate(d *schema.ResourceData, meta interface{}) error } func resourceAzureInstanceDelete(d *schema.ResourceData, meta interface{}) error { - mc := meta.(*Client).mgmtClient + azureClient := meta.(*Client) + mc := azureClient.mgmtClient + hostedServiceClient := azureClient.hostedServiceClient log.Printf("[DEBUG] Deleting instance: %s", d.Id()) - req, err := hostedservice.NewClient(mc).DeleteHostedService(d.Id(), true) + req, err := hostedServiceClient.DeleteHostedService(d.Id(), true) if err != nil { return fmt.Errorf("Error deleting instance %s: %s", d.Id(), err) } @@ -527,16 +536,21 @@ func resourceAzureEndpointHash(v interface{}) int { } func retrieveImageDetails( - mc management.Client, + meta interface{}, label string, name string, storage string) (func(*virtualmachine.Role) error, string, error) { - configureForImage, osType, VMLabels, err := retrieveVMImageDetails(mc, label) + + azureClient := meta.(*Client) + vmImageClient := azureClient.vmImageClient + osImageClient := azureClient.osImageClient + + configureForImage, osType, VMLabels, err := retrieveVMImageDetails(vmImageClient, label) if err == nil { return configureForImage, osType, nil } - configureForImage, osType, OSLabels, err := retrieveOSImageDetails(mc, label, name, storage) + configureForImage, osType, OSLabels, err := retrieveOSImageDetails(osImageClient, label, name, storage) if err == nil { return configureForImage, osType, nil } @@ -546,9 +560,9 @@ func retrieveImageDetails( } func retrieveVMImageDetails( - mc management.Client, + vmImageClient virtualmachineimage.Client, label string) (func(*virtualmachine.Role) error, string, []string, error) { - imgs, err := virtualmachineimage.NewClient(mc).ListVirtualMachineImages() + imgs, err := vmImageClient.ListVirtualMachineImages() if err != nil { return nil, "", nil, fmt.Errorf("Error retrieving image details: %s", err) } @@ -579,11 +593,11 @@ func retrieveVMImageDetails( } func retrieveOSImageDetails( - mc management.Client, + osImageClient osimage.OSImageClient, label string, name string, storage string) (func(*virtualmachine.Role) error, string, []string, error) { - imgs, err := osimage.NewClient(mc).ListOSImages() + imgs, err := osImageClient.ListOSImages() if err != nil { return nil, "", nil, fmt.Errorf("Error retrieving image details: %s", err) } diff --git a/builtin/providers/azure/resource_azure_instance_test.go b/builtin/providers/azure/resource_azure_instance_test.go index 938f306f4..1125fc9f3 100644 --- a/builtin/providers/azure/resource_azure_instance_test.go +++ b/builtin/providers/azure/resource_azure_instance_test.go @@ -5,7 +5,6 @@ import ( "testing" "github.com/Azure/azure-sdk-for-go/management" - "github.com/Azure/azure-sdk-for-go/management/hostedservice" "github.com/Azure/azure-sdk-for-go/management/virtualmachine" "github.com/hashicorp/terraform/helper/resource" "github.com/hashicorp/terraform/terraform" @@ -131,8 +130,8 @@ func testAccCheckAzureInstanceExists( return fmt.Errorf("No instance ID is set") } - mc := testAccProvider.Meta().(*Client).mgmtClient - vm, err := virtualmachine.NewClient(mc).GetDeployment(rs.Primary.ID, rs.Primary.ID) + vmClient := testAccProvider.Meta().(*Client).vmClient + vm, err := vmClient.GetDeployment(rs.Primary.ID, rs.Primary.ID) if err != nil { return err } @@ -283,7 +282,7 @@ func testAccCheckAzureInstanceUpdatedAttributes( } func testAccCheckAzureInstanceDestroy(s *terraform.State) error { - mc := testAccProvider.Meta().(*Client).mgmtClient + hostedServiceClient := testAccProvider.Meta().(*Client).hostedServiceClient for _, rs := range s.RootModule().Resources { if rs.Type != "azure_instance" { @@ -294,7 +293,7 @@ func testAccCheckAzureInstanceDestroy(s *terraform.State) error { return fmt.Errorf("No instance ID is set") } - _, err := hostedservice.NewClient(mc).GetHostedService(rs.Primary.ID) + _, err := hostedServiceClient.GetHostedService(rs.Primary.ID) if err == nil { return fmt.Errorf("Instance %s still exists", rs.Primary.ID) } diff --git a/builtin/providers/azure/resource_azure_local_network.go b/builtin/providers/azure/resource_azure_local_network.go index ef07ddcb3..c0cc74d61 100644 --- a/builtin/providers/azure/resource_azure_local_network.go +++ b/builtin/providers/azure/resource_azure_local_network.go @@ -47,12 +47,12 @@ func resourceAzureLocalNetworkConnection() *schema.Resource { func resourceAzureLocalNetworkConnectionCreate(d *schema.ResourceData, meta interface{}) error { azureClient := meta.(*Client) mgmtClient := azureClient.mgmtClient - networkClient := virtualnetwork.NewClient(mgmtClient) + vnetClient := azureClient.vnetClient log.Println("[INFO] Fetching current network configuration from Azure.") azureClient.mutex.Lock() defer azureClient.mutex.Unlock() - netConf, err := networkClient.GetVirtualNetworkConfiguration() + netConf, err := vnetClient.GetVirtualNetworkConfiguration() if err != nil { return fmt.Errorf("Failed to get the current network configuration from Azure: %s", err) } @@ -77,7 +77,7 @@ func resourceAzureLocalNetworkConnectionCreate(d *schema.ResourceData, meta inte // send the configuration back to Azure: log.Println("[INFO] Sending updated network configuration back to Azure.") - reqID, err := networkClient.SetVirtualNetworkConfiguration(netConf) + reqID, err := vnetClient.SetVirtualNetworkConfiguration(netConf) if err != nil { return fmt.Errorf("Failed setting updated network configuration: %s", err) } @@ -94,11 +94,10 @@ func resourceAzureLocalNetworkConnectionCreate(d *schema.ResourceData, meta inte // read the state of our local natwork from Azure. func resourceAzureLocalNetworkConnectionRead(d *schema.ResourceData, meta interface{}) error { azureClient := meta.(*Client) - mgmtClient := azureClient.mgmtClient - networkClient := virtualnetwork.NewClient(mgmtClient) + vnetClient := azureClient.vnetClient log.Println("[INFO] Fetching current network configuration from Azure.") - netConf, err := networkClient.GetVirtualNetworkConfiguration() + netConf, err := vnetClient.GetVirtualNetworkConfiguration() if err != nil { return fmt.Errorf("Failed to get the current network configuration from Azure: %s", err) } @@ -130,12 +129,12 @@ func resourceAzureLocalNetworkConnectionRead(d *schema.ResourceData, meta interf func resourceAzureLocalNetworkConnectionUpdate(d *schema.ResourceData, meta interface{}) error { azureClient := meta.(*Client) mgmtClient := azureClient.mgmtClient - networkClient := virtualnetwork.NewClient(mgmtClient) + vnetClient := azureClient.vnetClient log.Println("[INFO] Fetching current network configuration from Azure.") azureClient.mutex.Lock() defer azureClient.mutex.Unlock() - netConf, err := networkClient.GetVirtualNetworkConfiguration() + netConf, err := vnetClient.GetVirtualNetworkConfiguration() if err != nil { return fmt.Errorf("Failed to get the current network configuration from Azure: %s", err) } @@ -169,7 +168,7 @@ func resourceAzureLocalNetworkConnectionUpdate(d *schema.ResourceData, meta inte } else if cvpn || cprefixes { // else, send the configuration back to Azure: log.Println("[INFO] Sending updated network configuration back to Azure.") - reqID, err := networkClient.SetVirtualNetworkConfiguration(netConf) + reqID, err := vnetClient.SetVirtualNetworkConfiguration(netConf) if err != nil { return fmt.Errorf("Failed setting updated network configuration: %s", err) } @@ -185,12 +184,10 @@ func resourceAzureLocalNetworkConnectionUpdate(d *schema.ResourceData, meta inte // resourceAzureLocalNetworkConnectionExists does all the necessary API calls // to check if the local network already exists on Azure. func resourceAzureLocalNetworkConnectionExists(d *schema.ResourceData, meta interface{}) (bool, error) { - azureClient := meta.(*Client) - mgmtClient := azureClient.mgmtClient - networkClient := virtualnetwork.NewClient(mgmtClient) + vnetClient := meta.(*Client).vnetClient log.Println("[INFO] Fetching current network configuration from Azure.") - netConf, err := networkClient.GetVirtualNetworkConfiguration() + netConf, err := vnetClient.GetVirtualNetworkConfiguration() if err != nil { return false, fmt.Errorf("Failed to get the current network configuration from Azure: %s", err) } @@ -211,12 +208,12 @@ func resourceAzureLocalNetworkConnectionExists(d *schema.ResourceData, meta inte func resourceAzureLocalNetworkConnectionDelete(d *schema.ResourceData, meta interface{}) error { azureClient := meta.(*Client) mgmtClient := azureClient.mgmtClient - networkClient := virtualnetwork.NewClient(mgmtClient) + vnetClient := azureClient.vnetClient log.Println("[INFO] Fetching current network configuration from Azure.") azureClient.mutex.Lock() defer azureClient.mutex.Unlock() - netConf, err := networkClient.GetVirtualNetworkConfiguration() + netConf, err := vnetClient.GetVirtualNetworkConfiguration() if err != nil { return fmt.Errorf("Failed to get the current network configuration from Azure: %s", err) } @@ -236,7 +233,7 @@ func resourceAzureLocalNetworkConnectionDelete(d *schema.ResourceData, meta inte // send the configuration back to Azure: log.Println("[INFO] Sending updated network configuration back to Azure.") - reqID, err := networkClient.SetVirtualNetworkConfiguration(netConf) + reqID, err := vnetClient.SetVirtualNetworkConfiguration(netConf) if err != nil { return fmt.Errorf("Failed setting updated network configuration: %s", err) } diff --git a/builtin/providers/azure/resource_azure_local_network_test.go b/builtin/providers/azure/resource_azure_local_network_test.go index ae120569a..2f9f0fdda 100644 --- a/builtin/providers/azure/resource_azure_local_network_test.go +++ b/builtin/providers/azure/resource_azure_local_network_test.go @@ -4,7 +4,6 @@ import ( "fmt" "testing" - "github.com/Azure/azure-sdk-for-go/management/virtualnetwork" "github.com/hashicorp/terraform/helper/resource" "github.com/hashicorp/terraform/terraform" ) @@ -77,8 +76,8 @@ func testAccAzureLocalNetworkConnectionExists(name string) resource.TestCheckFun return fmt.Errorf("Azure Local Network Connection ID not set.") } - mgmtClient := testAccProvider.Meta().(*Client).mgmtClient - netConf, err := virtualnetwork.NewClient(mgmtClient).GetVirtualNetworkConfiguration() + vnetClient := testAccProvider.Meta().(*Client).vnetClient + netConf, err := vnetClient.GetVirtualNetworkConfiguration() if err != nil { return err } @@ -97,7 +96,7 @@ func testAccAzureLocalNetworkConnectionExists(name string) resource.TestCheckFun // testAccAzureLocalNetworkConnectionDestroyed checks whether the local network // connection has been destroyed on Azure or not. func testAccAzureLocalNetworkConnectionDestroyed(s *terraform.State) error { - mgmtClient := testAccProvider.Meta().(*Client).mgmtClient + vnetClient := testAccProvider.Meta().(*Client).vnetClient for _, resource := range s.RootModule().Resources { if resource.Type != "azure_local_network_connection" { @@ -108,7 +107,7 @@ func testAccAzureLocalNetworkConnectionDestroyed(s *terraform.State) error { return fmt.Errorf("Azure Local Network Connection ID not set.") } - netConf, err := virtualnetwork.NewClient(mgmtClient).GetVirtualNetworkConfiguration() + netConf, err := vnetClient.GetVirtualNetworkConfiguration() if err != nil { return err } diff --git a/builtin/providers/azure/resource_azure_security_group.go b/builtin/providers/azure/resource_azure_security_group.go index 4b2b8b6de..1d066d031 100644 --- a/builtin/providers/azure/resource_azure_security_group.go +++ b/builtin/providers/azure/resource_azure_security_group.go @@ -5,7 +5,6 @@ import ( "log" "github.com/Azure/azure-sdk-for-go/management" - "github.com/Azure/azure-sdk-for-go/management/networksecuritygroup" "github.com/hashicorp/terraform/helper/schema" ) @@ -39,7 +38,9 @@ func resourceAzureSecurityGroup() *schema.Resource { } func resourceAzureSecurityGroupCreate(d *schema.ResourceData, meta interface{}) (err error) { - mc := meta.(*Client).mgmtClient + azureClient := meta.(*Client) + mc := azureClient.mgmtClient + secGroupClient := azureClient.secGroupClient name := d.Get("name").(string) @@ -49,7 +50,7 @@ func resourceAzureSecurityGroupCreate(d *schema.ResourceData, meta interface{}) label = name } - req, err := networksecuritygroup.NewClient(mc).CreateNetworkSecurityGroup( + req, err := secGroupClient.CreateNetworkSecurityGroup( name, label, d.Get("location").(string), @@ -69,9 +70,9 @@ func resourceAzureSecurityGroupCreate(d *schema.ResourceData, meta interface{}) } func resourceAzureSecurityGroupRead(d *schema.ResourceData, meta interface{}) error { - mc := meta.(*Client).mgmtClient + secGroupClient := meta.(*Client).secGroupClient - sg, err := networksecuritygroup.NewClient(mc).GetNetworkSecurityGroup(d.Id()) + sg, err := secGroupClient.GetNetworkSecurityGroup(d.Id()) if err != nil { if management.IsResourceNotFoundError(err) { d.SetId("") @@ -87,10 +88,12 @@ func resourceAzureSecurityGroupRead(d *schema.ResourceData, meta interface{}) er } func resourceAzureSecurityGroupDelete(d *schema.ResourceData, meta interface{}) error { - mc := meta.(*Client).mgmtClient + azureClient := meta.(*Client) + mc := azureClient.mgmtClient + secGroupClient := azureClient.secGroupClient log.Printf("[DEBUG] Deleting Network Security Group: %s", d.Id()) - req, err := networksecuritygroup.NewClient(mc).DeleteNetworkSecurityGroup(d.Id()) + req, err := secGroupClient.DeleteNetworkSecurityGroup(d.Id()) if err != nil { return fmt.Errorf("Error deleting Network Security Group %s: %s", d.Id(), err) } diff --git a/builtin/providers/azure/resource_azure_security_group_rule.go b/builtin/providers/azure/resource_azure_security_group_rule.go index 23c313729..9adf7451d 100644 --- a/builtin/providers/azure/resource_azure_security_group_rule.go +++ b/builtin/providers/azure/resource_azure_security_group_rule.go @@ -81,7 +81,7 @@ func resourceAzureSecurityGroupRule() *schema.Resource { func resourceAzureSecurityGroupRuleCreate(d *schema.ResourceData, meta interface{}) error { azureClient := meta.(*Client) mgmtClient := azureClient.mgmtClient - netSecClient := netsecgroup.NewClient(mgmtClient) + secGroupClient := azureClient.secGroupClient // create and configure the RuleResponse: name := d.Get("name").(string) @@ -99,7 +99,7 @@ func resourceAzureSecurityGroupRuleCreate(d *schema.ResourceData, meta interface // send the create request to Azure: log.Println("[INFO] Sending network security group rule creation request to Azure.") - reqID, err := netSecClient.SetNetworkSecurityGroupRule( + reqID, err := secGroupClient.SetNetworkSecurityGroupRule( d.Get("security_group_name").(string), rule, ) @@ -119,14 +119,13 @@ func resourceAzureSecurityGroupRuleCreate(d *schema.ResourceData, meta interface // read the state of a network security group ruke off Azure. func resourceAzureSecurityGroupRuleRead(d *schema.ResourceData, meta interface{}) error { azureClient := meta.(*Client) - mgmtClient := azureClient.mgmtClient - netSecClient := netsecgroup.NewClient(mgmtClient) + secGroupClient := azureClient.secGroupClient secGroupName := d.Get("security_group_name").(string) // get info on the network security group and check its rules for this one: log.Println("[INFO] Sending network security group rule query to Azure.") - secgroup, err := netSecClient.GetNetworkSecurityGroup(secGroupName) + secgroup, err := secGroupClient.GetNetworkSecurityGroup(secGroupName) if err != nil { if !management.IsResourceNotFoundError(err) { return fmt.Errorf("Error issuing network security group rules query: %s", err) @@ -171,13 +170,13 @@ func resourceAzureSecurityGroupRuleRead(d *schema.ResourceData, meta interface{} func resourceAzureSecurityGroupRuleUpdate(d *schema.ResourceData, meta interface{}) error { azureClient := meta.(*Client) mgmtClient := azureClient.mgmtClient - netSecClient := netsecgroup.NewClient(mgmtClient) + secGroupClient := azureClient.secGroupClient secGroupName := d.Get("security_group_name").(string) // get info on the network security group and check its rules for this one: log.Println("[INFO] Sending network security group rule query for update to Azure.") - secgroup, err := netSecClient.GetNetworkSecurityGroup(secGroupName) + secgroup, err := secGroupClient.GetNetworkSecurityGroup(secGroupName) if err != nil { if !management.IsResourceNotFoundError(err) { return fmt.Errorf("Error issuing network security group rules query: %s", err) @@ -219,7 +218,7 @@ func resourceAzureSecurityGroupRuleUpdate(d *schema.ResourceData, meta interface // send the create request to Azure: log.Println("[INFO] Sending network security group rule update request to Azure.") - reqID, err := netSecClient.SetNetworkSecurityGroupRule( + reqID, err := secGroupClient.SetNetworkSecurityGroupRule( secGroupName, newRule, ) @@ -237,15 +236,13 @@ 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 := meta.(*Client) - mgmtClient := azureClient.mgmtClient - netSecClient := netsecgroup.NewClient(mgmtClient) + secGroupClient := meta.(*Client).secGroupClient secGroupName := d.Get("security_group_name").(string) // get info on the network security group and search for our rule: log.Println("[INFO] Sending network security group rule query for existence check to Azure.") - secgroup, err := netSecClient.GetNetworkSecurityGroup(secGroupName) + secgroup, err := secGroupClient.GetNetworkSecurityGroup(secGroupName) if err != nil { if !management.IsResourceNotFoundError(err) { return false, fmt.Errorf("Error issuing network security group rules query: %s", err) @@ -277,13 +274,13 @@ func resourceAzureSecurityGroupRuleExists(d *schema.ResourceData, meta interface func resourceAzureSecurityGroupRuleDelete(d *schema.ResourceData, meta interface{}) error { azureClient := meta.(*Client) mgmtClient := azureClient.mgmtClient - netSecClient := netsecgroup.NewClient(mgmtClient) + secGroupClient := azureClient.secGroupClient secGroupName := d.Get("security_group_name").(string) // get info on the network security group and search for our rule: log.Println("[INFO] Sending network security group rule query for deletion to Azure.") - secgroup, err := netSecClient.GetNetworkSecurityGroup(secGroupName) + secgroup, err := secGroupClient.GetNetworkSecurityGroup(secGroupName) if err != nil { if management.IsResourceNotFoundError(err) { // it meants that the network security group this rule belonged to has @@ -300,7 +297,7 @@ func resourceAzureSecurityGroupRuleDelete(d *schema.ResourceData, meta interface for _, rule := range secgroup.Rules { if rule.Name == name { // if not; we shall issue the delete: - reqID, err := netSecClient.DeleteNetworkSecurityGroupRule(secGroupName, name) + reqID, err := secGroupClient.DeleteNetworkSecurityGroupRule(secGroupName, name) if err != nil { return fmt.Errorf("Error sending network security group rule delete request to Azure: %s", err) } diff --git a/builtin/providers/azure/resource_azure_security_group_rule_test.go b/builtin/providers/azure/resource_azure_security_group_rule_test.go index f16f29ced..29b6e90ca 100644 --- a/builtin/providers/azure/resource_azure_security_group_rule_test.go +++ b/builtin/providers/azure/resource_azure_security_group_rule_test.go @@ -4,7 +4,6 @@ import ( "fmt" "testing" - netsecgroup "github.com/Azure/azure-sdk-for-go/management/networksecuritygroup" "github.com/hashicorp/terraform/helper/resource" "github.com/hashicorp/terraform/terraform" ) @@ -47,8 +46,7 @@ func testAccCheckAzureSecurityGroupRuleExists(name string) resource.TestCheckFun return fmt.Errorf("Azure network security group rule ID not set: %s", name) } - mgmtClient := testAccProvider.Meta().(*Client).mgmtClient - secGroupClient := netsecgroup.NewClient(mgmtClient) + secGroupClient := testAccProvider.Meta().(*Client).secGroupClient secGroup, err := secGroupClient.GetNetworkSecurityGroup(testAccSecurityGroupName) if err != nil { @@ -75,8 +73,7 @@ func testAccCheckAzureSecurityGroupRuleDeleted(s *terraform.State) error { return fmt.Errorf("Azure network security group ID not set.") } - mgmtClient := testAccProvider.Meta().(*Client).mgmtClient - secGroupClient := netsecgroup.NewClient(mgmtClient) + secGroupClient := testAccProvider.Meta().(*Client).secGroupClient secGroup, err := secGroupClient.GetNetworkSecurityGroup(testAccSecurityGroupName) if err != nil { diff --git a/builtin/providers/azure/resource_azure_security_group_test.go b/builtin/providers/azure/resource_azure_security_group_test.go index eb18d38e6..3b35d8731 100644 --- a/builtin/providers/azure/resource_azure_security_group_test.go +++ b/builtin/providers/azure/resource_azure_security_group_test.go @@ -48,8 +48,8 @@ func testAccCheckAzureSecurityGroupExists( return fmt.Errorf("No Network Security Group ID is set") } - mc := testAccProvider.Meta().(*Client).mgmtClient - sg, err := networksecuritygroup.NewClient(mc).GetNetworkSecurityGroup(rs.Primary.ID) + secGroupClient := testAccProvider.Meta().(*Client).secGroupClient + sg, err := secGroupClient.GetNetworkSecurityGroup(rs.Primary.ID) if err != nil { return err } @@ -65,7 +65,7 @@ func testAccCheckAzureSecurityGroupExists( } func testAccCheckAzureSecurityGroupDestroy(s *terraform.State) error { - mc := testAccProvider.Meta().(*Client).mgmtClient + secGroupClient := testAccProvider.Meta().(*Client).secGroupClient for _, rs := range s.RootModule().Resources { if rs.Type != "azure_security_group" { @@ -76,7 +76,7 @@ func testAccCheckAzureSecurityGroupDestroy(s *terraform.State) error { return fmt.Errorf("No Network Security Group ID is set") } - _, err := networksecuritygroup.NewClient(mc).GetNetworkSecurityGroup(rs.Primary.ID) + _, err := secGroupClient.GetNetworkSecurityGroup(rs.Primary.ID) if err == nil { return fmt.Errorf("Network Security Group %s still exists", rs.Primary.ID) } diff --git a/builtin/providers/azure/resource_azure_storage_blob.go b/builtin/providers/azure/resource_azure_storage_blob.go index 8007af4f8..de2431c2f 100644 --- a/builtin/providers/azure/resource_azure_storage_blob.go +++ b/builtin/providers/azure/resource_azure_storage_blob.go @@ -62,10 +62,10 @@ func resourceAzureStorageBlob() *schema.Resource { // resourceAzureStorageBlobCreate does all the necessary API calls to // create the storage blob on Azure. func resourceAzureStorageBlobCreate(d *schema.ResourceData, meta interface{}) error { - mgmtClient := meta.(*Client).mgmtClient + azureClient := meta.(*Client) storName := d.Get("storage_service_name").(string) - blobClient, err := getStorageServiceBlobClient(mgmtClient, storName) + blobClient, err := azureClient.getStorageServiceBlobClient(storName) if err != nil { return err } @@ -94,6 +94,8 @@ func resourceAzureStorageBlobCreate(d *schema.ResourceData, meta interface{}) er // resourceAzureStorageBlobRead does all the necessary API calls to // read the status of the storage blob off Azure. func resourceAzureStorageBlobRead(d *schema.ResourceData, meta interface{}) error { + azureClient := meta.(*Client) + // check for it's existence: exists, err := resourceAzureStorageBlobExists(d, meta) if err != nil { @@ -102,10 +104,9 @@ func resourceAzureStorageBlobRead(d *schema.ResourceData, meta interface{}) erro // if it exists; read relevant information: if exists { - mgmtClient := meta.(*Client).mgmtClient storName := d.Get("storage_service_name").(string) - blobClient, err := getStorageServiceBlobClient(mgmtClient, storName) + blobClient, err := azureClient.getStorageServiceBlobClient(storName) if err != nil { return err } @@ -135,10 +136,10 @@ func resourceAzureStorageBlobUpdate(d *schema.ResourceData, meta interface{}) er // resourceAzureStorageBlobExists does all the necessary API calls to // check for the existence of the blob on Azure. func resourceAzureStorageBlobExists(d *schema.ResourceData, meta interface{}) (bool, error) { - mgmtClient := meta.(*Client).mgmtClient + azureClient := meta.(*Client) storName := d.Get("storage_service_name").(string) - blobClient, err := getStorageServiceBlobClient(mgmtClient, storName) + blobClient, err := azureClient.getStorageServiceBlobClient(storName) if err != nil { return false, err } @@ -163,10 +164,10 @@ func resourceAzureStorageBlobExists(d *schema.ResourceData, meta interface{}) (b // resourceAzureStorageBlobDelete does all the necessary API calls to // delete the blob off Azure. func resourceAzureStorageBlobDelete(d *schema.ResourceData, meta interface{}) error { - mgmtClient := meta.(*Client).mgmtClient + azureClient := meta.(*Client) storName := d.Get("storage_service_name").(string) - blobClient, err := getStorageServiceBlobClient(mgmtClient, storName) + blobClient, err := azureClient.getStorageServiceBlobClient(storName) if err != nil { return err } diff --git a/builtin/providers/azure/resource_azure_storage_blob_test.go b/builtin/providers/azure/resource_azure_storage_blob_test.go index 6f44389f0..d559a6bcd 100644 --- a/builtin/providers/azure/resource_azure_storage_blob_test.go +++ b/builtin/providers/azure/resource_azure_storage_blob_test.go @@ -66,8 +66,8 @@ func testAccCheckAzureStorageBlobExists(name, typ string) resource.TestCheckFunc return fmt.Errorf("Azure Storage Container ID not set: %s", name) } - mgmtClient := testAccProvider.Meta().(*Client).mgmtClient - blobClient, err := getStorageServiceBlobClient(mgmtClient, testAccStorageServiceName) + azureClient := testAccProvider.Meta().(*Client) + blobClient, err := azureClient.getStorageServiceBlobClient(testAccStorageServiceName) if err != nil { return err } @@ -92,8 +92,8 @@ func testAccCheckAzureStorageBlobDeleted(typ string) resource.TestCheckFunc { continue } - mgmtClient := testAccProvider.Meta().(*Client).mgmtClient - blobClient, err := getStorageServiceBlobClient(mgmtClient, testAccStorageServiceName) + azureClient := testAccProvider.Meta().(*Client) + blobClient, err := azureClient.getStorageServiceBlobClient(testAccStorageServiceName) if err != nil { return err } diff --git a/builtin/providers/azure/resource_azure_storage_container.go b/builtin/providers/azure/resource_azure_storage_container.go index d97e6b9ef..8b8e375da 100644 --- a/builtin/providers/azure/resource_azure_storage_container.go +++ b/builtin/providers/azure/resource_azure_storage_container.go @@ -49,10 +49,10 @@ func resourceAzureStorageContainer() *schema.Resource { // resourceAzureStorageContainerCreate does all the necessary API calls to // create the storage container on Azure. func resourceAzureStorageContainerCreate(d *schema.ResourceData, meta interface{}) error { - mgmtClient := meta.(*Client).mgmtClient + azureClient := meta.(*Client) storName := d.Get("storage_service_name").(string) - blobClient, err := getStorageServiceBlobClient(mgmtClient, storName) + blobClient, err := azureClient.getStorageServiceBlobClient(storName) if err != nil { return err } @@ -72,10 +72,10 @@ func resourceAzureStorageContainerCreate(d *schema.ResourceData, meta interface{ // resourceAzureStorageContainerRead does all the necessary API calls to // read the status of the storage container off Azure. func resourceAzureStorageContainerRead(d *schema.ResourceData, meta interface{}) error { - mgmtClient := meta.(*Client).mgmtClient + azureClient := meta.(*Client) storName := d.Get("storage_service_name").(string) - blobClient, err := getStorageServiceBlobClient(mgmtClient, storName) + blobClient, err := azureClient.getStorageServiceBlobClient(storName) if err != nil { return err } @@ -119,10 +119,10 @@ func resourceAzureStorageContainerRead(d *schema.ResourceData, meta interface{}) // resourceAzureStorageContainerExists does all the necessary API calls to // check if the storage container already exists on Azure. func resourceAzureStorageContainerExists(d *schema.ResourceData, meta interface{}) (bool, error) { - mgmtClient := meta.(*Client).mgmtClient + azureClient := meta.(*Client) storName := d.Get("storage_service_name").(string) - blobClient, err := getStorageServiceBlobClient(mgmtClient, storName) + blobClient, err := azureClient.getStorageServiceBlobClient(storName) if err != nil { return false, err } @@ -144,10 +144,10 @@ func resourceAzureStorageContainerExists(d *schema.ResourceData, meta interface{ // resourceAzureStorageContainerDelete does all the necessary API calls to // delete a storage container off Azure. func resourceAzureStorageContainerDelete(d *schema.ResourceData, meta interface{}) error { - mgmtClient := meta.(*Client).mgmtClient + azureClient := meta.(*Client) storName := d.Get("storage_service_name").(string) - blobClient, err := getStorageServiceBlobClient(mgmtClient, storName) + blobClient, err := azureClient.getStorageServiceBlobClient(storName) if err != nil { return err } diff --git a/builtin/providers/azure/resource_azure_storage_container_test.go b/builtin/providers/azure/resource_azure_storage_container_test.go index 80f044ccb..6c110c5e4 100644 --- a/builtin/providers/azure/resource_azure_storage_container_test.go +++ b/builtin/providers/azure/resource_azure_storage_container_test.go @@ -44,8 +44,8 @@ func testAccCheckAzureStorageContainerExists(name string) resource.TestCheckFunc return fmt.Errorf("Azure Storage Container ID not set: %s", name) } - mgmtClient := testAccProvider.Meta().(*Client).mgmtClient - blobClient, err := getStorageServiceBlobClient(mgmtClient, testAccStorageServiceName) + azureClient := testAccProvider.Meta().(*Client) + blobClient, err := azureClient.getStorageServiceBlobClient(testAccStorageServiceName) if err != nil { return err } @@ -68,8 +68,8 @@ func testAccCheckAzureStorageContainerDestroyed(s *terraform.State) error { continue } - mgmtClient := testAccProvider.Meta().(*Client).mgmtClient - blobClient, err := getStorageServiceBlobClient(mgmtClient, testAccStorageServiceName) + azureClient := testAccProvider.Meta().(*Client) + blobClient, err := azureClient.getStorageServiceBlobClient(testAccStorageServiceName) if err != nil { return err } diff --git a/builtin/providers/azure/resource_azure_storage_queue.go b/builtin/providers/azure/resource_azure_storage_queue.go index db4edbb60..4799e236e 100644 --- a/builtin/providers/azure/resource_azure_storage_queue.go +++ b/builtin/providers/azure/resource_azure_storage_queue.go @@ -35,9 +35,9 @@ func resourceAzureStorageQueue() *schema.Resource { // resourceAzureStorageQueueCreate does all the necessary API calls to // create a storage queue on Azure. func resourceAzureStorageQueueCreate(d *schema.ResourceData, meta interface{}) error { - mgmtClient := meta.(*Client).mgmtClient + azureClient := meta.(*Client) storServName := d.Get("storage_service_name").(string) - queueClient, err := getStorageServiceQueueClient(mgmtClient, storServName) + queueClient, err := azureClient.getStorageServiceQueueClient(storServName) if err != nil { return err } @@ -57,9 +57,9 @@ func resourceAzureStorageQueueCreate(d *schema.ResourceData, meta interface{}) e // resourceAzureStorageQueueRead does all the necessary API calls to // read the state of the storage queue off Azure. func resourceAzureStorageQueueRead(d *schema.ResourceData, meta interface{}) error { - mgmtClient := meta.(*Client).mgmtClient + azureClient := meta.(*Client) storServName := d.Get("storage_service_name").(string) - queueClient, err := getStorageServiceQueueClient(mgmtClient, storServName) + queueClient, err := azureClient.getStorageServiceQueueClient(storServName) if err != nil { return err } @@ -84,9 +84,9 @@ func resourceAzureStorageQueueRead(d *schema.ResourceData, meta interface{}) err // resourceAzureStorageQueueDelete does all the necessary API calls to // delete the storage queue off Azure. func resourceAzureStorageQueueDelete(d *schema.ResourceData, meta interface{}) error { - mgmtClient := meta.(*Client).mgmtClient + azureClient := meta.(*Client) storServName := d.Get("storage_service_name").(string) - queueClient, err := getStorageServiceQueueClient(mgmtClient, storServName) + queueClient, err := azureClient.getStorageServiceQueueClient(storServName) if err != nil { return err } diff --git a/builtin/providers/azure/resource_azure_storage_queue_test.go b/builtin/providers/azure/resource_azure_storage_queue_test.go index de3702591..c7c691f4e 100644 --- a/builtin/providers/azure/resource_azure_storage_queue_test.go +++ b/builtin/providers/azure/resource_azure_storage_queue_test.go @@ -39,8 +39,8 @@ func testAccCheckAzureStorageQueueExists(name string) resource.TestCheckFunc { return fmt.Errorf("Azure Storage Service Queue ID %s is missing.", name) } - mgmtClient := testAccProvider.Meta().(*Client).mgmtClient - queueClient, err := getStorageServiceQueueClient(mgmtClient, testAccStorageServiceName) + azureClient := testAccProvider.Meta().(*Client) + queueClient, err := azureClient.getStorageServiceQueueClient(testAccStorageServiceName) if err != nil { return err } @@ -67,8 +67,8 @@ func testAccCheckAzureStorageQueueDeleted(s *terraform.State) error { return fmt.Errorf("Azure Storage Service Queue ID %s is missing.", resource.Primary.ID) } - mgmtClient := testAccProvider.Meta().(*Client).mgmtClient - queueClient, err := getStorageServiceQueueClient(mgmtClient, testAccStorageServiceName) + azureClient := testAccProvider.Meta().(*Client) + queueClient, err := azureClient.getStorageServiceQueueClient(testAccStorageServiceName) if err != nil { return err } diff --git a/builtin/providers/azure/resource_azure_storage_service.go b/builtin/providers/azure/resource_azure_storage_service.go index 4292cc47e..1d3a0c251 100644 --- a/builtin/providers/azure/resource_azure_storage_service.go +++ b/builtin/providers/azure/resource_azure_storage_service.go @@ -88,7 +88,7 @@ func resourceAzureStorageService() *schema.Resource { func resourceAzureStorageServiceCreate(d *schema.ResourceData, meta interface{}) error { azureClient := meta.(*Client) mgmtClient := azureClient.mgmtClient - storageServiceClient := storageservice.NewClient(mgmtClient) + storageServiceClient := azureClient.storageServiceClient // get all the values: log.Println("[INFO] Creating Azure Storage Service creation parameters.") @@ -138,9 +138,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 := meta.(*Client) - mgmtClient := azureClient.mgmtClient - storageServiceClient := storageservice.NewClient(mgmtClient) + storageServiceClient := meta.(*Client).storageServiceClient // get our storage service: log.Println("[INFO] Sending query about storage service to Azure.") @@ -174,12 +172,7 @@ func resourceAzureStorageServiceRead(d *schema.ResourceData, meta interface{}) e // resourceAzureStorageServiceExists does all the necessary API calls to // check if the storage service exists on Azure. func resourceAzureStorageServiceExists(d *schema.ResourceData, meta interface{}) (bool, error) { - azureClient, ok := meta.(*Client) - if !ok { - return false, fmt.Errorf("Failed to convert to *Client, got: %T", meta) - } - mgmtClient := azureClient.mgmtClient - storageServiceClient := storageservice.NewClient(mgmtClient) + storageServiceClient := meta.(*Client).storageServiceClient // get our storage service: log.Println("[INFO] Sending query about storage service to Azure.") @@ -203,17 +196,14 @@ func resourceAzureStorageServiceExists(d *schema.ResourceData, meta interface{}) // resourceAzureStorageServiceDelete does all the necessary API calls to // delete the storage service off Azure. func resourceAzureStorageServiceDelete(d *schema.ResourceData, meta interface{}) error { - azureClient, ok := meta.(*Client) - if !ok { - return fmt.Errorf("Failed to convert to *Client, got: %T", meta) - } + azureClient := meta.(*Client) mgmtClient := azureClient.mgmtClient - storageClient := storageservice.NewClient(mgmtClient) + storageServiceClient := azureClient.storageServiceClient // issue the deletion: name := d.Get("name").(string) log.Println("[INFO] Issuing delete of storage service off Azure.") - reqID, err := storageClient.DeleteStorageService(name) + reqID, err := storageServiceClient.DeleteStorageService(name) if err != nil { return fmt.Errorf("Error whilst issuing deletion of storage service off Azure: %s", err) } diff --git a/builtin/providers/azure/resource_azure_storage_service_helpers.go b/builtin/providers/azure/resource_azure_storage_service_helpers.go deleted file mode 100644 index 68c760920..000000000 --- a/builtin/providers/azure/resource_azure_storage_service_helpers.go +++ /dev/null @@ -1,50 +0,0 @@ -package azure - -import ( - "fmt" - - "github.com/Azure/azure-sdk-for-go/management" - "github.com/Azure/azure-sdk-for-go/management/storageservice" - "github.com/Azure/azure-sdk-for-go/storage" -) - -// getStorageClientForStorageService is helper function which returns the -// storage.Client associated to the given storage service name. -func getStorageClientForStorageService(mgmtClient management.Client, serviceName string) (storage.Client, error) { - var storageClient storage.Client - storageServiceClient := storageservice.NewClient(mgmtClient) - - keys, err := storageServiceClient.GetStorageServiceKeys(serviceName) - if err != nil { - return storageClient, fmt.Errorf("Failed getting Storage Service keys for %s: %s", serviceName, err) - } - - storageClient, err = storage.NewBasicClient(serviceName, keys.PrimaryKey) - if err != nil { - return storageClient, fmt.Errorf("Failed creating Storage Service client for %s: %s", serviceName, err) - } - - return storageClient, err -} - -// getStorageServiceBlobClient is a helper function which returns the -// storage.BlobStorageClient associated to the given storage service name. -func getStorageServiceBlobClient(mgmtClient management.Client, serviceName string) (storage.BlobStorageClient, error) { - storageClient, err := getStorageClientForStorageService(mgmtClient, serviceName) - if err != nil { - return storage.BlobStorageClient{}, err - } - - return storageClient.GetBlobService(), nil -} - -// getStorageServiceQueueClient is a helper function which returns the -// storage.QueueServiceClient associated to the given storage service name. -func getStorageServiceQueueClient(mgmtClient management.Client, serviceName string) (storage.QueueServiceClient, error) { - storageClient, err := getStorageClientForStorageService(mgmtClient, serviceName) - if err != nil { - return storage.QueueServiceClient{}, err - } - - return storageClient.GetQueueService(), err -} diff --git a/builtin/providers/azure/resource_azure_storage_service_test.go b/builtin/providers/azure/resource_azure_storage_service_test.go index 4f1a54e2b..5047ea35f 100644 --- a/builtin/providers/azure/resource_azure_storage_service_test.go +++ b/builtin/providers/azure/resource_azure_storage_service_test.go @@ -4,7 +4,6 @@ import ( "fmt" "testing" - "github.com/Azure/azure-sdk-for-go/management/storageservice" "github.com/hashicorp/terraform/helper/resource" "github.com/hashicorp/terraform/terraform" ) @@ -42,15 +41,15 @@ func testAccAzureStorageServiceExists(name string) resource.TestCheckFunc { return fmt.Errorf("Azure Storage Service ID not set.") } - mgmtClient := testAccProvider.Meta().(*Client).mgmtClient - _, err := storageservice.NewClient(mgmtClient).GetStorageService(resource.Primary.ID) + storageServiceClient := testAccProvider.Meta().(*Client).storageServiceClient + _, err := storageServiceClient.GetStorageService(resource.Primary.ID) return err } } func testAccAzureStorageServiceDestroyed(s *terraform.State) error { - mgmgClient := testAccProvider.Meta().(*Client).mgmtClient + storageServiceClient := testAccProvider.Meta().(*Client).storageServiceClient for _, resource := range s.RootModule().Resources { if resource.Type != "azure_storage_service" { @@ -61,7 +60,7 @@ func testAccAzureStorageServiceDestroyed(s *terraform.State) error { return fmt.Errorf("Azure Storage Service ID not set.") } - _, err := storageservice.NewClient(mgmgClient).GetStorageService(resource.Primary.ID) + _, err := storageServiceClient.GetStorageService(resource.Primary.ID) return testAccResourceDestroyedErrorFilter("Storage Service", err) } diff --git a/builtin/providers/azure/resource_azure_virtual_network.go b/builtin/providers/azure/resource_azure_virtual_network.go index c5c250c55..cc195e742 100644 --- a/builtin/providers/azure/resource_azure_virtual_network.go +++ b/builtin/providers/azure/resource_azure_virtual_network.go @@ -6,7 +6,6 @@ import ( "strings" "github.com/Azure/azure-sdk-for-go/management" - "github.com/Azure/azure-sdk-for-go/management/networksecuritygroup" "github.com/Azure/azure-sdk-for-go/management/virtualnetwork" "github.com/hashicorp/terraform/helper/hashcode" "github.com/hashicorp/terraform/helper/schema" @@ -78,6 +77,7 @@ func resourceAzureVirtualNetwork() *schema.Resource { func resourceAzureVirtualNetworkCreate(d *schema.ResourceData, meta interface{}) error { ac := meta.(*Client) mc := ac.mgmtClient + vnetClient := ac.vnetClient name := d.Get("name").(string) @@ -86,7 +86,7 @@ func resourceAzureVirtualNetworkCreate(d *schema.ResourceData, meta interface{}) ac.mutex.Lock() defer ac.mutex.Unlock() - nc, err := virtualnetwork.NewClient(mc).GetVirtualNetworkConfiguration() + nc, err := vnetClient.GetVirtualNetworkConfiguration() if err != nil { if strings.Contains(err.Error(), "ResourceNotFound") { nc = virtualnetwork.NetworkConfiguration{} @@ -104,7 +104,7 @@ func resourceAzureVirtualNetworkCreate(d *schema.ResourceData, meta interface{}) network := createVirtualNetwork(d) nc.Configuration.VirtualNetworkSites = append(nc.Configuration.VirtualNetworkSites, network) - req, err := virtualnetwork.NewClient(mc).SetVirtualNetworkConfiguration(nc) + req, err := vnetClient.SetVirtualNetworkConfiguration(nc) if err != nil { return fmt.Errorf("Error creating Virtual Network %s: %s", name, err) } @@ -124,9 +124,11 @@ func resourceAzureVirtualNetworkCreate(d *schema.ResourceData, meta interface{}) } func resourceAzureVirtualNetworkRead(d *schema.ResourceData, meta interface{}) error { - mc := meta.(*Client).mgmtClient + ac := meta.(*Client) + vnetClient := ac.vnetClient + secGroupClient := ac.secGroupClient - nc, err := virtualnetwork.NewClient(mc).GetVirtualNetworkConfiguration() + nc, err := vnetClient.GetVirtualNetworkConfiguration() if err != nil { return fmt.Errorf(virtualNetworkRetrievalError, err) } @@ -146,8 +148,7 @@ func resourceAzureVirtualNetworkRead(d *schema.ResourceData, meta interface{}) e subnet := map[string]interface{}{} // Get the associated (if any) security group - sg, err := networksecuritygroup.NewClient(mc). - GetNetworkSecurityGroupForSubnet(s.Name, d.Id()) + sg, err := secGroupClient.GetNetworkSecurityGroupForSubnet(s.Name, d.Id()) if err != nil && !management.IsResourceNotFoundError(err) { return fmt.Errorf( "Error retrieving Network Security Group associations of subnet %s: %s", s.Name, err) @@ -176,13 +177,14 @@ func resourceAzureVirtualNetworkRead(d *schema.ResourceData, meta interface{}) e func resourceAzureVirtualNetworkUpdate(d *schema.ResourceData, meta interface{}) error { ac := meta.(*Client) mc := ac.mgmtClient + vnetClient := ac.vnetClient // Lock the client just before we get the virtual network configuration and immediately // set an defer to unlock the client again whenever this function exits ac.mutex.Lock() defer ac.mutex.Unlock() - nc, err := virtualnetwork.NewClient(mc).GetVirtualNetworkConfiguration() + nc, err := vnetClient.GetVirtualNetworkConfiguration() if err != nil { return fmt.Errorf(virtualNetworkRetrievalError, err) } @@ -201,7 +203,7 @@ func resourceAzureVirtualNetworkUpdate(d *schema.ResourceData, meta interface{}) return fmt.Errorf("Virtual Network %s does not exists!", d.Id()) } - req, err := virtualnetwork.NewClient(mc).SetVirtualNetworkConfiguration(nc) + req, err := vnetClient.SetVirtualNetworkConfiguration(nc) if err != nil { return fmt.Errorf("Error updating Virtual Network %s: %s", d.Id(), err) } @@ -221,13 +223,14 @@ func resourceAzureVirtualNetworkUpdate(d *schema.ResourceData, meta interface{}) func resourceAzureVirtualNetworkDelete(d *schema.ResourceData, meta interface{}) error { ac := meta.(*Client) mc := ac.mgmtClient + vnetClient := ac.vnetClient // Lock the client just before we get the virtual network configuration and immediately // set an defer to unlock the client again whenever this function exits ac.mutex.Lock() defer ac.mutex.Unlock() - nc, err := virtualnetwork.NewClient(mc).GetVirtualNetworkConfiguration() + nc, err := vnetClient.GetVirtualNetworkConfiguration() if err != nil { return fmt.Errorf(virtualNetworkRetrievalError, err) } @@ -241,7 +244,7 @@ func resourceAzureVirtualNetworkDelete(d *schema.ResourceData, meta interface{}) nc.Configuration.VirtualNetworkSites = filtered - req, err := virtualnetwork.NewClient(mc).SetVirtualNetworkConfiguration(nc) + req, err := vnetClient.SetVirtualNetworkConfiguration(nc) if err != nil { return fmt.Errorf("Error deleting Virtual Network %s: %s", d.Id(), err) } @@ -301,8 +304,9 @@ func createVirtualNetwork(d *schema.ResourceData) virtualnetwork.VirtualNetworkS } func associateSecurityGroups(d *schema.ResourceData, meta interface{}) error { - mc := meta.(*Client).mgmtClient - nsgClient := networksecuritygroup.NewClient(mc) + azureClient := meta.(*Client) + mc := azureClient.mgmtClient + secGroupClient := azureClient.secGroupClient virtualNetwork := d.Get("name").(string) @@ -313,7 +317,7 @@ func associateSecurityGroups(d *schema.ResourceData, meta interface{}) error { subnetName := subnet["name"].(string) // Get the associated (if any) security group - sg, err := nsgClient.GetNetworkSecurityGroupForSubnet(subnetName, d.Id()) + sg, err := secGroupClient.GetNetworkSecurityGroupForSubnet(subnetName, d.Id()) if err != nil && !management.IsResourceNotFoundError(err) { return fmt.Errorf( "Error retrieving Network Security Group associations of subnet %s: %s", subnetName, err) @@ -326,7 +330,7 @@ func associateSecurityGroups(d *schema.ResourceData, meta interface{}) error { // If there is an associated security group, make sure we first remove it from the subnet if sg.Name != "" { - req, err := nsgClient.RemoveNetworkSecurityGroupFromSubnet(sg.Name, subnetName, virtualNetwork) + req, err := secGroupClient.RemoveNetworkSecurityGroupFromSubnet(sg.Name, subnetName, virtualNetwork) if err != nil { return fmt.Errorf("Error removing Network Security Group %s from subnet %s: %s", securityGroup, subnetName, err) @@ -342,7 +346,7 @@ func associateSecurityGroups(d *schema.ResourceData, meta interface{}) error { // If the desired security group is not empty, assign the security group to the subnet if securityGroup != "" { - req, err := nsgClient.AddNetworkSecurityToSubnet(securityGroup, subnetName, virtualNetwork) + req, err := secGroupClient.AddNetworkSecurityToSubnet(securityGroup, subnetName, virtualNetwork) if err != nil { return fmt.Errorf("Error associating Network Security Group %s to subnet %s: %s", securityGroup, subnetName, err) diff --git a/builtin/providers/azure/resource_azure_virtual_network_test.go b/builtin/providers/azure/resource_azure_virtual_network_test.go index 70327cad0..bb6b6c563 100644 --- a/builtin/providers/azure/resource_azure_virtual_network_test.go +++ b/builtin/providers/azure/resource_azure_virtual_network_test.go @@ -137,8 +137,8 @@ func testAccCheckAzureVirtualNetworkExists( return fmt.Errorf("No Virtual Network ID is set") } - mc := testAccProvider.Meta().(*Client).mgmtClient - nc, err := virtualnetwork.NewClient(mc).GetVirtualNetworkConfiguration() + vnetClient := testAccProvider.Meta().(*Client).vnetClient + nc, err := vnetClient.GetVirtualNetworkConfiguration() if err != nil { return err } @@ -172,7 +172,7 @@ func testAccCheckAzureVirtualNetworkAttributes( } func testAccCheckAzureVirtualNetworkDestroy(s *terraform.State) error { - mc := testAccProvider.Meta().(*Client).mgmtClient + vnetClient := testAccProvider.Meta().(*Client).vnetClient for _, rs := range s.RootModule().Resources { if rs.Type != "azure_virtual_network" { @@ -183,7 +183,7 @@ func testAccCheckAzureVirtualNetworkDestroy(s *terraform.State) error { return fmt.Errorf("No Virtual Network ID is set") } - nc, err := virtualnetwork.NewClient(mc).GetVirtualNetworkConfiguration() + nc, err := vnetClient.GetVirtualNetworkConfiguration() if err != nil { return fmt.Errorf("Error retrieving Virtual Network Configuration: %s", err) }