1
+ userAgent, err := tpgresource.GenerateUserAgentString(d, config.UserAgent)
2
+ if err != nil {
3
+ return err
4
+ }
5
+
6
+ billingProject := ""
7
+
8
+ obj := make(map[string]interface{})
9
+ nameProp, err := expandComputeFirewallPolicyAssociationName(d.Get("name"), d, config)
10
+ if err != nil {
11
+ return err
12
+ } else if v, ok := d.GetOkExists("name"); !tpgresource.IsEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, nameProp)) {
13
+ obj["name"] = nameProp
14
+ }
15
+ attachmentTargetProp, err := expandComputeFirewallPolicyAssociationAttachmentTarget(d.Get("attachment_target"), d, config)
16
+ if err != nil {
17
+ return err
18
+ } else if v, ok := d.GetOkExists("attachment_target"); !tpgresource.IsEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, attachmentTargetProp)) {
19
+ obj["attachmentTarget"] = attachmentTargetProp
20
+ }
21
+ firewallPolicyProp, err := expandComputeFirewallPolicyAssociationFirewallPolicy(d.Get("firewall_policy"), d, config)
22
+ if err != nil {
23
+ return err
24
+ } else if v, ok := d.GetOkExists("firewall_policy"); !tpgresource.IsEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, firewallPolicyProp)) {
25
+ obj["firewallPolicy"] = firewallPolicyProp
26
+ }
27
+
28
+ url, err := tpgresource.ReplaceVars(d, config, "{{"{{"}}ComputeBasePath{{"}}"}}locations/global/firewallPolicies/{{"{{"}}firewall_policy{{"}}"}}/addAssociation?replaceExistingAssociation=true")
29
+ if err != nil {
30
+ return err
31
+ }
32
+
33
+ log.Printf("[DEBUG] Updating FirewallPolicyAssociation %q: %#v", d.Id(), obj)
34
+ headers := make(http.Header)
35
+
36
+ // err == nil indicates that the billing_project value was found
37
+ if bp, err := tpgresource.GetBillingProject(d, config); err == nil {
38
+ billingProject = bp
39
+ }
40
+
41
+ res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{
42
+ Config: config,
43
+ Method: "POST",
44
+ Project: billingProject,
45
+ RawURL: url,
46
+ UserAgent: userAgent,
47
+ Body: obj,
48
+ Timeout: d.Timeout(schema.TimeoutUpdate),
49
+ Headers: headers,
50
+ })
51
+ //following section is the area of the `custom_update` function for this resource that is different
52
+ //`custom_update` was necessary over a `post_update"` because of the change to the error handler
53
+ if err != nil {
54
+ //before failing an update, restores the old firewall_policy value to prevent terraform state becoming broken
55
+ parts := strings.Split(d.Id(), "/")
56
+ oldPolicyPath := "locations/global/firewallPolicies/" + parts[len(parts)-3]
57
+ d.Set("firewall_policy", oldPolicyPath)
58
+ return fmt.Errorf("Error updating FirewallPolicyAssociation %q: %s", d.Id(), err)
59
+ } else {
60
+ log.Printf("[DEBUG] Finished updating FirewallPolicyAssociation %q: %#v", d.Id(), res)
61
+ }
62
+
63
+ // store the ID now, needed because this update function is changing a normally immutable URL parameter field
64
+ id, err := tpgresource.ReplaceVars(d, config, "locations/global/firewallPolicies/{{"{{"}}firewall_policy{{"}}"}}/associations/{{"{{"}}name{{"}}"}}")
65
+ if err != nil {
66
+ return fmt.Errorf("Error constructing id: %s", err)
67
+ }
68
+ d.SetId(id)
69
+
70
+ //following the swapover briefly both associations exist and this can cause operations to fail
71
+ time.Sleep(60 * time.Second)
72
+ //end `custom_update` changed zone
73
+
74
+ return resourceComputeFirewallPolicyAssociationRead(d, meta)
0 commit comments