forked from hashicorp/terraform
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_compute_url_map.go
702 lines (555 loc) · 17.8 KB
/
resource_compute_url_map.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
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
package google
import (
"fmt"
"log"
"strconv"
"github.com/hashicorp/terraform/helper/schema"
"google.golang.org/api/compute/v1"
"google.golang.org/api/googleapi"
)
func resourceComputeUrlMap() *schema.Resource {
return &schema.Resource{
Create: resourceComputeUrlMapCreate,
Read: resourceComputeUrlMapRead,
Update: resourceComputeUrlMapUpdate,
Delete: resourceComputeUrlMapDelete,
Schema: map[string]*schema.Schema{
"default_service": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"name": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"description": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
"fingerprint": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"host_rule": &schema.Schema{
Type: schema.TypeSet,
Optional: true,
// TODO(evandbrown): Enable when lists support validation
//ValidateFunc: validateHostRules,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"description": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
"hosts": &schema.Schema{
Type: schema.TypeList,
Required: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"path_matcher": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
},
},
},
"id": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"path_matcher": &schema.Schema{
Type: schema.TypeList,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"default_service": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"description": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
"name": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"path_rule": &schema.Schema{
Type: schema.TypeList,
Required: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"paths": &schema.Schema{
Type: schema.TypeList,
Required: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"service": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
},
},
},
},
},
},
"project": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"self_link": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"test": &schema.Schema{
Type: schema.TypeList,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"description": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
"host": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"path": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"service": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
},
},
},
},
}
}
func createHostRule(v interface{}) *compute.HostRule {
_hostRule := v.(map[string]interface{})
_hosts := _hostRule["hosts"].([]interface{})
hosts := make([]string, len(_hosts))
for i, v := range _hosts {
hosts[i] = v.(string)
}
pathMatcher := _hostRule["path_matcher"].(string)
hostRule := &compute.HostRule{
Hosts: hosts,
PathMatcher: pathMatcher,
}
if v, ok := _hostRule["description"]; ok {
hostRule.Description = v.(string)
}
return hostRule
}
func createPathMatcher(v interface{}) *compute.PathMatcher {
_pathMatcher := v.(map[string]interface{})
_pathRules := _pathMatcher["path_rule"].([]interface{})
pathRules := make([]*compute.PathRule, len(_pathRules))
for ip, vp := range _pathRules {
_pathRule := vp.(map[string]interface{})
_paths := _pathRule["paths"].([]interface{})
paths := make([]string, len(_paths))
for ipp, vpp := range _paths {
paths[ipp] = vpp.(string)
}
service := _pathRule["service"].(string)
pathRule := &compute.PathRule{
Paths: paths,
Service: service,
}
pathRules[ip] = pathRule
}
name := _pathMatcher["name"].(string)
defaultService := _pathMatcher["default_service"].(string)
pathMatcher := &compute.PathMatcher{
PathRules: pathRules,
Name: name,
DefaultService: defaultService,
}
if vp, okp := _pathMatcher["description"]; okp {
pathMatcher.Description = vp.(string)
}
return pathMatcher
}
func createUrlMapTest(v interface{}) *compute.UrlMapTest {
_test := v.(map[string]interface{})
host := _test["host"].(string)
path := _test["path"].(string)
service := _test["service"].(string)
test := &compute.UrlMapTest{
Host: host,
Path: path,
Service: service,
}
if vp, okp := _test["description"]; okp {
test.Description = vp.(string)
}
return test
}
func resourceComputeUrlMapCreate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
project, err := getProject(d, config)
if err != nil {
return err
}
name := d.Get("name").(string)
defaultService := d.Get("default_service").(string)
urlMap := &compute.UrlMap{
Name: name,
DefaultService: defaultService,
}
if v, ok := d.GetOk("description"); ok {
urlMap.Description = v.(string)
}
_hostRules := d.Get("host_rule").(*schema.Set)
urlMap.HostRules = make([]*compute.HostRule, _hostRules.Len())
for i, v := range _hostRules.List() {
urlMap.HostRules[i] = createHostRule(v)
}
_pathMatchers := d.Get("path_matcher").([]interface{})
urlMap.PathMatchers = make([]*compute.PathMatcher, len(_pathMatchers))
for i, v := range _pathMatchers {
urlMap.PathMatchers[i] = createPathMatcher(v)
}
_tests := make([]interface{}, 0)
if v, ok := d.GetOk("test"); ok {
_tests = v.([]interface{})
}
urlMap.Tests = make([]*compute.UrlMapTest, len(_tests))
for i, v := range _tests {
urlMap.Tests[i] = createUrlMapTest(v)
}
op, err := config.clientCompute.UrlMaps.Insert(project, urlMap).Do()
if err != nil {
return fmt.Errorf("Error, failed to insert Url Map %s: %s", name, err)
}
err = computeOperationWaitGlobal(config, op, project, "Insert Url Map")
if err != nil {
return fmt.Errorf("Error, failed waitng to insert Url Map %s: %s", name, err)
}
return resourceComputeUrlMapRead(d, meta)
}
func resourceComputeUrlMapRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
project, err := getProject(d, config)
if err != nil {
return err
}
name := d.Get("name").(string)
urlMap, err := config.clientCompute.UrlMaps.Get(project, name).Do()
if err != nil {
if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 {
log.Printf("[WARN] Removing URL Map %q because it's gone", d.Get("name").(string))
// The resource doesn't exist anymore
d.SetId("")
return nil
}
return fmt.Errorf("Error, failed to get Url Map %s: %s", name, err)
}
d.SetId(name)
d.Set("self_link", urlMap.SelfLink)
d.Set("id", strconv.FormatUint(urlMap.Id, 10))
d.Set("fingerprint", urlMap.Fingerprint)
hostRuleMap := make(map[string]*compute.HostRule)
for _, v := range urlMap.HostRules {
hostRuleMap[v.PathMatcher] = v
}
/* Only read host rules into our TF state that we have defined */
_hostRules := d.Get("host_rule").(*schema.Set).List()
_newHostRules := make([]interface{}, 0)
for _, v := range _hostRules {
_hostRule := v.(map[string]interface{})
_pathMatcher := _hostRule["path_matcher"].(string)
/* Delete local entries that are no longer found on the GCE server */
if hostRule, ok := hostRuleMap[_pathMatcher]; ok {
_newHostRule := make(map[string]interface{})
_newHostRule["path_matcher"] = _pathMatcher
hostsSet := make(map[string]bool)
for _, host := range hostRule.Hosts {
hostsSet[host] = true
}
/* Only store hosts we are keeping track of */
_newHosts := make([]interface{}, 0)
for _, vp := range _hostRule["hosts"].([]interface{}) {
if _, okp := hostsSet[vp.(string)]; okp {
_newHosts = append(_newHosts, vp)
}
}
_newHostRule["hosts"] = _newHosts
_newHostRule["description"] = hostRule.Description
_newHostRules = append(_newHostRules, _newHostRule)
}
}
d.Set("host_rule", _newHostRules)
pathMatcherMap := make(map[string]*compute.PathMatcher)
for _, v := range urlMap.PathMatchers {
pathMatcherMap[v.Name] = v
}
/* Only read path matchers into our TF state that we have defined */
_pathMatchers := d.Get("path_matcher").([]interface{})
_newPathMatchers := make([]interface{}, 0)
for _, v := range _pathMatchers {
_pathMatcher := v.(map[string]interface{})
_name := _pathMatcher["name"].(string)
if pathMatcher, ok := pathMatcherMap[_name]; ok {
_newPathMatcher := make(map[string]interface{})
_newPathMatcher["name"] = _name
_newPathMatcher["default_service"] = pathMatcher.DefaultService
_newPathMatcher["description"] = pathMatcher.Description
_newPathRules := make([]interface{}, len(pathMatcher.PathRules))
for ip, pathRule := range pathMatcher.PathRules {
_newPathRule := make(map[string]interface{})
_newPathRule["service"] = pathRule.Service
_paths := make([]interface{}, len(pathRule.Paths))
for ipp, vpp := range pathRule.Paths {
_paths[ipp] = vpp
}
_newPathRule["paths"] = _paths
_newPathRules[ip] = _newPathRule
}
_newPathMatcher["path_rule"] = _newPathRules
_newPathMatchers = append(_newPathMatchers, _newPathMatcher)
}
}
d.Set("path_matcher", _newPathMatchers)
testMap := make(map[string]*compute.UrlMapTest)
for _, v := range urlMap.Tests {
testMap[fmt.Sprintf("%s/%s", v.Host, v.Path)] = v
}
_tests := make([]interface{}, 0)
/* Only read tests into our TF state that we have defined */
if v, ok := d.GetOk("test"); ok {
_tests = v.([]interface{})
}
_newTests := make([]interface{}, 0)
for _, v := range _tests {
_test := v.(map[string]interface{})
_host := _test["host"].(string)
_path := _test["path"].(string)
/* Delete local entries that are no longer found on the GCE server */
if test, ok := testMap[fmt.Sprintf("%s/%s", _host, _path)]; ok {
_newTest := make(map[string]interface{})
_newTest["host"] = _host
_newTest["path"] = _path
_newTest["description"] = test.Description
_newTest["service"] = test.Service
_newTests = append(_newTests, _newTest)
}
}
d.Set("test", _newTests)
return nil
}
func resourceComputeUrlMapUpdate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
project, err := getProject(d, config)
if err != nil {
return err
}
name := d.Get("name").(string)
urlMap, err := config.clientCompute.UrlMaps.Get(project, name).Do()
if err != nil {
return fmt.Errorf("Error, failed to get Url Map %s: %s", name, err)
}
urlMap.DefaultService = d.Get("default_service").(string)
if v, ok := d.GetOk("description"); ok {
urlMap.Description = v.(string)
}
if d.HasChange("host_rule") {
_oldHostRules, _newHostRules := d.GetChange("host_rule")
_oldHostRulesMap := make(map[string]interface{})
_newHostRulesMap := make(map[string]interface{})
for _, v := range _oldHostRules.(*schema.Set).List() {
_hostRule := v.(map[string]interface{})
_oldHostRulesMap[_hostRule["path_matcher"].(string)] = v
}
for _, v := range _newHostRules.(*schema.Set).List() {
_hostRule := v.(map[string]interface{})
_newHostRulesMap[_hostRule["path_matcher"].(string)] = v
}
newHostRules := make([]*compute.HostRule, 0)
/* Decide which host rules to keep */
for _, v := range urlMap.HostRules {
/* If it's in the old state, we have ownership over the host rule */
if vOld, ok := _oldHostRulesMap[v.PathMatcher]; ok {
if vNew, ok := _newHostRulesMap[v.PathMatcher]; ok {
/* Adjust for any changes made to this rule */
_newHostRule := vNew.(map[string]interface{})
_oldHostRule := vOld.(map[string]interface{})
_newHostsSet := make(map[string]bool)
_oldHostsSet := make(map[string]bool)
hostRule := &compute.HostRule{
PathMatcher: v.PathMatcher,
}
for _, v := range _newHostRule["hosts"].([]interface{}) {
_newHostsSet[v.(string)] = true
}
for _, v := range _oldHostRule["hosts"].([]interface{}) {
_oldHostsSet[v.(string)] = true
}
/* Only add hosts that have been added locally or are new,
* not touching those from the GCE server state */
for _, host := range v.Hosts {
_, okNew := _newHostsSet[host]
_, okOld := _oldHostsSet[host]
/* Drop deleted hosts */
if okOld && !okNew {
continue
}
hostRule.Hosts = append(hostRule.Hosts, host)
/* Kep track of the fact that this host was added */
delete(_newHostsSet, host)
}
/* Now add in the brand new entries */
for host, _ := range _newHostsSet {
hostRule.Hosts = append(hostRule.Hosts, host)
}
if v, ok := _newHostRule["description"]; ok {
hostRule.Description = v.(string)
}
newHostRules = append(newHostRules, hostRule)
/* Record that we've include this host rule */
delete(_newHostRulesMap, v.PathMatcher)
} else {
/* It's been deleted */
continue
}
} else {
if vNew, ok := _newHostRulesMap[v.PathMatcher]; ok {
newHostRules = append(newHostRules, createHostRule(vNew))
/* Record that we've include this host rule */
delete(_newHostRulesMap, v.PathMatcher)
} else {
/* It wasn't created or modified locally */
newHostRules = append(newHostRules, v)
}
}
}
/* Record brand new host rules (ones not deleted above) */
for _, v := range _newHostRulesMap {
newHostRules = append(newHostRules, createHostRule(v))
}
urlMap.HostRules = newHostRules
}
if d.HasChange("path_matcher") {
_oldPathMatchers, _newPathMatchers := d.GetChange("path_matcher")
_oldPathMatchersMap := make(map[string]interface{})
_newPathMatchersMap := make(map[string]interface{})
for _, v := range _oldPathMatchers.([]interface{}) {
_pathMatcher := v.(map[string]interface{})
_oldPathMatchersMap[_pathMatcher["name"].(string)] = v
}
for _, v := range _newPathMatchers.([]interface{}) {
_pathMatcher := v.(map[string]interface{})
_newPathMatchersMap[_pathMatcher["name"].(string)] = v
}
newPathMatchers := make([]*compute.PathMatcher, 0)
/* Decide which path matchers to keep */
for _, v := range urlMap.PathMatchers {
/* If it's in the old state, we have ownership over the host rule */
_, okOld := _oldPathMatchersMap[v.Name]
vNew, okNew := _newPathMatchersMap[v.Name]
/* Drop deleted entries */
if okOld && !okNew {
continue
}
/* Don't change entries that don't belong to us */
if !okNew {
newPathMatchers = append(newPathMatchers, v)
} else {
newPathMatchers = append(newPathMatchers, createPathMatcher(vNew))
delete(_newPathMatchersMap, v.Name)
}
}
/* Record brand new host rules */
for _, v := range _newPathMatchersMap {
newPathMatchers = append(newPathMatchers, createPathMatcher(v))
}
urlMap.PathMatchers = newPathMatchers
}
if d.HasChange("tests") {
_oldTests, _newTests := d.GetChange("path_matcher")
_oldTestsMap := make(map[string]interface{})
_newTestsMap := make(map[string]interface{})
for _, v := range _oldTests.([]interface{}) {
_test := v.(map[string]interface{})
ident := fmt.Sprintf("%s/%s", _test["host"].(string), _test["path"].(string))
_oldTestsMap[ident] = v
}
for _, v := range _newTests.([]interface{}) {
_test := v.(map[string]interface{})
ident := fmt.Sprintf("%s/%s", _test["host"].(string), _test["path"].(string))
_newTestsMap[ident] = v
}
newTests := make([]*compute.UrlMapTest, 0)
/* Decide which path matchers to keep */
for _, v := range urlMap.Tests {
ident := fmt.Sprintf("%s/%s", v.Host, v.Path)
/* If it's in the old state, we have ownership over the host rule */
_, okOld := _oldTestsMap[ident]
vNew, okNew := _newTestsMap[ident]
/* Drop deleted entries */
if okOld && !okNew {
continue
}
/* Don't change entries that don't belong to us */
if !okNew {
newTests = append(newTests, v)
} else {
newTests = append(newTests, createUrlMapTest(vNew))
delete(_newTestsMap, ident)
}
}
/* Record brand new host rules */
for _, v := range _newTestsMap {
newTests = append(newTests, createUrlMapTest(v))
}
urlMap.Tests = newTests
}
op, err := config.clientCompute.UrlMaps.Update(project, urlMap.Name, urlMap).Do()
if err != nil {
return fmt.Errorf("Error, failed to update Url Map %s: %s", name, err)
}
err = computeOperationWaitGlobal(config, op, project, "Update Url Map")
if err != nil {
return fmt.Errorf("Error, failed waitng to update Url Map %s: %s", name, err)
}
return resourceComputeUrlMapRead(d, meta)
}
func resourceComputeUrlMapDelete(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
project, err := getProject(d, config)
if err != nil {
return err
}
name := d.Get("name").(string)
op, err := config.clientCompute.UrlMaps.Delete(project, name).Do()
if err != nil {
return fmt.Errorf("Error, failed to delete Url Map %s: %s", name, err)
}
err = computeOperationWaitGlobal(config, op, project, "Delete Url Map")
if err != nil {
return fmt.Errorf("Error, failed waitng to delete Url Map %s: %s", name, err)
}
return nil
}
func validateHostRules(v interface{}, k string) (ws []string, es []error) {
pathMatchers := make(map[string]bool)
hostRules := v.([]interface{})
for _, hri := range hostRules {
hr := hri.(map[string]interface{})
pm := hr["path_matcher"].(string)
if pathMatchers[pm] {
es = append(es, fmt.Errorf("Multiple host_rule entries with the same path_matcher are not allowed. Please collapse all hosts with the same path_matcher into one host_rule"))
return
}
pathMatchers[pm] = true
}
return
}