pypdras how do you create a service or integration?

questions

#1

I’ve searched around a bit and couldn’t find any information on how to create a service and integration with pypdras. Lots of examples for listing and it has the ability to delete according to the documentation, but I don’t see any create examples. Could someone help me out and provide me with an example of how to create a service and then associate an integration with that service using pypdras?


(Demitri Morgan) #2

Hi @dm,

The important thing to keep in mind is that pdpyras primarily an interface that makes it easier to use the REST API directly, i.e. make the HTTP requests. That being said, all of the options for setting up any given thing in PagerDuty are available in the request schemas shown in the API reference.

Firstly, to create a service: one would make a POST request to /services. With pdpyras, there are a few options (post and rpost) but in any case you will define the body of the post request as a dictionary object and then pass it to the json keyword argument much like you would if using requests.post.

Finally, a prerequisite for creating a service is having an escalation policy to associate with the service, so you’ll need to have that ready. So, if your session object is simply called session, the API call to create the service could be, for example:

service_body = {
    "name": "example service", 
    "escalation_policy":{
        "id":"ESCALATION-POLICY-ID-HERE",
        "type":"escalation_policy_reference"
    },
}
    
new_service = session.rpost('/services', json=service_body)

To create an integration will involve a post request to /services/{id}/integrations where {id} is the ID of the new service. You do however need to select the vendor corresponding to the integration type that you want to create, and you can find the list of available vendors in the vendors index endpoint. You can use as an iterator session.iter_all('/vendors') to search through vendors.

For instance, this statement will return the first vendor with “CloudWatch” in its name:

cw_vendor = next(iter(filter(
    lambda v: "CloudWatch" in v['summary'], 
    session.iter_all('/vendors')
)), None)

Also, to do this much faster (BUT full disclosure, it’s not 100% stable, as it is currently unsupported/undocumented for general REST API use): the vendors endpoint will actually accept a query parameter similar to other index endpoints (see: filtering). So, similar to above but with filtering:

cw_vendor = next(iter(filter(
    lambda v: "CloudWatch" in v['summary'],
    session.iter_all('/vendors', params={'query':"CloudWatch"})
)), None)

So with our vendor we can make an Amazon CloudWatch integration on the service and get the integration key/url:

new_integration = session.rpost(
    '/services/'+new_service['id']+'/integrations',
    json={
        "name": "Amazon CloudWatch Integration",
        "type":"generic_events_api_inbound_integration",
        "vendor": cw_vendor
    }
)
integration_key = new_integration['integration_key']
integration_url = 'https://events.pagerduty.com/integration/'+integration_key+'/enqueue'

#3

Thank you for the explanation and sample, it really helped. I’ve just got a shell so far but with your examples I was able to get it working. Thanks again!


(system) #4

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.