forked from hashicorp/terraform-provider-google
-
Notifications
You must be signed in to change notification settings - Fork 1
/
resource_cloudfunctions_function.go
496 lines (416 loc) · 13.9 KB
/
resource_cloudfunctions_function.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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
package google
import (
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
"google.golang.org/api/cloudfunctions/v1"
"fmt"
"log"
"net/url"
"regexp"
"strconv"
"strings"
"time"
)
// Min is 1 second, max is 9 minutes 540 sec
const functionTimeOutMax = 540
const functionTimeOutMin = 1
const functionDefaultTimeout = 60
var functionAllowedMemory = map[int]bool{
128: true,
256: true,
512: true,
1024: true,
2048: true,
}
// For now CloudFunctions are allowed only in the following locations.
// Please see https://cloud.google.com/about/locations/
var validCloudFunctionRegion = validation.StringInSlice([]string{"us-central1", "us-east1", "europe-west1", "asia-northeast1"}, true)
const functionDefaultAllowedMemoryMb = 256
type cloudFunctionId struct {
Project string
Region string
Name string
}
func (s *cloudFunctionId) cloudFunctionId() string {
return fmt.Sprintf("projects/%s/locations/%s/functions/%s", s.Project, s.Region, s.Name)
}
func (s *cloudFunctionId) locationId() string {
return fmt.Sprintf("projects/%s/locations/%s", s.Project, s.Region)
}
func (s *cloudFunctionId) terraformId() string {
return fmt.Sprintf("%s/%s/%s", s.Project, s.Region, s.Name)
}
func parseCloudFunctionId(id string, config *Config) (*cloudFunctionId, error) {
parts := strings.Split(id, "/")
cloudFuncIdRegex := regexp.MustCompile("^([a-z0-9-]+)/([a-z0-9-])+/([a-zA-Z0-9_-]{1,63})$")
if cloudFuncIdRegex.MatchString(id) {
return &cloudFunctionId{
Project: parts[0],
Region: parts[1],
Name: parts[2],
}, nil
}
return nil, fmt.Errorf("Invalid CloudFunction id format, expecting " +
"`{projectId}/{regionId}/{cloudFunctionName}`")
}
func joinMapKeys(mapToJoin *map[int]bool) string {
var keys []string
for key := range *mapToJoin {
keys = append(keys, strconv.Itoa(key))
}
return strings.Join(keys, ",")
}
func resourceCloudFunctionsFunction() *schema.Resource {
return &schema.Resource{
Create: resourceCloudFunctionsCreate,
Read: resourceCloudFunctionsRead,
Update: resourceCloudFunctionsUpdate,
Delete: resourceCloudFunctionsDestroy,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(5 * time.Minute),
Read: schema.DefaultTimeout(5 * time.Minute),
Delete: schema.DefaultTimeout(5 * time.Minute),
},
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) {
value := v.(string)
if len(value) > 48 {
errors = append(errors, fmt.Errorf(
"%q cannot be longer than 48 characters", k))
}
if !regexp.MustCompile("^[a-zA-Z0-9-]+$").MatchString(value) {
errors = append(errors, fmt.Errorf(
"%q can only contain letters, numbers and hyphens", k))
}
if !regexp.MustCompile("^[a-zA-Z]").MatchString(value) {
errors = append(errors, fmt.Errorf(
"%q must start with a letter", k))
}
if !regexp.MustCompile("[a-zA-Z0-9]$").MatchString(value) {
errors = append(errors, fmt.Errorf(
"%q must end with a number or a letter", k))
}
return
},
},
"source_archive_bucket": {
Type: schema.TypeString,
Required: true,
},
"source_archive_object": {
Type: schema.TypeString,
Required: true,
},
"description": {
Type: schema.TypeString,
Optional: true,
},
"available_memory_mb": {
Type: schema.TypeInt,
Optional: true,
Default: functionDefaultAllowedMemoryMb,
ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) {
availableMemoryMB := v.(int)
if functionAllowedMemory[availableMemoryMB] != true {
errors = append(errors, fmt.Errorf("Allowed values for memory (in MB) are: %s . Got %d",
joinMapKeys(&functionAllowedMemory), availableMemoryMB))
}
return
},
},
"timeout": {
Type: schema.TypeInt,
Optional: true,
Default: functionDefaultTimeout,
ValidateFunc: validation.IntBetween(functionTimeOutMin, functionTimeOutMax),
},
"entry_point": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"labels": {
Type: schema.TypeMap,
Optional: true,
},
"trigger_bucket": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ConflictsWith: []string{"trigger_http", "trigger_topic"},
},
"trigger_http": {
Type: schema.TypeBool,
Optional: true,
ForceNew: true,
ConflictsWith: []string{"trigger_bucket", "trigger_topic"},
},
"trigger_topic": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ConflictsWith: []string{"trigger_http", "trigger_bucket"},
},
"https_trigger_url": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"retry_on_failure": {
Type: schema.TypeBool,
Optional: true,
ConflictsWith: []string{"trigger_http"},
},
"project": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"region": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
ValidateFunc: validCloudFunctionRegion,
},
},
}
}
func resourceCloudFunctionsCreate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
project, err := getProject(d, config)
if err != nil {
return err
}
region, err := getRegion(d, config)
if err != nil {
return err
}
// We do this extra validation here since most regions are not valid, and the
// error message that Cloud Functions has for "wrong region" is not specific.
// Provider-level region fetching skips validation, because it's not possible
// for the provider-level region to know about the field-level validator.
_, errs := validCloudFunctionRegion(region, "region")
if len(errs) > 0 {
return errs[0]
}
cloudFuncId := &cloudFunctionId{
Project: project,
Region: region,
Name: d.Get("name").(string),
}
function := &cloudfunctions.CloudFunction{
Name: cloudFuncId.cloudFunctionId(),
}
sourceArchiveBucket := d.Get("source_archive_bucket").(string)
sourceArchiveObj := d.Get("source_archive_object").(string)
function.SourceArchiveUrl = fmt.Sprintf("gs://%v/%v", sourceArchiveBucket, sourceArchiveObj)
if v, ok := d.GetOk("available_memory_mb"); ok {
availableMemoryMb := v.(int)
function.AvailableMemoryMb = int64(availableMemoryMb)
}
if v, ok := d.GetOk("description"); ok {
function.Description = v.(string)
}
if v, ok := d.GetOk("entry_point"); ok {
function.EntryPoint = v.(string)
}
if v, ok := d.GetOk("timeout"); ok {
function.Timeout = fmt.Sprintf("%vs", v.(int))
}
v, triggHttpOk := d.GetOk("trigger_http")
if triggHttpOk && v.(bool) {
function.HttpsTrigger = &cloudfunctions.HttpsTrigger{}
}
v, triggTopicOk := d.GetOk("trigger_topic")
if triggTopicOk {
// Make PubSub event publish as in https://cloud.google.com/functions/docs/calling/pubsub
function.EventTrigger = &cloudfunctions.EventTrigger{
// Other events are not supported
EventType: "google.pubsub.topic.publish",
// Must be like projects/PROJECT_ID/topics/NAME
// Topic must be in same project as function
Resource: fmt.Sprintf("projects/%s/topics/%s", project, v.(string)),
}
if d.Get("retry_on_failure").(bool) {
function.EventTrigger.FailurePolicy = &cloudfunctions.FailurePolicy{
Retry: &cloudfunctions.Retry{},
}
}
}
v, triggBucketOk := d.GetOk("trigger_bucket")
if triggBucketOk {
// Make Storage event as in https://cloud.google.com/functions/docs/calling/storage
function.EventTrigger = &cloudfunctions.EventTrigger{
EventType: "providers/cloud.storage/eventTypes/object.change",
// Must be like projects/PROJECT_ID/buckets/NAME
// Bucket must be in same project as function
Resource: fmt.Sprintf("projects/%s/buckets/%s", project, v.(string)),
}
if d.Get("retry_on_failure").(bool) {
function.EventTrigger.FailurePolicy = &cloudfunctions.FailurePolicy{
Retry: &cloudfunctions.Retry{},
}
}
}
if !triggHttpOk && !triggTopicOk && !triggBucketOk {
return fmt.Errorf("One of arguments [trigger_topic, trigger_bucket, trigger_http] is required: " +
"You must specify a trigger when deploying a new function.")
}
if _, ok := d.GetOk("labels"); ok {
function.Labels = expandLabels(d)
}
log.Printf("[DEBUG] Creating cloud function: %s", function.Name)
op, err := config.clientCloudFunctions.Projects.Locations.Functions.Create(
cloudFuncId.locationId(), function).Do()
if err != nil {
return err
}
// Name of function should be unique
d.SetId(cloudFuncId.terraformId())
err = cloudFunctionsOperationWait(config.clientCloudFunctions, op, "Creating CloudFunctions Function")
if err != nil {
return err
}
return resourceCloudFunctionsRead(d, meta)
}
func resourceCloudFunctionsRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
cloudFuncId, err := parseCloudFunctionId(d.Id(), config)
if err != nil {
return err
}
function, err := config.clientCloudFunctions.Projects.Locations.Functions.Get(cloudFuncId.cloudFunctionId()).Do()
if err != nil {
return handleNotFoundError(err, d, fmt.Sprintf("Target CloudFunctions Function %q", cloudFuncId.Name))
}
d.Set("name", cloudFuncId.Name)
d.Set("description", function.Description)
d.Set("entry_point", function.EntryPoint)
d.Set("available_memory_mb", function.AvailableMemoryMb)
sRemoved := strings.Replace(function.Timeout, "s", "", -1)
timeout, err := strconv.Atoi(sRemoved)
if err != nil {
return err
}
d.Set("timeout", timeout)
d.Set("labels", function.Labels)
if function.SourceArchiveUrl != "" {
// sourceArchiveUrl should always be a Google Cloud Storage URL (e.g. gs://bucket/object)
// https://cloud.google.com/functions/docs/reference/rest/v1/projects.locations.functions
sourceURL, err := url.Parse(function.SourceArchiveUrl)
if err != nil {
return err
}
bucket := sourceURL.Host
object := strings.TrimLeft(sourceURL.Path, "/")
d.Set("source_archive_bucket", bucket)
d.Set("source_archive_object", object)
}
if function.HttpsTrigger != nil {
d.Set("trigger_http", true)
d.Set("https_trigger_url", function.HttpsTrigger.Url)
}
if function.EventTrigger != nil {
switch function.EventTrigger.EventType {
// From https://github.com/google/google-api-go-client/blob/master/cloudfunctions/v1/cloudfunctions-gen.go#L335
case "google.pubsub.topic.publish":
d.Set("trigger_topic", GetResourceNameFromSelfLink(function.EventTrigger.Resource))
case "providers/cloud.storage/eventTypes/object.change":
d.Set("trigger_bucket", GetResourceNameFromSelfLink(function.EventTrigger.Resource))
}
retry := function.EventTrigger.FailurePolicy != nil && function.EventTrigger.FailurePolicy.Retry != nil
d.Set("retry_on_failure", retry)
}
d.Set("region", cloudFuncId.Region)
d.Set("project", cloudFuncId.Project)
return nil
}
func resourceCloudFunctionsUpdate(d *schema.ResourceData, meta interface{}) error {
log.Printf("[DEBUG]: Updating google_cloudfunctions_function")
config := meta.(*Config)
cloudFuncId, err := parseCloudFunctionId(d.Id(), config)
if err != nil {
return err
}
d.Partial(true)
function := cloudfunctions.CloudFunction{
Name: cloudFuncId.cloudFunctionId(),
}
var updateMaskArr []string
if d.HasChange("available_memory_mb") {
availableMemoryMb := d.Get("available_memory_mb").(int)
function.AvailableMemoryMb = int64(availableMemoryMb)
updateMaskArr = append(updateMaskArr, "availableMemoryMb")
}
if d.HasChange("source_archive_bucket") || d.HasChange("source_archive_object") {
sourceArchiveBucket := d.Get("source_archive_bucket").(string)
sourceArchiveObj := d.Get("source_archive_object").(string)
function.SourceArchiveUrl = fmt.Sprintf("gs://%v/%v", sourceArchiveBucket, sourceArchiveObj)
updateMaskArr = append(updateMaskArr, "sourceArchiveUrl")
}
if d.HasChange("description") {
function.Description = d.Get("description").(string)
updateMaskArr = append(updateMaskArr, "description")
}
if d.HasChange("timeout") {
function.Timeout = fmt.Sprintf("%vs", d.Get("timeout").(int))
updateMaskArr = append(updateMaskArr, "timeout")
}
if d.HasChange("labels") {
function.Labels = expandLabels(d)
updateMaskArr = append(updateMaskArr, "labels")
}
if d.HasChange("retry_on_failure") {
if d.Get("retry_on_failure").(bool) {
if function.EventTrigger == nil {
function.EventTrigger = &cloudfunctions.EventTrigger{}
}
function.EventTrigger.FailurePolicy = &cloudfunctions.FailurePolicy{
Retry: &cloudfunctions.Retry{},
}
}
updateMaskArr = append(updateMaskArr, "eventTrigger.failurePolicy.retry")
}
if len(updateMaskArr) > 0 {
log.Printf("[DEBUG] Send Patch CloudFunction Configuration request: %#v", function)
updateMask := strings.Join(updateMaskArr, ",")
op, err := config.clientCloudFunctions.Projects.Locations.Functions.Patch(function.Name, &function).
UpdateMask(updateMask).Do()
if err != nil {
return fmt.Errorf("Error while updating cloudfunction configuration: %s", err)
}
err = cloudFunctionsOperationWait(config.clientCloudFunctions, op,
"Updating CloudFunctions Function")
if err != nil {
return err
}
}
d.Partial(false)
return resourceCloudFunctionsRead(d, meta)
}
func resourceCloudFunctionsDestroy(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
cloudFuncId, err := parseCloudFunctionId(d.Id(), config)
if err != nil {
return err
}
op, err := config.clientCloudFunctions.Projects.Locations.Functions.Delete(cloudFuncId.cloudFunctionId()).Do()
if err != nil {
return err
}
err = cloudFunctionsOperationWait(config.clientCloudFunctions, op, "Deleting CloudFunctions Function")
if err != nil {
return err
}
d.SetId("")
return nil
}