Skip to content

Commit

Permalink
Merge pull request #3832 from terraform-providers/f/storage-queue
Browse files Browse the repository at this point in the history
r/storage_queue: switching to use the new SDK
  • Loading branch information
tombuildsstuff authored Jul 12, 2019
2 parents 945ba31 + afb8ee1 commit 00daa48
Show file tree
Hide file tree
Showing 18 changed files with 972 additions and 175 deletions.
19 changes: 0 additions & 19 deletions azurerm/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -920,22 +920,3 @@ func (c *ArmClient) getTableServiceClientForStorageAccount(ctx context.Context,
tableClient := storageClient.GetTableService()
return &tableClient, true, nil
}

func (c *ArmClient) getQueueServiceClientForStorageAccount(ctx context.Context, resourceGroupName, storageAccountName string) (*mainStorage.QueueServiceClient, bool, error) {
key, accountExists, err := c.getKeyForStorageAccount(ctx, resourceGroupName, storageAccountName)
if err != nil {
return nil, accountExists, err
}
if !accountExists {
return nil, false, nil
}

storageClient, err := mainStorage.NewClient(storageAccountName, key, c.environment.StorageEndpointSuffix,
mainStorage.DefaultAPIVersion, true)
if err != nil {
return nil, true, fmt.Errorf("Error creating storage client for storage storeAccount %q: %s", storageAccountName, err)
}

queueClient := storageClient.GetQueueService()
return &queueClient, true, nil
}
12 changes: 12 additions & 0 deletions azurerm/internal/services/storage/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/authorizers"
"github.com/tombuildsstuff/giovanni/storage/2018-11-09/file/directories"
"github.com/tombuildsstuff/giovanni/storage/2018-11-09/file/shares"
"github.com/tombuildsstuff/giovanni/storage/2018-11-09/queue/queues"
"github.com/tombuildsstuff/giovanni/storage/2018-11-09/table/entities"
)

Expand Down Expand Up @@ -82,6 +83,17 @@ 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.New()
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
197 changes: 102 additions & 95 deletions azurerm/resource_arm_storage_queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,21 @@ package azurerm
import (
"fmt"
"log"
"net/url"
"regexp"
"strings"

"github.com/Azure/azure-sdk-for-go/storage"
"github.com/hashicorp/terraform/helper/schema"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/storage"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
"github.com/tombuildsstuff/giovanni/storage/2018-11-09/queue/queues"
)

func resourceArmStorageQueue() *schema.Resource {
return &schema.Resource{
Create: resourceArmStorageQueueCreate,
Read: resourceArmStorageQueueRead,
Update: resourceArmStorageQueueUpdate,
Delete: resourceArmStorageQueueDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
Expand All @@ -31,12 +32,19 @@ func resourceArmStorageQueue() *schema.Resource {
ForceNew: true,
ValidateFunc: validateArmStorageQueueName,
},
"resource_group_name": azure.SchemaResourceGroupName(),

"storage_account_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validateArmStorageAccountName,
},

"resource_group_name": azure.SchemaResourceGroupNameDeprecated(),

"metadata": storage.MetaDataSchema(),

// TODO: properties
},
}
}
Expand Down Expand Up @@ -71,157 +79,156 @@ func validateArmStorageQueueName(v interface{}, k string) (warnings []string, er
}

func resourceArmStorageQueueCreate(d *schema.ResourceData, meta interface{}) error {
armClient := meta.(*ArmClient)
ctx := armClient.StopContext
environment := armClient.environment
storageClient := meta.(*ArmClient).storage
ctx := meta.(*ArmClient).StopContext

name := d.Get("name").(string)
resourceGroupName := d.Get("resource_group_name").(string)
storageAccountName := d.Get("storage_account_name").(string)
queueName := d.Get("name").(string)
accountName := d.Get("storage_account_name").(string)

queueClient, accountExists, err := armClient.getQueueServiceClientForStorageAccount(ctx, resourceGroupName, storageAccountName)
metaDataRaw := d.Get("metadata").(map[string]interface{})
metaData := storage.ExpandMetaData(metaDataRaw)

resourceGroup, err := storageClient.FindResourceGroup(ctx, accountName)
if err != nil {
return err
return fmt.Errorf("Error locating Resource Group: %s", err)
}
if !accountExists {
return fmt.Errorf("Storage Account %q Not Found", storageAccountName)

client, err := storageClient.QueuesClient(ctx, *resourceGroup, accountName)
if err != nil {
return fmt.Errorf("Error building Queues Client: %s", err)
}

queueReference := queueClient.GetQueueReference(name)
id := fmt.Sprintf("https://%s.queue.%s/%s", storageAccountName, environment.StorageEndpointSuffix, name)
resourceID := client.GetResourceID(accountName, queueName)
if requireResourcesToBeImported {
exists, e := queueReference.Exists()
if e != nil {
return fmt.Errorf("Error checking if Queue %q exists (Account %q / Resource Group %q): %s", name, storageAccountName, resourceGroupName, e)
existing, err := client.GetMetaData(ctx, accountName, queueName)
if err != nil {
if !utils.ResponseWasNotFound(existing.Response) {
return fmt.Errorf("Error checking for presence of existing Queue %q (Storage Account %q / Resource Group %q): %s", queueName, accountName, *resourceGroup, err)
}
}

if exists {
return tf.ImportAsExistsError("azurerm_storage_queue", id)
if !utils.ResponseWasNotFound(existing.Response) {
return tf.ImportAsExistsError("azurerm_storage_queue", resourceID)
}
}

log.Printf("[INFO] Creating queue %q in storage account %q", name, storageAccountName)
options := &storage.QueueServiceOptions{}
err = queueReference.Create(options)
if err != nil {
return fmt.Errorf("Error creating storage queue on Azure: %s", err)
if _, err := client.Create(ctx, accountName, queueName, metaData); err != nil {
return fmt.Errorf("Error creating Queue %q (Account %q): %+v", queueName, accountName, err)
}

d.SetId(id)
d.SetId(resourceID)

return resourceArmStorageQueueRead(d, meta)
}

func resourceArmStorageQueueRead(d *schema.ResourceData, meta interface{}) error {
armClient := meta.(*ArmClient)
ctx := armClient.StopContext
func resourceArmStorageQueueUpdate(d *schema.ResourceData, meta interface{}) error {
storageClient := meta.(*ArmClient).storage
ctx := meta.(*ArmClient).StopContext

id, err := parseStorageQueueID(d.Id())
id, err := queues.ParseResourceID(d.Id())
if err != nil {
return err
}

resourceGroup, err := determineResourceGroupForStorageAccount(id.storageAccountName, armClient)
resourceGroup, err := storageClient.FindResourceGroup(ctx, id.AccountName)
if err != nil {
return err
return fmt.Errorf("Error locating Resource Group: %s", err)
}

if resourceGroup == nil {
log.Printf("[WARN] Unable to determine Resource Group for Storage Account %q (assuming removed) - removing from state", id.storageAccountName)
d.SetId("")
return nil
return fmt.Errorf("Error determine Resource Group for Storage Account %q: %s", id.AccountName, err)
}

queueClient, accountExists, err := armClient.getQueueServiceClientForStorageAccount(ctx, *resourceGroup, id.storageAccountName)
client, err := storageClient.QueuesClient(ctx, *resourceGroup, id.AccountName)
if err != nil {
return fmt.Errorf("Error building Queues Client: %s", err)
}

metaDataRaw := d.Get("metadata").(map[string]interface{})
metaData := storage.ExpandMetaData(metaDataRaw)

if _, err := client.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)
}

return resourceArmStorageQueueRead(d, meta)
}

func resourceArmStorageQueueRead(d *schema.ResourceData, meta interface{}) error {
storageClient := meta.(*ArmClient).storage
ctx := meta.(*ArmClient).StopContext

id, err := queues.ParseResourceID(d.Id())
if err != nil {
return err
}
if !accountExists {
log.Printf("[DEBUG] Storage account %q not found, removing queue %q from state", id.storageAccountName, id.queueName)

resourceGroup, err := storageClient.FindResourceGroup(ctx, id.AccountName)
if err != nil {
return fmt.Errorf("Error locating Resource Group: %s", err)
}

if resourceGroup == nil {
log.Printf("[WARN] Unable to determine Resource Group for Storage Account %q (assuming removed) - removing from state", id.AccountName)
d.SetId("")
return nil
}

log.Printf("[INFO] Checking for existence of storage queue %q.", id.queueName)
queueReference := queueClient.GetQueueReference(id.queueName)
exists, err := queueReference.Exists()
client, err := storageClient.QueuesClient(ctx, *resourceGroup, id.AccountName)
if err != nil {
return fmt.Errorf("error checking if storage queue %q exists: %s", id.queueName, err)
return fmt.Errorf("Error building Queues Client: %s", err)
}

if !exists {
log.Printf("[INFO] Storage queue %q no longer exists, removing from state...", id.queueName)
d.SetId("")
metaData, err := client.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)
d.SetId("")
return nil
}

return nil
}

d.Set("name", id.queueName)
d.Set("storage_account_name", id.storageAccountName)
d.Set("name", id.QueueName)
d.Set("storage_account_name", id.AccountName)
d.Set("resource_group_name", *resourceGroup)

if err := d.Set("metadata", storage.FlattenMetaData(metaData.MetaData)); err != nil {
return fmt.Errorf("Error setting `metadata`: %s", err)
}

return nil
}

func resourceArmStorageQueueDelete(d *schema.ResourceData, meta interface{}) error {
armClient := meta.(*ArmClient)
ctx := armClient.StopContext
storageClient := meta.(*ArmClient).storage
ctx := meta.(*ArmClient).StopContext

id, err := parseStorageQueueID(d.Id())
id, err := queues.ParseResourceID(d.Id())
if err != nil {
return err
}

resourceGroup, err := determineResourceGroupForStorageAccount(id.storageAccountName, armClient)
resourceGroup, err := storageClient.FindResourceGroup(ctx, id.AccountName)
if err != nil {
return err
return fmt.Errorf("Error locating Resource Group: %s", err)
}

if resourceGroup == nil {
log.Printf("[WARN] Unable to determine Resource Group for Storage Account %q (assuming removed) - removing from state", id.storageAccountName)
log.Printf("[WARN] Unable to determine Resource Group for Storage Account %q (assuming removed) - removing from state", id.AccountName)
d.SetId("")
return nil
}

queueClient, accountExists, err := armClient.getQueueServiceClientForStorageAccount(ctx, *resourceGroup, id.storageAccountName)
client, err := storageClient.QueuesClient(ctx, *resourceGroup, id.AccountName)
if err != nil {
return err
}
if !accountExists {
log.Printf("[INFO]Storage Account %q doesn't exist so the blob won't exist", id.storageAccountName)
return nil
return fmt.Errorf("Error building Queues Client: %s", err)
}

log.Printf("[INFO] Deleting storage queue %q", id.queueName)
queueReference := queueClient.GetQueueReference(id.queueName)
options := &storage.QueueServiceOptions{}
if err = queueReference.Delete(options); err != nil {
return fmt.Errorf("Error deleting storage queue %q: %s", id.queueName, err)
if _, err := client.Delete(ctx, id.AccountName, id.QueueName); err != nil {
return fmt.Errorf("Error deleting Storage Queue %q: %s", id.QueueName, err)
}

return nil
}

type storageQueueId struct {
storageAccountName string
queueName string
}

func parseStorageQueueID(input string) (*storageQueueId, error) {
// https://myaccount.queue.core.windows.net/myqueue
uri, err := url.Parse(input)
if err != nil {
return nil, fmt.Errorf("Error parsing %q as a URI: %+v", input, err)
}

segments := strings.Split(uri.Host, ".")
if len(segments) > 0 {
storageAccountName := segments[0]
// remove the leading `/`
queue := strings.TrimPrefix(uri.Path, "/")
id := storageQueueId{
storageAccountName: storageAccountName,
queueName: queue,
}
return &id, nil
}

return nil, nil
}
Loading

0 comments on commit 00daa48

Please sign in to comment.