forked from hashicorp/terraform-provider-google-beta
/
resource_compute_firewall_policy.go
338 lines (293 loc) · 12 KB
/
resource_compute_firewall_policy.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
// 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 ResourceComputeFirewallPolicy() *schema.Resource {
return &schema.Resource{
Create: resourceComputeFirewallPolicyCreate,
Read: resourceComputeFirewallPolicyRead,
Update: resourceComputeFirewallPolicyUpdate,
Delete: resourceComputeFirewallPolicyDelete,
Importer: &schema.ResourceImporter{
State: resourceComputeFirewallPolicyImport,
},
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{
"parent": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
DiffSuppressFunc: tpgresource.CompareSelfLinkOrResourceName,
Description: "The parent of the firewall policy.",
},
"short_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "User-provided name of the Organization firewall policy. The name should be unique in the organization in which the firewall policy is created. The name must be 1-63 characters long, and comply with RFC1035. 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.",
},
"description": {
Type: schema.TypeString,
Optional: true,
Description: "An optional description of this resource. Provide this property when you create the resource.",
},
"creation_timestamp": {
Type: schema.TypeString,
Computed: true,
Description: "Creation timestamp in RFC3339 text format.",
},
"fingerprint": {
Type: schema.TypeString,
Computed: true,
Description: "Fingerprint of the resource. This field is used internally during updates of this resource.",
},
"firewall_policy_id": {
Type: schema.TypeString,
Computed: true,
Description: "The unique identifier for the resource. This identifier is defined by the server.",
},
"name": {
Type: schema.TypeString,
Computed: true,
Description: "Name of the resource. It is a numeric ID allocated by GCP which uniquely identifies the Firewall Policy.",
},
"rule_tuple_count": {
Type: schema.TypeInt,
Computed: true,
Description: "Total count of all firewall policy rule tuples. A firewall policy can not exceed a set number of tuples.",
},
"self_link": {
Type: schema.TypeString,
Computed: true,
Description: "Server-defined URL for the resource.",
},
"self_link_with_id": {
Type: schema.TypeString,
Computed: true,
Description: "Server-defined URL for this resource with the resource id.",
},
},
}
}
func resourceComputeFirewallPolicyCreate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*transport_tpg.Config)
obj := &compute.FirewallPolicy{
Parent: dcl.String(d.Get("parent").(string)),
ShortName: dcl.String(d.Get("short_name").(string)),
Description: dcl.String(d.Get("description").(string)),
}
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.ApplyFirewallPolicy(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 FirewallPolicy: %s", err)
}
if err = d.Set("name", res.Name); err != nil {
return fmt.Errorf("error setting name in state: %s", err)
}
// ID has a server-generated value, set again after creation.
id, err = res.ID()
if err != nil {
return fmt.Errorf("error constructing id: %s", err)
}
d.SetId(id)
log.Printf("[DEBUG] Finished creating FirewallPolicy %q: %#v", d.Id(), res)
return resourceComputeFirewallPolicyRead(d, meta)
}
func resourceComputeFirewallPolicyRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*transport_tpg.Config)
obj := &compute.FirewallPolicy{
Parent: dcl.String(d.Get("parent").(string)),
ShortName: dcl.String(d.Get("short_name").(string)),
Description: dcl.String(d.Get("description").(string)),
Name: dcl.StringOrNil(d.Get("name").(string)),
}
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.GetFirewallPolicy(context.Background(), obj)
if err != nil {
resourceName := fmt.Sprintf("ComputeFirewallPolicy %q", d.Id())
return tpgdclresource.HandleNotFoundDCLError(err, d, resourceName)
}
if err = d.Set("parent", res.Parent); err != nil {
return fmt.Errorf("error setting parent in state: %s", err)
}
if err = d.Set("short_name", res.ShortName); err != nil {
return fmt.Errorf("error setting short_name 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("creation_timestamp", res.CreationTimestamp); err != nil {
return fmt.Errorf("error setting creation_timestamp in state: %s", err)
}
if err = d.Set("fingerprint", res.Fingerprint); err != nil {
return fmt.Errorf("error setting fingerprint in state: %s", err)
}
if err = d.Set("firewall_policy_id", res.Id); err != nil {
return fmt.Errorf("error setting firewall_policy_id in state: %s", err)
}
if err = d.Set("name", res.Name); err != nil {
return fmt.Errorf("error setting name 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)
}
if err = d.Set("self_link", res.SelfLink); err != nil {
return fmt.Errorf("error setting self_link in state: %s", err)
}
if err = d.Set("self_link_with_id", res.SelfLinkWithId); err != nil {
return fmt.Errorf("error setting self_link_with_id in state: %s", err)
}
return nil
}
func resourceComputeFirewallPolicyUpdate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*transport_tpg.Config)
obj := &compute.FirewallPolicy{
Parent: dcl.String(d.Get("parent").(string)),
ShortName: dcl.String(d.Get("short_name").(string)),
Description: dcl.String(d.Get("description").(string)),
Name: dcl.StringOrNil(d.Get("name").(string)),
}
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.ApplyFirewallPolicy(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 FirewallPolicy: %s", err)
}
log.Printf("[DEBUG] Finished creating FirewallPolicy %q: %#v", d.Id(), res)
return resourceComputeFirewallPolicyRead(d, meta)
}
func resourceComputeFirewallPolicyDelete(d *schema.ResourceData, meta interface{}) error {
config := meta.(*transport_tpg.Config)
obj := &compute.FirewallPolicy{
Parent: dcl.String(d.Get("parent").(string)),
ShortName: dcl.String(d.Get("short_name").(string)),
Description: dcl.String(d.Get("description").(string)),
Name: dcl.StringOrNil(d.Get("name").(string)),
}
log.Printf("[DEBUG] Deleting FirewallPolicy %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.DeleteFirewallPolicy(context.Background(), obj); err != nil {
return fmt.Errorf("Error deleting FirewallPolicy: %s", err)
}
log.Printf("[DEBUG] Finished deleting FirewallPolicy %q", d.Id())
return nil
}
func resourceComputeFirewallPolicyImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
config := meta.(*transport_tpg.Config)
if err := tpgresource.ParseImportId([]string{
"locations/global/firewallPolicies/(?P<name>[^/]+)",
"(?P<name>[^/]+)",
}, d, config); err != nil {
return nil, err
}
// Replace import id for the resource id
id, err := tpgresource.ReplaceVars(d, config, "locations/global/firewallPolicies/{{name}}")
if err != nil {
return nil, fmt.Errorf("Error constructing id: %s", err)
}
d.SetId(id)
return []*schema.ResourceData{d}, nil
}