2016-01-27 13:27:58 +01:00
|
|
|
package azurerm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"regexp"
|
|
|
|
|
2017-06-01 11:18:22 +02:00
|
|
|
"github.com/Azure/azure-sdk-for-go/storage"
|
2016-01-27 13:27:58 +01:00
|
|
|
"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{
|
2016-06-01 22:17:21 +02:00
|
|
|
"name": {
|
2016-01-27 13:27:58 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
ValidateFunc: validateArmStorageQueueName,
|
|
|
|
},
|
2016-06-01 22:17:21 +02:00
|
|
|
"resource_group_name": {
|
2016-01-27 13:27:58 +01:00
|
|
|
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)
|
|
|
|
|
2016-05-25 00:01:26 +02:00
|
|
|
queueClient, accountExists, err := armClient.getQueueServiceClientForStorageAccount(resourceGroupName, storageAccountName)
|
2016-01-27 13:27:58 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-05-25 00:01:26 +02:00
|
|
|
if !accountExists {
|
|
|
|
return fmt.Errorf("Storage Account %q Not Found", storageAccountName)
|
|
|
|
}
|
2016-01-27 13:27:58 +01:00
|
|
|
|
|
|
|
name := d.Get("name").(string)
|
|
|
|
|
|
|
|
log.Printf("[INFO] Creating queue %q in storage account %q", name, storageAccountName)
|
2017-06-01 11:18:22 +02:00
|
|
|
queueReference := queueClient.GetQueueReference(name)
|
|
|
|
options := &storage.QueueServiceOptions{}
|
|
|
|
err = queueReference.Create(options)
|
2016-01-27 13:27:58 +01:00
|
|
|
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)
|
|
|
|
|
2016-05-25 00:01:26 +02:00
|
|
|
queueClient, accountExists, err := armClient.getQueueServiceClientForStorageAccount(resourceGroupName, storageAccountName)
|
2016-01-27 13:27:58 +01:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
2016-05-25 00:01:26 +02:00
|
|
|
if !accountExists {
|
|
|
|
log.Printf("[DEBUG] Storage account %q not found, removing queue %q from state", storageAccountName, d.Id())
|
|
|
|
d.SetId("")
|
|
|
|
return false, nil
|
|
|
|
}
|
2016-01-27 13:27:58 +01:00
|
|
|
|
|
|
|
name := d.Get("name").(string)
|
|
|
|
|
|
|
|
log.Printf("[INFO] Checking for existence of storage queue %q.", name)
|
2017-06-01 11:18:22 +02:00
|
|
|
queueReference := queueClient.GetQueueReference(name)
|
|
|
|
exists, err := queueReference.Exists()
|
2016-01-27 13:27:58 +01:00
|
|
|
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)
|
|
|
|
|
2016-05-25 00:01:26 +02:00
|
|
|
queueClient, accountExists, err := armClient.getQueueServiceClientForStorageAccount(resourceGroupName, storageAccountName)
|
2016-01-27 13:27:58 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-05-25 00:01:26 +02:00
|
|
|
if !accountExists {
|
|
|
|
log.Printf("[INFO]Storage Account %q doesn't exist so the blob won't exist", storageAccountName)
|
|
|
|
return nil
|
|
|
|
}
|
2016-01-27 13:27:58 +01:00
|
|
|
|
|
|
|
name := d.Get("name").(string)
|
|
|
|
|
|
|
|
log.Printf("[INFO] Deleting storage queue %q", name)
|
2017-06-01 11:18:22 +02:00
|
|
|
queueReference := queueClient.GetQueueReference(name)
|
|
|
|
options := &storage.QueueServiceOptions{}
|
|
|
|
if err = queueReference.Delete(options); err != nil {
|
2016-01-27 13:27:58 +01:00
|
|
|
return fmt.Errorf("Error deleting storage queue %q: %s", name, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
d.SetId("")
|
|
|
|
return nil
|
|
|
|
}
|