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

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