github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/azurerm/resource_arm_storage_table.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 resourceArmStorageTable() *schema.Resource { 13 return &schema.Resource{ 14 Create: resourceArmStorageTableCreate, 15 Read: resourceArmStorageTableRead, 16 Delete: resourceArmStorageTableDelete, 17 18 Schema: map[string]*schema.Schema{ 19 "name": { 20 Type: schema.TypeString, 21 Required: true, 22 ForceNew: true, 23 ValidateFunc: validateArmStorageTableName, 24 }, 25 "resource_group_name": { 26 Type: schema.TypeString, 27 Required: true, 28 ForceNew: true, 29 }, 30 "storage_account_name": { 31 Type: schema.TypeString, 32 Required: true, 33 ForceNew: true, 34 }, 35 }, 36 } 37 } 38 39 func validateArmStorageTableName(v interface{}, k string) (ws []string, errors []error) { 40 value := v.(string) 41 if value == "table" { 42 errors = append(errors, fmt.Errorf( 43 "Table Storage %q cannot use the word `table`: %q", 44 k, value)) 45 } 46 if !regexp.MustCompile(`^[A-Za-z][A-Za-z0-9]{6,63}$`).MatchString(value) { 47 errors = append(errors, fmt.Errorf( 48 "Table Storage %q cannot begin with a numeric character, only alphanumeric characters are allowed and must be between 6 and 63 characters long: %q", 49 k, value)) 50 } 51 52 return 53 } 54 55 func resourceArmStorageTableCreate(d *schema.ResourceData, meta interface{}) error { 56 armClient := meta.(*ArmClient) 57 58 resourceGroupName := d.Get("resource_group_name").(string) 59 storageAccountName := d.Get("storage_account_name").(string) 60 61 tableClient, accountExists, err := armClient.getTableServiceClientForStorageAccount(resourceGroupName, storageAccountName) 62 if err != nil { 63 return err 64 } 65 if !accountExists { 66 return fmt.Errorf("Storage Account %q Not Found", storageAccountName) 67 } 68 69 name := d.Get("name").(string) 70 table := storage.AzureTable(name) 71 72 log.Printf("[INFO] Creating table %q in storage account %q.", name, storageAccountName) 73 err = tableClient.CreateTable(table) 74 if err != nil { 75 return fmt.Errorf("Error creating table %q in storage account %q: %s", name, storageAccountName, err) 76 } 77 78 d.SetId(name) 79 80 return resourceArmStorageTableRead(d, meta) 81 } 82 83 func resourceArmStorageTableRead(d *schema.ResourceData, meta interface{}) error { 84 armClient := meta.(*ArmClient) 85 86 resourceGroupName := d.Get("resource_group_name").(string) 87 storageAccountName := d.Get("storage_account_name").(string) 88 89 tableClient, accountExists, err := armClient.getTableServiceClientForStorageAccount(resourceGroupName, storageAccountName) 90 if err != nil { 91 return err 92 } 93 if !accountExists { 94 log.Printf("[DEBUG] Storage account %q not found, removing table %q from state", storageAccountName, d.Id()) 95 d.SetId("") 96 return nil 97 } 98 99 name := d.Get("name").(string) 100 tables, err := tableClient.QueryTables() 101 if err != nil { 102 return fmt.Errorf("Failed to retrieve storage tables in account %q: %s", name, err) 103 } 104 105 var found bool 106 for _, table := range tables { 107 if string(table) == name { 108 found = true 109 d.Set("name", string(table)) 110 } 111 } 112 113 if !found { 114 log.Printf("[INFO] Storage table %q does not exist in account %q, removing from state...", name, storageAccountName) 115 d.SetId("") 116 } 117 118 return nil 119 } 120 121 func resourceArmStorageTableDelete(d *schema.ResourceData, meta interface{}) error { 122 armClient := meta.(*ArmClient) 123 124 resourceGroupName := d.Get("resource_group_name").(string) 125 storageAccountName := d.Get("storage_account_name").(string) 126 127 tableClient, accountExists, err := armClient.getTableServiceClientForStorageAccount(resourceGroupName, storageAccountName) 128 if err != nil { 129 return err 130 } 131 if !accountExists { 132 log.Printf("[INFO]Storage Account %q doesn't exist so the table won't exist", storageAccountName) 133 return nil 134 } 135 136 name := d.Get("name").(string) 137 table := storage.AzureTable(name) 138 139 log.Printf("[INFO] Deleting storage table %q in account %q", name, storageAccountName) 140 if err := tableClient.DeleteTable(table); err != nil { 141 return fmt.Errorf("Error deleting storage table %q from storage account %q: %s", name, storageAccountName, err) 142 } 143 144 d.SetId("") 145 return nil 146 }