Skip to content

Fixes issue #21347 related to google_chronicle_rule_deployment resource #9422

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/13130.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
chronicle: fixed an error during resource creation with certain `run_frequency` configurations in `google_chronicle_rule_deployment`
```
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ import (
transport_tpg "github.com/hashicorp/terraform-provider-google-beta/google-beta/transport"
)

func chronicleRuleDeploymentNilRunFrequencyDiffSuppressFunc(_, old, new string, _ *schema.ResourceData) bool {
if new == "" {
return true
}
return false
}

func ResourceChronicleRuleDeployment() *schema.Resource {
return &schema.Resource{
Create: resourceChronicleRuleDeploymentCreate,
Expand Down Expand Up @@ -97,8 +104,9 @@ updated.`,
Description: `Whether the rule is currently deployed continuously against incoming data.`,
},
"run_frequency": {
Type: schema.TypeString,
Optional: true,
Type: schema.TypeString,
Optional: true,
DiffSuppressFunc: chronicleRuleDeploymentNilRunFrequencyDiffSuppressFunc,
Description: `The run frequency of the rule deployment.
Possible values:
LIVE
Expand Down Expand Up @@ -274,7 +282,8 @@ func resourceChronicleRuleDeploymentCreate(d *schema.ResourceData, meta interfac
runFrequencyValue, runFrequencyExists := res["runFrequency"]
if runFrequencyExists {
runFrequency := flattenChronicleRuleDeploymentRunFrequency(runFrequencyValue, d, config)
if !reflect.DeepEqual(runFrequencyProp, runFrequency) {
_, ok := d.GetOkExists("run_frequency")
if !reflect.DeepEqual(runFrequencyProp, runFrequency) && ok {
obj["runFrequency"] = runFrequencyProp
updateMask = append(updateMask, "runFrequency")
}
Expand Down Expand Up @@ -491,6 +500,26 @@ func resourceChronicleRuleDeploymentUpdate(d *schema.ResourceData, meta interfac
if err != nil {
return err
}
// removeRunFrequencyFromUpdateMask removes 'runFrequency' from the updateMask in a URL.
removeRunFrequencyFromUpdateMask := func(url string) string {
// Remove "runFrequency" and handle commas.
url = strings.ReplaceAll(url, "%2CrunFrequency", "")
url = strings.ReplaceAll(url, "runFrequency%2C", "")
url = strings.ReplaceAll(url, "runFrequency", "")

// Remove extra commas.
url = strings.ReplaceAll(url, "%2C%2C", "%2C")

//Remove trailing commas.
url = strings.TrimSuffix(url, "%2C")

return url
}

// Remove "runFrequency" and handle commas if run_frequency not configured by user
if _, ok := d.GetOk("run_frequency"); !ok {
url = removeRunFrequencyFromUpdateMask(url)
}

// err == nil indicates that the billing_project value was found
if bp, err := tpgresource.GetBillingProject(d, config); err == nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,51 @@ resource "google_chronicle_rule_deployment" "example" {
}
`, context)
}

func TestAccChronicleRuleDeployment_chronicleRuledeploymentRunFrequencyMissingExample(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_chronicleRuledeploymentRunFrequencyMissingExample(context),
},
{
ResourceName: "google_chronicle_rule_deployment.example",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"instance", "location", "rule"},
},
},
})
}

func testAccChronicleRuleDeployment_chronicleRuledeploymentRunFrequencyMissingExample(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 = true
alerting = true
archived = false
}
`, context)
}
23 changes: 23 additions & 0 deletions website/docs/r/chronicle_rule_deployment.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,29 @@ resource "google_chronicle_rule_deployment" "example" {
run_frequency = "LIVE"
}
```
## Example Usage - Chronicle Ruledeployment Run Frequency Missing


```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 = true
alerting = true
archived = false
}
```

## Argument Reference

Expand Down