Skip to content

Fixes issue #21346 related to google_chronicle_rule_deployment resource #9343

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 all commits
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
3 changes: 3 additions & 0 deletions .changelog/13080.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note: bug
chronicle: fixed bug setting `enabled` on creation in `google_chronicle_rule_deployment`
```
156 changes: 117 additions & 39 deletions google-beta/services/chronicle/resource_chronicle_rule_deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,38 +172,11 @@ func resourceChronicleRuleDeploymentCreate(d *schema.ResourceData, meta interfac
return err
}

obj := make(map[string]interface{})
enabledProp, err := expandChronicleRuleDeploymentEnabled(d.Get("enabled"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("enabled"); !tpgresource.IsEmptyValue(reflect.ValueOf(enabledProp)) && (ok || !reflect.DeepEqual(v, enabledProp)) {
obj["enabled"] = enabledProp
}
alertingProp, err := expandChronicleRuleDeploymentAlerting(d.Get("alerting"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("alerting"); !tpgresource.IsEmptyValue(reflect.ValueOf(alertingProp)) && (ok || !reflect.DeepEqual(v, alertingProp)) {
obj["alerting"] = alertingProp
}
archivedProp, err := expandChronicleRuleDeploymentArchived(d.Get("archived"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("archived"); !tpgresource.IsEmptyValue(reflect.ValueOf(archivedProp)) && (ok || !reflect.DeepEqual(v, archivedProp)) {
obj["archived"] = archivedProp
}
runFrequencyProp, err := expandChronicleRuleDeploymentRunFrequency(d.Get("run_frequency"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("run_frequency"); !tpgresource.IsEmptyValue(reflect.ValueOf(runFrequencyProp)) && (ok || !reflect.DeepEqual(v, runFrequencyProp)) {
obj["runFrequency"] = runFrequencyProp
}

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

log.Printf("[DEBUG] Creating new RuleDeployment: %#v", obj)
billingProject := ""

project, err := tpgresource.GetProject(d, config)
Expand All @@ -218,36 +191,141 @@ func resourceChronicleRuleDeploymentCreate(d *schema.ResourceData, meta interfac
}

headers := make(http.Header)
// Read the config and update the mask only if set by the user in terraform code
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()))
}

params := []string{"enabled", "alerting", "archived", "run_frequency"}
var existingParams []string
obj := make(map[string]interface{})
updateMask := []string{}

// Populating existingParams with the params that exist
for _, param := range params {
if _, ok := d.GetOk(param); ok {
existingParams = append(existingParams, param)
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")
}
}

// Updating the url to have updateMask
url, err = transport_tpg.AddQueryParams(url, map[string]string{"updateMask": strings.Join(existingParams, ",")})
url, err = transport_tpg.AddQueryParams(url, map[string]string{"updateMask": strings.Join(updateMask, ",")})
if err != nil {
return err
}
res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{

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.TimeoutCreate),
Timeout: d.Timeout(schema.TimeoutUpdate),
Headers: headers,
})

if err != nil {
return fmt.Errorf("Error creating RuleDeployment: %s", err)
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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,50 @@ resource "google_chronicle_rule_deployment" "example" {
}
`, context)
}

func TestAccChronicleRuleDeployment_chronicleRuledeploymentDisabledExample(t *testing.T) {
t.Parallel()

context := map[string]interface{}{
"chronicle_id": envvar.GetTestChronicleInstanceIdFromEnv(t),
"random_suffix": acctest.RandString(t, 10),
}

acctest.VcrTest(t, resource.TestCase{
PreCheck: func() { acctest.AccTestPreCheck(t) },
ProtoV5ProviderFactories: acctest.ProtoV5ProviderBetaFactories(t),
Steps: []resource.TestStep{
{
Config: testAccChronicleRuleDeployment_chronicleRuledeploymentDisabledExample(context),
},
{
ResourceName: "google_chronicle_rule_deployment.example",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"instance", "location", "rule"},
},
},
})
}

func testAccChronicleRuleDeployment_chronicleRuledeploymentDisabledExample(context map[string]interface{}) string {
return acctest.Nprintf(`
resource "google_chronicle_rule" "my-rule" {
provider = "google-beta"
location = "us"
instance = "%{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" "example" {
provider = "google-beta"
location = "us"
instance = "%{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"
}
`, context)
}
22 changes: 22 additions & 0 deletions website/docs/r/chronicle_rule_deployment.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,28 @@ resource "google_chronicle_rule_deployment" "example" {
run_frequency = "DAILY"
}
```
## Example Usage - Chronicle Ruledeployment Disabled


```hcl
resource "google_chronicle_rule" "my-rule" {
provider = "google-beta"
location = "us"
instance = "00000000-0000-0000-0000-000000000000"
text = <<-EOT
rule test_rule { meta: events: $userid = $e.principal.user.userid match: $userid over 10m condition: $e }
EOT
}

resource "google_chronicle_rule_deployment" "example" {
provider = "google-beta"
location = "us"
instance = "00000000-0000-0000-0000-000000000000"
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"
}
```

## Argument Reference

Expand Down