Skip to content

Fixes issue #21346 related to google_chronicle_rule_deployment resource #13080

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion mmv1/products/chronicle/RuleDeployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,19 @@ exclude_delete: true
autogen_status: UnVsZURlcGxveW1lbnQ=

custom_code:
pre_create: 'templates/terraform/pre_create/chronicle_rule_deployment.go.tmpl'
custom_create: 'templates/terraform/custom_create/chronicle_rule_deployment.go.tmpl'

examples:
- name: 'chronicle_ruledeployment_basic'
primary_resource_id: 'example'
min_version: 'beta'
test_env_vars:
chronicle_id: 'CHRONICLE_ID'
- name: 'chronicle_ruledeployment_disabled'
primary_resource_id: 'example'
min_version: 'beta'
test_env_vars:
chronicle_id: 'CHRONICLE_ID'

parameters:
- name: location
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
userAgent, err := tpgresource.GenerateUserAgentString(d, config.UserAgent)
if err != nil {
return err
}

url, err := tpgresource.ReplaceVars(d, config, "{{"{{ChronicleBasePath}}projects/{{project}}/locations/{{location}}/instances/{{instance}}/rules/{{rule}}/deployment"}}")
if err != nil {
return err
}

billingProject := ""

project, err := tpgresource.GetProject(d, config)
if err != nil {
return fmt.Errorf("Error fetching project for RuleDeployment: %s", err)
}
billingProject = project

// err == nil indicates that the billing_project value was found
if bp, err := tpgresource.GetBillingProject(d, config); err == nil {
billingProject = bp
}

headers := make(http.Header)
res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{
Config: config,
Method: "GET",
Project: billingProject,
RawURL: url,
UserAgent: userAgent,
Headers: headers,
})
if err != nil {
return transport_tpg.HandleNotFoundError(err, d, fmt.Sprintf("ChronicleRuleDeployment %q", d.Id()))
}

obj := make(map[string]interface{})
updateMask := []string{}

enabledProp, err := expandChronicleRuleDeploymentEnabled(d.Get("enabled"), d, config)
if err != nil {
return err
}
alertingProp, err := expandChronicleRuleDeploymentAlerting(d.Get("alerting"), d, config)
if err != nil {
return err
}
archivedProp, err := expandChronicleRuleDeploymentArchived(d.Get("archived"), d, config)
if err != nil {
return err
}
runFrequencyProp, err := expandChronicleRuleDeploymentRunFrequency(d.Get("run_frequency"), d, config)
if err != nil {
return err
}

if res != nil {
enabledValue, enabledExists:= res["enabled"]
if enabledExists {
enabled:= flattenChronicleRuleDeploymentEnabled(enabledValue, d, config)
if!reflect.DeepEqual(enabledProp, enabled) {
obj["enabled"] = enabledProp
updateMask = append(updateMask, "enabled")
}
} else {
// Handle the case where "enabled" is missing from the API response
if v, ok:= d.GetOkExists("enabled"); ok &&!tpgresource.IsEmptyValue(reflect.ValueOf(v)) {
obj["enabled"] = enabledProp
updateMask = append(updateMask, "enabled")
}
}

alertingValue, alertingExists:= res["alerting"]
if alertingExists {
alerting:= flattenChronicleRuleDeploymentAlerting(alertingValue, d, config)
if!reflect.DeepEqual(alertingProp, alerting) {
obj["alerting"] = alertingProp
updateMask = append(updateMask, "alerting")
}
} else {
// Handle the case where "alerting" is missing from the API response
if v, ok:= d.GetOkExists("alerting"); ok &&!tpgresource.IsEmptyValue(reflect.ValueOf(v)) {
obj["alerting"] = alertingProp
updateMask = append(updateMask, "alerting")
}
}

archivedValue, archivedExists:= res["archived"]
if archivedExists {
archived:= flattenChronicleRuleDeploymentArchived(archivedValue, d, config)
if!reflect.DeepEqual(archivedProp, archived) {
obj["archived"] = archivedProp
updateMask = append(updateMask, "archived")
}
} else {
// Handle the case where "archived" is missing from the API response
if v, ok:= d.GetOkExists("archived"); ok &&!tpgresource.IsEmptyValue(reflect.ValueOf(v)) {
obj["archived"] = archivedProp
updateMask = append(updateMask, "archived")
}
}

runFrequencyValue, runFrequencyExists:= res["runFrequency"]
if runFrequencyExists {
runFrequency:= flattenChronicleRuleDeploymentRunFrequency(runFrequencyValue, d, config)
if!reflect.DeepEqual(runFrequencyProp, runFrequency) {
obj["runFrequency"] = runFrequencyProp
updateMask = append(updateMask, "runFrequency")
}
} else {
// Handle the case where "run_frequency" is missing from the API response
if v, ok:= d.GetOkExists("run_frequency"); ok &&!tpgresource.IsEmptyValue(reflect.ValueOf(v)) {
obj["runFrequency"] = runFrequencyProp
updateMask = append(updateMask, "runFrequency")
}
}
} else {
if v, ok:= d.GetOkExists("enabled"); ok &&!tpgresource.IsEmptyValue(reflect.ValueOf(v)) {
obj["enabled"] = enabledProp
updateMask = append(updateMask, "enabled")
}
if v, ok:= d.GetOkExists("alerting"); ok &&!tpgresource.IsEmptyValue(reflect.ValueOf(v)) {
obj["alerting"] = alertingProp
updateMask = append(updateMask, "alerting")
}
if v, ok:= d.GetOkExists("archived"); ok &&!tpgresource.IsEmptyValue(reflect.ValueOf(v)) {
obj["archived"] = archivedProp
updateMask = append(updateMask, "archived")
}
if v, ok:= d.GetOkExists("run_frequency"); ok &&!tpgresource.IsEmptyValue(reflect.ValueOf(v)) {
obj["runFrequency"] = runFrequencyProp
updateMask = append(updateMask, "runFrequency")
}
}

url, err = transport_tpg.AddQueryParams(url, map[string]string{"updateMask": strings.Join(updateMask, ",")})
if err != nil {
return err
}

log.Printf("[DEBUG] Updating RuleDeployment %q: %#v", d.Id(), obj)

res, err = transport_tpg.SendRequest(transport_tpg.SendRequestOptions{
Config: config,
Method: "PATCH",
Project: billingProject,
RawURL: url,
UserAgent: userAgent,
Body: obj,
Timeout: d.Timeout(schema.TimeoutUpdate),
Headers: headers,
})

if err != nil {
return fmt.Errorf("Error updating RuleDeployment %q: %s", d.Id(), err)
} else {
log.Printf("[DEBUG] Finished updating RuleDeployment %q: %#v", d.Id(), res)
}

if err := d.Set("name", flattenChronicleRuleDeploymentName(res["name"], d, config)); err != nil {
return fmt.Errorf(`Error setting computed identity field "name": %s`, err)
}

// Store the ID now
id, err := tpgresource.ReplaceVars(d, config, "{{"projects/{{project}}/locations/{{location}}/instances/{{instance}}/rules/{{rule}}/deployment"}}")
if err != nil {
return fmt.Errorf("Error constructing id: %s", err)
}
d.SetId(id)

log.Printf("[DEBUG] Finished creating RuleDeployment %q: %#v", d.Id(), res)

return resourceChronicleRuleDeploymentRead(d, meta)
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
resource "google_chronicle_rule" "my-rule" {
provider = "google-beta"
location = "us"
instance = "{{index $.TestEnvVars "chronicle_id"}}"
text = <<-EOT
rule test_rule { meta: events: $userid = $e.principal.user.userid match: $userid over 10m condition: $e }
EOT
}

resource "google_chronicle_rule_deployment" "{{$.PrimaryResourceId}}" {
provider = "google-beta"
location = "us"
instance = "{{index $.TestEnvVars "chronicle_id"}}"
rule = element(split("/", resource.google_chronicle_rule.my-rule.name), length(split("/", resource.google_chronicle_rule.my-rule.name)) - 1)
enabled = false
run_frequency = "LIVE"
}

This file was deleted.

Loading