github.com/aspring/terraform@v0.8.2-0.20161216122603-6a8619a5db2e/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  	metaData := make(map[string]string) // TODO: support MetaData
    67  
    68  	log.Printf("[INFO] Creating share %q in storage account %q", name, storageAccountName)
    69  	err = fileClient.CreateShare(name, metaData)
    70  
    71  	log.Printf("[INFO] Setting share %q properties in storage account %q", name, storageAccountName)
    72  	fileClient.SetShareProperties(name, storage.ShareHeaders{Quota: strconv.Itoa(d.Get("quota").(int))})
    73  
    74  	d.SetId(name)
    75  	return resourceArmStorageShareRead(d, meta)
    76  }
    77  
    78  func resourceArmStorageShareRead(d *schema.ResourceData, meta interface{}) error {
    79  	armClient := meta.(*ArmClient)
    80  
    81  	resourceGroupName := d.Get("resource_group_name").(string)
    82  	storageAccountName := d.Get("storage_account_name").(string)
    83  
    84  	fileClient, accountExists, err := armClient.getFileServiceClientForStorageAccount(resourceGroupName, storageAccountName)
    85  	if err != nil {
    86  		return err
    87  	}
    88  	if !accountExists {
    89  		log.Printf("[DEBUG] Storage account %q not found, removing file %q from state", storageAccountName, d.Id())
    90  		d.SetId("")
    91  		return nil
    92  	}
    93  
    94  	exists, err := resourceArmStorageShareExists(d, meta)
    95  	if err != nil {
    96  		return err
    97  	}
    98  
    99  	if !exists {
   100  		// Exists already removed this from state
   101  		return nil
   102  	}
   103  
   104  	name := d.Get("name").(string)
   105  
   106  	url := fileClient.GetShareURL(name)
   107  	if url == "" {
   108  		log.Printf("[INFO] URL for %q is empty", name)
   109  	}
   110  	d.Set("url", url)
   111  
   112  	return nil
   113  }
   114  
   115  func resourceArmStorageShareExists(d *schema.ResourceData, meta interface{}) (bool, error) {
   116  	armClient := meta.(*ArmClient)
   117  
   118  	resourceGroupName := d.Get("resource_group_name").(string)
   119  	storageAccountName := d.Get("storage_account_name").(string)
   120  
   121  	fileClient, accountExists, err := armClient.getFileServiceClientForStorageAccount(resourceGroupName, storageAccountName)
   122  	if err != nil {
   123  		return false, err
   124  	}
   125  	if !accountExists {
   126  		log.Printf("[DEBUG] Storage account %q not found, removing share %q from state", storageAccountName, d.Id())
   127  		d.SetId("")
   128  		return false, nil
   129  	}
   130  
   131  	name := d.Get("name").(string)
   132  
   133  	log.Printf("[INFO] Checking for existence of share %q.", name)
   134  	exists, err := fileClient.ShareExists(name)
   135  	if err != nil {
   136  		return false, fmt.Errorf("Error testing existence of share %q: %s", name, err)
   137  	}
   138  
   139  	if !exists {
   140  		log.Printf("[INFO] Share %q no longer exists, removing from state...", name)
   141  		d.SetId("")
   142  	}
   143  
   144  	return exists, nil
   145  }
   146  
   147  func resourceArmStorageShareDelete(d *schema.ResourceData, meta interface{}) error {
   148  	armClient := meta.(*ArmClient)
   149  
   150  	resourceGroupName := d.Get("resource_group_name").(string)
   151  	storageAccountName := d.Get("storage_account_name").(string)
   152  
   153  	fileClient, accountExists, err := armClient.getFileServiceClientForStorageAccount(resourceGroupName, storageAccountName)
   154  	if err != nil {
   155  		return err
   156  	}
   157  	if !accountExists {
   158  		log.Printf("[INFO]Storage Account %q doesn't exist so the file won't exist", storageAccountName)
   159  		return nil
   160  	}
   161  
   162  	name := d.Get("name").(string)
   163  
   164  	log.Printf("[INFO] Deleting storage file %q", name)
   165  	if _, err = fileClient.DeleteShareIfExists(name); err != nil {
   166  		return fmt.Errorf("Error deleting storage file %q: %s", name, err)
   167  	}
   168  
   169  	d.SetId("")
   170  	return nil
   171  }
   172  
   173  //Following the naming convention as laid out in the docs https://msdn.microsoft.com/library/azure/dn167011.aspx
   174  func validateArmStorageShareName(v interface{}, k string) (ws []string, errors []error) {
   175  	value := v.(string)
   176  	if !regexp.MustCompile(`^[0-9a-z-]+$`).MatchString(value) {
   177  		errors = append(errors, fmt.Errorf(
   178  			"only lowercase alphanumeric characters and hyphens allowed in %q: %q",
   179  			k, value))
   180  	}
   181  	if len(value) < 3 || len(value) > 63 {
   182  		errors = append(errors, fmt.Errorf(
   183  			"%q must be between 3 and 63 characters: %q", k, value))
   184  	}
   185  	if regexp.MustCompile(`^-`).MatchString(value) {
   186  		errors = append(errors, fmt.Errorf(
   187  			"%q cannot begin with a hyphen: %q", k, value))
   188  	}
   189  	if regexp.MustCompile(`[-]{2,}`).MatchString(value) {
   190  		errors = append(errors, fmt.Errorf(
   191  			"%q does not allow consecutive hyphens: %q", k, value))
   192  	}
   193  	return
   194  }