Skip to content

Commit

Permalink
r/storage_queue: switching to use SharedKey for auth
Browse files Browse the repository at this point in the history
  • Loading branch information
tombuildsstuff committed Aug 20, 2019
1 parent 746689a commit 3b7f0ae
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 15 deletions.
18 changes: 12 additions & 6 deletions azurerm/internal/services/storage/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
}
}

Expand Down Expand Up @@ -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 {
Expand Down
16 changes: 13 additions & 3 deletions azurerm/resource_arm_storage_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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) {
Expand Down
44 changes: 40 additions & 4 deletions azurerm/resource_arm_storage_queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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)
}

Expand All @@ -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)
Expand Down Expand Up @@ -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)
}

Expand Down
30 changes: 28 additions & 2 deletions azurerm/resource_arm_storage_queue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down

0 comments on commit 3b7f0ae

Please sign in to comment.