terraform-provider-pagerduty icon indicating copy to clipboard operation
terraform-provider-pagerduty copied to clipboard

pagerduty_ruleset_rule - empty conditions block causes crash

Open erhudy opened this issue 4 years ago • 16 comments

The PagerDuty provider documentation implies that the way to configure a catch-all rule in a ruleset is via an empty conditions block:

conditions - (Required) Conditions evaluated to check if an event matches this event rule. Is always empty for the catch all rule, though.

However, creating a ruleset rule resource with an empty conditions block results in a crash during the apply phase.

Terraform Version

Terraform v0.12.29
+ provider.newrelic v2.6.0
+ provider.pagerduty v1.7.9

Affected Resource(s)

Please list the resources as a list, for example:

  • pagerduty_ruleset_rule

Terraform Configuration Files

resource "pagerduty_ruleset" "test_ruleset" {
  name = "Test rule set"
  team {
    id = pagerduty_team.covid_green_team.id
  }
}

resource "pagerduty_ruleset_rule" "only_prod_high_priority" {
  ruleset = pagerduty_ruleset.test_ruleset.id
  position = 0
  conditions {
    operator = "and"
    subconditions {
      operator = "contains"
      parameter {
        value = "DEV"
        path = "policy_name"
      }
    }
  }
  actions {
    severity {
      value = "info"
    }
    route {
      value = pagerduty_service.newrelic.id
    }
  }
}

resource "pagerduty_ruleset_rule" "default_route" {
  ruleset = pagerduty_ruleset.test_ruleset.id
  position = 1
  conditions {}
  actions {
    severity {
      value = "warning"
    }
    route {
      value = pagerduty_service.newrelic.id
    }
  }
}

Panic Output

Terraform did panic but the crash log contains some proprietary information. The stack trace from the crash:

panic: interface conversion: interface {} is nil, not map[string]interface {}
2020-08-26T22:42:38.867-0400 [DEBUG] plugin.terraform-provider-pagerduty_v1.7.9.exe:
2020-08-26T22:42:38.867-0400 [DEBUG] plugin.terraform-provider-pagerduty_v1.7.9.exe: goroutine 164 [running]:       
2020-08-26T22:42:38.867-0400 [DEBUG] plugin.terraform-provider-pagerduty_v1.7.9.exe: github.com/terraform-providers/terraform-provider-pagerduty/pagerduty.expandConditions(0xe5b160, 0xc000421630, 0xa)
2020-08-26T22:42:38.867-0400 [DEBUG] plugin.terraform-provider-pagerduty_v1.7.9.exe:    github.com/terraform-providers/terraform-provider-pagerduty@/pagerduty/resource_pagerduty_ruleset_rule.go:290 +0x294
2020-08-26T22:42:38.867-0400 [DEBUG] plugin.terraform-provider-pagerduty_v1.7.9.exe: github.com/terraform-providers/terraform-provider-pagerduty/pagerduty.buildRulesetRuleStruct(0xc00027f420, 0xc0005bfa80)
2020-08-26T22:42:38.867-0400 [DEBUG] plugin.terraform-provider-pagerduty_v1.7.9.exe:    github.com/terraform-providers/terraform-provider-pagerduty@/pagerduty/resource_pagerduty_ruleset_rule.go:267 +0x100
2020-08-26T22:42:38.867-0400 [DEBUG] plugin.terraform-provider-pagerduty_v1.7.9.exe: github.com/terraform-providers/terraform-provider-pagerduty/pagerduty.resourcePagerDutyRulesetRuleCreate(0xc00027f420, 0xf8b240, 0xc00039ee60, 0x2, 0x1995cc0)
2020-08-26T22:42:38.867-0400 [DEBUG] plugin.terraform-provider-pagerduty_v1.7.9.exe:    github.com/terraform-providers/terraform-provider-pagerduty@/pagerduty/resource_pagerduty_ruleset_rule.go:599 +0x4b
2020-08-26T22:42:38.868-0400 [DEBUG] plugin.terraform-provider-pagerduty_v1.7.9.exe: github.com/hashicorp/terraform-plugin-sdk/helper/schema.(*Resource).Apply(0xc000112280, 0xc0004e1950, 0xc0005bfa80, 0xf8b240, 0xc00039ee60, 0xc000254701, 0xc00024f920, 0xc0002547f0)
2020-08-26T22:42:38.868-0400 [DEBUG] plugin.terraform-provider-pagerduty_v1.7.9.exe:    github.com/hashicorp/[email protected]/helper/schema/resource.go:305 +0x36c
2020-08-26T22:42:38.868-0400 [DEBUG] plugin.terraform-provider-pagerduty_v1.7.9.exe: github.com/hashicorp/terraform-plugin-sdk/helper/schema.(*Provider).Apply(0xc000112580, 0xc000421918, 0xc0004e1950, 0xc0005bfa80, 0xc00024d168, 0xc0000bd8b8, 0xec0f00)
2020-08-26T22:42:38.868-0400 [DEBUG] plugin.terraform-provider-pagerduty_v1.7.9.exe:    github.com/hashicorp/[email protected]/helper/schema/provider.go:294 +0xa0
2020-08-26T22:42:38.868-0400 [DEBUG] plugin.terraform-provider-pagerduty_v1.7.9.exe: github.com/hashicorp/terraform-plugin-sdk/internal/helper/plugin.(*GRPCProviderServer).ApplyResourceChange(0xc0000bc560, 0x127dba0, 0xc000231890, 0xc0004c7380, 0xc0000bc560, 0xc000231890, 0xc000415a80)
2020-08-26T22:42:38.868-0400 [DEBUG] plugin.terraform-provider-pagerduty_v1.7.9.exe:    github.com/hashicorp/[email protected]/internal/helper/plugin/grpc_provider.go:885 +0x8bb
2020-08-26T22:42:38.868-0400 [DEBUG] plugin.terraform-provider-pagerduty_v1.7.9.exe: github.com/hashicorp/terraform-plugin-sdk/internal/tfplugin5._Provider_ApplyResourceChange_Handler(0xff5a00, 0xc0000bc560, 0x127dba0, 0xc000231890, 0xc0004c7320, 0x0, 0x127dba0, 0xc000231890, 0xc0001b6480, 0x219)
2020-08-26T22:42:38.868-0400 [DEBUG] plugin.terraform-provider-pagerduty_v1.7.9.exe:    github.com/hashicorp/[email protected]/internal/tfplugin5/tfplugin5.pb.go:3189 +0x21e
2020-08-26T22:42:38.869-0400 [DEBUG] plugin.terraform-provider-pagerduty_v1.7.9.exe: google.golang.org/grpc.(*Server).processUnaryRPC(0xc0003a0160, 0x1289980, 0xc0004b1080, 0xc000140600, 0xc000130690, 0x196bd40, 0x0, 0x0, 0x0)      
2020-08-26T22:42:38.869-0400 [DEBUG] plugin.terraform-provider-pagerduty_v1.7.9.exe:    google.golang.org/[email protected]/server.go:995 +0x467
2020-08-26T22:42:38.869-0400 [DEBUG] plugin.terraform-provider-pagerduty_v1.7.9.exe: google.golang.org/grpc.(*Server).handleStream(0xc0003a0160, 0x1289980, 0xc0004b1080, 0xc000140600, 0x0)
2020-08-26T22:42:38.869-0400 [DEBUG] plugin.terraform-provider-pagerduty_v1.7.9.exe:    google.golang.org/[email protected]/server.go:1275 +0xd9e
2020-08-26T22:42:38.869-0400 [DEBUG] plugin.terraform-provider-pagerduty_v1.7.9.exe: google.golang.org/grpc.(*Server).serveStreams.func1.1(0xc000034160, 0xc0003a0160, 0x1289980, 0xc0004b1080, 0xc000140600)
2020-08-26T22:42:38.869-0400 [DEBUG] plugin.terraform-provider-pagerduty_v1.7.9.exe:    google.golang.org/[email protected]/server.go:710 +0xc2
2020-08-26T22:42:38.869-0400 [DEBUG] plugin.terraform-provider-pagerduty_v1.7.9.exe: created by google.golang.org/grpc.(*Server).serveStreams.func1
2020-08-26T22:42:38.869-0400 [DEBUG] plugin.terraform-provider-pagerduty_v1.7.9.exe:    google.golang.org/[email protected]/server.go:708 +0xa8

Expected Behavior

What should have happened?

I expected the provider to create a catch-all rule in my rule set.

Actual Behavior

What actually happened?

Terraform crashed.

Steps to Reproduce

Please list the steps required to reproduce the issue, for example:

  1. Create a manifest with a ruleset and a rule set rule with an empty conditions block
  2. Apply and watch it crash

erhudy avatar Aug 27 '20 02:08 erhudy

I was facing the same issue. You can somehow work around the problem by importing the catch-all rule using terraform import.

The examples are expecting that you have a module xyz with the rule named catch_all.

terraform import module.xyz.pagerduty_ruleset_rule.catch_all <id of ruleset>.<id of rule>

You'll find the Ids by navigating through the UI and editing the catch all rule. Have then a look at the URL.

# ./xyz/main.tf
data "pagerduty_ruleset" "my_ruleset" { name = "My Ruleset" }

resource "pagerduty_ruleset_rule" "catch_all" {
  ruleset  = pagerduty_ruleset.my_ruleset.id
  actions {
    route { value = "..." }
    annotate { value = "Events was processed by catch-all rule. Rule generated by Terraform." }
  }
}

Hope this helps, but would also prefer if this gets implemented correctly.

foxylion avatar Nov 18 '20 18:11 foxylion

Different problem, but similar result, the provider also panics when time_frame is defined as an empty block.

artburkart avatar Dec 21 '20 01:12 artburkart

With Terraform provider 1.8.0, I don't see a crash, but an API error after 2 minutes:

Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  # pagerduty_ruleset.pagerduty_support will be created
  + resource "pagerduty_ruleset" "pagerduty_support" {
      + id           = (known after apply)
      + name         = "PagerDuty Support"
      + routing_keys = (known after apply)
      + type         = (known after apply)

      + team {
          + id = "<REDACTED>"
        }
    }

  # pagerduty_ruleset_rule.pagerduty_support_catch_all will be created
  + resource "pagerduty_ruleset_rule" "pagerduty_support_catch_all" {
      + id      = (known after apply)
      + ruleset = (known after apply)

      + actions {

          + route {
              + value = "<REDACTED>"
            }
        }
    }

Plan: 2 to add, 0 to change, 0 to destroy.

Do you want to perform these actions?
  Terraform will perform the actions described above.
  Only 'yes' will be accepted to approve.

  Enter a value: yes

pagerduty_ruleset.pagerduty_support: Creating...
pagerduty_ruleset.pagerduty_support: Creation complete after 1s [id=<REDACTED>]
pagerduty_ruleset_rule.pagerduty_support_catch_all: Creating...
pagerduty_ruleset_rule.pagerduty_support_catch_all: Still creating... [10s elapsed]
pagerduty_ruleset_rule.pagerduty_support_catch_all: Still creating... [20s elapsed]
pagerduty_ruleset_rule.pagerduty_support_catch_all: Still creating... [30s elapsed]
pagerduty_ruleset_rule.pagerduty_support_catch_all: Still creating... [40s elapsed]
pagerduty_ruleset_rule.pagerduty_support_catch_all: Still creating... [50s elapsed]
pagerduty_ruleset_rule.pagerduty_support_catch_all: Still creating... [1m0s elapsed]
pagerduty_ruleset_rule.pagerduty_support_catch_all: Still creating... [1m10s elapsed]
pagerduty_ruleset_rule.pagerduty_support_catch_all: Still creating... [1m20s elapsed]
pagerduty_ruleset_rule.pagerduty_support_catch_all: Still creating... [1m30s elapsed]
pagerduty_ruleset_rule.pagerduty_support_catch_all: Still creating... [1m40s elapsed]
pagerduty_ruleset_rule.pagerduty_support_catch_all: Still creating... [1m50s elapsed]
pagerduty_ruleset_rule.pagerduty_support_catch_all: Still creating... [2m0s elapsed]

Error: POST API call to https://api.pagerduty.com/rulesets/<REDACTED>/rules failed 400 Bad Request. Code: 0, Errors: map[conditions:[can't be blank]], Message: Invalid payload

  on pagerduty_support.tf line 10, in resource "pagerduty_ruleset_rule" "pagerduty_support_catch_all":
  10: resource "pagerduty_ruleset_rule" "pagerduty_support_catch_all" {

Suggested terraform import workaround works well though, even if it's absolutely not ideal.

jbfavre avatar Jan 22 '21 07:01 jbfavre

Yeah, the API doesn't support creating the catch-all rule through the API because the catch-all rule already exists when you create the Ruleset. As mentioned, you'll need to import the catch-all rule and then manage it that way.

stmcallister avatar Jan 23 '21 00:01 stmcallister

@stmcallister thanks for the details, sounds logical.

But, IMHO, there's a misalignment between PagerDuty API and terraform provider here. Terraform provider makes possible to create a catch-all rule, which is unsupported by the API. As you said, the catch-all rule is created at the same time as its ruleset.

The only solution we currently have is to import the catch-all rule into terraform before being able to update it. Unfortunately, terraform doesn't allow to automagically import resources when applying a configuration. That make manual work mandatory, which in turns breaks a full gitops workflow.

Another solution could be: since the catch-all rule is automatically created right after the ruleset, we can (and should) consider it as a ruleset property. Thus, having a ruleset resource like:

resource "pagerduty_ruleset" "foo" {
    name = "Primary Ruleset"
    team {
        id = pagerduty_team.foo.id
    }
    default_action {
        [like pagerduty_ruleset_rule resource definition]
    }
}

Would allow people to create ruleset AND corresponding catch-all customization in a single operation.

Hope this will help

jbfavre avatar Jan 25 '21 07:01 jbfavre

@stmcallister any news on this topic ? It would be a very usefull improvement to avoid multiple "apply/import/update/apply" setup

jbfavre avatar Jun 25 '21 11:06 jbfavre

@stmcallister are there any plans to support this use case?

matan129 avatar Jul 27 '21 09:07 matan129

@jbfavre Sorry for the delay on this! So, you're saying we should include the catch_all rule object inside the ruleset definition? 🤔

stmcallister avatar Jul 28 '21 16:07 stmcallister

@jbfavre Sorry for the delay on this! So, you're saying we should include the catch_all rule object inside the ruleset definition?

That's one solution. Another one would be to add a reference only, keeping the default rule having its own resource:

resource "pagerduty_ruleset" "foo" {
    name = "Primary Ruleset"
    team {
        id = pagerduty_team.foo.id
    }
    default_action = pagerduty_ruleset_rule.foo.id
}

resource "pagerduty_ruleset_rule" "foo" {
  ruleset = pagerduty_ruleset.foo.id
  actions {
    route {
      value = pagerduty_service.catch_all_service.id
    }
  }
}

In any case, as far as I can tell, this might need API changes because current API create both resources with one API call only. But maybe one can manage to get that working from within the provider.

jbfavre avatar Jul 28 '21 16:07 jbfavre

As discussed during Quaterly Terraform Roundtable, another design is to add the catch_all only into pagerduty_ruleset block. Using previous example, it could be like:

resource "pagerduty_ruleset" "foo" {
    name = "Primary Ruleset"
    team {
        id = pagerduty_team.foo.id
    }
    default_action {
      route {
        value = pagerduty_service.catch_all_service.id
      }
    }
}

jbfavre avatar Jul 28 '21 17:07 jbfavre

Both options look good to me. I'd just call it catch_all_rule to keep the terminology consistent across the board:

resource "pagerduty_ruleset" "foo" {
  name = "Primary Ruleset"
  team {
    id = pagerduty_team.foo.id
  }
  catch_all_rule {
    actions {
      route {
        value = pagerduty_service.catch_all_service.id
      }
    }
    # no support for conditions { ... } clause here, of course.
  }
}

matan129 avatar Jul 28 '21 20:07 matan129

Is this issue being worked on atm? The issue still occurs with provider version 2.0.0

v0lumehi avatar Oct 19 '21 12:10 v0lumehi

With empty conditions {} block:

panic: interface conversion: interface {} is nil, not map[string]interface {}

goroutine 379 [running]:
github.com/terraform-providers/terraform-provider-pagerduty/pagerduty.expandConditions(0xfb8dc0, 0xc0003bf6c0, 0xa)
	github.com/terraform-providers/terraform-provider-pagerduty/pagerduty/resource_pagerduty_ruleset_rule.go:341 +0x29f
github.com/terraform-providers/terraform-provider-pagerduty/pagerduty.buildRulesetRuleStruct(0xc00029cc00, 0xc0003bf778)
	github.com/terraform-providers/terraform-provider-pagerduty/pagerduty/resource_pagerduty_ruleset_rule.go:315 +0x105
github.com/terraform-providers/terraform-provider-pagerduty/pagerduty.resourcePagerDutyRulesetRuleCreate(0xc00029cc00, 0x10d9700, 0xc000463d90, 0x0, 0xffffffffffffffff)
	github.com/terraform-providers/terraform-provider-pagerduty/pagerduty/resource_pagerduty_ruleset_rule.go:734 +0x45
github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema.(*Resource).create(0xc000493880, 0x13b82a8, 0xc000655e00, 0xc00029cc00, 0x10d9700, 0xc000463d90, 0x0, 0x0, 0x0)
	github.com/hashicorp/terraform-plugin-sdk/[email protected]/helper/schema/resource.go:318 +0x1ee
github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema.(*Resource).Apply(0xc000493880, 0x13b82a8, 0xc000655e00, 0xc00031a230, 0xc0004aed80, 0x10d9700, 0xc000463d90, 0x0, 0x0, 0x0, ...)
	github.com/hashicorp/terraform-plugin-sdk/[email protected]/helper/schema/resource.go:456 +0x67b
github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema.(*GRPCProviderServer).ApplyResourceChange(0xc00000d818, 0x13b82a8, 0xc000655e00, 0xc00029e730, 0xc000655e00, 0x1116c80, 0xc000579400)
	github.com/hashicorp/terraform-plugin-sdk/[email protected]/helper/schema/grpc_provider.go:955 +0x8ef
github.com/hashicorp/terraform-plugin-go/tfprotov5/server.(*server).ApplyResourceChange(0xc0004af5c0, 0x13b8350, 0xc000655e00, 0xc00031a0e0, 0xc0004af5c0, 0xc000579470, 0xc000593ba0)
	github.com/hashicorp/[email protected]/tfprotov5/server/server.go:332 +0xb5
github.com/hashicorp/terraform-plugin-go/tfprotov5/internal/tfplugin5._Provider_ApplyResourceChange_Handler(0x1116c80, 0xc0004af5c0, 0x13b8350, 0xc000579470, 0xc00006d560, 0x0, 0x13b8350, 0xc000579470, 0xc00063f200, 0x226)
	github.com/hashicorp/[email protected]/tfprotov5/internal/tfplugin5/tfplugin5_grpc.pb.go:380 +0x214
google.golang.org/grpc.(*Server).processUnaryRPC(0xc000171c00, 0x13c23d8, 0xc000549080, 0xc00070b700, 0xc000105230, 0x19b2540, 0x0, 0x0, 0x0)
	google.golang.org/[email protected]/server.go:1210 +0x52b
google.golang.org/grpc.(*Server).handleStream(0xc000171c00, 0x13c23d8, 0xc000549080, 0xc00070b700, 0x0)
	google.golang.org/[email protected]/server.go:1533 +0xd0c
google.golang.org/grpc.(*Server).serveStreams.func1.2(0xc00043a250, 0xc000171c00, 0x13c23d8, 0xc000549080, 0xc00070b700)
	google.golang.org/[email protected]/server.go:871 +0xab
created by google.golang.org/grpc.(*Server).serveStreams.func1
	google.golang.org/[email protected]/server.go:869 +0x1fd

Error: The terraform-provider-pagerduty_v2.0.0 plugin crashed!

v0lumehi avatar Oct 19 '21 13:10 v0lumehi

Same issue here with version 2.0.0. Will this be prioritized? This is one of the fundamental features of rulesets.

noahtrilling avatar Oct 21 '21 21:10 noahtrilling

Im also looking forward for this fix. In my case I would like to use the catch_all rule to route to a given service

franklinlindemberg avatar Oct 30 '21 16:10 franklinlindemberg

Is there a timeline for getting this fixed?

jbentonGx avatar Jun 07 '22 18:06 jbentonGx

Rules are being deprecated and replaced

gsreynolds avatar Jun 28 '23 14:06 gsreynolds