Skip to content

Commit

Permalink
provider/azurerm: azurerm_storage_table resource (#7327)
Browse files Browse the repository at this point in the history
* provider/azurerm: `azurerm_storage_table` resource

Fixes #7257

``````

* Update resource_arm_storage_table.go

* Update resource_arm_storage_table.go
  • Loading branch information
stack72 authored Jul 27, 2016
1 parent ba86744 commit 61c5c9f
Show file tree
Hide file tree
Showing 6 changed files with 456 additions and 0 deletions.
17 changes: 17 additions & 0 deletions builtin/providers/azurerm/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,23 @@ func (armClient *ArmClient) getBlobStorageClientForStorageAccount(resourceGroupN
blobClient := storageClient.GetBlobService()
return &blobClient, true, nil
}
func (armClient *ArmClient) getTableServiceClientForStorageAccount(resourceGroupName, storageAccountName string) (*mainStorage.TableServiceClient, 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)
}

tableClient := storageClient.GetTableService()
return &tableClient, true, nil
}

func (armClient *ArmClient) getQueueServiceClientForStorageAccount(resourceGroupName, storageAccountName string) (*mainStorage.QueueServiceClient, bool, error) {
key, accountExists, err := armClient.getKeyForStorageAccount(resourceGroupName, storageAccountName)
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_blob": resourceArmStorageBlob(),
"azurerm_storage_container": resourceArmStorageContainer(),
"azurerm_storage_queue": resourceArmStorageQueue(),
"azurerm_storage_table": resourceArmStorageTable(),
"azurerm_subnet": resourceArmSubnet(),
"azurerm_template_deployment": resourceArmTemplateDeployment(),
"azurerm_virtual_machine": resourceArmVirtualMachine(),
Expand Down
146 changes: 146 additions & 0 deletions builtin/providers/azurerm/resource_arm_storage_table.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
package azurerm

import (
"fmt"
"log"
"regexp"

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

func resourceArmStorageTable() *schema.Resource {
return &schema.Resource{
Create: resourceArmStorageTableCreate,
Read: resourceArmStorageTableRead,
Delete: resourceArmStorageTableDelete,

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validateArmStorageTableName,
},
"resource_group_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"storage_account_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
},
}
}

func validateArmStorageTableName(v interface{}, k string) (ws []string, errors []error) {
value := v.(string)
if value == "table" {
errors = append(errors, fmt.Errorf(
"Table Storage %q cannot use the word `table`: %q",
k, value))
}
if !regexp.MustCompile(`^[A-Za-z][A-Za-z0-9]{6,63}$`).MatchString(value) {
errors = append(errors, fmt.Errorf(
"Table Storage %q cannot begin with a numeric character, only alphanumeric characters are allowed and must be between 6 and 63 characters long: %q",
k, value))
}

return
}

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

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

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

name := d.Get("name").(string)
table := storage.AzureTable(name)

log.Printf("[INFO] Creating table %q in storage account %q.", name, storageAccountName)
err = tableClient.CreateTable(table)
if err != nil {
return fmt.Errorf("Error creating table %q in storage account %q: %s", name, storageAccountName, err)
}

d.SetId(name)

return resourceArmStorageTableRead(d, meta)
}

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

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

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

name := d.Get("name").(string)
tables, err := tableClient.QueryTables()
if err != nil {
return fmt.Errorf("Failed to retrieve storage tables in account %q: %s", name, err)
}

var found bool
for _, table := range tables {
if string(table) == name {
found = true
d.Set("name", string(table))
}
}

if !found {
log.Printf("[INFO] Storage table %q does not exist in account %q, removing from state...", name, storageAccountName)
d.SetId("")
}

return nil
}

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

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

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

name := d.Get("name").(string)
table := storage.AzureTable(name)

log.Printf("[INFO] Deleting storage table %q in account %q", name, storageAccountName)
if err := tableClient.DeleteTable(table); err != nil {
return fmt.Errorf("Error deleting storage table %q from storage account %q: %s", name, storageAccountName, err)
}

d.SetId("")
return nil
}
Loading

0 comments on commit 61c5c9f

Please sign in to comment.