1
+ userAgent, err := tpgresource.GenerateUserAgentString(d, config.UserAgent)
2
+ if err != nil {
3
+ return err
4
+ }
5
+
6
+ url, err := tpgresource.ReplaceVars(d, config, "{{"{{ChronicleBasePath}}projects/{{project}}/locations/{{location}}/instances/{{instance}}/rules/{{rule}}/deployment"}}")
7
+ if err != nil {
8
+ return err
9
+ }
10
+
11
+ billingProject := ""
12
+
13
+ project, err := tpgresource.GetProject(d, config)
14
+ if err != nil {
15
+ return fmt.Errorf("Error fetching project for RuleDeployment: %s", err)
16
+ }
17
+ billingProject = project
18
+
19
+ // err == nil indicates that the billing_project value was found
20
+ if bp, err := tpgresource.GetBillingProject(d, config); err == nil {
21
+ billingProject = bp
22
+ }
23
+
24
+ headers := make(http.Header)
25
+ res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{
26
+ Config: config,
27
+ Method: "GET",
28
+ Project: billingProject,
29
+ RawURL: url,
30
+ UserAgent: userAgent,
31
+ Headers: headers,
32
+ })
33
+ if err != nil {
34
+ return transport_tpg.HandleNotFoundError(err, d, fmt.Sprintf("ChronicleRuleDeployment %q", d.Id()))
35
+ }
36
+
37
+ obj := make(map[string]interface{})
38
+ updateMask := []string{}
39
+
40
+ enabledProp, err := expandChronicleRuleDeploymentEnabled(d.Get("enabled"), d, config)
41
+ if err != nil {
42
+ return err
43
+ }
44
+ alertingProp, err := expandChronicleRuleDeploymentAlerting(d.Get("alerting"), d, config)
45
+ if err != nil {
46
+ return err
47
+ }
48
+ archivedProp, err := expandChronicleRuleDeploymentArchived(d.Get("archived"), d, config)
49
+ if err != nil {
50
+ return err
51
+ }
52
+ runFrequencyProp, err := expandChronicleRuleDeploymentRunFrequency(d.Get("run_frequency"), d, config)
53
+ if err != nil {
54
+ return err
55
+ }
56
+
57
+ if res != nil {
58
+ enabledValue, enabledExists:= res["enabled"]
59
+ if enabledExists {
60
+ enabled:= flattenChronicleRuleDeploymentEnabled(enabledValue, d, config)
61
+ if!reflect.DeepEqual(enabledProp, enabled) {
62
+ obj["enabled"] = enabledProp
63
+ updateMask = append(updateMask, "enabled")
64
+ }
65
+ } else {
66
+ // Handle the case where "enabled" is missing from the API response
67
+ if v, ok:= d.GetOkExists("enabled"); ok && !tpgresource.IsEmptyValue(reflect.ValueOf(v)) {
68
+ obj["enabled"] = enabledProp
69
+ updateMask = append(updateMask, "enabled")
70
+ }
71
+ }
72
+
73
+ alertingValue, alertingExists:= res["alerting"]
74
+ if alertingExists {
75
+ alerting:= flattenChronicleRuleDeploymentAlerting(alertingValue, d, config)
76
+ if!reflect.DeepEqual(alertingProp, alerting) {
77
+ obj["alerting"] = alertingProp
78
+ updateMask = append(updateMask, "alerting")
79
+ }
80
+ } else {
81
+ // Handle the case where "alerting" is missing from the API response
82
+ if v, ok:= d.GetOkExists("alerting"); ok && !tpgresource.IsEmptyValue(reflect.ValueOf(v)) {
83
+ obj["alerting"] = alertingProp
84
+ updateMask = append(updateMask, "alerting")
85
+ }
86
+ }
87
+
88
+ archivedValue, archivedExists:= res["archived"]
89
+ if archivedExists {
90
+ archived:= flattenChronicleRuleDeploymentArchived(archivedValue, d, config)
91
+ if!reflect.DeepEqual(archivedProp, archived) {
92
+ obj["archived"] = archivedProp
93
+ updateMask = append(updateMask, "archived")
94
+ }
95
+ } else {
96
+ // Handle the case where "archived" is missing from the API response
97
+ if v, ok:= d.GetOkExists("archived"); ok && !tpgresource.IsEmptyValue(reflect.ValueOf(v)) {
98
+ obj["archived"] = archivedProp
99
+ updateMask = append(updateMask, "archived")
100
+ }
101
+ }
102
+
103
+ runFrequencyValue, runFrequencyExists:= res["runFrequency"]
104
+ if runFrequencyExists {
105
+ runFrequency:= flattenChronicleRuleDeploymentRunFrequency(runFrequencyValue, d, config)
106
+ if!reflect.DeepEqual(runFrequencyProp, runFrequency) {
107
+ obj["runFrequency"] = runFrequencyProp
108
+ updateMask = append(updateMask, "runFrequency")
109
+ }
110
+ } else {
111
+ // Handle the case where "run_frequency" is missing from the API response
112
+ if v, ok:= d.GetOkExists("run_frequency"); ok && !tpgresource.IsEmptyValue(reflect.ValueOf(v)) {
113
+ obj["runFrequency"] = runFrequencyProp
114
+ updateMask = append(updateMask, "runFrequency")
115
+ }
116
+ }
117
+ } else {
118
+ if v, ok:= d.GetOkExists("enabled"); ok && !tpgresource.IsEmptyValue(reflect.ValueOf(v)) {
119
+ obj["enabled"] = enabledProp
120
+ updateMask = append(updateMask, "enabled")
121
+ }
122
+ if v, ok:= d.GetOkExists("alerting"); ok && !tpgresource.IsEmptyValue(reflect.ValueOf(v)) {
123
+ obj["alerting"] = alertingProp
124
+ updateMask = append(updateMask, "alerting")
125
+ }
126
+ if v, ok:= d.GetOkExists("archived"); ok && !tpgresource.IsEmptyValue(reflect.ValueOf(v)) {
127
+ obj["archived"] = archivedProp
128
+ updateMask = append(updateMask, "archived")
129
+ }
130
+ if v, ok:= d.GetOkExists("run_frequency"); ok && !tpgresource.IsEmptyValue(reflect.ValueOf(v)) {
131
+ obj["runFrequency"] = runFrequencyProp
132
+ updateMask = append(updateMask, "runFrequency")
133
+ }
134
+ }
135
+
136
+ url, err = transport_tpg.AddQueryParams(url, map[string]string{"updateMask": strings.Join(updateMask, ",")})
137
+ if err != nil {
138
+ return err
139
+ }
140
+
141
+ log.Printf("[DEBUG] Updating RuleDeployment %q: %#v", d.Id(), obj)
142
+
143
+ res, err = transport_tpg.SendRequest(transport_tpg.SendRequestOptions{
144
+ Config: config,
145
+ Method: "PATCH",
146
+ Project: billingProject,
147
+ RawURL: url,
148
+ UserAgent: userAgent,
149
+ Body: obj,
150
+ Timeout: d.Timeout(schema.TimeoutUpdate),
151
+ Headers: headers,
152
+ })
153
+
154
+ if err != nil {
155
+ return fmt.Errorf("Error updating RuleDeployment %q: %s", d.Id(), err)
156
+ } else {
157
+ log.Printf("[DEBUG] Finished updating RuleDeployment %q: %#v", d.Id(), res)
158
+ }
159
+
160
+ if err := d.Set("name", flattenChronicleRuleDeploymentName(res["name"], d, config)); err != nil {
161
+ return fmt.Errorf(`Error setting computed identity field "name": %s`, err)
162
+ }
163
+
164
+ // Store the ID now
165
+ id, err := tpgresource.ReplaceVars(d, config, "{{"projects/{{project}}/locations/{{location}}/instances/{{instance}}/rules/{{rule}}/deployment"}}")
166
+ if err != nil {
167
+ return fmt.Errorf("Error constructing id: %s", err)
168
+ }
169
+ d.SetId(id)
170
+
171
+ log.Printf("[DEBUG] Finished creating RuleDeployment %q: %#v", d.Id(), res)
172
+
173
+ return resourceChronicleRuleDeploymentRead(d, meta)
0 commit comments