Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

provider/azurerm: azurerm_storage_table resource #7327

Merged
merged 3 commits into from
Jul 27, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions builtin/providers/azurerm/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,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))
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you mean strings.Contains here instead of ==? Or is it really just that the literal name "table" is not allowed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the literal name table is not allowed

if !regexp.MustCompile(`^[A-Za-z][A-Za-z0-9]{2,62}$`).MatchString(value) {
errors = append(errors, fmt.Errorf(
"Table Storage %q cannot begin with a numberic character, only alphanumeric characters are allowed and must be between 6 and 63 characters long: %q",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/numberic/numeric/

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Message says must be between 6 & 63 chars but regexp looks like 2 - 62

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