-
Notifications
You must be signed in to change notification settings - Fork 9.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
provider/azurerm:
azurerm_storage_table
resource (#7327)
* provider/azurerm: `azurerm_storage_table` resource Fixes #7257 `````` * Update resource_arm_storage_table.go * Update resource_arm_storage_table.go
- Loading branch information
Showing
6 changed files
with
456 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
146 changes: 146 additions & 0 deletions
146
builtin/providers/azurerm/resource_arm_storage_table.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.