opsgenie / terraform-provider-opsgenie

Terraform OpsGenie provider
https://registry.terraform.io/providers/opsgenie/opsgenie/latest/docs
Mozilla Public License 2.0
103 stars 137 forks source link

Can't create opsgenie_service_incident_rule : 422 response #454

Closed ppiotrlach closed 2 weeks ago

ppiotrlach commented 2 weeks ago

Terraform Version

Affected Resource(s)

Terraform Configuration Files

resource "opsgenie_service" "empty" {
  name = "Empty Service"
  description = "Terraform requires an associated service to create incident rules, unlike the Opsgenie GUI, which allows rule creation without a service. This service fulfills that requirement and serves as work-around to create such incident rules."
  team_id = data.opsgenie_team.team.id
}

resource "opsgenie_service_incident_rule" "manual_reports_rule" {
  service_id = opsgenie_service.empty.id
  incident_rule {
    condition_match_type = "match-all-conditions"
    conditions {
      field          = "priority"
      not            = false
      operation      = "equals"
      expected_value = "P1"
    }
    conditions {
      field          = "tags"
      not            = false
      operation      = "contains"
      expected_value = "manual"
    }

    incident_properties {
      message     = "{{message}}"
      description = "{{description}}"
      priority    = "P1"
      tags        = ["{{tags}}"]

      stakeholder_properties {
        message     = "{{message}}"
        description = "{{description}}"
        enable      = "true"
      }
    }
  }
}

Expected Behavior

New incident rule should be created.

Actual Behavior

opsgenie_service_incident_rule.manual_reports_rule: Creating...
╷
│ Error: Error occurred with Status code: 422, Message: Request body is not processable. Please check the errors., Took: 0.004000, RequestId: 2d0929fa-c74e-4cd6-9d4c-0f50729ed0de, Error Detail: map[incidentRule:Request property [incidentRule] may not be empty.]
│ 
│   with opsgenie_service_incident_rule.manual_reports_rule,
│   on incidentrules.tf line [91](https://gitlab.xxx.com/opsgenie/opsgenie-alerting/some-team/-/jobs/8390788#L91), in resource "opsgenie_service_incident_rule" "manual_reports_rule":
│   91: resource "opsgenie_service_incident_rule" "manual_reports_rule" {

I also have tried without some of optional fields but result was all the same.

Additional notes

I was updating multiple Opsgenie repositories across our organization with mentioned changes. MRs which were merged previous week were applied without issues, afterwards we started experiencing this issue. Seems like something have changed in Opsgenie API during this period?

T0tt1 commented 2 weeks ago

I have the same problem.

Looks like API request expected by OG API has changed. Please help!

Talking with the support currently. According to https://docs.opsgenie.com/docs/service-incident-rules-api

expected body is

curl -X POST "https://api.opsgenie.com/v1/services/<OBFUSCATED>/incident-rules" \
-H "Authorization: GenieKey <OBFUSCATED>" \
-H "Content-Type: application/json" \
-d '{
  "conditionMatchType": "match-all-conditions",
  "conditions": [{
      "expectedValue": "Incident",
      "operation": "contains",
      "not": false,
      "field": "message"
  }],
  "incidentProperties": {
      "message": "Incident Message",
      "description": "Incident Description",
      "tags": ["Tag1", "Tag2"],
      "priority": "P4",
      "stakeholderProperties": {
          "enable": true,
          "message": "Stakeholder Message",
          "description": "Stakeholder Description"
      }
  }
}'

However this returns 422. Support asked to add one layer of abstraction in the payload and this below then returns 200 but we need to work on this to reflect that change to the go file for TF module:

curl -X POST "https://api.opsgenie.com/v1/services/<OBFUSCATED>/incident-rules" \
-H "Authorization: GenieKey <OBFUSCATED>" \
-H "Content-Type: application/json" \
-d '{
 "incidentRule": { <<<--- This line added
  "conditionMatchType": "match-all-conditions",
  "conditions": [{
      "expectedValue": "Incident",
      "operation": "contains",
      "not": false,
      "field": "message"
  }],
  "incidentProperties": {
      "message": "Incident Message",
      "description": "Incident Description",
      "tags": ["Tag1", "Tag2"],
      "priority": "P4",
      "stakeholderProperties": {
          "enable": true,
          "message": "Stakeholder Message",
          "description": "Stakeholder Description"
      }
   }
  } <<<--- ends here
}'
T0tt1 commented 2 weeks ago

Maybe Solution would be something similar? https://github.com/opsgenie/terraform-provider-opsgenie/pull/455

edletifov commented 2 weeks ago

This has been accepted as a bug by Atlassian after P1 for a customer has been reported by us, so perhaps fixing it here is not correct, as instead it may be fixed by Atlassian: https://jira.atlassian.com/browse/OPSGENIE-2359

T0tt1 commented 2 weeks ago

This has been accepted as a bug by Atlassian after P1 for a customer has been reported by us, so perhaps fixing it here is not correct, as instead it may be fixed by Atlassian: https://jira.atlassian.com/browse/OPSGENIE-2359

Whatever works for them, sir. I just tried to help but still, they have the full context of what has happened and how to mitigate it. Proposal here maybe not the cleanest and long term solution, more on setting fire off. :)

ppiotrlach commented 2 weeks ago

Fixed - https://jira.atlassian.com/browse/OPSGENIE-2359