Skip to content

Commit 5df13ef

Browse files
ankitgoyal0301Dawid212
authored andcommitted
Fixes issue #21346 related to google_chronicle_rule_deployment resource (GoogleCloudPlatform#13080)
1 parent f99d45d commit 5df13ef

File tree

4 files changed

+196
-18
lines changed

4 files changed

+196
-18
lines changed

mmv1/products/chronicle/RuleDeployment.yaml

+6-1
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,19 @@ exclude_delete: true
3232
autogen_status: UnVsZURlcGxveW1lbnQ=
3333

3434
custom_code:
35-
pre_create: 'templates/terraform/pre_create/chronicle_rule_deployment.go.tmpl'
35+
custom_create: 'templates/terraform/custom_create/chronicle_rule_deployment.go.tmpl'
3636

3737
examples:
3838
- name: 'chronicle_ruledeployment_basic'
3939
primary_resource_id: 'example'
4040
min_version: 'beta'
4141
test_env_vars:
4242
chronicle_id: 'CHRONICLE_ID'
43+
- name: 'chronicle_ruledeployment_disabled'
44+
primary_resource_id: 'example'
45+
min_version: 'beta'
46+
test_env_vars:
47+
chronicle_id: 'CHRONICLE_ID'
4348

4449
parameters:
4550
- name: location
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
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)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
resource "google_chronicle_rule" "my-rule" {
2+
provider = "google-beta"
3+
location = "us"
4+
instance = "{{index $.TestEnvVars "chronicle_id"}}"
5+
text = <<-EOT
6+
rule test_rule { meta: events: $userid = $e.principal.user.userid match: $userid over 10m condition: $e }
7+
EOT
8+
}
9+
10+
resource "google_chronicle_rule_deployment" "{{$.PrimaryResourceId}}" {
11+
provider = "google-beta"
12+
location = "us"
13+
instance = "{{index $.TestEnvVars "chronicle_id"}}"
14+
rule = element(split("/", resource.google_chronicle_rule.my-rule.name), length(split("/", resource.google_chronicle_rule.my-rule.name)) - 1)
15+
enabled = false
16+
run_frequency = "LIVE"
17+
}

mmv1/templates/terraform/pre_create/chronicle_rule_deployment.go.tmpl

-17
This file was deleted.

0 commit comments

Comments
 (0)