This application is an example of how the Harness CLI commands can be exposed via a REST API. There are a handful of commmands that have been implemented in this repo and are documented below.
Note: there is minimal error handling implemented in these examples.
Requires: NodeJS v12+
- Clone this repo
- Run
npm install
- Run
node index.js
Or running in docker:
docker build -t harness-api .
docker run -p 3000:3000 -d harness-api
There are a handful of common parameters that can be provided in various ways:
- Harness Account ID
- Harness API Key
- Harness Username (required for config as code APIs)
- Harness Password (required for config as code APIs)
The simplest way to pass these is to call the login command which returns a token that can be reused across subsequent requests. The options include:
- Cookie from
/login
persists across requests - Token from
/login
passed inx-jwt-token
header - Using basic auth (if password is blank, the username is used as the API key)
- Querystring parameters:
http://localhost:3000/apps?harnessAccountId=xxxx
- Header:
x-harness-account-id: xxx
- Setting environment variable:
HARNESS_USERNAME=xxx
POST /login
Generate a JWT token that contains all required account information, which can be used to authenticate to other API requests. The token is returned in the response body as well as in a cookie that can be persisted across requests.
ACCOUNT_ID='your account id'
API_KEY='your api key'
USERNAME='your harness username'
PASSWORD='your harness password'
curl --location --request POST "http://localhost:3000/login" \
--header "Content-Type: application/x-www-form-urlencoded" \
--data-urlencode "harnessAccountId=${ACCOUNT_ID}" \
--data-urlencode "harnessApiKey=${API_KEY}" \
--data-urlencode "harnessUsername=${USERNAME}" \
--data-urlencode "harnessPassword=${PASSWORD}"
{
"token": "..."
}
curl 'http://localhost:3000/apps' \
--header "x-jwt-token: ${JWT_TOKEN}"
[
{
"id": "tA-8xI-UQrWIc50h6hdamg",
"name": "cv-demo",
"description": null,
"createdAt": 1607545302159,
"createdBy": null
},
...
]
curl 'http://localhost:3000/apps/cv-demo' \
--header "x-jwt-token: ${JWT_TOKEN}"
{
"id": "tA-8xI-UQrWIc50h6hdamg",
"name": "cv-demo",
"description": null,
"createdAt": 1607545302159,
"createdBy": null
}
Requires Harness username and password global variables to be provided.
curl 'http://localhost:3000/config-as-code/list-files' \
--header "x-jwt-token: ${JWT_TOKEN}"
[
"Setup/Defaults.yaml",
"Setup/Tags.yaml",
...
]
Requires Harness username and password global variables to be provided.
Fetch YAML contents for config as code file at a specific path.
Query string params:
path
: the path to the fileraw
: Optional - if true, then the response will be raw YAML. Otherwise, the yaml will be returned inside a JSON array.
curl 'http://localhost:3000/config-as-code/path=Setup/Tags.yaml&raw=true' \
--header "x-jwt-token: ${JWT_TOKEN}"
harnessApiVersion: '1.0'
type: TAG
tag:
- name: some_tag
OR
[
{
"path": "Setup/Tags.yaml",
"content": "harnessApiVersion: '1.0'\ntype: TAG\ntag:\n- name: some_tag"
}
]
Requires Harness username and password global variables to be provided.
FILE_PATH='Setup/Tags.yaml'
CONTENT='
harnessApiVersion: "1.0"
type: TAG
tag:
- name: some_tag
- name: another_tag
'
curl --location --request POST 'http://localhost:3000/config-as-code/upsert' \
--header "x-jwt-token: ${JWT_TOKEN}" \
--header "Content-Type: application/x-www-form-urlencoded" \
--data-urlencode "content=${CONTENT}" \
--data-urlencode "path=${FILE_PATH}"
{
"metaData": {},
"resource": {
"yamlFilePath": "Setup/Tags.yaml",
"status": "SUCCESS",
"errorMssg": ""
},
"responseMessages": []
}
Or in the event of a failure:
{
"metaData": {},
"resource": {
"yamlFilePath": "Setup/Tags.yaml",
"status": "FAILED",
"errorMssg": "Invalid request: Tag is in use. Cannot delete"
},
"responseMessages": []
}