how to regenerate pagerduty integration key programmatically

390 views Asked by At

I have integrated Jenkins CI with pagerduty. Once I do that, I can see intergration key generated. enter image description here

That will be used in jenkins to send the events to pagerduty.

The requirement is to rotate the keys after some time. I want to automate this. Is there any api to regenerate the intergration key and return the key in response to be stored in jenkins?

2

There are 2 answers

0
Hannele On

I think the simplest solution here is to use the REST API -- it isn't possible to regenerate the integration key directly, but you can delete the integration and create a new one programmatically.

First fetch the service details:

curl --location --request GET 'https://api.pagerduty.com/services/<service_id>' \
--header 'Accept: application/vnd.pagerduty+json;version=2' \
--header 'Authorization: Bearer <bearer_token>'

This will include all of the integrations on the service -- make note of the integration_id and the vendor_id.

The delete endpoint isn't documented but it does seem to exist:

curl --location --request DELETE 'https://api.pagerduty.com/services/<service_id>/integrations/<integration_id>' \
--header 'Accept: application/vnd.pagerduty+json;version=2' \
--header 'Authorization: Bearer <bearer_token>'

And finally you can create the new integration, using the vendor_id from the GET request:

curl --request POST \
  --url https://api.pagerduty.com/services/id/integrations \
  --header 'Accept: application/vnd.pagerduty+json;version=2' \
  --header 'Authorization: Bearer <bearer_token>' \
  --header 'Content-Type: application/json' \
  --data '{
  "integration": {
    "type": "generic_email_inbound_integration",
    "name": "Email",
    "service": {
      "id": "<service_id>",
      "type": "service_reference"
    },
    "integration_email": "[email protected]",
    "vendor": {
      "type": "vendor_reference",
      "id": "<vendor_id>"
    }
  }
3
Akhil Jain On

On doing inspect element to UI button UI button to regenerate key

Its executing POST API:

https://xxxxxxx.pagerduty.com/api/v1/services/XXXXXXX/integrations/XXXXXXX/regenerate_key