github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/builtin/providers/azurerm/resource_arm_storage_share.go (about)

     1  package azurerm
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	//	"strings"
     7  	"regexp"
     8  	"strconv"
     9  
    10  	"github.com/Azure/azure-sdk-for-go/storage"
    11  	"github.com/hashicorp/terraform/helper/schema"
    12  )
    13  
    14  func resourceArmStorageShare() *schema.Resource {
    15  	return &schema.Resource{
    16  		Create: resourceArmStorageShareCreate,
    17  		Read:   resourceArmStorageShareRead,
    18  		Exists: resourceArmStorageShareExists,
    19  		Delete: resourceArmStorageShareDelete,
    20  
    21  		Schema: map[string]*schema.Schema{
    22  			"name": {
    23  				Type:         schema.TypeString,
    24  				Required:     true,
    25  				ForceNew:     true,
    26  				ValidateFunc: validateArmStorageShareName,
    27  			},
    28  			"resource_group_name": {
    29  				Type:     schema.TypeString,
    30  				Required: true,
    31  				ForceNew: true,
    32  			},
    33  			"storage_account_name": {
    34  				Type:     schema.TypeString,
    35  				Required: true,
    36  				ForceNew: true,
    37  			},
    38  			"quota": {
    39  				Type:     schema.TypeInt,
    40  				Optional: true,
    41  				ForceNew: true,
    42  				Default:  0,
    43  			},
    44  			"url": {
    45  				Type:     schema.TypeString,
    46  				Computed: true,
    47  			},
    48  		},
    49  	}
    50  }
    51  func resourceArmStorageShareCreate(d *schema.ResourceData, meta interface{}) error {
    52  	armClient := meta.(*ArmClient)
    53  
    54  	resourceGroupName := d.Get("resource_group_name").(string)
    55  	storageAccountName := d.Get("storage_account_name").(string)
    56  
    57  	fileClient, accountExists, err := armClient.getFileServiceClientForStorageAccount(resourceGroupName, storageAccountName)
    58  	if err != nil {
    59  		return err
    60  	}
    61  	if !accountExists {
    62  		return fmt.Errorf("Storage Account %q Not Found", storageAccountName)
    63  	}
    64  
    65  	name := d.Get("name").(string)
    66  
    67  	log.Printf("[INFO] Creating share %q in storage account %q", name, storageAccountName)
    68  	err = fileClient.CreateShare(name)
    69  
    70  	log.Printf("[INFO] Setting share %q properties in storage account %q", name, storageAccountName)
    71  	fileClient.SetShareProperties(name, storage.ShareHeaders{Quota: strconv.Itoa(d.Get("quota").(int))})
    72  
    73  	d.SetId(name)
    74  	return resourceArmStorageShareRead(d, meta)
    75  }
    76  
    77  func resourceArmStorageShareRead(d *schema.ResourceData, meta interface{}) error {
    78  	armClient := meta.(*ArmClient)
    79  
    80  	resourceGroupName := d.Get("resource_group_name").(string)
    81  	storageAccountName := d.Get("storage_account_name").(string)
    82  
    83  	fileClient, accountExists, err := armClient.getFileServiceClientForStorageAccount(resourceGroupName, storageAccountName)
    84  	if err != nil {
    85  		return err
    86  	}
    87  	if !accountExists {
    88  		log.Printf("[DEBUG] Storage account %q not found, removing file %q from state", storageAccountName, d.Id())
    89  		d.SetId("")
    90  		return nil
    91  	}
    92  
    93  	exists, err := resourceArmStorageShareExists(d, meta)
    94  	if err != nil {
    95  		return err
    96  	}
    97  
    98  	if !exists {
    99  		// Exists already removed this from state
   100  		return nil
   101  	}
   102  
   103  	name := d.Get("name").(string)
   104  
   105  	url := fileClient.GetShareURL(name)
   106  	if url == "" {
   107  		log.Printf("[INFO] URL for %q is empty", name)
   108  	}
   109  	d.Set("url", url)
   110  
   111  	return nil
   112  }
   113  
   114  func resourceArmStorageShareExists(d *schema.ResourceData, meta interface{}) (bool, error) {
   115  	armClient := meta.(*ArmClient)
   116  
   117  	resourceGroupName := d.Get("resource_group_name").(string)
   118  	storageAccountName := d.Get("storage_account_name").(string)
   119  
   120  	fileClient, accountExists, err := armClient.getFileServiceClientForStorageAccount(resourceGroupName, storageAccountName)
   121  	if err != nil {
   122  		return false, err
   123  	}
   124  	if !accountExists {
   125  		log.Printf("[DEBUG] Storage account %q not found, removing share %q from state", storageAccountName, d.Id())
   126  		d.SetId("")
   127  		return false, nil
   128  	}
   129  
   130  	name := d.Get("name").(string)
   131  
   132  	log.Printf("[INFO] Checking for existence of share %q.", name)
   133  	exists, err := fileClient.ShareExists(name)
   134  	if err != nil {
   135  		return false, fmt.Errorf("Error testing existence of share %q: %s", name, err)
   136  	}
   137  
   138  	if !exists {
   139  		log.Printf("[INFO] Share %q no longer exists, removing from state...", name)
   140  		d.SetId("")
   141  	}
   142  
   143  	return exists, nil
   144  }
   145  
   146  func resourceArmStorageShareDelete(d *schema.ResourceData, meta interface{}) error {
   147  	armClient := meta.(*ArmClient)
   148  
   149  	resourceGroupName := d.Get("resource_group_name").(string)
   150  	storageAccountName := d.Get("storage_account_name").(string)
   151  
   152  	fileClient, accountExists, err := armClient.getFileServiceClientForStorageAccount(resourceGroupName, storageAccountName)
   153  	if err != nil {
   154  		return err
   155  	}
   156  	if !accountExists {
   157  		log.Printf("[INFO]Storage Account %q doesn't exist so the file won't exist", storageAccountName)
   158  		return nil
   159  	}
   160  
   161  	name := d.Get("name").(string)
   162  
   163  	log.Printf("[INFO] Deleting storage file %q", name)
   164  	if _, err = fileClient.DeleteShareIfExists(name); err != nil {
   165  		return fmt.Errorf("Error deleting storage file %q: %s", name, err)
   166  	}
   167  
   168  	d.SetId("")
   169  	return nil
   170  }
   171  
   172  //Following the naming convention as laid out in the docs https://msdn.microsoft.com/library/azure/dn167011.aspx
   173  func validateArmStorageShareName(v interface{}, k string) (ws []string, errors []error) {
   174  	value := v.(string)
   175  	if !regexp.MustCompile(`^[0-9a-z-]+$`).MatchString(value) {
   176  		errors = append(errors, fmt.Errorf(
   177  			"only lowercase alphanumeric characters and hyphens allowed in %q: %q",
   178  			k, value))
   179  	}
   180  	if len(value) < 3 || len(value) > 63 {
   181  		errors = append(errors, fmt.Errorf(
   182  			"%q must be between 3 and 63 characters: %q", k, value))
   183  	}
   184  	if regexp.MustCompile(`^-`).MatchString(value) {
   185  		errors = append(errors, fmt.Errorf(
   186  			"%q cannot begin with a hyphen: %q", k, value))
   187  	}
   188  	if regexp.MustCompile(`[-]{2,}`).MatchString(value) {
   189  		errors = append(errors, fmt.Errorf(
   190  			"%q does not allow consecutive hyphens: %q", k, value))
   191  	}
   192  	return
   193  }