From 5a5c32e7d247d07503b4cdb4ea53e603615d1e02 Mon Sep 17 00:00:00 2001 From: stack72 Date: Wed, 27 Jan 2016 12:27:58 +0000 Subject: [PATCH] Azure RM Storage Queue: Adds the schema, CRUD, acceptance tests and documentation for the AzureRM storage Queue resource --- builtin/providers/azurerm/config.go | 14 ++ builtin/providers/azurerm/provider.go | 1 + .../azurerm/resource_arm_storage_queue.go | 153 +++++++++++++++++ .../resource_arm_storage_queue_test.go | 162 ++++++++++++++++++ .../azurerm/r/storage_queue.html.markdown | 51 ++++++ website/source/layouts/azurerm.erb | 4 + 6 files changed, 385 insertions(+) create mode 100644 builtin/providers/azurerm/resource_arm_storage_queue.go create mode 100644 builtin/providers/azurerm/resource_arm_storage_queue_test.go create mode 100644 website/source/docs/providers/azurerm/r/storage_queue.html.markdown diff --git a/builtin/providers/azurerm/config.go b/builtin/providers/azurerm/config.go index 5136a22b01..cbc3784b28 100644 --- a/builtin/providers/azurerm/config.go +++ b/builtin/providers/azurerm/config.go @@ -310,3 +310,17 @@ func (armClient *ArmClient) getBlobStorageClientForStorageAccount(resourceGroupN blobClient := storageClient.GetBlobService() return &blobClient, nil } +func (armClient *ArmClient) getQueueServiceClientForStorageAccount(resourceGroupName, storageAccountName string) (*mainStorage.QueueServiceClient, error) { + key, err := armClient.getKeyForStorageAccount(resourceGroupName, storageAccountName) + if err != nil { + return nil, err + } + + storageClient, err := mainStorage.NewBasicClient(storageAccountName, key) + if err != nil { + return nil, fmt.Errorf("Error creating storage client for storage account %q: %s", storageAccountName, err) + } + + queueClient := storageClient.GetQueueService() + return &queueClient, nil +} diff --git a/builtin/providers/azurerm/provider.go b/builtin/providers/azurerm/provider.go index 4ff5e7f3fc..fd92f1655d 100644 --- a/builtin/providers/azurerm/provider.go +++ b/builtin/providers/azurerm/provider.go @@ -58,6 +58,7 @@ func Provider() terraform.ResourceProvider { "azurerm_storage_account": resourceArmStorageAccount(), "azurerm_storage_container": resourceArmStorageContainer(), "azurerm_storage_blob": resourceArmStorageBlob(), + "azurerm_storage_queue": resourceArmStorageQueue(), }, ConfigureFunc: providerConfigure, } diff --git a/builtin/providers/azurerm/resource_arm_storage_queue.go b/builtin/providers/azurerm/resource_arm_storage_queue.go new file mode 100644 index 0000000000..9488395bc1 --- /dev/null +++ b/builtin/providers/azurerm/resource_arm_storage_queue.go @@ -0,0 +1,153 @@ +package azurerm + +import ( + "fmt" + "log" + "regexp" + + "github.com/hashicorp/terraform/helper/schema" +) + +func resourceArmStorageQueue() *schema.Resource { + return &schema.Resource{ + Create: resourceArmStorageQueueCreate, + Read: resourceArmStorageQueueRead, + Exists: resourceArmStorageQueueExists, + Delete: resourceArmStorageQueueDelete, + + Schema: map[string]*schema.Schema{ + "name": &schema.Schema{ + Type: schema.TypeString, + Required: true, + ForceNew: true, + ValidateFunc: validateArmStorageQueueName, + }, + "resource_group_name": &schema.Schema{ + Type: schema.TypeString, + Required: true, + ForceNew: true, + }, + "storage_account_name": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + }, + }, + } +} + +func validateArmStorageQueueName(v interface{}, k string) (ws []string, errors []error) { + value := v.(string) + + if !regexp.MustCompile(`^[a-z0-9-]+$`).MatchString(value) { + errors = append(errors, fmt.Errorf( + "only lowercase alphanumeric characters and hyphens allowed in %q", k)) + } + + if regexp.MustCompile(`^-`).MatchString(value) { + errors = append(errors, fmt.Errorf("%q cannot start with a hyphen", k)) + } + + if regexp.MustCompile(`-$`).MatchString(value) { + errors = append(errors, fmt.Errorf("%q cannot end with a hyphen", k)) + } + + if len(value) > 63 { + errors = append(errors, fmt.Errorf( + "%q cannot be longer than 63 characters", k)) + } + + if len(value) < 3 { + errors = append(errors, fmt.Errorf( + "%q must be at least 3 characters", k)) + } + + return +} + +func resourceArmStorageQueueCreate(d *schema.ResourceData, meta interface{}) error { + armClient := meta.(*ArmClient) + + resourceGroupName := d.Get("resource_group_name").(string) + storageAccountName := d.Get("storage_account_name").(string) + + queueClient, err := armClient.getQueueServiceClientForStorageAccount(resourceGroupName, storageAccountName) + if err != nil { + return err + } + + name := d.Get("name").(string) + + log.Printf("[INFO] Creating queue %q in storage account %q", name, storageAccountName) + err = queueClient.CreateQueue(name) + if err != nil { + return fmt.Errorf("Error creating storage queue on Azure: %s", err) + } + + d.SetId(name) + return resourceArmStorageQueueRead(d, meta) +} + +func resourceArmStorageQueueRead(d *schema.ResourceData, meta interface{}) error { + + exists, err := resourceArmStorageQueueExists(d, meta) + if err != nil { + return err + } + + if !exists { + // Exists already removed this from state + return nil + } + + return nil +} + +func resourceArmStorageQueueExists(d *schema.ResourceData, meta interface{}) (bool, error) { + armClient := meta.(*ArmClient) + + resourceGroupName := d.Get("resource_group_name").(string) + storageAccountName := d.Get("storage_account_name").(string) + + queueClient, err := armClient.getQueueServiceClientForStorageAccount(resourceGroupName, storageAccountName) + if err != nil { + return false, err + } + + name := d.Get("name").(string) + + log.Printf("[INFO] Checking for existence of storage queue %q.", name) + exists, err := queueClient.QueueExists(name) + if err != nil { + return false, fmt.Errorf("error testing existence of storage queue %q: %s", name, err) + } + + if !exists { + log.Printf("[INFO] Storage queue %q no longer exists, removing from state...", name) + d.SetId("") + } + + return exists, nil +} + +func resourceArmStorageQueueDelete(d *schema.ResourceData, meta interface{}) error { + armClient := meta.(*ArmClient) + + resourceGroupName := d.Get("resource_group_name").(string) + storageAccountName := d.Get("storage_account_name").(string) + + queueClient, err := armClient.getQueueServiceClientForStorageAccount(resourceGroupName, storageAccountName) + if err != nil { + return err + } + + name := d.Get("name").(string) + + log.Printf("[INFO] Deleting storage queue %q", name) + if err = queueClient.DeleteQueue(name); err != nil { + return fmt.Errorf("Error deleting storage queue %q: %s", name, err) + } + + d.SetId("") + return nil +} diff --git a/builtin/providers/azurerm/resource_arm_storage_queue_test.go b/builtin/providers/azurerm/resource_arm_storage_queue_test.go new file mode 100644 index 0000000000..cba82de61c --- /dev/null +++ b/builtin/providers/azurerm/resource_arm_storage_queue_test.go @@ -0,0 +1,162 @@ +package azurerm + +import ( + "fmt" + "testing" + + "strings" + + "github.com/hashicorp/terraform/helper/acctest" + "github.com/hashicorp/terraform/helper/resource" + "github.com/hashicorp/terraform/terraform" +) + +func TestResourceAzureRMStorageQueueName_Validation(t *testing.T) { + cases := []struct { + Value string + ErrCount int + }{ + { + Value: "testing_123", + ErrCount: 1, + }, + { + Value: "testing123-", + ErrCount: 1, + }, + { + Value: "-testing123", + ErrCount: 1, + }, + { + Value: "TestingSG", + ErrCount: 1, + }, + { + Value: acctest.RandString(256), + ErrCount: 1, + }, + { + Value: acctest.RandString(1), + ErrCount: 1, + }, + } + + for _, tc := range cases { + _, errors := validateArmStorageQueueName(tc.Value, "azurerm_storage_queue") + + if len(errors) != tc.ErrCount { + t.Fatalf("Expected the ARM Storage Queue Name to trigger a validation error") + } + } +} + +func TestAccAzureRMStorageQueue_basic(t *testing.T) { + ri := acctest.RandInt() + rs := strings.ToLower(acctest.RandString(11)) + config := fmt.Sprintf(testAccAzureRMStorageQueue_basic, ri, rs, ri) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testCheckAzureRMStorageQueueDestroy, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: config, + Check: resource.ComposeTestCheckFunc( + testCheckAzureRMStorageQueueExists("azurerm_storage_queue.test"), + ), + }, + }, + }) +} + +func testCheckAzureRMStorageQueueExists(name string) resource.TestCheckFunc { + return func(s *terraform.State) error { + + rs, ok := s.RootModule().Resources[name] + if !ok { + return fmt.Errorf("Not found: %s", name) + } + + name := rs.Primary.Attributes["name"] + storageAccountName := rs.Primary.Attributes["storage_account_name"] + resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"] + if !hasResourceGroup { + return fmt.Errorf("Bad: no resource group found in state for storage queue: %s", name) + } + + armClient := testAccProvider.Meta().(*ArmClient) + queueClient, err := armClient.getQueueServiceClientForStorageAccount(resourceGroup, storageAccountName) + if err != nil { + return err + } + + exists, err := queueClient.QueueExists(name) + if err != nil { + return err + } + + if !exists { + return fmt.Errorf("Bad: Storage Queue %q (storage account: %q) does not exist", name, storageAccountName) + } + + return nil + } +} + +func testCheckAzureRMStorageQueueDestroy(s *terraform.State) error { + for _, rs := range s.RootModule().Resources { + if rs.Type != "azurerm_storage_queue" { + continue + } + + name := rs.Primary.Attributes["name"] + storageAccountName := rs.Primary.Attributes["storage_account_name"] + resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"] + if !hasResourceGroup { + return fmt.Errorf("Bad: no resource group found in state for storage queue: %s", name) + } + + armClient := testAccProvider.Meta().(*ArmClient) + queueClient, err := armClient.getQueueServiceClientForStorageAccount(resourceGroup, storageAccountName) + if err != nil { + return nil + } + + exists, err := queueClient.QueueExists(name) + if err != nil { + return nil + } + + if exists { + return fmt.Errorf("Bad: Storage Queue %q (storage account: %q) still exists", name, storageAccountName) + } + } + + return nil +} + +var testAccAzureRMStorageQueue_basic = ` +resource "azurerm_resource_group" "test" { + name = "acctestrg-%d" + location = "westus" +} + +resource "azurerm_storage_account" "test" { + name = "acctestacc%s" + resource_group_name = "${azurerm_resource_group.test.name}" + location = "westus" + account_type = "Standard_LRS" + + tags { + environment = "staging" + } +} + +resource "azurerm_storage_queue" "test" { + name = "mysamplequeue-%d" + resource_group_name = "${azurerm_resource_group.test.name}" + storage_account_name = "${azurerm_storage_account.test.name}" +} +` diff --git a/website/source/docs/providers/azurerm/r/storage_queue.html.markdown b/website/source/docs/providers/azurerm/r/storage_queue.html.markdown new file mode 100644 index 0000000000..d4e6bdd7c8 --- /dev/null +++ b/website/source/docs/providers/azurerm/r/storage_queue.html.markdown @@ -0,0 +1,51 @@ +--- +layout: "azurerm" +page_title: "Azure Resource Manager: azurerm_storage_queue" +sidebar_current: "docs-azurerm-resource-storage-queue" +description: |- + Create a Azure Storage Queue. +--- + +# azurerm\_storage\_queue + +Create an Azure Storage Queue. + +## Example Usage + +``` +resource "azurerm_resource_group" "test" { + name = "acctestrg-%d" + location = "westus" +} + +resource "azurerm_storage_account" "test" { + name = "acctestacc%s" + resource_group_name = "${azurerm_resource_group.test.name}" + location = "westus" + account_type = "Standard_LRS" +} + +resource "azurerm_storage_queue" "test" { + name = "mysamplequeue" + resource_group_name = "${azurerm_resource_group.test.name}" + storage_account_name = "${azurerm_storage_account.test.name}" +} +``` + +## Argument Reference + +The following arguments are supported: + +* `name` - (Required) The name of the storage queue. Must be unique within the storage account the queue is located. + +* `resource_group_name` - (Required) The name of the resource group in which to + create the storage queue. Changing this forces a new resource to be created. + +* `storage_account_name` - (Required) Specifies the storage account in which to create the storage queue. + Changing this forces a new resource to be created. + +## Attributes Reference + +The following attributes are exported in addition to the arguments listed above: + +* `id` - The storage queue Resource ID. diff --git a/website/source/layouts/azurerm.erb b/website/source/layouts/azurerm.erb index b1b9d0d9ee..73e42e537c 100644 --- a/website/source/layouts/azurerm.erb +++ b/website/source/layouts/azurerm.erb @@ -89,6 +89,10 @@ azurerm_storage_blob + > + azurerm_storage_queue + +