PagerDuty / pdpyras

Low-level PagerDuty REST/Events API client for Python
MIT License
129 stars 29 forks source link

Could we get more detailed update examples? #75

Closed ajmcateer closed 2 years ago

ajmcateer commented 2 years ago

I would like to use the API to change and escalation_rules target from a schedule_reference to a user_reference.

I am getting all the Escalation Policies like so

services = list(session.iter_all('escalation_policies', params={'query': 'myquery'}))

How could I go through them and update the escalation_rules target?

Deconstrained commented 2 years ago

Hi @ajmcateer,

I'll start a folder in the repo with examples.

The escalation_rules property is an array (translates to a list type in Python); each entry should be an object (dict) with another list of dicts in targets property. In general, array -> list and object -> dict when decoded from JSON.

https://developer.pagerduty.com/api-reference/b3A6Mjc0ODEyOA-update-an-escalation-policy

So, for example, the first target on level 2 of an escalation policy (represented as ep = an item yielded from the iterator session.iter_all('escalation_policies' ...)) would be ep['escalation_rules'][1]['targets'][0]. Setting this to a new dict representing a user reference, i.e. ep['escalation_rules'][1]['targets'][0] = {'type': 'user_reference', 'id': '{user_id}'}, and then using session.rput(ep, json=ep), would update it accordingly.

The client doesn't have any logic that pertains specifically to any given resource, i.e. abstraction for escalation policies, and so one must work directly with API schemas. For that reason I've not really written documentation that pertains only to a given API resource as that would be redundant (there's already documentation for our schemas in the API reference).