Skip to content

Commit

Permalink
provider/azurerm: Implement azurerm_storage_share
Browse files Browse the repository at this point in the history
  • Loading branch information
enieuw committed Sep 9, 2016
1 parent f446c66 commit d95d3d6
Show file tree
Hide file tree
Showing 9 changed files with 770 additions and 5 deletions.
19 changes: 19 additions & 0 deletions builtin/providers/azurerm/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,25 @@ func (armClient *ArmClient) getBlobStorageClientForStorageAccount(resourceGroupN
blobClient := storageClient.GetBlobService()
return &blobClient, true, nil
}

func (armClient *ArmClient) getFileServiceClientForStorageAccount(resourceGroupName, storageAccountName string) (*mainStorage.FileServiceClient, bool, error) {
key, accountExists, err := armClient.getKeyForStorageAccount(resourceGroupName, storageAccountName)
if err != nil {
return nil, accountExists, err
}
if accountExists == false {
return nil, false, nil
}

storageClient, err := mainStorage.NewBasicClient(storageAccountName, key)
if err != nil {
return nil, true, fmt.Errorf("Error creating storage client for storage account %q: %s", storageAccountName, err)
}

fileClient := storageClient.GetFileService()
return &fileClient, true, nil
}

func (armClient *ArmClient) getTableServiceClientForStorageAccount(resourceGroupName, storageAccountName string) (*mainStorage.TableServiceClient, bool, error) {
key, accountExists, err := armClient.getKeyForStorageAccount(resourceGroupName, storageAccountName)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions builtin/providers/azurerm/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ func Provider() terraform.ResourceProvider {
"azurerm_storage_account": resourceArmStorageAccount(),
"azurerm_storage_blob": resourceArmStorageBlob(),
"azurerm_storage_container": resourceArmStorageContainer(),
"azurerm_storage_share": resourceArmStorageShare(),
"azurerm_storage_queue": resourceArmStorageQueue(),
"azurerm_storage_table": resourceArmStorageTable(),
"azurerm_subnet": resourceArmSubnet(),
Expand Down
193 changes: 193 additions & 0 deletions builtin/providers/azurerm/resource_arm_storage_share.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
package azurerm

import (
"fmt"
"log"
// "strings"
"regexp"
"strconv"

"github.com/Azure/azure-sdk-for-go/storage"
"github.com/hashicorp/terraform/helper/schema"
)

func resourceArmStorageShare() *schema.Resource {
return &schema.Resource{
Create: resourceArmStorageShareCreate,
Read: resourceArmStorageShareRead,
Exists: resourceArmStorageShareExists,
Delete: resourceArmStorageShareDelete,

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validateArmStorageShareName,
},
"resource_group_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"storage_account_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"quota": {
Type: schema.TypeInt,
Optional: true,
ForceNew: true,
Default: 0,
},
"url": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
func resourceArmStorageShareCreate(d *schema.ResourceData, meta interface{}) error {
armClient := meta.(*ArmClient)

resourceGroupName := d.Get("resource_group_name").(string)
storageAccountName := d.Get("storage_account_name").(string)

fileClient, accountExists, err := armClient.getFileServiceClientForStorageAccount(resourceGroupName, storageAccountName)
if err != nil {
return err
}
if !accountExists {
return fmt.Errorf("Storage Account %q Not Found", storageAccountName)
}

name := d.Get("name").(string)

log.Printf("[INFO] Creating share %q in storage account %q", name, storageAccountName)
err = fileClient.CreateShare(name)

log.Printf("[INFO] Setting share %q properties in storage account %q", name, storageAccountName)
fileClient.SetShareProperties(name, storage.ShareHeaders{Quota: strconv.Itoa(d.Get("quota").(int))})

d.SetId(name)
return resourceArmStorageShareRead(d, meta)
}

func resourceArmStorageShareRead(d *schema.ResourceData, meta interface{}) error {
armClient := meta.(*ArmClient)

resourceGroupName := d.Get("resource_group_name").(string)
storageAccountName := d.Get("storage_account_name").(string)

fileClient, accountExists, err := armClient.getFileServiceClientForStorageAccount(resourceGroupName, storageAccountName)
if err != nil {
return err
}
if !accountExists {
log.Printf("[DEBUG] Storage account %q not found, removing file %q from state", storageAccountName, d.Id())
d.SetId("")
return nil
}

exists, err := resourceArmStorageShareExists(d, meta)
if err != nil {
return err
}

if !exists {
// Exists already removed this from state
return nil
}

name := d.Get("name").(string)

url := fileClient.GetShareURL(name)
if url == "" {
log.Printf("[INFO] URL for %q is empty", name)
}
d.Set("url", url)

return nil
}

func resourceArmStorageShareExists(d *schema.ResourceData, meta interface{}) (bool, error) {
armClient := meta.(*ArmClient)

resourceGroupName := d.Get("resource_group_name").(string)
storageAccountName := d.Get("storage_account_name").(string)

fileClient, accountExists, err := armClient.getFileServiceClientForStorageAccount(resourceGroupName, storageAccountName)
if err != nil {
return false, err
}
if !accountExists {
log.Printf("[DEBUG] Storage account %q not found, removing share %q from state", storageAccountName, d.Id())
d.SetId("")
return false, nil
}

name := d.Get("name").(string)

log.Printf("[INFO] Checking for existence of share %q.", name)
exists, err := fileClient.ShareExists(name)
if err != nil {
return false, fmt.Errorf("Error testing existence of share %q: %s", name, err)
}

if !exists {
log.Printf("[INFO] Share %q no longer exists, removing from state...", name)
d.SetId("")
}

return exists, nil
}

func resourceArmStorageShareDelete(d *schema.ResourceData, meta interface{}) error {
armClient := meta.(*ArmClient)

resourceGroupName := d.Get("resource_group_name").(string)
storageAccountName := d.Get("storage_account_name").(string)

fileClient, accountExists, err := armClient.getFileServiceClientForStorageAccount(resourceGroupName, storageAccountName)
if err != nil {
return err
}
if !accountExists {
log.Printf("[INFO]Storage Account %q doesn't exist so the file won't exist", storageAccountName)
return nil
}

name := d.Get("name").(string)

log.Printf("[INFO] Deleting storage file %q", name)
if _, err = fileClient.DeleteShareIfExists(name); err != nil {
return fmt.Errorf("Error deleting storage file %q: %s", name, err)
}

d.SetId("")
return nil
}

//Following the naming convention as laid out in the docs https://msdn.microsoft.com/library/azure/dn167011.aspx
func validateArmStorageShareName(v interface{}, k string) (ws []string, errors []error) {
value := v.(string)
if !regexp.MustCompile(`^[0-9a-z-]+$`).MatchString(value) {
errors = append(errors, fmt.Errorf(
"only lowercase alphanumeric characters and hyphens allowed in %q: %q",
k, value))
}
if len(value) < 3 || len(value) > 63 {
errors = append(errors, fmt.Errorf(
"%q must be between 3 and 63 characters: %q", k, value))
}
if regexp.MustCompile(`^-`).MatchString(value) {
errors = append(errors, fmt.Errorf(
"%q cannot begin with a hyphen: %q", k, value))
}
if regexp.MustCompile(`[-]{2,}`).MatchString(value) {
errors = append(errors, fmt.Errorf(
"%q does not allow consecutive hyphens: %q", k, value))
}
return
}
Loading

0 comments on commit d95d3d6

Please sign in to comment.