From 3b7f0ae1df3598e873fc913472367c44d8fbccbf Mon Sep 17 00:00:00 2001 From: tombuildsstuff Date: Tue, 20 Aug 2019 07:53:21 +0200 Subject: [PATCH] r/storage_queue: switching to use SharedKey for auth --- azurerm/internal/services/storage/client.go | 18 ++++++--- azurerm/resource_arm_storage_account.go | 16 ++++++-- azurerm/resource_arm_storage_queue.go | 44 +++++++++++++++++++-- azurerm/resource_arm_storage_queue_test.go | 30 +++++++++++++- 4 files changed, 93 insertions(+), 15 deletions(-) diff --git a/azurerm/internal/services/storage/client.go b/azurerm/internal/services/storage/client.go index f8cbb49de990..60d3c609712b 100644 --- a/azurerm/internal/services/storage/client.go +++ b/azurerm/internal/services/storage/client.go @@ -20,7 +20,6 @@ import ( ) type Client struct { - QueuesClient queues.Client // this is currently unexported since we only use it to look up the account key // we could export/use this in the future - but there's no point it being public // until that time @@ -31,16 +30,11 @@ type Client struct { // NOTE: this temporarily diverges from the other clients until we move this client in here // once we have this, can take an Options like everything else func BuildClient(accountsClient storage.AccountsClient, options *common.ClientOptions) *Client { - queuesClient := queues.New() - options.ConfigureClient(&queuesClient.Client, options.StorageAuthorizer) - // TODO: switch Storage Containers to using the storage.BlobContainersClient // (which should fix #2977) when the storage clients have been moved in here return &Client{ accountsClient: accountsClient, environment: options.Environment, - - QueuesClient: queuesClient, } } @@ -122,6 +116,18 @@ func (client Client) FileSharesClient(ctx context.Context, resourceGroup, accoun return &directoriesClient, nil } +func (client Client) QueuesClient(ctx context.Context, resourceGroup, accountName string) (*queues.Client, error) { + accountKey, err := client.findAccountKey(ctx, resourceGroup, accountName) + if err != nil { + return nil, fmt.Errorf("Error retrieving Account Key: %s", err) + } + + storageAuth := authorizers.NewSharedKeyLiteAuthorizer(accountName, *accountKey) + queuesClient := queues.NewWithEnvironment(client.environment) + queuesClient.Client.Authorizer = storageAuth + return &queuesClient, nil +} + func (client Client) TableEntityClient(ctx context.Context, resourceGroup, accountName string) (*entities.Client, error) { accountKey, err := client.findAccountKey(ctx, resourceGroup, accountName) if err != nil { diff --git a/azurerm/resource_arm_storage_account.go b/azurerm/resource_arm_storage_account.go index a197cd5a2d75..20219198d585 100644 --- a/azurerm/resource_arm_storage_account.go +++ b/azurerm/resource_arm_storage_account.go @@ -734,7 +734,10 @@ func resourceArmStorageAccountCreate(d *schema.ResourceData, meta interface{}) e } if val, ok := d.GetOk("queue_properties"); ok { - queueClient := meta.(*ArmClient).storage.QueuesClient + queueClient, err := meta.(*ArmClient).storage.QueuesClient(ctx, resourceGroupName, storageAccountName) + if err != nil { + return fmt.Errorf("Error building Queues Client: %s", err) + } queueProperties, err := expandQueueProperties(val.([]interface{})) if err != nil { @@ -925,7 +928,10 @@ func resourceArmStorageAccountUpdate(d *schema.ResourceData, meta interface{}) e } if d.HasChange("queue_properties") { - queueClient := meta.(*ArmClient).storage.QueuesClient + queueClient, err := meta.(*ArmClient).storage.QueuesClient(ctx, resourceGroupName, storageAccountName) + if err != nil { + return fmt.Errorf("Error building Queues Client: %s", err) + } queueProperties, err := expandQueueProperties(d.Get("queue_properties").([]interface{})) if err != nil { @@ -1071,7 +1077,11 @@ func resourceArmStorageAccountRead(d *schema.ResourceData, meta interface{}) err } } - queueClient := meta.(*ArmClient).storage.QueuesClient + queueClient, err := meta.(*ArmClient).storage.QueuesClient(ctx, resGroup, name) + if err != nil { + return fmt.Errorf("Error building Queues Client: %s", err) + } + queueProps, err := queueClient.GetServiceProperties(ctx, name) if err != nil { if queueProps.Response.Response != nil && !utils.ResponseWasNotFound(queueProps.Response) { diff --git a/azurerm/resource_arm_storage_queue.go b/azurerm/resource_arm_storage_queue.go index ba3b46082e7d..48f413bfad78 100644 --- a/azurerm/resource_arm_storage_queue.go +++ b/azurerm/resource_arm_storage_queue.go @@ -77,7 +77,7 @@ func validateArmStorageQueueName(v interface{}, k string) (warnings []string, er } func resourceArmStorageQueueCreate(d *schema.ResourceData, meta interface{}) error { - queueClient := meta.(*ArmClient).storage.QueuesClient + storageClient := meta.(*ArmClient).storage ctx := meta.(*ArmClient).StopContext queueName := d.Get("name").(string) @@ -86,6 +86,19 @@ func resourceArmStorageQueueCreate(d *schema.ResourceData, meta interface{}) err metaDataRaw := d.Get("metadata").(map[string]interface{}) metaData := storage.ExpandMetaData(metaDataRaw) + resourceGroup, err := storageClient.FindResourceGroup(ctx, accountName) + if err != nil { + return fmt.Errorf("Error locating Resource Group for Storage Queue %q (Account %s): %s", queueName, accountName, err) + } + if resourceGroup == nil { + return fmt.Errorf("Unable to locate Resource Group for Storage Queue %q (Account %s) - assuming removed & removing from state", queueName, accountName) + } + + queueClient, err := storageClient.QueuesClient(ctx, *resourceGroup, accountName) + if err != nil { + return fmt.Errorf("Error building Queues Client: %s", err) + } + resourceID := queueClient.GetResourceID(accountName, queueName) if requireResourcesToBeImported { existing, err := queueClient.GetMetaData(ctx, accountName, queueName) @@ -121,7 +134,20 @@ func resourceArmStorageQueueUpdate(d *schema.ResourceData, meta interface{}) err metaDataRaw := d.Get("metadata").(map[string]interface{}) metaData := storage.ExpandMetaData(metaDataRaw) - if _, err := storageClient.QueuesClient.SetMetaData(ctx, id.AccountName, id.QueueName, metaData); err != nil { + resourceGroup, err := storageClient.FindResourceGroup(ctx, id.AccountName) + if err != nil { + return fmt.Errorf("Error locating Resource Group for Storage Queue %q (Account %s): %s", id.QueueName, id.AccountName, err) + } + if resourceGroup == nil { + return fmt.Errorf("Unable to locate Resource Group for Storage Queue %q (Account %s) - assuming removed & removing from state", id.QueueName, id.AccountName) + } + + queuesClient, err := storageClient.QueuesClient(ctx, *resourceGroup, id.AccountName) + if err != nil { + return fmt.Errorf("Error building Queues Client: %s", err) + } + + if _, err := queuesClient.SetMetaData(ctx, id.AccountName, id.QueueName, metaData); err != nil { return fmt.Errorf("Error setting MetaData for Queue %q (Storage Account %q): %s", id.QueueName, id.AccountName, err) } @@ -147,7 +173,12 @@ func resourceArmStorageQueueRead(d *schema.ResourceData, meta interface{}) error return nil } - metaData, err := storageClient.QueuesClient.GetMetaData(ctx, id.AccountName, id.QueueName) + queuesClient, err := storageClient.QueuesClient(ctx, *resourceGroup, id.AccountName) + if err != nil { + return fmt.Errorf("Error building Queues Client: %s", err) + } + + metaData, err := queuesClient.GetMetaData(ctx, id.AccountName, id.QueueName) if err != nil { if utils.ResponseWasNotFound(metaData.Response) { log.Printf("[INFO] Storage Queue %q no longer exists, removing from state...", id.QueueName) @@ -188,7 +219,12 @@ func resourceArmStorageQueueDelete(d *schema.ResourceData, meta interface{}) err return nil } - if _, err := storageClient.QueuesClient.Delete(ctx, id.AccountName, id.QueueName); err != nil { + queuesClient, err := storageClient.QueuesClient(ctx, *resourceGroup, id.AccountName) + if err != nil { + return fmt.Errorf("Error building Queues Client: %s", err) + } + + if _, err := queuesClient.Delete(ctx, id.AccountName, id.QueueName); err != nil { return fmt.Errorf("Error deleting Storage Queue %q: %s", id.QueueName, err) } diff --git a/azurerm/resource_arm_storage_queue_test.go b/azurerm/resource_arm_storage_queue_test.go index ae3a15f48bcc..2000d98e8560 100644 --- a/azurerm/resource_arm_storage_queue_test.go +++ b/azurerm/resource_arm_storage_queue_test.go @@ -156,8 +156,21 @@ func testCheckAzureRMStorageQueueExists(resourceName string) resource.TestCheckF name := rs.Primary.Attributes["name"] accountName := rs.Primary.Attributes["storage_account_name"] - queueClient := testAccProvider.Meta().(*ArmClient).storage.QueuesClient ctx := testAccProvider.Meta().(*ArmClient).StopContext + storageClient := testAccProvider.Meta().(*ArmClient).storage + + resourceGroup, err := storageClient.FindResourceGroup(ctx, accountName) + if err != nil { + return fmt.Errorf("Error locating Resource Group for Storage Queue %q (Account %s): %s", name, accountName, err) + } + if resourceGroup == nil { + return fmt.Errorf("Unable to locate Resource Group for Storage Queue %q (Account %s) - assuming removed", name, accountName) + } + + queueClient, err := storageClient.QueuesClient(ctx, *resourceGroup, accountName) + if err != nil { + return fmt.Errorf("Error building Queues Client: %s", err) + } metaData, err := queueClient.GetMetaData(ctx, accountName, name) if err != nil { @@ -181,8 +194,21 @@ func testCheckAzureRMStorageQueueDestroy(s *terraform.State) error { name := rs.Primary.Attributes["name"] accountName := rs.Primary.Attributes["storage_account_name"] - queueClient := testAccProvider.Meta().(*ArmClient).storage.QueuesClient ctx := testAccProvider.Meta().(*ArmClient).StopContext + storageClient := testAccProvider.Meta().(*ArmClient).storage + + resourceGroup, err := storageClient.FindResourceGroup(ctx, accountName) + if err != nil { + return fmt.Errorf("Error locating Resource Group for Storage Queue %q (Account %s): %s", name, accountName, err) + } + if resourceGroup == nil { + return fmt.Errorf("Unable to locate Resource Group for Storage Queue %q (Account %s) - assuming removed", name, accountName) + } + + queueClient, err := storageClient.QueuesClient(ctx, *resourceGroup, accountName) + if err != nil { + return fmt.Errorf("Error building Queues Client: %s", err) + } metaData, err := queueClient.GetMetaData(ctx, accountName, name) if err != nil {