Common API workflows
Continuous Delivery for Puppet Enterprise’s API enables users to automate many common workflows.
Get your authorization token
You need a personal access token to automate common workflows using the API.
Make sure that you have read the Authentication section and have a personal access token. You need the personal access token you created in Add a personal access token. It is required for authentication.
Find a workspace ID
When using most API endpoints, you need to specify a workspace ID.
GET
/v1/user
endpoint. The domain of each workspace is its ID.
auth_header="Authorization: <API Token>"
uri="https://<CD4PE hostname>/cd4pe/api/v1/user
curl --insecure --header "$auth_header" --request GET "$uri”
{
"user" : { ... },
"workspaces" : [
{
"domain" : "d6",
"name" : "example_workspace_1"
},
{
"domain" : "d7",
"name" : "example_workspace_2"
}
]
}
Find a Puppet Enterprise environment node group ID
Find the ID of the environment node group in Puppet Enterprise using the ID of your Puppet Enterprise integration.
Get the SHA of a repo branch
Find the SHA of the HEAD of a given repo branch using Continuous Delivery for PE’s /v1/vcs/branches API endpoint.
To find the SHA of the HEAD of a given repo branch, you can either find the commit in your VCS provider or you can use Continuous Delivery for PE’s /v1/vcs/branches API endpoint. Here, project is used to refer to the owner/org of the repo for all providers except GitHub. If your repo comes from GitHub, you must supply either organization or owner instead, depending on whether the repo belongs to an individual or an organization.
auth_header="Authorization: <API Token>"
uri="https://<CD4PE host>/cd4pe/api/v1/vcs/branch?workspaceId=<workspace ID>&provider=BITBUCKET&project=puppet&name=my_control_repo&branch=main
curl --insecure --header "$auth_header" --request GET "$uri”
{
"name" : "main",
"headSha" : "4c2f400a77cd73ebf67271361e1dd9b1102b43b4"
}
Trigger an Impact Analysis
Use the Continuous Delivery for PE API to trigger an impact analysis on the module of a control repo.
- The ID of the workspace containing the module of control repo you want to test
- The name of the module or control repo being analyzed
- The ID of the Puppet Enterprise node group for each environment you want to see the impact on
- The branch and SHA of the code you want to test the impact of
data="$(cat <<EOF
{
"workspaceId": "<workspace ID>",
"projectName": "control-repo",
"projectType": "CONTROL_REPO",
"settings": {
"environments": [
{
"nodeGroupId": "<environment node group ID>",
"peCredentialsId": {
"name": "example-pe",
"domain": "<workspace ID>"
}
},
{
"nodeGroupId": "defcea94-e7e5-4fe3-b971-1acd44785695",
"peCredentialsId": {
"name": "example-pe",
"domain": "<workspace ID>"
}
}
],
"sourceBranch": "<branch to test>",
"sourceSha": "<SHA to test>",
"analyzeAllDeployments": false,
"compileBatchSize": 10,
"controlRepoId": {
"name": "control-repo",
"domain": "<workspace ID>"
}
}
}
EOF
)"
type_header='Content-Type: application/json'
auth_header="Authorization: <authorization token>"
uri="https://<cd4pe host>/cd4pe/api/v1/impact-analysis/trigger"
curl --insecure --header "$type_header" --header "$auth_header" --request POST "$uri" --data "$data"
This returns something similar to the following:
{
"appEventId" : 18900,
"baseTaskUrl" : "https://<cd4pe host>/cd4pe/root/deployments",
"domain" : "d5",
"id" : 110,
...data repeated from request...
"state" : "QUEUED"
}
Once the impact analysis has finished, you might want to check the state of the run and fetch the analysis report, to do so use the ID of the response that was returned like so:
type_header='Content-Type: application/json'
auth_header="Authorization: <API token>"
uri="https://<cd4pe host>/cd4pe/api/v1/impact-analysis/<IA run ID>/csv?workspaceId=<workspace ID>"
curl --insecure --header "$type_header" --header "$auth_header" --request GET "$uri"
Trigger a deployment
Use the Continuous Delivery for PE API to trigger a deployment from one of your branches to a Puppet Enterprise environment.
- The ID of the workspace containing the module of control repo you want to deploy from
- The ID of the Puppet Enterprise node group for each environment you want to deploy to
- The source and target branch of the repo and the SHA of the source branch that you want to deploy
- The details of the deployment policy you want to use
Trigger a pipeline
Use the Continuous Delivery for PE API to trigger a pipeline from one of your branches to a Puppet Enterprise environment.
- The ID of the workspace containing the pipeline you want to trigger
- The name of the control repo or module whose pipeline you want to trigger
- The ID of the pipeline belonging to that code project
- The SHA with which you want to trigger the pipeline