-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
resource_arm_storage_share.go
250 lines (213 loc) · 7.55 KB
/
resource_arm_storage_share.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
package azurerm
import (
"fmt"
"log"
"regexp"
"strings"
"github.com/hashicorp/terraform/helper/validation"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf"
"github.com/Azure/azure-sdk-for-go/storage"
"github.com/hashicorp/terraform/helper/schema"
)
func resourceArmStorageShare() *schema.Resource {
return &schema.Resource{
Create: resourceArmStorageShareCreate,
Read: resourceArmStorageShareRead,
Update: resourceArmStorageShareUpdate,
Delete: resourceArmStorageShareDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
SchemaVersion: 1,
MigrateState: resourceStorageShareMigrateState,
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validateArmStorageShareName,
},
"resource_group_name": resourceGroupNameSchema(),
"storage_account_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"quota": {
Type: schema.TypeInt,
Optional: true,
Default: 5120,
ValidateFunc: validation.IntBetween(1, 5120),
},
"url": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
func resourceArmStorageShareCreate(d *schema.ResourceData, meta interface{}) error {
armClient := meta.(*ArmClient)
ctx := armClient.StopContext
resourceGroupName := d.Get("resource_group_name").(string)
storageAccountName := d.Get("storage_account_name").(string)
fileClient, accountExists, err := armClient.getFileServiceClientForStorageAccount(ctx, resourceGroupName, storageAccountName)
if err != nil {
return err
}
if !accountExists {
return fmt.Errorf("Storage Account %q Not Found", storageAccountName)
}
name := d.Get("name").(string)
metaData := make(map[string]string) // TODO: support MetaData
options := &storage.FileRequestOptions{}
log.Printf("[INFO] Creating share %q in storage account %q", name, storageAccountName)
reference := fileClient.GetShareReference(name)
id := fmt.Sprintf("%s/%s/%s", name, resourceGroupName, storageAccountName)
if requireResourcesToBeImported {
exists, e := reference.Exists()
if e != nil {
return fmt.Errorf("Error checking if Share %q exists (Account %q / Resource Group %q): %s", name, storageAccountName, resourceGroupName, e)
}
if exists {
return tf.ImportAsExistsError("azurerm_storage_share", id)
}
}
err = reference.Create(options)
if err != nil {
return fmt.Errorf("Error creating Storage Share %q reference (storage account: %q) : %+v", name, storageAccountName, err)
}
log.Printf("[INFO] Setting share %q metadata in storage account %q", name, storageAccountName)
reference.Metadata = metaData
if err := reference.SetMetadata(options); err != nil {
return fmt.Errorf("Error setting metadata on Storage Share %q: %+v", name, err)
}
log.Printf("[INFO] Setting share %q properties in storage account %q", name, storageAccountName)
reference.Properties = storage.ShareProperties{
Quota: d.Get("quota").(int),
}
if err := reference.SetProperties(options); err != nil {
return fmt.Errorf("Error setting properties on Storage Share %q: %+v", name, err)
}
d.SetId(id)
return resourceArmStorageShareRead(d, meta)
}
func resourceArmStorageShareRead(d *schema.ResourceData, meta interface{}) error {
armClient := meta.(*ArmClient)
ctx := armClient.StopContext
id := strings.Split(d.Id(), "/")
if len(id) != 3 {
return fmt.Errorf("ID was not in the expected format - expected `{name}/{resourceGroup}/{storageAccountName}` got %q", id)
}
name := id[0]
resourceGroupName := id[1]
storageAccountName := id[2]
fileClient, accountExists, err := armClient.getFileServiceClientForStorageAccount(ctx, resourceGroupName, storageAccountName)
if err != nil {
return err
}
if !accountExists {
log.Printf("[DEBUG] Storage account %q not found, removing file %q from state", storageAccountName, d.Id())
d.SetId("")
return nil
}
reference := fileClient.GetShareReference(name)
exists, err := reference.Exists()
if err != nil {
return fmt.Errorf("Error testing existence of share %q: %s", name, err)
}
if !exists {
log.Printf("[INFO] Share %q no longer exists, removing from state...", name)
d.SetId("")
return nil
}
url := reference.URL()
if url == "" {
log.Printf("[INFO] URL for %q is empty", name)
}
d.Set("name", name)
d.Set("resource_group_name", resourceGroupName)
d.Set("storage_account_name", storageAccountName)
d.Set("url", url)
if err := reference.FetchAttributes(nil); err != nil {
return fmt.Errorf("Error fetching properties on Storage Share %q: %+v", name, err)
}
d.Set("quota", reference.Properties.Quota)
return nil
}
func resourceArmStorageShareUpdate(d *schema.ResourceData, meta interface{}) error {
armClient := meta.(*ArmClient)
ctx := armClient.StopContext
id := strings.Split(d.Id(), "/")
if len(id) != 3 {
return fmt.Errorf("ID was not in the expected format - expected `{name}/{resourceGroup}/{storageAccountName}` got %q", id)
}
name := id[0]
resourceGroupName := id[1]
storageAccountName := id[2]
fileClient, accountExists, err := armClient.getFileServiceClientForStorageAccount(ctx, resourceGroupName, storageAccountName)
if err != nil {
return err
}
if !accountExists {
return fmt.Errorf("Storage Account %q Not Found", storageAccountName)
}
options := &storage.FileRequestOptions{}
reference := fileClient.GetShareReference(name)
log.Printf("[INFO] Setting share %q properties in storage account %q", name, storageAccountName)
reference.Properties = storage.ShareProperties{
Quota: d.Get("quota").(int),
}
if err := reference.SetProperties(options); err != nil {
return fmt.Errorf("Error setting properties on Storage Share %q: %+v", name, err)
}
return resourceArmStorageShareRead(d, meta)
}
func resourceArmStorageShareDelete(d *schema.ResourceData, meta interface{}) error {
armClient := meta.(*ArmClient)
ctx := armClient.StopContext
id := strings.Split(d.Id(), "/")
if len(id) != 3 {
return fmt.Errorf("ID was not in the expected format - expected `{name}/{resourceGroup}/{storageAccountName}` got %q", id)
}
name := id[0]
resourceGroupName := id[1]
storageAccountName := id[2]
fileClient, accountExists, err := armClient.getFileServiceClientForStorageAccount(ctx, resourceGroupName, storageAccountName)
if err != nil {
return err
}
if !accountExists {
log.Printf("[INFO]Storage Account %q doesn't exist so the file won't exist", storageAccountName)
return nil
}
reference := fileClient.GetShareReference(name)
options := &storage.FileRequestOptions{}
if _, err = reference.DeleteIfExists(options); err != nil {
return fmt.Errorf("Error deleting storage file %q: %s", name, err)
}
d.SetId("")
return nil
}
//Following the naming convention as laid out in the docs https://msdn.microsoft.com/library/azure/dn167011.aspx
func validateArmStorageShareName(v interface{}, k string) (warnings []string, errors []error) {
value := v.(string)
if !regexp.MustCompile(`^[0-9a-z-]+$`).MatchString(value) {
errors = append(errors, fmt.Errorf(
"only lowercase alphanumeric characters and hyphens allowed in %q: %q",
k, value))
}
if len(value) < 3 || len(value) > 63 {
errors = append(errors, fmt.Errorf(
"%q must be between 3 and 63 characters: %q", k, value))
}
if regexp.MustCompile(`^-`).MatchString(value) {
errors = append(errors, fmt.Errorf(
"%q cannot begin with a hyphen: %q", k, value))
}
if regexp.MustCompile(`[-]{2,}`).MatchString(value) {
errors = append(errors, fmt.Errorf(
"%q does not allow consecutive hyphens: %q", k, value))
}
return warnings, errors
}