Skip to content

Commit d391866

Browse files
ankitgoyal0301maxi-cit
authored andcommitted
Fixes issue #21347 related to google_chronicle_rule_deployment resource (GoogleCloudPlatform#13130)
1 parent 741989c commit d391866

File tree

5 files changed

+57
-4
lines changed

5 files changed

+57
-4
lines changed

mmv1/products/chronicle/RuleDeployment.yaml

+8
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@ exclude_delete: true
3232
autogen_status: UnVsZURlcGxveW1lbnQ=
3333

3434
custom_code:
35+
constants: 'templates/terraform/constants/chronicle_rule_deployment.go.tmpl'
3536
custom_create: 'templates/terraform/custom_create/chronicle_rule_deployment.go.tmpl'
37+
pre_update: 'templates/terraform/pre_update/chronicle_rule_deployment.go.tmpl'
3638

3739
examples:
3840
- name: 'chronicle_ruledeployment_basic'
@@ -45,6 +47,11 @@ examples:
4547
min_version: 'beta'
4648
test_env_vars:
4749
chronicle_id: 'CHRONICLE_ID'
50+
- name: 'chronicle_ruledeployment_run_frequency_missing'
51+
primary_resource_id: 'example'
52+
min_version: 'beta'
53+
test_env_vars:
54+
chronicle_id: 'CHRONICLE_ID'
4855

4956
parameters:
5057
- name: location
@@ -101,6 +108,7 @@ properties:
101108
output: true
102109
- name: runFrequency
103110
type: String
111+
diff_suppress_func: 'chronicleRuleDeploymentNilRunFrequencyDiffSuppressFunc'
104112
description: |2-
105113
106114
The run frequency of the rule deployment.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
func chronicleRuleDeploymentNilRunFrequencyDiffSuppressFunc(_, old, new string, _ *schema.ResourceData) bool {
2+
if new == "" {
3+
return true
4+
}
5+
return false
6+
}

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

+5-4
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ if res != nil {
5858
enabledValue, enabledExists:= res["enabled"]
5959
if enabledExists {
6060
enabled:= flattenChronicleRuleDeploymentEnabled(enabledValue, d, config)
61-
if!reflect.DeepEqual(enabledProp, enabled) {
61+
if !reflect.DeepEqual(enabledProp, enabled) {
6262
obj["enabled"] = enabledProp
6363
updateMask = append(updateMask, "enabled")
6464
}
@@ -73,7 +73,7 @@ if res != nil {
7373
alertingValue, alertingExists:= res["alerting"]
7474
if alertingExists {
7575
alerting:= flattenChronicleRuleDeploymentAlerting(alertingValue, d, config)
76-
if!reflect.DeepEqual(alertingProp, alerting) {
76+
if !reflect.DeepEqual(alertingProp, alerting) {
7777
obj["alerting"] = alertingProp
7878
updateMask = append(updateMask, "alerting")
7979
}
@@ -88,7 +88,7 @@ if res != nil {
8888
archivedValue, archivedExists:= res["archived"]
8989
if archivedExists {
9090
archived:= flattenChronicleRuleDeploymentArchived(archivedValue, d, config)
91-
if!reflect.DeepEqual(archivedProp, archived) {
91+
if !reflect.DeepEqual(archivedProp, archived) {
9292
obj["archived"] = archivedProp
9393
updateMask = append(updateMask, "archived")
9494
}
@@ -103,7 +103,8 @@ if res != nil {
103103
runFrequencyValue, runFrequencyExists:= res["runFrequency"]
104104
if runFrequencyExists {
105105
runFrequency:= flattenChronicleRuleDeploymentRunFrequency(runFrequencyValue, d, config)
106-
if!reflect.DeepEqual(runFrequencyProp, runFrequency) {
106+
_, ok := d.GetOkExists("run_frequency")
107+
if !reflect.DeepEqual(runFrequencyProp, runFrequency) && ok {
107108
obj["runFrequency"] = runFrequencyProp
108109
updateMask = append(updateMask, "runFrequency")
109110
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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 = true
16+
alerting = true
17+
archived = false
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// removeRunFrequencyFromUpdateMask removes 'runFrequency' from the updateMask in a URL.
2+
removeRunFrequencyFromUpdateMask := func(url string) (string) {
3+
// Remove "runFrequency" and handle commas.
4+
url = strings.ReplaceAll(url, "%2CrunFrequency", "")
5+
url = strings.ReplaceAll(url, "runFrequency%2C", "")
6+
url = strings.ReplaceAll(url, "runFrequency", "")
7+
8+
// Remove extra commas.
9+
url = strings.ReplaceAll(url, "%2C%2C", "%2C")
10+
11+
//Remove trailing commas.
12+
url = strings.TrimSuffix(url, "%2C")
13+
14+
return url
15+
}
16+
17+
// Remove "runFrequency" and handle commas if run_frequency not configured by user
18+
if _, ok := d.GetOk("run_frequency"); !ok {
19+
url = removeRunFrequencyFromUpdateMask(url)
20+
}

0 commit comments

Comments
 (0)