Azure RM Storage Queue:

Adds the schema, CRUD, acceptance tests and documentation for the
AzureRM storage Queue resource
This commit is contained in:
stack72 2016-01-27 12:27:58 +00:00
parent e470ffd0be
commit 5a5c32e7d2
6 changed files with 385 additions and 0 deletions

View File

@ -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
}

View File

@ -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,
}

View File

@ -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
}

View File

@ -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}"
}
`

View File

@ -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.

View File

@ -89,6 +89,10 @@
<a href="/docs/providers/azurerm/r/storage_blob.html">azurerm_storage_blob</a>
</li>
<li<%= sidebar_current("docs-azurerm-resource-storage-queue") %>>
<a href="/docs/providers/azurerm/r/storage_queue.html">azurerm_storage_queue</a>
</li>
</ul>
</li>