-
Notifications
You must be signed in to change notification settings - Fork 4.5k
/
resource_arm_storage_table_entity.go
224 lines (192 loc) · 7.71 KB
/
resource_arm_storage_table_entity.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
215
216
217
218
219
220
221
222
223
224
package azurerm
import (
"fmt"
"log"
"time"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/validate"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/features"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/timeouts"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
"github.com/tombuildsstuff/giovanni/storage/2018-11-09/table/entities"
)
func resourceArmStorageTableEntity() *schema.Resource {
return &schema.Resource{
Create: resourceArmStorageTableEntityCreateUpdate,
Read: resourceArmStorageTableEntityRead,
Update: resourceArmStorageTableEntityCreateUpdate,
Delete: resourceArmStorageTableEntityDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(30 * time.Minute),
Read: schema.DefaultTimeout(5 * time.Minute),
Update: schema.DefaultTimeout(30 * time.Minute),
Delete: schema.DefaultTimeout(30 * time.Minute),
},
Schema: map[string]*schema.Schema{
"table_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validateArmStorageTableName,
},
"storage_account_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validateArmStorageAccountName,
},
"partition_key": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validate.NoEmptyStrings,
},
"row_key": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validate.NoEmptyStrings,
},
"entity": {
Type: schema.TypeMap,
Required: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
},
}
}
func resourceArmStorageTableEntityCreateUpdate(d *schema.ResourceData, meta interface{}) error {
ctx, cancel := timeouts.ForCreateUpdate(meta.(*ArmClient).StopContext, d)
defer cancel()
storageClient := meta.(*ArmClient).Storage
accountName := d.Get("storage_account_name").(string)
tableName := d.Get("table_name").(string)
partitionKey := d.Get("partition_key").(string)
rowKey := d.Get("row_key").(string)
entity := d.Get("entity").(map[string]interface{})
account, err := storageClient.FindAccount(ctx, accountName)
if err != nil {
return fmt.Errorf("Error retrieving Account %q for Table %q: %s", accountName, tableName, err)
}
if account == nil {
if d.IsNewResource() {
return fmt.Errorf("Unable to locate Account %q for Storage Table %q", accountName, tableName)
} else {
log.Printf("[DEBUG] Unable to locate Account %q for Storage Table %q - assuming removed & removing from state", accountName, tableName)
d.SetId("")
return nil
}
}
client, err := storageClient.TableEntityClient(ctx, *account)
if err != nil {
return fmt.Errorf("Error building Entity Client: %s", err)
}
if features.ShouldResourcesBeImported() && d.IsNewResource() {
input := entities.GetEntityInput{
PartitionKey: partitionKey,
RowKey: rowKey,
MetaDataLevel: entities.NoMetaData,
}
existing, err := client.Get(ctx, accountName, tableName, input)
if err != nil {
if !utils.ResponseWasNotFound(existing.Response) {
return fmt.Errorf("Error checking for presence of existing Entity (Partition Key %q / Row Key %q) (Table %q / Storage Account %q / Resource Group %q): %s", partitionKey, rowKey, tableName, accountName, account.ResourceGroup, err)
}
}
if !utils.ResponseWasNotFound(existing.Response) {
id := client.GetResourceID(accountName, tableName, partitionKey, rowKey)
return tf.ImportAsExistsError("azurerm_storage_table_entity", id)
}
}
input := entities.InsertOrMergeEntityInput{
PartitionKey: partitionKey,
RowKey: rowKey,
Entity: entity,
}
if _, err := client.InsertOrMerge(ctx, accountName, tableName, input); err != nil {
return fmt.Errorf("Error creating Entity (Partition Key %q / Row Key %q) (Table %q / Storage Account %q / Resource Group %q): %+v", partitionKey, rowKey, tableName, accountName, account.ResourceGroup, err)
}
resourceID := client.GetResourceID(accountName, tableName, partitionKey, rowKey)
d.SetId(resourceID)
return resourceArmStorageTableEntityRead(d, meta)
}
func resourceArmStorageTableEntityRead(d *schema.ResourceData, meta interface{}) error {
ctx, cancel := timeouts.ForRead(meta.(*ArmClient).StopContext, d)
defer cancel()
storageClient := meta.(*ArmClient).Storage
id, err := entities.ParseResourceID(d.Id())
if err != nil {
return err
}
account, err := storageClient.FindAccount(ctx, id.AccountName)
if err != nil {
return fmt.Errorf("Error retrieving Account %q for Table %q: %s", id.AccountName, id.TableName, err)
}
if account == nil {
log.Printf("[WARN] Unable to determine Resource Group for Storage Table %q (Account %s) - assuming removed & removing from state", id.TableName, id.AccountName)
d.SetId("")
return nil
}
client, err := storageClient.TableEntityClient(ctx, *account)
if err != nil {
return fmt.Errorf("Error building Table Entity Client for Storage Account %q (Resource Group %q): %s", id.AccountName, account.ResourceGroup, err)
}
input := entities.GetEntityInput{
PartitionKey: id.PartitionKey,
RowKey: id.RowKey,
MetaDataLevel: entities.NoMetaData,
}
result, err := client.Get(ctx, id.AccountName, id.TableName, input)
if err != nil {
return fmt.Errorf("Error retrieving Entity (Partition Key %q / Row Key %q) (Table %q / Storage Account %q / Resource Group %q): %s", id.PartitionKey, id.RowKey, id.TableName, id.AccountName, account.ResourceGroup, err)
}
d.Set("storage_account_name", id.AccountName)
d.Set("table_name", id.TableName)
d.Set("partition_key", id.PartitionKey)
d.Set("row_key", id.RowKey)
if err := d.Set("entity", flattenEntity(result.Entity)); err != nil {
return fmt.Errorf("Error setting `entity` for Entity (Partition Key %q / Row Key %q) (Table %q / Storage Account %q / Resource Group %q): %s", id.PartitionKey, id.RowKey, id.TableName, id.AccountName, account.ResourceGroup, err)
}
return nil
}
func resourceArmStorageTableEntityDelete(d *schema.ResourceData, meta interface{}) error {
ctx, cancel := timeouts.ForDelete(meta.(*ArmClient).StopContext, d)
defer cancel()
storageClient := meta.(*ArmClient).Storage
id, err := entities.ParseResourceID(d.Id())
if err != nil {
return err
}
account, err := storageClient.FindAccount(ctx, id.AccountName)
if err != nil {
return fmt.Errorf("Error retrieving Account %q for Table %q: %s", id.AccountName, id.TableName, err)
}
if account == nil {
return fmt.Errorf("Storage Account %q was not found!", id.AccountName)
}
client, err := storageClient.TableEntityClient(ctx, *account)
if err != nil {
return fmt.Errorf("Error building Entity Client for Storage Account %q (Resource Group %q): %s", id.AccountName, account.ResourceGroup, err)
}
input := entities.DeleteEntityInput{
PartitionKey: id.PartitionKey,
RowKey: id.RowKey,
}
if _, err := client.Delete(ctx, id.AccountName, id.TableName, input); err != nil {
return fmt.Errorf("Error deleting Entity (Partition Key %q / Row Key %q) (Table %q / Storage Account %q / Resource Group %q): %s", id.PartitionKey, id.RowKey, id.TableName, id.AccountName, account.ResourceGroup, err)
}
return nil
}
// The api returns extra information that we already have. We'll remove it here before setting it in state.
func flattenEntity(entity map[string]interface{}) map[string]interface{} {
delete(entity, "PartitionKey")
delete(entity, "RowKey")
delete(entity, "Timestamp")
return entity
}