forked from hashicorp/terraform-provider-google-beta
/
resource_compute_forwarding_rule.go
1519 lines (1333 loc) · 59.6 KB
/
resource_compute_forwarding_rule.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
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
// ----------------------------------------------------------------------------
//
// *** AUTO GENERATED CODE *** Type: MMv1 ***
//
// ----------------------------------------------------------------------------
//
// This file is automatically generated by Magic Modules and manual
// changes will be clobbered when the file is regenerated.
//
// Please read more about how to change this file in
// .github/CONTRIBUTING.md.
//
// ----------------------------------------------------------------------------
package compute
import (
"fmt"
"log"
"reflect"
"strings"
"time"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/lorioux/google-beta/google-beta/tpgresource"
transport_tpg "github.com/lorioux/google-beta/google-beta/transport"
"github.com/lorioux/google-beta/google-beta/verify"
)
func ResourceComputeForwardingRule() *schema.Resource {
return &schema.Resource{
Create: resourceComputeForwardingRuleCreate,
Read: resourceComputeForwardingRuleRead,
Update: resourceComputeForwardingRuleUpdate,
Delete: resourceComputeForwardingRuleDelete,
Importer: &schema.ResourceImporter{
State: resourceComputeForwardingRuleImport,
},
Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(20 * time.Minute),
Update: schema.DefaultTimeout(20 * time.Minute),
Delete: schema.DefaultTimeout(20 * time.Minute),
},
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: `Name of the resource; provided by the client when the resource is created.
The name must be 1-63 characters long, and comply with
[RFC1035](https://www.ietf.org/rfc/rfc1035.txt).
Specifically, the name must be 1-63 characters long and match the regular
expression '[a-z]([-a-z0-9]*[a-z0-9])?' which means the first
character must be a lowercase letter, and all following characters must
be a dash, lowercase letter, or digit, except the last character, which
cannot be a dash.
For Private Service Connect forwarding rules that forward traffic to Google
APIs, the forwarding rule name must be a 1-20 characters string with
lowercase letters and numbers and must start with a letter.`,
},
"ip_address": {
Type: schema.TypeString,
Computed: true,
Optional: true,
ForceNew: true,
DiffSuppressFunc: tpgresource.InternalIpDiffSuppress,
Description: `IP address for which this forwarding rule accepts traffic. When a client
sends traffic to this IP address, the forwarding rule directs the traffic
to the referenced 'target' or 'backendService'.
While creating a forwarding rule, specifying an 'IPAddress' is
required under the following circumstances:
* When the 'target' is set to 'targetGrpcProxy' and
'validateForProxyless' is set to 'true', the
'IPAddress' should be set to '0.0.0.0'.
* When the 'target' is a Private Service Connect Google APIs
bundle, you must specify an 'IPAddress'.
Otherwise, you can optionally specify an IP address that references an
existing static (reserved) IP address resource. When omitted, Google Cloud
assigns an ephemeral IP address.
Use one of the following formats to specify an IP address while creating a
forwarding rule:
* IP address number, as in '100.1.2.3'
* IPv6 address range, as in '2600:1234::/96'
* Full resource URL, as in
'https://www.googleapis.com/compute/v1/projects/project_id/regions/region/addresses/address-name'
* Partial URL or by name, as in:
* 'projects/project_id/regions/region/addresses/address-name'
* 'regions/region/addresses/address-name'
* 'global/addresses/address-name'
* 'address-name'
The forwarding rule's 'target' or 'backendService',
and in most cases, also the 'loadBalancingScheme', determine the
type of IP address that you can use. For detailed information, see
[IP address
specifications](https://cloud.google.com/load-balancing/docs/forwarding-rule-concepts#ip_address_specifications).
When reading an 'IPAddress', the API always returns the IP
address number.`,
},
"ip_protocol": {
Type: schema.TypeString,
Computed: true,
Optional: true,
ForceNew: true,
ValidateFunc: verify.ValidateEnum([]string{"TCP", "UDP", "ESP", "AH", "SCTP", "ICMP", "L3_DEFAULT", ""}),
DiffSuppressFunc: tpgresource.CaseDiffSuppress,
Description: `The IP protocol to which this rule applies.
For protocol forwarding, valid
options are 'TCP', 'UDP', 'ESP',
'AH', 'SCTP', 'ICMP' and
'L3_DEFAULT'.
The valid IP protocols are different for different load balancing products
as described in [Load balancing
features](https://cloud.google.com/load-balancing/docs/features#protocols_from_the_load_balancer_to_the_backends).
A Forwarding Rule with protocol L3_DEFAULT can attach with target instance or
backend service with UNSPECIFIED protocol.
A forwarding rule with "L3_DEFAULT" IPProtocal cannot be attached to a backend service with TCP or UDP. Possible values: ["TCP", "UDP", "ESP", "AH", "SCTP", "ICMP", "L3_DEFAULT"]`,
},
"all_ports": {
Type: schema.TypeBool,
Optional: true,
ForceNew: true,
Description: `This field can only be used:
* If 'IPProtocol' is one of TCP, UDP, or SCTP.
* By internal TCP/UDP load balancers, backend service-based network load
balancers, and internal and external protocol forwarding.
This option should be set to TRUE when the Forwarding Rule
IPProtocol is set to L3_DEFAULT.
Set this field to true to allow packets addressed to any port or packets
lacking destination port information (for example, UDP fragments after the
first fragment) to be forwarded to the backends configured with this
forwarding rule.
The 'ports', 'port_range', and
'allPorts' fields are mutually exclusive.`,
},
"allow_global_access": {
Type: schema.TypeBool,
Optional: true,
Description: `This field is used along with the 'backend_service' field for
internal load balancing or with the 'target' field for internal
TargetInstance.
If the field is set to 'TRUE', clients can access ILB from all
regions.
Otherwise only allows access from clients in the same region as the
internal load balancer.`,
},
"allow_psc_global_access": {
Type: schema.TypeBool,
Optional: true,
Description: `This is used in PSC consumer ForwardingRule to control whether the PSC endpoint can be accessed from another region.`,
},
"backend_service": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
DiffSuppressFunc: tpgresource.CompareSelfLinkOrResourceName,
Description: `Identifies the backend service to which the forwarding rule sends traffic.
Required for Internal TCP/UDP Load Balancing and Network Load Balancing;
must be omitted for all other load balancer types.`,
},
"description": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Description: `An optional description of this resource. Provide this property when
you create the resource.`,
},
"ip_version": {
Type: schema.TypeString,
Computed: true,
Optional: true,
ForceNew: true,
ValidateFunc: verify.ValidateEnum([]string{"IPV4", "IPV6", ""}),
Description: `The IP address version that will be used by this forwarding rule.
Valid options are IPV4 and IPV6.
If not set, the IPv4 address will be used by default. Possible values: ["IPV4", "IPV6"]`,
},
"is_mirroring_collector": {
Type: schema.TypeBool,
Optional: true,
ForceNew: true,
Description: `Indicates whether or not this load balancer can be used as a collector for
packet mirroring. To prevent mirroring loops, instances behind this
load balancer will not have their traffic mirrored even if a
'PacketMirroring' rule applies to them.
This can only be set to true for load balancers that have their
'loadBalancingScheme' set to 'INTERNAL'.`,
},
"labels": {
Type: schema.TypeMap,
Optional: true,
Description: `Labels to apply to this forwarding rule. A list of key->value pairs.`,
Elem: &schema.Schema{Type: schema.TypeString},
},
"load_balancing_scheme": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ValidateFunc: verify.ValidateEnum([]string{"EXTERNAL", "EXTERNAL_MANAGED", "INTERNAL", "INTERNAL_MANAGED", ""}),
Description: `Specifies the forwarding rule type.
For more information about forwarding rules, refer to
[Forwarding rule concepts](https://cloud.google.com/load-balancing/docs/forwarding-rule-concepts). Default value: "EXTERNAL" Possible values: ["EXTERNAL", "EXTERNAL_MANAGED", "INTERNAL", "INTERNAL_MANAGED"]`,
Default: "EXTERNAL",
},
"network": {
Type: schema.TypeString,
Computed: true,
Optional: true,
ForceNew: true,
DiffSuppressFunc: tpgresource.CompareSelfLinkOrResourceName,
Description: `This field is not used for external load balancing.
For Internal TCP/UDP Load Balancing, this field identifies the network that
the load balanced IP should belong to for this Forwarding Rule.
If the subnetwork is specified, the network of the subnetwork will be used.
If neither subnetwork nor this field is specified, the default network will
be used.
For Private Service Connect forwarding rules that forward traffic to Google
APIs, a network must be provided.`,
},
"network_tier": {
Type: schema.TypeString,
Computed: true,
Optional: true,
ForceNew: true,
ValidateFunc: verify.ValidateEnum([]string{"PREMIUM", "STANDARD", ""}),
Description: `This signifies the networking tier used for configuring
this load balancer and can only take the following values:
'PREMIUM', 'STANDARD'.
For regional ForwardingRule, the valid values are 'PREMIUM' and
'STANDARD'. For GlobalForwardingRule, the valid value is
'PREMIUM'.
If this field is not specified, it is assumed to be 'PREMIUM'.
If 'IPAddress' is specified, this value must be equal to the
networkTier of the Address. Possible values: ["PREMIUM", "STANDARD"]`,
},
"no_automate_dns_zone": {
Type: schema.TypeBool,
Optional: true,
ForceNew: true,
Description: `This is used in PSC consumer ForwardingRule to control whether it should try to auto-generate a DNS zone or not. Non-PSC forwarding rules do not use this field.`,
},
"port_range": {
Type: schema.TypeString,
Computed: true,
Optional: true,
ForceNew: true,
DiffSuppressFunc: tpgresource.PortRangeDiffSuppress,
Description: `This field can only be used:
* If 'IPProtocol' is one of TCP, UDP, or SCTP.
* By backend service-based network load balancers, target pool-based
network load balancers, internal proxy load balancers, external proxy load
balancers, Traffic Director, external protocol forwarding, and Classic VPN.
Some products have restrictions on what ports can be used. See
[port specifications](https://cloud.google.com/load-balancing/docs/forwarding-rule-concepts#port_specifications)
for details.
Only packets addressed to ports in the specified range will be forwarded to
the backends configured with this forwarding rule.
The 'ports' and 'port_range' fields are mutually exclusive.
For external forwarding rules, two or more forwarding rules cannot use the
same '[IPAddress, IPProtocol]' pair, and cannot have
overlapping 'portRange's.
For internal forwarding rules within the same VPC network, two or more
forwarding rules cannot use the same '[IPAddress, IPProtocol]'
pair, and cannot have overlapping 'portRange's.`,
},
"ports": {
Type: schema.TypeSet,
Optional: true,
ForceNew: true,
Description: `This field can only be used:
* If 'IPProtocol' is one of TCP, UDP, or SCTP.
* By internal TCP/UDP load balancers, backend service-based network load
balancers, internal protocol forwarding and when protocol is not L3_DEFAULT.
You can specify a list of up to five ports by number, separated by commas.
The ports can be contiguous or discontiguous. Only packets addressed to
these ports will be forwarded to the backends configured with this
forwarding rule.
For external forwarding rules, two or more forwarding rules cannot use the
same '[IPAddress, IPProtocol]' pair, and cannot share any values
defined in 'ports'.
For internal forwarding rules within the same VPC network, two or more
forwarding rules cannot use the same '[IPAddress, IPProtocol]'
pair, and cannot share any values defined in 'ports'.
The 'ports' and 'port_range' fields are mutually exclusive.`,
MaxItems: 5,
Elem: &schema.Schema{
Type: schema.TypeString,
},
Set: schema.HashString,
},
"region": {
Type: schema.TypeString,
Computed: true,
Optional: true,
ForceNew: true,
DiffSuppressFunc: tpgresource.CompareSelfLinkOrResourceName,
Description: `A reference to the region where the regional forwarding rule resides.
This field is not applicable to global forwarding rules.`,
},
"service_directory_registrations": {
Type: schema.TypeList,
Computed: true,
Optional: true,
ForceNew: true,
Description: `Service Directory resources to register this forwarding rule with.
Currently, only supports a single Service Directory resource.`,
MinItems: 0,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"namespace": {
Type: schema.TypeString,
Computed: true,
Optional: true,
ForceNew: true,
Description: `Service Directory namespace to register the forwarding rule under.`,
},
"service": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Description: `Service Directory service to register the forwarding rule under.`,
},
},
},
},
"service_label": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ValidateFunc: verify.ValidateGCEName,
Description: `An optional prefix to the service name for this Forwarding Rule.
If specified, will be the first label of the fully qualified service
name.
The label must be 1-63 characters long, and comply with RFC1035.
Specifically, the label must be 1-63 characters long and match the
regular expression '[a-z]([-a-z0-9]*[a-z0-9])?' which means the first
character must be a lowercase letter, and all following characters
must be a dash, lowercase letter, or digit, except the last
character, which cannot be a dash.
This field is only used for INTERNAL load balancing.`,
},
"source_ip_ranges": {
Type: schema.TypeList,
Optional: true,
ForceNew: true,
Description: `If not empty, this Forwarding Rule will only forward the traffic when the source IP address matches one of the IP addresses or CIDR ranges set here. Note that a Forwarding Rule can only have up to 64 source IP ranges, and this field can only be used with a regional Forwarding Rule whose scheme is EXTERNAL. Each sourceIpRange entry should be either an IP address (for example, 1.2.3.4) or a CIDR range (for example, 1.2.3.0/24).`,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"subnetwork": {
Type: schema.TypeString,
Computed: true,
Optional: true,
ForceNew: true,
DiffSuppressFunc: tpgresource.CompareSelfLinkOrResourceName,
Description: `This field identifies the subnetwork that the load balanced IP should
belong to for this Forwarding Rule, used in internal load balancing and
network load balancing with IPv6.
If the network specified is in auto subnet mode, this field is optional.
However, a subnetwork must be specified if the network is in custom subnet
mode or when creating external forwarding rule with IPv6.`,
},
"target": {
Type: schema.TypeString,
Optional: true,
DiffSuppressFunc: tpgresource.CompareSelfLinkRelativePaths,
Description: `The URL of the target resource to receive the matched traffic. For
regional forwarding rules, this target must be in the same region as the
forwarding rule. For global forwarding rules, this target must be a global
load balancing resource.
The forwarded traffic must be of a type appropriate to the target object.
* For load balancers, see the "Target" column in [Port specifications](https://cloud.google.com/load-balancing/docs/forwarding-rule-concepts#ip_address_specifications).
* For Private Service Connect forwarding rules that forward traffic to Google APIs, provide the name of a supported Google API bundle:
* 'vpc-sc' - [ APIs that support VPC Service Controls](https://cloud.google.com/vpc-service-controls/docs/supported-products).
* 'all-apis' - [All supported Google APIs](https://cloud.google.com/vpc/docs/private-service-connect#supported-apis).
For Private Service Connect forwarding rules that forward traffic to managed services, the target must be a service attachment.`,
},
"base_forwarding_rule": {
Type: schema.TypeString,
Computed: true,
Description: `[Output Only] The URL for the corresponding base Forwarding Rule. By base Forwarding Rule, we mean the Forwarding Rule that has the same IP address, protocol, and port settings with the current Forwarding Rule, but without sourceIPRanges specified. Always empty if the current Forwarding Rule does not have sourceIPRanges specified.`,
},
"creation_timestamp": {
Type: schema.TypeString,
Computed: true,
Description: `Creation timestamp in RFC3339 text format.`,
},
"label_fingerprint": {
Type: schema.TypeString,
Computed: true,
Description: `The fingerprint used for optimistic locking of this resource. Used
internally during updates.`,
},
"psc_connection_id": {
Type: schema.TypeString,
Computed: true,
Description: `The PSC connection id of the PSC Forwarding Rule.`,
},
"psc_connection_status": {
Type: schema.TypeString,
Computed: true,
Description: `The PSC connection status of the PSC Forwarding Rule. Possible values: 'STATUS_UNSPECIFIED', 'PENDING', 'ACCEPTED', 'REJECTED', 'CLOSED'`,
},
"service_name": {
Type: schema.TypeString,
Computed: true,
Description: `The internal fully qualified service name for this Forwarding Rule.
This field is only used for INTERNAL load balancing.`,
},
"project": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"self_link": {
Type: schema.TypeString,
Computed: true,
},
},
UseJSONNumber: true,
}
}
func resourceComputeForwardingRuleCreate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*transport_tpg.Config)
userAgent, err := tpgresource.GenerateUserAgentString(d, config.UserAgent)
if err != nil {
return err
}
obj := make(map[string]interface{})
isMirroringCollectorProp, err := expandComputeForwardingRuleIsMirroringCollector(d.Get("is_mirroring_collector"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("is_mirroring_collector"); !tpgresource.IsEmptyValue(reflect.ValueOf(isMirroringCollectorProp)) && (ok || !reflect.DeepEqual(v, isMirroringCollectorProp)) {
obj["isMirroringCollector"] = isMirroringCollectorProp
}
descriptionProp, err := expandComputeForwardingRuleDescription(d.Get("description"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("description"); !tpgresource.IsEmptyValue(reflect.ValueOf(descriptionProp)) && (ok || !reflect.DeepEqual(v, descriptionProp)) {
obj["description"] = descriptionProp
}
IPAddressProp, err := expandComputeForwardingRuleIPAddress(d.Get("ip_address"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("ip_address"); !tpgresource.IsEmptyValue(reflect.ValueOf(IPAddressProp)) && (ok || !reflect.DeepEqual(v, IPAddressProp)) {
obj["IPAddress"] = IPAddressProp
}
IPProtocolProp, err := expandComputeForwardingRuleIPProtocol(d.Get("ip_protocol"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("ip_protocol"); !tpgresource.IsEmptyValue(reflect.ValueOf(IPProtocolProp)) && (ok || !reflect.DeepEqual(v, IPProtocolProp)) {
obj["IPProtocol"] = IPProtocolProp
}
backendServiceProp, err := expandComputeForwardingRuleBackendService(d.Get("backend_service"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("backend_service"); !tpgresource.IsEmptyValue(reflect.ValueOf(backendServiceProp)) && (ok || !reflect.DeepEqual(v, backendServiceProp)) {
obj["backendService"] = backendServiceProp
}
loadBalancingSchemeProp, err := expandComputeForwardingRuleLoadBalancingScheme(d.Get("load_balancing_scheme"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("load_balancing_scheme"); !tpgresource.IsEmptyValue(reflect.ValueOf(loadBalancingSchemeProp)) && (ok || !reflect.DeepEqual(v, loadBalancingSchemeProp)) {
obj["loadBalancingScheme"] = loadBalancingSchemeProp
}
nameProp, err := expandComputeForwardingRuleName(d.Get("name"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("name"); !tpgresource.IsEmptyValue(reflect.ValueOf(nameProp)) && (ok || !reflect.DeepEqual(v, nameProp)) {
obj["name"] = nameProp
}
networkProp, err := expandComputeForwardingRuleNetwork(d.Get("network"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("network"); !tpgresource.IsEmptyValue(reflect.ValueOf(networkProp)) && (ok || !reflect.DeepEqual(v, networkProp)) {
obj["network"] = networkProp
}
portRangeProp, err := expandComputeForwardingRulePortRange(d.Get("port_range"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("port_range"); !tpgresource.IsEmptyValue(reflect.ValueOf(portRangeProp)) && (ok || !reflect.DeepEqual(v, portRangeProp)) {
obj["portRange"] = portRangeProp
}
portsProp, err := expandComputeForwardingRulePorts(d.Get("ports"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("ports"); !tpgresource.IsEmptyValue(reflect.ValueOf(portsProp)) && (ok || !reflect.DeepEqual(v, portsProp)) {
obj["ports"] = portsProp
}
subnetworkProp, err := expandComputeForwardingRuleSubnetwork(d.Get("subnetwork"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("subnetwork"); !tpgresource.IsEmptyValue(reflect.ValueOf(subnetworkProp)) && (ok || !reflect.DeepEqual(v, subnetworkProp)) {
obj["subnetwork"] = subnetworkProp
}
targetProp, err := expandComputeForwardingRuleTarget(d.Get("target"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("target"); !tpgresource.IsEmptyValue(reflect.ValueOf(targetProp)) && (ok || !reflect.DeepEqual(v, targetProp)) {
obj["target"] = targetProp
}
allowGlobalAccessProp, err := expandComputeForwardingRuleAllowGlobalAccess(d.Get("allow_global_access"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("allow_global_access"); ok || !reflect.DeepEqual(v, allowGlobalAccessProp) {
obj["allowGlobalAccess"] = allowGlobalAccessProp
}
labelsProp, err := expandComputeForwardingRuleLabels(d.Get("labels"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("labels"); !tpgresource.IsEmptyValue(reflect.ValueOf(labelsProp)) && (ok || !reflect.DeepEqual(v, labelsProp)) {
obj["labels"] = labelsProp
}
labelFingerprintProp, err := expandComputeForwardingRuleLabelFingerprint(d.Get("label_fingerprint"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("label_fingerprint"); !tpgresource.IsEmptyValue(reflect.ValueOf(labelFingerprintProp)) && (ok || !reflect.DeepEqual(v, labelFingerprintProp)) {
obj["labelFingerprint"] = labelFingerprintProp
}
allPortsProp, err := expandComputeForwardingRuleAllPorts(d.Get("all_ports"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("all_ports"); !tpgresource.IsEmptyValue(reflect.ValueOf(allPortsProp)) && (ok || !reflect.DeepEqual(v, allPortsProp)) {
obj["allPorts"] = allPortsProp
}
networkTierProp, err := expandComputeForwardingRuleNetworkTier(d.Get("network_tier"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("network_tier"); !tpgresource.IsEmptyValue(reflect.ValueOf(networkTierProp)) && (ok || !reflect.DeepEqual(v, networkTierProp)) {
obj["networkTier"] = networkTierProp
}
serviceDirectoryRegistrationsProp, err := expandComputeForwardingRuleServiceDirectoryRegistrations(d.Get("service_directory_registrations"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("service_directory_registrations"); !tpgresource.IsEmptyValue(reflect.ValueOf(serviceDirectoryRegistrationsProp)) && (ok || !reflect.DeepEqual(v, serviceDirectoryRegistrationsProp)) {
obj["serviceDirectoryRegistrations"] = serviceDirectoryRegistrationsProp
}
serviceLabelProp, err := expandComputeForwardingRuleServiceLabel(d.Get("service_label"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("service_label"); !tpgresource.IsEmptyValue(reflect.ValueOf(serviceLabelProp)) && (ok || !reflect.DeepEqual(v, serviceLabelProp)) {
obj["serviceLabel"] = serviceLabelProp
}
sourceIpRangesProp, err := expandComputeForwardingRuleSourceIpRanges(d.Get("source_ip_ranges"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("source_ip_ranges"); !tpgresource.IsEmptyValue(reflect.ValueOf(sourceIpRangesProp)) && (ok || !reflect.DeepEqual(v, sourceIpRangesProp)) {
obj["sourceIpRanges"] = sourceIpRangesProp
}
allowPscGlobalAccessProp, err := expandComputeForwardingRuleAllowPscGlobalAccess(d.Get("allow_psc_global_access"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("allow_psc_global_access"); ok || !reflect.DeepEqual(v, allowPscGlobalAccessProp) {
obj["allowPscGlobalAccess"] = allowPscGlobalAccessProp
}
noAutomateDnsZoneProp, err := expandComputeForwardingRuleNoAutomateDnsZone(d.Get("no_automate_dns_zone"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("no_automate_dns_zone"); ok || !reflect.DeepEqual(v, noAutomateDnsZoneProp) {
obj["noAutomateDnsZone"] = noAutomateDnsZoneProp
}
ipVersionProp, err := expandComputeForwardingRuleIpVersion(d.Get("ip_version"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("ip_version"); !tpgresource.IsEmptyValue(reflect.ValueOf(ipVersionProp)) && (ok || !reflect.DeepEqual(v, ipVersionProp)) {
obj["ipVersion"] = ipVersionProp
}
regionProp, err := expandComputeForwardingRuleRegion(d.Get("region"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("region"); !tpgresource.IsEmptyValue(reflect.ValueOf(regionProp)) && (ok || !reflect.DeepEqual(v, regionProp)) {
obj["region"] = regionProp
}
url, err := tpgresource.ReplaceVars(d, config, "{{ComputeBasePath}}projects/{{project}}/regions/{{region}}/forwardingRules")
if err != nil {
return err
}
url = strings.ReplaceAll(url, "projects/projects/", "projects/")
log.Printf("[DEBUG] Creating new ForwardingRule: %#v", obj)
billingProject := ""
project, err := tpgresource.GetProject(d, config)
if err != nil {
return fmt.Errorf("Error fetching project for ForwardingRule: %s", err)
}
billingProject = strings.TrimPrefix(project, "projects/")
// err == nil indicates that the billing_project value was found
if bp, err := tpgresource.GetBillingProject(d, config); err == nil {
billingProject = bp
}
res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{
Config: config,
Method: "POST",
Project: billingProject,
RawURL: url,
UserAgent: userAgent,
Body: obj,
Timeout: d.Timeout(schema.TimeoutCreate),
})
if err != nil {
return fmt.Errorf("Error creating ForwardingRule: %s", err)
}
// Store the ID now
id, err := tpgresource.ReplaceVars(d, config, "projects/{{project}}/regions/{{region}}/forwardingRules/{{name}}")
if err != nil {
return fmt.Errorf("Error constructing id: %s", err)
}
id = strings.ReplaceAll(id, "projects/projects/", "projects/")
d.SetId(id)
err = ComputeOperationWaitTime(
config, res, project, "Creating ForwardingRule", userAgent,
d.Timeout(schema.TimeoutCreate))
if err != nil {
// The resource didn't actually create
d.SetId("")
return fmt.Errorf("Error waiting to create ForwardingRule: %s", err)
}
if v, ok := d.GetOkExists("labels"); !tpgresource.IsEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, labelsProp)) {
// Labels cannot be set in a create. We'll have to set them here.
err = resourceComputeForwardingRuleRead(d, meta)
if err != nil {
return err
}
obj := make(map[string]interface{})
// d.Get("labels") will have been overridden by the Read call.
labelsProp, err := expandComputeForwardingRuleLabels(v, d, config)
if err != nil {
return err
}
obj["labels"] = labelsProp
labelFingerprintProp := d.Get("label_fingerprint")
obj["labelFingerprint"] = labelFingerprintProp
url, err = tpgresource.ReplaceVars(d, config, "{{ComputeBasePath}}projects/{{project}}/regions/{{region}}/forwardingRules/{{name}}/setLabels")
if err != nil {
return err
}
res, err = transport_tpg.SendRequest(transport_tpg.SendRequestOptions{
Config: config,
Method: "POST",
Project: project,
RawURL: url,
UserAgent: userAgent,
Body: obj,
})
if err != nil {
return fmt.Errorf("Error adding labels to ComputeForwardingRule %q: %s", d.Id(), err)
}
err = ComputeOperationWaitTime(
config, res, project, "Updating ComputeForwardingRule Labels", userAgent,
d.Timeout(schema.TimeoutUpdate))
if err != nil {
return err
}
}
log.Printf("[DEBUG] Finished creating ForwardingRule %q: %#v", d.Id(), res)
return resourceComputeForwardingRuleRead(d, meta)
}
func resourceComputeForwardingRuleRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*transport_tpg.Config)
userAgent, err := tpgresource.GenerateUserAgentString(d, config.UserAgent)
if err != nil {
return err
}
url, err := tpgresource.ReplaceVars(d, config, "{{ComputeBasePath}}projects/{{project}}/regions/{{region}}/forwardingRules/{{name}}")
if err != nil {
return err
}
url = strings.ReplaceAll(url, "projects/projects/", "projects/")
billingProject := ""
project, err := tpgresource.GetProject(d, config)
if err != nil {
return fmt.Errorf("Error fetching project for ForwardingRule: %s", err)
}
billingProject = strings.TrimPrefix(project, "projects/")
// err == nil indicates that the billing_project value was found
if bp, err := tpgresource.GetBillingProject(d, config); err == nil {
billingProject = bp
}
res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{
Config: config,
Method: "GET",
Project: billingProject,
RawURL: url,
UserAgent: userAgent,
})
if err != nil {
return transport_tpg.HandleNotFoundError(err, d, fmt.Sprintf("ComputeForwardingRule %q", d.Id()))
}
if err := d.Set("project", project); err != nil {
return fmt.Errorf("Error reading ForwardingRule: %s", err)
}
if err := d.Set("creation_timestamp", flattenComputeForwardingRuleCreationTimestamp(res["creationTimestamp"], d, config)); err != nil {
return fmt.Errorf("Error reading ForwardingRule: %s", err)
}
if err := d.Set("is_mirroring_collector", flattenComputeForwardingRuleIsMirroringCollector(res["isMirroringCollector"], d, config)); err != nil {
return fmt.Errorf("Error reading ForwardingRule: %s", err)
}
if err := d.Set("psc_connection_id", flattenComputeForwardingRulePscConnectionId(res["pscConnectionId"], d, config)); err != nil {
return fmt.Errorf("Error reading ForwardingRule: %s", err)
}
if err := d.Set("psc_connection_status", flattenComputeForwardingRulePscConnectionStatus(res["pscConnectionStatus"], d, config)); err != nil {
return fmt.Errorf("Error reading ForwardingRule: %s", err)
}
if err := d.Set("description", flattenComputeForwardingRuleDescription(res["description"], d, config)); err != nil {
return fmt.Errorf("Error reading ForwardingRule: %s", err)
}
if err := d.Set("ip_address", flattenComputeForwardingRuleIPAddress(res["IPAddress"], d, config)); err != nil {
return fmt.Errorf("Error reading ForwardingRule: %s", err)
}
if err := d.Set("ip_protocol", flattenComputeForwardingRuleIPProtocol(res["IPProtocol"], d, config)); err != nil {
return fmt.Errorf("Error reading ForwardingRule: %s", err)
}
if err := d.Set("backend_service", flattenComputeForwardingRuleBackendService(res["backendService"], d, config)); err != nil {
return fmt.Errorf("Error reading ForwardingRule: %s", err)
}
if err := d.Set("load_balancing_scheme", flattenComputeForwardingRuleLoadBalancingScheme(res["loadBalancingScheme"], d, config)); err != nil {
return fmt.Errorf("Error reading ForwardingRule: %s", err)
}
if err := d.Set("name", flattenComputeForwardingRuleName(res["name"], d, config)); err != nil {
return fmt.Errorf("Error reading ForwardingRule: %s", err)
}
if err := d.Set("network", flattenComputeForwardingRuleNetwork(res["network"], d, config)); err != nil {
return fmt.Errorf("Error reading ForwardingRule: %s", err)
}
if err := d.Set("port_range", flattenComputeForwardingRulePortRange(res["portRange"], d, config)); err != nil {
return fmt.Errorf("Error reading ForwardingRule: %s", err)
}
if err := d.Set("ports", flattenComputeForwardingRulePorts(res["ports"], d, config)); err != nil {
return fmt.Errorf("Error reading ForwardingRule: %s", err)
}
if err := d.Set("subnetwork", flattenComputeForwardingRuleSubnetwork(res["subnetwork"], d, config)); err != nil {
return fmt.Errorf("Error reading ForwardingRule: %s", err)
}
if err := d.Set("target", flattenComputeForwardingRuleTarget(res["target"], d, config)); err != nil {
return fmt.Errorf("Error reading ForwardingRule: %s", err)
}
if err := d.Set("allow_global_access", flattenComputeForwardingRuleAllowGlobalAccess(res["allowGlobalAccess"], d, config)); err != nil {
return fmt.Errorf("Error reading ForwardingRule: %s", err)
}
if err := d.Set("labels", flattenComputeForwardingRuleLabels(res["labels"], d, config)); err != nil {
return fmt.Errorf("Error reading ForwardingRule: %s", err)
}
if err := d.Set("label_fingerprint", flattenComputeForwardingRuleLabelFingerprint(res["labelFingerprint"], d, config)); err != nil {
return fmt.Errorf("Error reading ForwardingRule: %s", err)
}
if err := d.Set("all_ports", flattenComputeForwardingRuleAllPorts(res["allPorts"], d, config)); err != nil {
return fmt.Errorf("Error reading ForwardingRule: %s", err)
}
if err := d.Set("network_tier", flattenComputeForwardingRuleNetworkTier(res["networkTier"], d, config)); err != nil {
return fmt.Errorf("Error reading ForwardingRule: %s", err)
}
if err := d.Set("service_directory_registrations", flattenComputeForwardingRuleServiceDirectoryRegistrations(res["serviceDirectoryRegistrations"], d, config)); err != nil {
return fmt.Errorf("Error reading ForwardingRule: %s", err)
}
if err := d.Set("service_label", flattenComputeForwardingRuleServiceLabel(res["serviceLabel"], d, config)); err != nil {
return fmt.Errorf("Error reading ForwardingRule: %s", err)
}
if err := d.Set("service_name", flattenComputeForwardingRuleServiceName(res["serviceName"], d, config)); err != nil {
return fmt.Errorf("Error reading ForwardingRule: %s", err)
}
if err := d.Set("source_ip_ranges", flattenComputeForwardingRuleSourceIpRanges(res["sourceIpRanges"], d, config)); err != nil {
return fmt.Errorf("Error reading ForwardingRule: %s", err)
}
if err := d.Set("base_forwarding_rule", flattenComputeForwardingRuleBaseForwardingRule(res["baseForwardingRule"], d, config)); err != nil {
return fmt.Errorf("Error reading ForwardingRule: %s", err)
}
if err := d.Set("allow_psc_global_access", flattenComputeForwardingRuleAllowPscGlobalAccess(res["allowPscGlobalAccess"], d, config)); err != nil {
return fmt.Errorf("Error reading ForwardingRule: %s", err)
}
if err := d.Set("ip_version", flattenComputeForwardingRuleIpVersion(res["ipVersion"], d, config)); err != nil {
return fmt.Errorf("Error reading ForwardingRule: %s", err)
}
if err := d.Set("region", flattenComputeForwardingRuleRegion(res["region"], d, config)); err != nil {
return fmt.Errorf("Error reading ForwardingRule: %s", err)
}
if err := d.Set("self_link", tpgresource.ConvertSelfLinkToV1(res["selfLink"].(string))); err != nil {
return fmt.Errorf("Error reading ForwardingRule: %s", err)
}
return nil
}
func resourceComputeForwardingRuleUpdate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*transport_tpg.Config)
userAgent, err := tpgresource.GenerateUserAgentString(d, config.UserAgent)
if err != nil {
return err
}
billingProject := ""
project, err := tpgresource.GetProject(d, config)
if err != nil {
return fmt.Errorf("Error fetching project for ForwardingRule: %s", err)
}
billingProject = strings.TrimPrefix(project, "projects/")
d.Partial(true)
if d.HasChange("target") {
obj := make(map[string]interface{})
targetProp, err := expandComputeForwardingRuleTarget(d.Get("target"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("target"); !tpgresource.IsEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, targetProp)) {
obj["target"] = targetProp
}
url, err := tpgresource.ReplaceVars(d, config, "{{ComputeBasePath}}projects/{{project}}/regions/{{region}}/forwardingRules/{{name}}/setTarget")
if err != nil {
return err
}
url = strings.ReplaceAll(url, "projects/projects/", "projects/")
// err == nil indicates that the billing_project value was found
if bp, err := tpgresource.GetBillingProject(d, config); err == nil {
billingProject = bp
}
res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{
Config: config,
Method: "POST",
Project: billingProject,
RawURL: url,
UserAgent: userAgent,
Body: obj,
Timeout: d.Timeout(schema.TimeoutUpdate),
})
if err != nil {
return fmt.Errorf("Error updating ForwardingRule %q: %s", d.Id(), err)
} else {
log.Printf("[DEBUG] Finished updating ForwardingRule %q: %#v", d.Id(), res)
}
err = ComputeOperationWaitTime(
config, res, project, "Updating ForwardingRule", userAgent,
d.Timeout(schema.TimeoutUpdate))
if err != nil {
return err
}
}
if d.HasChange("allow_global_access") {
obj := make(map[string]interface{})
allowGlobalAccessProp, err := expandComputeForwardingRuleAllowGlobalAccess(d.Get("allow_global_access"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("allow_global_access"); ok || !reflect.DeepEqual(v, allowGlobalAccessProp) {
obj["allowGlobalAccess"] = allowGlobalAccessProp
}
url, err := tpgresource.ReplaceVars(d, config, "{{ComputeBasePath}}projects/{{project}}/regions/{{region}}/forwardingRules/{{name}}")
if err != nil {
return err
}
url = strings.ReplaceAll(url, "projects/projects/", "projects/")
// err == nil indicates that the billing_project value was found
if bp, err := tpgresource.GetBillingProject(d, config); err == nil {
billingProject = bp
}
res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{
Config: config,
Method: "PATCH",
Project: billingProject,
RawURL: url,
UserAgent: userAgent,
Body: obj,
Timeout: d.Timeout(schema.TimeoutUpdate),
})
if err != nil {
return fmt.Errorf("Error updating ForwardingRule %q: %s", d.Id(), err)
} else {
log.Printf("[DEBUG] Finished updating ForwardingRule %q: %#v", d.Id(), res)
}
err = ComputeOperationWaitTime(
config, res, project, "Updating ForwardingRule", userAgent,
d.Timeout(schema.TimeoutUpdate))
if err != nil {
return err
}
}
if d.HasChange("labels") || d.HasChange("label_fingerprint") {
obj := make(map[string]interface{})
labelsProp, err := expandComputeForwardingRuleLabels(d.Get("labels"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("labels"); !tpgresource.IsEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, labelsProp)) {
obj["labels"] = labelsProp
}
labelFingerprintProp, err := expandComputeForwardingRuleLabelFingerprint(d.Get("label_fingerprint"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("label_fingerprint"); !tpgresource.IsEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, labelFingerprintProp)) {
obj["labelFingerprint"] = labelFingerprintProp
}
url, err := tpgresource.ReplaceVars(d, config, "{{ComputeBasePath}}projects/{{project}}/regions/{{region}}/forwardingRules/{{name}}/setLabels")
if err != nil {
return err
}
url = strings.ReplaceAll(url, "projects/projects/", "projects/")
// err == nil indicates that the billing_project value was found
if bp, err := tpgresource.GetBillingProject(d, config); err == nil {
billingProject = bp
}
res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{
Config: config,
Method: "POST",
Project: billingProject,
RawURL: url,
UserAgent: userAgent,