diff --git a/builtin/providers/azure/provider.go b/builtin/providers/azure/provider.go index dbb6eeb86..98a7b2a09 100644 --- a/builtin/providers/azure/provider.go +++ b/builtin/providers/azure/provider.go @@ -38,6 +38,7 @@ func Provider() terraform.ResourceProvider { "azure_storage_service": resourceAzureStorageService(), "azure_storage_container": resourceAzureStorageContainer(), "azure_storage_blob": resourceAzureStorageBlob(), + "azure_storage_queue": resourceAzureStorageQueue(), "azure_virtual_network": resourceAzureVirtualNetwork(), "azure_dns_server": resourceAzureDnsServer(), "azure_local_network_connection": resourceAzureLocalNetworkConnection(), diff --git a/builtin/providers/azure/resource_azure_storage_queue.go b/builtin/providers/azure/resource_azure_storage_queue.go new file mode 100644 index 000000000..db4edbb60 --- /dev/null +++ b/builtin/providers/azure/resource_azure_storage_queue.go @@ -0,0 +1,103 @@ +package azure + +import ( + "fmt" + "log" + + "github.com/hashicorp/terraform/helper/schema" +) + +// resourceAzureStorageQueue returns the *schema.Resource associated +// to a storage queue on Azure. +func resourceAzureStorageQueue() *schema.Resource { + return &schema.Resource{ + Create: resourceAzureStorageQueueCreate, + Read: resourceAzureStorageQueueRead, + Delete: resourceAzureStorageQueueDelete, + + Schema: map[string]*schema.Schema{ + "name": &schema.Schema{ + Type: schema.TypeString, + Required: true, + ForceNew: true, + Description: parameterDescriptions["name"], + }, + "storage_service_name": &schema.Schema{ + Type: schema.TypeString, + Required: true, + ForceNew: true, + Description: parameterDescriptions["storage_service_name"], + }, + }, + } +} + +// 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 + storServName := d.Get("storage_service_name").(string) + queueClient, err := getStorageServiceQueueClient(mgmtClient, storServName) + if err != nil { + return err + } + + // create the queue: + log.Println("Sending Storage Queue creation request to Azure.") + name := d.Get("name").(string) + err = queueClient.CreateQueue(name) + if err != nil { + return fmt.Errorf("Error creation Storage Queue on Azure: %s", err) + } + + d.SetId(name) + return nil +} + +// 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 + storServName := d.Get("storage_service_name").(string) + queueClient, err := getStorageServiceQueueClient(mgmtClient, storServName) + if err != nil { + return err + } + + // check for queue's existence: + log.Println("[INFO] Sending Storage Queue existence query to Azure.") + name := d.Get("name").(string) + exists, err := queueClient.QueueExists(name) + if err != nil { + return fmt.Errorf("Error checking for Storage Queue existence: %s", err) + } + + // If the queue has been deleted in the meantime; + // untrack the resource from the schema. + if !exists { + d.SetId("") + } + + return nil +} + +// 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 + storServName := d.Get("storage_service_name").(string) + queueClient, err := getStorageServiceQueueClient(mgmtClient, storServName) + if err != nil { + return err + } + + // issue the deletion of the storage queue: + log.Println("[INFO] Sending Storage Queue deletion request to Azure.") + name := d.Get("name").(string) + err = queueClient.DeleteQueue(name) + if err != nil { + return fmt.Errorf("Error deleting Storage queue off Azure: %s", err) + } + + return nil +} diff --git a/builtin/providers/azure/resource_azure_storage_queue_test.go b/builtin/providers/azure/resource_azure_storage_queue_test.go new file mode 100644 index 000000000..de3702591 --- /dev/null +++ b/builtin/providers/azure/resource_azure_storage_queue_test.go @@ -0,0 +1,93 @@ +package azure + +import ( + "fmt" + "testing" + + "github.com/hashicorp/terraform/helper/resource" + "github.com/hashicorp/terraform/terraform" +) + +func TestAccAzureStorageQueue(t *testing.T) { + name := "azure_storage_queue.foo" + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckAzureStorageQueueDeleted, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testAccAzureStorageQueueConfig, + Check: resource.ComposeTestCheckFunc( + testAccCheckAzureStorageQueueExists(name), + resource.TestCheckResourceAttr(name, "name", "terraform-queue"), + resource.TestCheckResourceAttr(name, "storage_service_name", testAccStorageServiceName), + ), + }, + }, + }) +} + +func testAccCheckAzureStorageQueueExists(name string) resource.TestCheckFunc { + return func(s *terraform.State) error { + resource, ok := s.RootModule().Resources[name] + if !ok { + return fmt.Errorf("Azure Storage Queue resource '%s' is missing.", name) + } + + if resource.Primary.ID == "" { + return fmt.Errorf("Azure Storage Service Queue ID %s is missing.", name) + } + + mgmtClient := testAccProvider.Meta().(*Client).mgmtClient + queueClient, err := getStorageServiceQueueClient(mgmtClient, testAccStorageServiceName) + if err != nil { + return err + } + + exists, err := queueClient.QueueExists(resource.Primary.ID) + if err != nil { + return fmt.Errorf("Error querying Azure for Storage Queue existence: %s", err) + } + if !exists { + return fmt.Errorf("Azure Storage Queue %s doesn't exist!", resource.Primary.ID) + } + + return nil + } +} + +func testAccCheckAzureStorageQueueDeleted(s *terraform.State) error { + for _, resource := range s.RootModule().Resources { + if resource.Type != "azure_storage_queue" { + continue + } + + if resource.Primary.ID == "" { + return fmt.Errorf("Azure Storage Service Queue ID %s is missing.", resource.Primary.ID) + } + + mgmtClient := testAccProvider.Meta().(*Client).mgmtClient + queueClient, err := getStorageServiceQueueClient(mgmtClient, testAccStorageServiceName) + if err != nil { + return err + } + + exists, err := queueClient.QueueExists(resource.Primary.ID) + if err != nil { + return fmt.Errorf("Error querying Azure for Storage Queue existence: %s", err) + } + if exists { + return fmt.Errorf("Azure Storage Queue %s still exists!", resource.Primary.ID) + } + } + + return nil +} + +var testAccAzureStorageQueueConfig = fmt.Sprintf(` +resource "azure_storage_queue" "foo" { + name = "terraform-queue" + storage_service_name = "%s" +} +`, testAccStorageServiceName) diff --git a/builtin/providers/azure/resource_azure_storage_service_helpers.go b/builtin/providers/azure/resource_azure_storage_service_helpers.go index 26cce502f..68c760920 100644 --- a/builtin/providers/azure/resource_azure_storage_service_helpers.go +++ b/builtin/providers/azure/resource_azure_storage_service_helpers.go @@ -2,30 +2,49 @@ package azure import ( "fmt" - "log" "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" ) -// getStorageServiceBlobClient is a helper function which returns the -// storage.BlobStorageClient associated to the given storage account name. -func getStorageServiceBlobClient(mgmtClient management.Client, serviceName string) (storage.BlobStorageClient, error) { - log.Println("[INFO] Begun generating Azure Storage Service Blob client.") - var blobClient storage.BlobStorageClient - +// 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 blobClient, fmt.Errorf("Error reading Storage Service %s's keys from Azure: %s", serviceName, err) + return storageClient, fmt.Errorf("Failed getting Storage Service keys for %s: %s", serviceName, err) } - storageClient, err := storage.NewBasicClient(serviceName, keys.PrimaryKey) + storageClient, err = storage.NewBasicClient(serviceName, keys.PrimaryKey) if err != nil { - return blobClient, fmt.Errorf("Error creating Storage Service Client for %s: %s", serviceName, err) + 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/website/source/docs/providers/azure/r/storage_queue.html.markdown b/website/source/docs/providers/azure/r/storage_queue.html.markdown new file mode 100644 index 000000000..8b2abc5d8 --- /dev/null +++ b/website/source/docs/providers/azure/r/storage_queue.html.markdown @@ -0,0 +1,36 @@ +--- +layout: "azure" +page_title: "Azure: azure_storage_queue" +sidebar_current: "docs-azure-storage-queue" +description: |- + Creates a new storage queue within a given storage service on Azure. +--- + +# azure\_storage\_queue + +Creates a new storage queue within a given storage service on Azure. + +## Example Usage + +``` +resource "azure_storage_queue" "stor-queue" { + name = "terraform-storage-queue" + storage_service_name = "tfstorserv" +} +```` + +## Argument Reference + +The following arguments are supported: + +* `name` - (Required) The name of the storage queue. Must be unique within + the storage service the queue is located. + +* `storage_service_name` - (Required) The name of the storage service within + which the storage queue should be created. + +## Attributes Reference + +The following attributes are exported: + +* `id` - The storage queue ID. Coincides with the given `name`. diff --git a/website/source/layouts/azure.erb b/website/source/layouts/azure.erb index 5970dab91..f01048285 100644 --- a/website/source/layouts/azure.erb +++ b/website/source/layouts/azure.erb @@ -49,6 +49,10 @@ azure_storage_container + > + azure_storage_queue + + > azure_storage_service