github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/azurerm/resource_arm_storage_queue.go (about)

     1  package azurerm
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"regexp"
     7  
     8  	"github.com/hashicorp/terraform/helper/schema"
     9  )
    10  
    11  func resourceArmStorageQueue() *schema.Resource {
    12  	return &schema.Resource{
    13  		Create: resourceArmStorageQueueCreate,
    14  		Read:   resourceArmStorageQueueRead,
    15  		Exists: resourceArmStorageQueueExists,
    16  		Delete: resourceArmStorageQueueDelete,
    17  
    18  		Schema: map[string]*schema.Schema{
    19  			"name": {
    20  				Type:         schema.TypeString,
    21  				Required:     true,
    22  				ForceNew:     true,
    23  				ValidateFunc: validateArmStorageQueueName,
    24  			},
    25  			"resource_group_name": {
    26  				Type:     schema.TypeString,
    27  				Required: true,
    28  				ForceNew: true,
    29  			},
    30  			"storage_account_name": {
    31  				Type:     schema.TypeString,
    32  				Required: true,
    33  				ForceNew: true,
    34  			},
    35  		},
    36  	}
    37  }
    38  
    39  func validateArmStorageQueueName(v interface{}, k string) (ws []string, errors []error) {
    40  	value := v.(string)
    41  
    42  	if !regexp.MustCompile(`^[a-z0-9-]+$`).MatchString(value) {
    43  		errors = append(errors, fmt.Errorf(
    44  			"only lowercase alphanumeric characters and hyphens allowed in %q", k))
    45  	}
    46  
    47  	if regexp.MustCompile(`^-`).MatchString(value) {
    48  		errors = append(errors, fmt.Errorf("%q cannot start with a hyphen", k))
    49  	}
    50  
    51  	if regexp.MustCompile(`-$`).MatchString(value) {
    52  		errors = append(errors, fmt.Errorf("%q cannot end with a hyphen", k))
    53  	}
    54  
    55  	if len(value) > 63 {
    56  		errors = append(errors, fmt.Errorf(
    57  			"%q cannot be longer than 63 characters", k))
    58  	}
    59  
    60  	if len(value) < 3 {
    61  		errors = append(errors, fmt.Errorf(
    62  			"%q must be at least 3 characters", k))
    63  	}
    64  
    65  	return
    66  }
    67  
    68  func resourceArmStorageQueueCreate(d *schema.ResourceData, meta interface{}) error {
    69  	armClient := meta.(*ArmClient)
    70  
    71  	resourceGroupName := d.Get("resource_group_name").(string)
    72  	storageAccountName := d.Get("storage_account_name").(string)
    73  
    74  	queueClient, accountExists, err := armClient.getQueueServiceClientForStorageAccount(resourceGroupName, storageAccountName)
    75  	if err != nil {
    76  		return err
    77  	}
    78  	if !accountExists {
    79  		return fmt.Errorf("Storage Account %q Not Found", storageAccountName)
    80  	}
    81  
    82  	name := d.Get("name").(string)
    83  
    84  	log.Printf("[INFO] Creating queue %q in storage account %q", name, storageAccountName)
    85  	err = queueClient.CreateQueue(name)
    86  	if err != nil {
    87  		return fmt.Errorf("Error creating storage queue on Azure: %s", err)
    88  	}
    89  
    90  	d.SetId(name)
    91  	return resourceArmStorageQueueRead(d, meta)
    92  }
    93  
    94  func resourceArmStorageQueueRead(d *schema.ResourceData, meta interface{}) error {
    95  
    96  	exists, err := resourceArmStorageQueueExists(d, meta)
    97  	if err != nil {
    98  		return err
    99  	}
   100  
   101  	if !exists {
   102  		// Exists already removed this from state
   103  		return nil
   104  	}
   105  
   106  	return nil
   107  }
   108  
   109  func resourceArmStorageQueueExists(d *schema.ResourceData, meta interface{}) (bool, error) {
   110  	armClient := meta.(*ArmClient)
   111  
   112  	resourceGroupName := d.Get("resource_group_name").(string)
   113  	storageAccountName := d.Get("storage_account_name").(string)
   114  
   115  	queueClient, accountExists, err := armClient.getQueueServiceClientForStorageAccount(resourceGroupName, storageAccountName)
   116  	if err != nil {
   117  		return false, err
   118  	}
   119  	if !accountExists {
   120  		log.Printf("[DEBUG] Storage account %q not found, removing queue %q from state", storageAccountName, d.Id())
   121  		d.SetId("")
   122  		return false, nil
   123  	}
   124  
   125  	name := d.Get("name").(string)
   126  
   127  	log.Printf("[INFO] Checking for existence of storage queue %q.", name)
   128  	exists, err := queueClient.QueueExists(name)
   129  	if err != nil {
   130  		return false, fmt.Errorf("error testing existence of storage queue %q: %s", name, err)
   131  	}
   132  
   133  	if !exists {
   134  		log.Printf("[INFO] Storage queue %q no longer exists, removing from state...", name)
   135  		d.SetId("")
   136  	}
   137  
   138  	return exists, nil
   139  }
   140  
   141  func resourceArmStorageQueueDelete(d *schema.ResourceData, meta interface{}) error {
   142  	armClient := meta.(*ArmClient)
   143  
   144  	resourceGroupName := d.Get("resource_group_name").(string)
   145  	storageAccountName := d.Get("storage_account_name").(string)
   146  
   147  	queueClient, accountExists, err := armClient.getQueueServiceClientForStorageAccount(resourceGroupName, storageAccountName)
   148  	if err != nil {
   149  		return err
   150  	}
   151  	if !accountExists {
   152  		log.Printf("[INFO]Storage Account %q doesn't exist so the blob won't exist", storageAccountName)
   153  		return nil
   154  	}
   155  
   156  	name := d.Get("name").(string)
   157  
   158  	log.Printf("[INFO] Deleting storage queue %q", name)
   159  	if err = queueClient.DeleteQueue(name); err != nil {
   160  		return fmt.Errorf("Error deleting storage queue %q: %s", name, err)
   161  	}
   162  
   163  	d.SetId("")
   164  	return nil
   165  }