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

Ruleset Rule position cannot exceed current rule count

Open Jarpy opened this issue 2 years ago • 1 comments

The PagerDuty API allows the position of a ruleset rule to exceed the current number of rules. For example, if I have 31 rules, I can move a rule to position 1000, which actually places it at position 31.

$ https PUT https://api.pagerduty.com/rulesets/[redacted]/rules/[redacted] \
            Accept:"application/vnd.pagerduty+json;version=2" \
            Authorization:"Token token=[redacted]" \
            Content-Type:"application/json" \
            rule:='{"position": 1000}'
HTTP/1.1 200 OK
[...]
{
    "rule": {
        "actions": {
            "annotate": null,
            "automation_actions": [],
            "event_action": null,
            "extractions": [],
            "priority": null,
            "route": {
                "value": "[redacted]"
            },
            "severity": null,
            "suppress": null,
            "suspend": null
        },
        "catch_all": false,
        "conditions": {
            "operator": "and",
            "subconditions": [
                {
                    "operator": "equals",
                    "parameters": {
                        "path": "payload.custom_details.instance",
                        "value": "apps-jarpy/ea"
                    }
                }
            ]
        },
        "disabled": false,
        "id": "[redacted]",
        "position": 31,
        "self": "https://api.pagerduty.com/rulesets/[redacted]/rules/[redacted]",
        "time_frame": null,
        "variables": []
    }
}

This can be useful for expressing "please place the rule at the bottom, not the top". Rules are placed at the top when position is not specified.

The provider prevents this from happening, even though it is supported by the PagerDuty API.

https://github.com/PagerDuty/terraform-provider-pagerduty/blob/2cd3cf0880eb3ae8c122ac9f2e73a01a85569e2c/pagerduty/resource_pagerduty_service_event_rule.go#L347-L353

With that validation in place, inserting a rule at the bottom of the list requires knowledge of every rule in the ruleset (or least knowledge of the count). If the rules are not all provisioned by the same Terraform stack, this is, so far as I have discovered, not possible to know.

(If dataSourcePagerDutyRuleset had a length attribute, that might help, but could also be tricky to use in a parallel context). For me, being able to skip the validation would allow me to use the full PagerDuty API. It has its limits, but I could make it work for my use-case if this extra constraint could be bypassed.

Jarpy avatar Jul 07 '22 04:07 Jarpy

I'm just dreaming out loud now, but being able to say position = ">2" would be just amazing. I recognise this is not part of PagerDuty API, but it lets you express "I don't care where, as long as it's after that one". I think this is generally what people want to do, in practice, rather than really caring that "this rule must be number 142".

Jarpy avatar Jul 07 '22 04:07 Jarpy

Rules are being deprecated and replaced

gsreynolds avatar Jun 28 '23 14:06 gsreynolds