forked from hashicorp/terraform-provider-azurerm
/
resource_arm_storage_table.go
214 lines (178 loc) · 5.89 KB
/
resource_arm_storage_table.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package azurerm
import (
"fmt"
"log"
"net/url"
"regexp"
"strings"
"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,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
SchemaVersion: 1,
MigrateState: resourceStorageTableMigrateState,
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validateArmStorageTableName,
},
"resource_group_name": resourceGroupNameSchema(),
"storage_account_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
},
}
}
func validateArmStorageTableName(v interface{}, k string) (warnings []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]{2,62}$`).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 3 and 63 characters long: %q",
k, value))
}
return warnings, errors
}
func resourceArmStorageTableCreate(d *schema.ResourceData, meta interface{}) error {
armClient := meta.(*ArmClient)
ctx := armClient.StopContext
environment := armClient.environment
name := d.Get("name").(string)
resourceGroupName := d.Get("resource_group_name").(string)
storageAccountName := d.Get("storage_account_name").(string)
tableClient, accountExists, err := armClient.getTableServiceClientForStorageAccount(ctx, resourceGroupName, storageAccountName)
if err != nil {
return err
}
if !accountExists {
return fmt.Errorf("Storage Account %q Not Found", storageAccountName)
}
table := tableClient.GetTableReference(name)
log.Printf("[INFO] Creating table %q in storage account %q.", name, storageAccountName)
timeout := uint(60)
options := &storage.TableOptions{}
err = table.Create(timeout, storage.NoMetadata, options)
if err != nil {
return fmt.Errorf("Error creating table %q in storage account %q: %s", name, storageAccountName, err)
}
id := fmt.Sprintf("https://%s.table.%s/%s", storageAccountName, environment.StorageEndpointSuffix, name)
d.SetId(id)
return resourceArmStorageTableRead(d, meta)
}
func resourceArmStorageTableRead(d *schema.ResourceData, meta interface{}) error {
armClient := meta.(*ArmClient)
ctx := armClient.StopContext
id, err := parseStorageTableID(d.Id())
if err != nil {
return err
}
resourceGroup, err := determineResourceGroupForStorageAccount(id.storageAccountName, armClient)
if err != nil {
return err
}
if resourceGroup == nil {
log.Printf("Unable to determine Resource Group for Storage Account %q (assuming removed)", id.storageAccountName)
d.SetId("")
return nil
}
tableClient, accountExists, err := armClient.getTableServiceClientForStorageAccount(ctx, *resourceGroup, id.storageAccountName)
if err != nil {
return err
}
if !accountExists {
log.Printf("[DEBUG] Storage account %q not found, removing table %q from state", id.storageAccountName, id.tableName)
d.SetId("")
return nil
}
metaDataLevel := storage.MinimalMetadata
options := &storage.QueryTablesOptions{}
tables, err := tableClient.QueryTables(metaDataLevel, options)
if err != nil {
return fmt.Errorf("Failed to retrieve Tables in Storage Account %q: %s", id.tableName, err)
}
var storageTable *storage.Table
for _, table := range tables.Tables {
if table.Name == id.tableName {
storageTable = &table
break
}
}
if storageTable == nil {
log.Printf("[INFO] Table %q does not exist in Storage Account %q, removing from state...", id.tableName, id.storageAccountName)
d.SetId("")
return nil
}
d.Set("name", id.tableName)
d.Set("storage_account_name", id.storageAccountName)
d.Set("resource_group_name", resourceGroup)
return nil
}
func resourceArmStorageTableDelete(d *schema.ResourceData, meta interface{}) error {
armClient := meta.(*ArmClient)
ctx := armClient.StopContext
id, err := parseStorageTableID(d.Id())
if err != nil {
return err
}
resourceGroup, err := determineResourceGroupForStorageAccount(id.storageAccountName, armClient)
if err != nil {
return err
}
if resourceGroup == nil {
log.Printf("Unable to determine Resource Group for Storage Account %q (assuming removed)", id.storageAccountName)
return nil
}
tableClient, accountExists, err := armClient.getTableServiceClientForStorageAccount(ctx, *resourceGroup, id.storageAccountName)
if err != nil {
return err
}
if !accountExists {
log.Printf("[INFO] Storage Account %q doesn't exist so the table won't exist", id.storageAccountName)
return nil
}
table := tableClient.GetTableReference(id.tableName)
timeout := uint(60)
options := &storage.TableOptions{}
log.Printf("[INFO] Deleting Table %q in Storage Account %q", id.tableName, id.storageAccountName)
if err := table.Delete(timeout, options); err != nil {
return fmt.Errorf("Error deleting table %q from Storage Account %q: %s", id.tableName, id.storageAccountName, err)
}
return nil
}
type storageTableId struct {
storageAccountName string
tableName string
}
func parseStorageTableID(input string) (*storageTableId, error) {
// https://myaccount.table.core.windows.net/table1
uri, err := url.Parse(input)
if err != nil {
return nil, fmt.Errorf("Error parsing %q as a URI: %+v", input, err)
}
segments := strings.Split(uri.Host, ".")
if len(segments) > 0 {
storageAccountName := segments[0]
table := strings.Replace(uri.Path, "/", "", 1)
id := storageTableId{
storageAccountName: storageAccountName,
tableName: table,
}
return &id, nil
}
return nil, nil
}