forked from hashicorp/terraform-provider-google-beta
/
resource_compute_firewall_policy_rule.go
580 lines (507 loc) · 23 KB
/
resource_compute_firewall_policy_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
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
// ----------------------------------------------------------------------------
//
// *** AUTO GENERATED CODE *** Type: DCL ***
//
// ----------------------------------------------------------------------------
//
// This file is managed by Magic Modules (https://github.com/GoogleCloudPlatform/magic-modules)
// and is based on the DCL (https://github.com/GoogleCloudPlatform/declarative-resource-client-library).
// Changes will need to be made to the DCL or Magic Modules instead of here.
//
// We are not currently able to accept contributions to this file. If changes
// are required, please file an issue at https://github.com/hashicorp/terraform-provider-google/issues/new/choose
//
// ----------------------------------------------------------------------------
package compute
import (
"context"
"fmt"
"log"
"time"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
dcl "github.com/GoogleCloudPlatform/declarative-resource-client-library/dcl"
compute "github.com/GoogleCloudPlatform/declarative-resource-client-library/services/google/compute/beta"
"github.com/lorioux/google-beta/google-beta/tpgdclresource"
"github.com/lorioux/google-beta/google-beta/tpgresource"
transport_tpg "github.com/lorioux/google-beta/google-beta/transport"
)
func ResourceComputeFirewallPolicyRule() *schema.Resource {
return &schema.Resource{
Create: resourceComputeFirewallPolicyRuleCreate,
Read: resourceComputeFirewallPolicyRuleRead,
Update: resourceComputeFirewallPolicyRuleUpdate,
Delete: resourceComputeFirewallPolicyRuleDelete,
Importer: &schema.ResourceImporter{
State: resourceComputeFirewallPolicyRuleImport,
},
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{
"action": {
Type: schema.TypeString,
Required: true,
Description: "The Action to perform when the client connection triggers the rule. Valid actions are \"allow\", \"deny\" and \"goto_next\".",
},
"direction": {
Type: schema.TypeString,
Required: true,
Description: "The direction in which this rule applies. Possible values: INGRESS, EGRESS",
},
"firewall_policy": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
DiffSuppressFunc: tpgresource.CompareSelfLinkOrResourceName,
Description: "The firewall policy of the resource.",
},
"match": {
Type: schema.TypeList,
Required: true,
Description: "A match condition that incoming traffic is evaluated against. If it evaluates to true, the corresponding 'action' is enforced.",
MaxItems: 1,
Elem: ComputeFirewallPolicyRuleMatchSchema(),
},
"priority": {
Type: schema.TypeInt,
Required: true,
ForceNew: true,
Description: "An integer indicating the priority of a rule in the list. The priority must be a positive value between 0 and 2147483647. Rules are evaluated from highest to lowest priority where 0 is the highest priority and 2147483647 is the lowest prority.",
},
"description": {
Type: schema.TypeString,
Optional: true,
Description: "An optional description for this resource.",
},
"disabled": {
Type: schema.TypeBool,
Optional: true,
Description: "Denotes whether the firewall policy rule is disabled. When set to true, the firewall policy rule is not enforced and traffic behaves as if it did not exist. If this is unspecified, the firewall policy rule will be enabled.",
},
"enable_logging": {
Type: schema.TypeBool,
Optional: true,
Description: "Denotes whether to enable logging for a particular rule. If logging is enabled, logs will be exported to the configured export destination in Stackdriver. Logs may be exported to BigQuery or Pub/Sub. Note: you cannot enable logging on \"goto_next\" rules.",
},
"target_resources": {
Type: schema.TypeList,
Optional: true,
DiffSuppressFunc: tpgresource.CompareSelfLinkOrResourceName,
Description: "A list of network resource URLs to which this rule applies. This field allows you to control which network's VMs get this rule. If this field is left blank, all VMs within the organization will receive the rule.",
Elem: &schema.Schema{Type: schema.TypeString},
},
"target_service_accounts": {
Type: schema.TypeList,
Optional: true,
Description: "A list of service accounts indicating the sets of instances that are applied with this rule.",
Elem: &schema.Schema{Type: schema.TypeString},
},
"kind": {
Type: schema.TypeString,
Computed: true,
Description: "Type of the resource. Always `compute#firewallPolicyRule` for firewall policy rules",
},
"rule_tuple_count": {
Type: schema.TypeInt,
Computed: true,
Description: "Calculation of the complexity of a single firewall policy rule.",
},
},
}
}
func ComputeFirewallPolicyRuleMatchSchema() *schema.Resource {
return &schema.Resource{
Schema: map[string]*schema.Schema{
"layer4_configs": {
Type: schema.TypeList,
Required: true,
Description: "Pairs of IP protocols and ports that the rule should match.",
Elem: ComputeFirewallPolicyRuleMatchLayer4ConfigsSchema(),
},
"dest_address_groups": {
Type: schema.TypeList,
Optional: true,
Description: "Address groups which should be matched against the traffic destination. Maximum number of destination address groups is 10. Destination address groups is only supported in Egress rules.",
Elem: &schema.Schema{Type: schema.TypeString},
},
"dest_fqdns": {
Type: schema.TypeList,
Optional: true,
Description: "Domain names that will be used to match against the resolved domain name of destination of traffic. Can only be specified if DIRECTION is egress.",
Elem: &schema.Schema{Type: schema.TypeString},
},
"dest_ip_ranges": {
Type: schema.TypeList,
Optional: true,
Description: "CIDR IP address range. Maximum number of destination CIDR IP ranges allowed is 256.",
Elem: &schema.Schema{Type: schema.TypeString},
},
"dest_region_codes": {
Type: schema.TypeList,
Optional: true,
Description: "The Unicode country codes whose IP addresses will be used to match against the source of traffic. Can only be specified if DIRECTION is egress.",
Elem: &schema.Schema{Type: schema.TypeString},
},
"dest_threat_intelligences": {
Type: schema.TypeList,
Optional: true,
Description: "Name of the Google Cloud Threat Intelligence list.",
Elem: &schema.Schema{Type: schema.TypeString},
},
"src_address_groups": {
Type: schema.TypeList,
Optional: true,
Description: "Address groups which should be matched against the traffic source. Maximum number of source address groups is 10. Source address groups is only supported in Ingress rules.",
Elem: &schema.Schema{Type: schema.TypeString},
},
"src_fqdns": {
Type: schema.TypeList,
Optional: true,
Description: "Domain names that will be used to match against the resolved domain name of source of traffic. Can only be specified if DIRECTION is ingress.",
Elem: &schema.Schema{Type: schema.TypeString},
},
"src_ip_ranges": {
Type: schema.TypeList,
Optional: true,
Description: "CIDR IP address range. Maximum number of source CIDR IP ranges allowed is 256.",
Elem: &schema.Schema{Type: schema.TypeString},
},
"src_region_codes": {
Type: schema.TypeList,
Optional: true,
Description: "The Unicode country codes whose IP addresses will be used to match against the source of traffic. Can only be specified if DIRECTION is ingress.",
Elem: &schema.Schema{Type: schema.TypeString},
},
"src_threat_intelligences": {
Type: schema.TypeList,
Optional: true,
Description: "Name of the Google Cloud Threat Intelligence list.",
Elem: &schema.Schema{Type: schema.TypeString},
},
},
}
}
func ComputeFirewallPolicyRuleMatchLayer4ConfigsSchema() *schema.Resource {
return &schema.Resource{
Schema: map[string]*schema.Schema{
"ip_protocol": {
Type: schema.TypeString,
Required: true,
Description: "The IP protocol to which this rule applies. The protocol type is required when creating a firewall rule. This value can either be one of the following well known protocol strings (`tcp`, `udp`, `icmp`, `esp`, `ah`, `ipip`, `sctp`), or the IP protocol number.",
},
"ports": {
Type: schema.TypeList,
Optional: true,
Description: "An optional list of ports to which this rule applies. This field is only applicable for UDP or TCP protocol. Each entry must be either an integer or a range. If not specified, this rule applies to connections through any port. Example inputs include: ``.",
Elem: &schema.Schema{Type: schema.TypeString},
},
},
}
}
func resourceComputeFirewallPolicyRuleCreate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*transport_tpg.Config)
obj := &compute.FirewallPolicyRule{
Action: dcl.String(d.Get("action").(string)),
Direction: compute.FirewallPolicyRuleDirectionEnumRef(d.Get("direction").(string)),
FirewallPolicy: dcl.String(d.Get("firewall_policy").(string)),
Match: expandComputeFirewallPolicyRuleMatch(d.Get("match")),
Priority: dcl.Int64(int64(d.Get("priority").(int))),
Description: dcl.String(d.Get("description").(string)),
Disabled: dcl.Bool(d.Get("disabled").(bool)),
EnableLogging: dcl.Bool(d.Get("enable_logging").(bool)),
TargetResources: tpgdclresource.ExpandStringArray(d.Get("target_resources")),
TargetServiceAccounts: tpgdclresource.ExpandStringArray(d.Get("target_service_accounts")),
}
id, err := obj.ID()
if err != nil {
return fmt.Errorf("error constructing id: %s", err)
}
d.SetId(id)
directive := tpgdclresource.CreateDirective
userAgent, err := tpgresource.GenerateUserAgentString(d, config.UserAgent)
if err != nil {
return err
}
billingProject := ""
// err == nil indicates that the billing_project value was found
if bp, err := tpgresource.GetBillingProject(d, config); err == nil {
billingProject = bp
}
client := transport_tpg.NewDCLComputeClient(config, userAgent, billingProject, d.Timeout(schema.TimeoutCreate))
if bp, err := tpgresource.ReplaceVars(d, config, client.Config.BasePath); err != nil {
d.SetId("")
return fmt.Errorf("Could not format %q: %w", client.Config.BasePath, err)
} else {
client.Config.BasePath = bp
}
res, err := client.ApplyFirewallPolicyRule(context.Background(), obj, directive...)
if _, ok := err.(dcl.DiffAfterApplyError); ok {
log.Printf("[DEBUG] Diff after apply returned from the DCL: %s", err)
} else if err != nil {
// The resource didn't actually create
d.SetId("")
return fmt.Errorf("Error creating FirewallPolicyRule: %s", err)
}
log.Printf("[DEBUG] Finished creating FirewallPolicyRule %q: %#v", d.Id(), res)
return resourceComputeFirewallPolicyRuleRead(d, meta)
}
func resourceComputeFirewallPolicyRuleRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*transport_tpg.Config)
obj := &compute.FirewallPolicyRule{
Action: dcl.String(d.Get("action").(string)),
Direction: compute.FirewallPolicyRuleDirectionEnumRef(d.Get("direction").(string)),
FirewallPolicy: dcl.String(d.Get("firewall_policy").(string)),
Match: expandComputeFirewallPolicyRuleMatch(d.Get("match")),
Priority: dcl.Int64(int64(d.Get("priority").(int))),
Description: dcl.String(d.Get("description").(string)),
Disabled: dcl.Bool(d.Get("disabled").(bool)),
EnableLogging: dcl.Bool(d.Get("enable_logging").(bool)),
TargetResources: tpgdclresource.ExpandStringArray(d.Get("target_resources")),
TargetServiceAccounts: tpgdclresource.ExpandStringArray(d.Get("target_service_accounts")),
}
userAgent, err := tpgresource.GenerateUserAgentString(d, config.UserAgent)
if err != nil {
return err
}
billingProject := ""
// err == nil indicates that the billing_project value was found
if bp, err := tpgresource.GetBillingProject(d, config); err == nil {
billingProject = bp
}
client := transport_tpg.NewDCLComputeClient(config, userAgent, billingProject, d.Timeout(schema.TimeoutRead))
if bp, err := tpgresource.ReplaceVars(d, config, client.Config.BasePath); err != nil {
d.SetId("")
return fmt.Errorf("Could not format %q: %w", client.Config.BasePath, err)
} else {
client.Config.BasePath = bp
}
res, err := client.GetFirewallPolicyRule(context.Background(), obj)
if err != nil {
resourceName := fmt.Sprintf("ComputeFirewallPolicyRule %q", d.Id())
return tpgdclresource.HandleNotFoundDCLError(err, d, resourceName)
}
if err = d.Set("action", res.Action); err != nil {
return fmt.Errorf("error setting action in state: %s", err)
}
if err = d.Set("direction", res.Direction); err != nil {
return fmt.Errorf("error setting direction in state: %s", err)
}
if err = d.Set("firewall_policy", res.FirewallPolicy); err != nil {
return fmt.Errorf("error setting firewall_policy in state: %s", err)
}
if err = d.Set("match", flattenComputeFirewallPolicyRuleMatch(res.Match)); err != nil {
return fmt.Errorf("error setting match in state: %s", err)
}
if err = d.Set("priority", res.Priority); err != nil {
return fmt.Errorf("error setting priority in state: %s", err)
}
if err = d.Set("description", res.Description); err != nil {
return fmt.Errorf("error setting description in state: %s", err)
}
if err = d.Set("disabled", res.Disabled); err != nil {
return fmt.Errorf("error setting disabled in state: %s", err)
}
if err = d.Set("enable_logging", res.EnableLogging); err != nil {
return fmt.Errorf("error setting enable_logging in state: %s", err)
}
if err = d.Set("target_resources", res.TargetResources); err != nil {
return fmt.Errorf("error setting target_resources in state: %s", err)
}
if err = d.Set("target_service_accounts", res.TargetServiceAccounts); err != nil {
return fmt.Errorf("error setting target_service_accounts in state: %s", err)
}
if err = d.Set("kind", res.Kind); err != nil {
return fmt.Errorf("error setting kind in state: %s", err)
}
if err = d.Set("rule_tuple_count", res.RuleTupleCount); err != nil {
return fmt.Errorf("error setting rule_tuple_count in state: %s", err)
}
return nil
}
func resourceComputeFirewallPolicyRuleUpdate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*transport_tpg.Config)
obj := &compute.FirewallPolicyRule{
Action: dcl.String(d.Get("action").(string)),
Direction: compute.FirewallPolicyRuleDirectionEnumRef(d.Get("direction").(string)),
FirewallPolicy: dcl.String(d.Get("firewall_policy").(string)),
Match: expandComputeFirewallPolicyRuleMatch(d.Get("match")),
Priority: dcl.Int64(int64(d.Get("priority").(int))),
Description: dcl.String(d.Get("description").(string)),
Disabled: dcl.Bool(d.Get("disabled").(bool)),
EnableLogging: dcl.Bool(d.Get("enable_logging").(bool)),
TargetResources: tpgdclresource.ExpandStringArray(d.Get("target_resources")),
TargetServiceAccounts: tpgdclresource.ExpandStringArray(d.Get("target_service_accounts")),
}
directive := tpgdclresource.UpdateDirective
userAgent, err := tpgresource.GenerateUserAgentString(d, config.UserAgent)
if err != nil {
return err
}
billingProject := ""
// err == nil indicates that the billing_project value was found
if bp, err := tpgresource.GetBillingProject(d, config); err == nil {
billingProject = bp
}
client := transport_tpg.NewDCLComputeClient(config, userAgent, billingProject, d.Timeout(schema.TimeoutUpdate))
if bp, err := tpgresource.ReplaceVars(d, config, client.Config.BasePath); err != nil {
d.SetId("")
return fmt.Errorf("Could not format %q: %w", client.Config.BasePath, err)
} else {
client.Config.BasePath = bp
}
res, err := client.ApplyFirewallPolicyRule(context.Background(), obj, directive...)
if _, ok := err.(dcl.DiffAfterApplyError); ok {
log.Printf("[DEBUG] Diff after apply returned from the DCL: %s", err)
} else if err != nil {
// The resource didn't actually create
d.SetId("")
return fmt.Errorf("Error updating FirewallPolicyRule: %s", err)
}
log.Printf("[DEBUG] Finished creating FirewallPolicyRule %q: %#v", d.Id(), res)
return resourceComputeFirewallPolicyRuleRead(d, meta)
}
func resourceComputeFirewallPolicyRuleDelete(d *schema.ResourceData, meta interface{}) error {
config := meta.(*transport_tpg.Config)
obj := &compute.FirewallPolicyRule{
Action: dcl.String(d.Get("action").(string)),
Direction: compute.FirewallPolicyRuleDirectionEnumRef(d.Get("direction").(string)),
FirewallPolicy: dcl.String(d.Get("firewall_policy").(string)),
Match: expandComputeFirewallPolicyRuleMatch(d.Get("match")),
Priority: dcl.Int64(int64(d.Get("priority").(int))),
Description: dcl.String(d.Get("description").(string)),
Disabled: dcl.Bool(d.Get("disabled").(bool)),
EnableLogging: dcl.Bool(d.Get("enable_logging").(bool)),
TargetResources: tpgdclresource.ExpandStringArray(d.Get("target_resources")),
TargetServiceAccounts: tpgdclresource.ExpandStringArray(d.Get("target_service_accounts")),
}
log.Printf("[DEBUG] Deleting FirewallPolicyRule %q", d.Id())
userAgent, err := tpgresource.GenerateUserAgentString(d, config.UserAgent)
if err != nil {
return err
}
billingProject := ""
// err == nil indicates that the billing_project value was found
if bp, err := tpgresource.GetBillingProject(d, config); err == nil {
billingProject = bp
}
client := transport_tpg.NewDCLComputeClient(config, userAgent, billingProject, d.Timeout(schema.TimeoutDelete))
if bp, err := tpgresource.ReplaceVars(d, config, client.Config.BasePath); err != nil {
d.SetId("")
return fmt.Errorf("Could not format %q: %w", client.Config.BasePath, err)
} else {
client.Config.BasePath = bp
}
if err := client.DeleteFirewallPolicyRule(context.Background(), obj); err != nil {
return fmt.Errorf("Error deleting FirewallPolicyRule: %s", err)
}
log.Printf("[DEBUG] Finished deleting FirewallPolicyRule %q", d.Id())
return nil
}
func resourceComputeFirewallPolicyRuleImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
config := meta.(*transport_tpg.Config)
if err := tpgresource.ParseImportId([]string{
"locations/global/firewallPolicies/(?P<firewall_policy>[^/]+)/rules/(?P<priority>[^/]+)",
"(?P<firewall_policy>[^/]+)/(?P<priority>[^/]+)",
}, d, config); err != nil {
return nil, err
}
// Replace import id for the resource id
id, err := tpgresource.ReplaceVarsForId(d, config, "locations/global/firewallPolicies/{{firewall_policy}}/rules/{{priority}}")
if err != nil {
return nil, fmt.Errorf("Error constructing id: %s", err)
}
d.SetId(id)
return []*schema.ResourceData{d}, nil
}
func expandComputeFirewallPolicyRuleMatch(o interface{}) *compute.FirewallPolicyRuleMatch {
if o == nil {
return compute.EmptyFirewallPolicyRuleMatch
}
objArr := o.([]interface{})
if len(objArr) == 0 || objArr[0] == nil {
return compute.EmptyFirewallPolicyRuleMatch
}
obj := objArr[0].(map[string]interface{})
return &compute.FirewallPolicyRuleMatch{
Layer4Configs: expandComputeFirewallPolicyRuleMatchLayer4ConfigsArray(obj["layer4_configs"]),
DestAddressGroups: tpgdclresource.ExpandStringArray(obj["dest_address_groups"]),
DestFqdns: tpgdclresource.ExpandStringArray(obj["dest_fqdns"]),
DestIPRanges: tpgdclresource.ExpandStringArray(obj["dest_ip_ranges"]),
DestRegionCodes: tpgdclresource.ExpandStringArray(obj["dest_region_codes"]),
DestThreatIntelligences: tpgdclresource.ExpandStringArray(obj["dest_threat_intelligences"]),
SrcAddressGroups: tpgdclresource.ExpandStringArray(obj["src_address_groups"]),
SrcFqdns: tpgdclresource.ExpandStringArray(obj["src_fqdns"]),
SrcIPRanges: tpgdclresource.ExpandStringArray(obj["src_ip_ranges"]),
SrcRegionCodes: tpgdclresource.ExpandStringArray(obj["src_region_codes"]),
SrcThreatIntelligences: tpgdclresource.ExpandStringArray(obj["src_threat_intelligences"]),
}
}
func flattenComputeFirewallPolicyRuleMatch(obj *compute.FirewallPolicyRuleMatch) interface{} {
if obj == nil || obj.Empty() {
return nil
}
transformed := map[string]interface{}{
"layer4_configs": flattenComputeFirewallPolicyRuleMatchLayer4ConfigsArray(obj.Layer4Configs),
"dest_address_groups": obj.DestAddressGroups,
"dest_fqdns": obj.DestFqdns,
"dest_ip_ranges": obj.DestIPRanges,
"dest_region_codes": obj.DestRegionCodes,
"dest_threat_intelligences": obj.DestThreatIntelligences,
"src_address_groups": obj.SrcAddressGroups,
"src_fqdns": obj.SrcFqdns,
"src_ip_ranges": obj.SrcIPRanges,
"src_region_codes": obj.SrcRegionCodes,
"src_threat_intelligences": obj.SrcThreatIntelligences,
}
return []interface{}{transformed}
}
func expandComputeFirewallPolicyRuleMatchLayer4ConfigsArray(o interface{}) []compute.FirewallPolicyRuleMatchLayer4Configs {
if o == nil {
return make([]compute.FirewallPolicyRuleMatchLayer4Configs, 0)
}
objs := o.([]interface{})
if len(objs) == 0 || objs[0] == nil {
return make([]compute.FirewallPolicyRuleMatchLayer4Configs, 0)
}
items := make([]compute.FirewallPolicyRuleMatchLayer4Configs, 0, len(objs))
for _, item := range objs {
i := expandComputeFirewallPolicyRuleMatchLayer4Configs(item)
items = append(items, *i)
}
return items
}
func expandComputeFirewallPolicyRuleMatchLayer4Configs(o interface{}) *compute.FirewallPolicyRuleMatchLayer4Configs {
if o == nil {
return compute.EmptyFirewallPolicyRuleMatchLayer4Configs
}
obj := o.(map[string]interface{})
return &compute.FirewallPolicyRuleMatchLayer4Configs{
IPProtocol: dcl.String(obj["ip_protocol"].(string)),
Ports: tpgdclresource.ExpandStringArray(obj["ports"]),
}
}
func flattenComputeFirewallPolicyRuleMatchLayer4ConfigsArray(objs []compute.FirewallPolicyRuleMatchLayer4Configs) []interface{} {
if objs == nil {
return nil
}
items := []interface{}{}
for _, item := range objs {
i := flattenComputeFirewallPolicyRuleMatchLayer4Configs(&item)
items = append(items, i)
}
return items
}
func flattenComputeFirewallPolicyRuleMatchLayer4Configs(obj *compute.FirewallPolicyRuleMatchLayer4Configs) interface{} {
if obj == nil || obj.Empty() {
return nil
}
transformed := map[string]interface{}{
"ip_protocol": obj.IPProtocol,
"ports": obj.Ports,
}
return transformed
}