-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
resource_arm_container_registry_webhook.go
366 lines (296 loc) · 12 KB
/
resource_arm_container_registry_webhook.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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
package azurerm
import (
"fmt"
"log"
"regexp"
"time"
"github.com/Azure/azure-sdk-for-go/services/containerregistry/mgmt/2018-09-01/containerregistry"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/response"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/features"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tags"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/timeouts"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)
func resourceArmContainerRegistryWebhook() *schema.Resource {
return &schema.Resource{
Create: resourceArmContainerRegistryWebhookCreate,
Read: resourceArmContainerRegistryWebhookRead,
Update: resourceArmContainerRegistryWebhookUpdate,
Delete: resourceArmContainerRegistryWebhookDelete,
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{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validateAzureRMContainerRegistryWebhookName,
},
"resource_group_name": azure.SchemaResourceGroupName(),
"registry_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validateAzureRMContainerRegistryName,
},
"service_uri": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validateAzureRMContainerRegistryWebhookServiceUri,
},
"custom_headers": {
Type: schema.TypeMap,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"status": {
Type: schema.TypeString,
Optional: true,
Default: containerregistry.WebhookStatusEnabled,
ValidateFunc: validation.StringInSlice([]string{
string(containerregistry.WebhookStatusDisabled),
string(containerregistry.WebhookStatusEnabled),
}, false),
},
"scope": {
Type: schema.TypeString,
Optional: true,
Default: "",
},
"actions": {
Type: schema.TypeSet,
Required: true,
MinItems: 1,
Elem: &schema.Schema{
Type: schema.TypeString,
ValidateFunc: validation.StringInSlice([]string{
string(containerregistry.ChartDelete),
string(containerregistry.ChartPush),
string(containerregistry.Delete),
string(containerregistry.Push),
string(containerregistry.Quarantine),
}, false),
},
},
"location": azure.SchemaLocation(),
"tags": tags.Schema(),
},
}
}
func resourceArmContainerRegistryWebhookCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).Containers.WebhooksClient
ctx, cancel := timeouts.ForCreate(meta.(*ArmClient).StopContext, d)
defer cancel()
log.Printf("[INFO] preparing arguments for AzureRM Container Registry Webhook creation.")
resourceGroup := d.Get("resource_group_name").(string)
registryName := d.Get("registry_name").(string)
name := d.Get("name").(string)
if features.ShouldResourcesBeImported() && d.IsNewResource() {
existing, err := client.Get(ctx, resourceGroup, registryName, name)
if err != nil {
if !utils.ResponseWasNotFound(existing.Response) {
return fmt.Errorf("Error checking for presence of existing Container Registry Webhook %q (Resource Group %q, Registry %q): %s", name, resourceGroup, registryName, err)
}
}
if existing.ID != nil && *existing.ID != "" {
return tf.ImportAsExistsError("azurerm_container_registry_webhook", *existing.ID)
}
}
location := azure.NormalizeLocation(d.Get("location").(string))
t := d.Get("tags").(map[string]interface{})
webhook := containerregistry.WebhookCreateParameters{
Location: &location,
WebhookPropertiesCreateParameters: expandWebhookPropertiesCreateParameters(d),
Tags: tags.Expand(t),
}
future, err := client.Create(ctx, resourceGroup, registryName, name, webhook)
if err != nil {
return fmt.Errorf("Error creating Container Registry Webhook %q (Resource Group %q, Registry %q): %+v", name, resourceGroup, registryName, err)
}
if err = future.WaitForCompletionRef(ctx, client.Client); err != nil {
return fmt.Errorf("Error waiting for creation of Container Registry %q (Resource Group %q, Registry %q): %+v", name, resourceGroup, registryName, err)
}
read, err := client.Get(ctx, resourceGroup, registryName, name)
if err != nil {
return fmt.Errorf("Error retrieving Container Registry %q (Resource Group %q, Registry %q): %+v", name, resourceGroup, registryName, err)
}
if read.ID == nil {
return fmt.Errorf("Cannot read Container Registry %q (resource group %q, Registry %q) ID", name, resourceGroup, registryName)
}
d.SetId(*read.ID)
return resourceArmContainerRegistryWebhookRead(d, meta)
}
func resourceArmContainerRegistryWebhookUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).Containers.WebhooksClient
ctx, cancel := timeouts.ForUpdate(meta.(*ArmClient).StopContext, d)
defer cancel()
log.Printf("[INFO] preparing arguments for AzureRM Container Registry Webhook update.")
id, err := azure.ParseAzureResourceID(d.Id())
if err != nil {
return err
}
resourceGroup := id.ResourceGroup
registryName := id.Path["registries"]
name := id.Path["webhooks"]
t := d.Get("tags").(map[string]interface{})
webhook := containerregistry.WebhookUpdateParameters{
WebhookPropertiesUpdateParameters: expandWebhookPropertiesUpdateParameters(d),
Tags: tags.Expand(t),
}
future, err := client.Update(ctx, resourceGroup, registryName, name, webhook)
if err != nil {
return fmt.Errorf("Error updating Container Registry Webhook %q (Resource Group %q, Registry %q): %+v", name, resourceGroup, registryName, err)
}
if err = future.WaitForCompletionRef(ctx, client.Client); err != nil {
return fmt.Errorf("Error waiting for completion of Container Registry Webhook %q (Resource Group %q, Registry %q): %+v", name, resourceGroup, registryName, err)
}
return resourceArmContainerRegistryWebhookRead(d, meta)
}
func resourceArmContainerRegistryWebhookRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).Containers.WebhooksClient
ctx, cancel := timeouts.ForRead(meta.(*ArmClient).StopContext, d)
defer cancel()
id, err := azure.ParseAzureResourceID(d.Id())
if err != nil {
return err
}
resourceGroup := id.ResourceGroup
registryName := id.Path["registries"]
name := id.Path["webhooks"]
resp, err := client.Get(ctx, resourceGroup, registryName, name)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
log.Printf("[DEBUG] Container Registry Webhook %q was not found in Resource Group %q for Registry %q", name, resourceGroup, registryName)
d.SetId("")
return nil
}
return fmt.Errorf("Error making Read request on Azure Container Registry Webhook %q (Resource Group %q, Registry %q): %+v", name, resourceGroup, registryName, err)
}
callbackConfig, err := client.GetCallbackConfig(ctx, resourceGroup, registryName, name)
if err != nil {
return fmt.Errorf("Error making Read request on Azure Container Registry Webhook Callback Config %q (Resource Group %q, Registry %q): %+v", name, resourceGroup, registryName, err)
}
d.Set("resource_group_name", resourceGroup)
d.Set("registry_name", registryName)
d.Set("name", name)
if location := resp.Location; location != nil {
d.Set("location", azure.NormalizeLocation(*location))
}
d.Set("service_uri", callbackConfig.ServiceURI)
if callbackConfig.CustomHeaders != nil {
customHeaders := make(map[string]string)
for k, v := range callbackConfig.CustomHeaders {
customHeaders[k] = *v
}
d.Set("custom_headers", customHeaders)
}
if webhookProps := resp.WebhookProperties; webhookProps != nil {
if webhookProps.Status != "" {
d.Set("status", string(webhookProps.Status))
}
if webhookProps.Scope != nil {
d.Set("scope", webhookProps.Scope)
}
webhookActions := make([]string, len(*webhookProps.Actions))
for i, action := range *webhookProps.Actions {
webhookActions[i] = string(action)
}
d.Set("actions", webhookActions)
}
return tags.FlattenAndSet(d, resp.Tags)
}
func resourceArmContainerRegistryWebhookDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).Containers.WebhooksClient
ctx, cancel := timeouts.ForDelete(meta.(*ArmClient).StopContext, d)
defer cancel()
id, err := azure.ParseAzureResourceID(d.Id())
if err != nil {
return err
}
resourceGroup := id.ResourceGroup
registryName := id.Path["registries"]
name := id.Path["webhooks"]
future, err := client.Delete(ctx, resourceGroup, registryName, name)
if err != nil {
if response.WasNotFound(future.Response()) {
return nil
}
return fmt.Errorf("Error issuing Azure ARM delete request of Container Registry Webhook '%s': %+v", name, err)
}
if err = future.WaitForCompletionRef(ctx, client.Client); err != nil {
if response.WasNotFound(future.Response()) {
return nil
}
return fmt.Errorf("Error issuing Azure ARM delete request of Container Registry Webhook '%s': %+v", name, err)
}
return nil
}
func validateAzureRMContainerRegistryWebhookName(v interface{}, k string) (warnings []string, errors []error) {
value := v.(string)
if !regexp.MustCompile(`^[a-zA-Z0-9]{5,50}$`).MatchString(value) {
errors = append(errors, fmt.Errorf(
"alpha numeric characters only are allowed and between 5 and 50 characters in %q: %q", k, value))
}
return warnings, errors
}
func validateAzureRMContainerRegistryWebhookServiceUri(v interface{}, k string) (warnings []string, errors []error) {
value := v.(string)
if !regexp.MustCompile(`^https?://[^\s]+$`).MatchString(value) {
errors = append(errors, fmt.Errorf(
"%q must start with http:// or https:// and must not contain whitespaces: %q", k, value))
}
return warnings, errors
}
func expandWebhookPropertiesCreateParameters(d *schema.ResourceData) *containerregistry.WebhookPropertiesCreateParameters {
serviceUri := d.Get("service_uri").(string)
scope := d.Get("scope").(string)
customHeaders := make(map[string]*string)
for k, v := range d.Get("custom_headers").(map[string]interface{}) {
customHeaders[k] = utils.String(v.(string))
}
actions := expandWebhookActions(d)
webhookProperties := containerregistry.WebhookPropertiesCreateParameters{
ServiceURI: &serviceUri,
CustomHeaders: customHeaders,
Actions: actions,
Scope: &scope,
}
webhookProperties.Status = containerregistry.WebhookStatus(d.Get("status").(string))
return &webhookProperties
}
func expandWebhookPropertiesUpdateParameters(d *schema.ResourceData) *containerregistry.WebhookPropertiesUpdateParameters {
serviceUri := d.Get("service_uri").(string)
scope := d.Get("scope").(string)
customHeaders := make(map[string]*string)
for k, v := range d.Get("custom_headers").(map[string]interface{}) {
customHeaders[k] = utils.String(v.(string))
}
webhookProperties := containerregistry.WebhookPropertiesUpdateParameters{
ServiceURI: &serviceUri,
CustomHeaders: customHeaders,
Actions: expandWebhookActions(d),
Scope: &scope,
Status: containerregistry.WebhookStatus(d.Get("status").(string)),
}
return &webhookProperties
}
func expandWebhookActions(d *schema.ResourceData) *[]containerregistry.WebhookAction {
actions := make([]containerregistry.WebhookAction, 0)
for _, action := range d.Get("actions").(*schema.Set).List() {
actions = append(actions, containerregistry.WebhookAction(action.(string)))
}
return &actions
}