-
Notifications
You must be signed in to change notification settings - Fork 1
/
resource_arm_storage_queue.go
170 lines (136 loc) · 4.59 KB
/
resource_arm_storage_queue.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
package azurerm
import (
"fmt"
"log"
"regexp"
"github.com/Azure/azure-sdk-for-go/storage"
"github.com/hashicorp/terraform/helper/schema"
)
func resourceArmStorageQueue() *schema.Resource {
return &schema.Resource{
Create: resourceArmStorageQueueCreate,
Read: resourceArmStorageQueueRead,
Exists: resourceArmStorageQueueExists,
Delete: resourceArmStorageQueueDelete,
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validateArmStorageQueueName,
},
"resource_group_name": resourceGroupNameSchema(),
"storage_account_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
},
}
}
func validateArmStorageQueueName(v interface{}, k string) (ws []string, errors []error) {
value := v.(string)
if !regexp.MustCompile(`^[a-z0-9-]+$`).MatchString(value) {
errors = append(errors, fmt.Errorf(
"only lowercase alphanumeric characters and hyphens allowed in %q", k))
}
if regexp.MustCompile(`^-`).MatchString(value) {
errors = append(errors, fmt.Errorf("%q cannot start with a hyphen", k))
}
if regexp.MustCompile(`-$`).MatchString(value) {
errors = append(errors, fmt.Errorf("%q cannot end with a hyphen", k))
}
if len(value) > 63 {
errors = append(errors, fmt.Errorf(
"%q cannot be longer than 63 characters", k))
}
if len(value) < 3 {
errors = append(errors, fmt.Errorf(
"%q must be at least 3 characters", k))
}
return
}
func resourceArmStorageQueueCreate(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)
queueClient, accountExists, err := armClient.getQueueServiceClientForStorageAccount(ctx, resourceGroupName, storageAccountName)
if err != nil {
return err
}
if !accountExists {
return fmt.Errorf("Storage Account %q Not Found", storageAccountName)
}
name := d.Get("name").(string)
log.Printf("[INFO] Creating queue %q in storage account %q", name, storageAccountName)
queueReference := queueClient.GetQueueReference(name)
options := &storage.QueueServiceOptions{}
err = queueReference.Create(options)
if err != nil {
return fmt.Errorf("Error creating storage queue on Azure: %s", err)
}
d.SetId(name)
return resourceArmStorageQueueRead(d, meta)
}
func resourceArmStorageQueueRead(d *schema.ResourceData, meta interface{}) error {
exists, err := resourceArmStorageQueueExists(d, meta)
if err != nil {
return err
}
if !exists {
// Exists already removed this from state
return nil
}
return nil
}
func resourceArmStorageQueueExists(d *schema.ResourceData, meta interface{}) (bool, error) {
armClient := meta.(*ArmClient)
ctx := armClient.StopContext
resourceGroupName := d.Get("resource_group_name").(string)
storageAccountName := d.Get("storage_account_name").(string)
queueClient, accountExists, err := armClient.getQueueServiceClientForStorageAccount(ctx, resourceGroupName, storageAccountName)
if err != nil {
return false, err
}
if !accountExists {
log.Printf("[DEBUG] Storage account %q not found, removing queue %q from state", storageAccountName, d.Id())
d.SetId("")
return false, nil
}
name := d.Get("name").(string)
log.Printf("[INFO] Checking for existence of storage queue %q.", name)
queueReference := queueClient.GetQueueReference(name)
exists, err := queueReference.Exists()
if err != nil {
return false, fmt.Errorf("error testing existence of storage queue %q: %s", name, err)
}
if !exists {
log.Printf("[INFO] Storage queue %q no longer exists, removing from state...", name)
d.SetId("")
}
return exists, nil
}
func resourceArmStorageQueueDelete(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)
queueClient, accountExists, err := armClient.getQueueServiceClientForStorageAccount(ctx, resourceGroupName, storageAccountName)
if err != nil {
return err
}
if !accountExists {
log.Printf("[INFO]Storage Account %q doesn't exist so the blob won't exist", storageAccountName)
return nil
}
name := d.Get("name").(string)
log.Printf("[INFO] Deleting storage queue %q", name)
queueReference := queueClient.GetQueueReference(name)
options := &storage.QueueServiceOptions{}
if err = queueReference.Delete(options); err != nil {
return fmt.Errorf("Error deleting storage queue %q: %s", name, err)
}
d.SetId("")
return nil
}