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