Skip to content

Instantly share code, notes, and snippets.

@rssnyder
Last active December 13, 2022 18:02
Show Gist options
  • Save rssnyder/19e05e1e7d665cec9a6b89f1ceefc1c5 to your computer and use it in GitHub Desktop.
Save rssnyder/19e05e1e7d665cec9a6b89f1ceefc1c5 to your computer and use it in GitHub Desktop.

harness first-gen api review

function to call graphql api

docs: https://docs.harness.io/article/tm0w6rruqv-harness-api

fn_run_query () {

  #
  # env config
  #  - HARNESS_API_KEY
  #. - HARNESS_ACCOUNT_ID
  #

  curl -s \
    -H 'x-api-key: '$HARNESS_API_KEY \
    -X POST \
    -H 'Content-Type: application/json' \
    --data @- \
    'https://app.harness.io/gateway/api/graphql?accountId='$HARNESS_ACCOUNT_ID
}

query example

get git connectors

cat <<_EOF_ | fn_run_query
{"query":"

  query GetGits {
    connectors(limit: 5, filters: [{connectorType: {operator: EQUALS, values: [GIT]}}]) {
      nodes {
        id
        name
      }
    }
  }

"}
_EOF_

mutation example with inputs

be sure to escape all $ characters within the query

env:

  • APP_NAME
  • APP_DESC
export APP_NAME="myapp"
export APP_DESC="my first app"

cat <<_EOF_ | fn_run_query
{"query":"

  mutation(\$app: CreateApplicationInput!) {
    createApplication(input: \$app) {
      clientMutationId
      application {
        name
        id
      }
    }

  }",
   "variables":{
     "app": {
        "name": "$APP_NAME",
        "description": "$APP_DESC"
      }
  }
}
_EOF_

using config-as-code and REST api

useful for resources that are not supported in the graphql api

docs: https://docs.harness.io/article/bbmhnoqrzw-config-as-code-using-rest-apis

function to upload file

with this you can create resources using their config-as-code yaml specification

fn_put_file () {

  #
  # env config
  #  - HARNESS_API_KEY
  #. - HARNESS_ACCOUNT_ID
  #
  # usage: fn_put_file "harness/file/path" "path/to/loca/file"
  #

  FILE_PATH=$1
  FILE=$2

  curl -X POST \
    "https://app.harness.io/gateway/api/setup-as-code/yaml/upsert-entity?accountId=$HARNESS_ACCOUNT_ID&yamlFilePath=$FILE_PATH" \
    -H 'accept: application/json, text/plain, */*' \
    -H "x-api-key: $HARNESS_API_KEY" \
    -H 'content-type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW' \
    --form "yamlContent=@\"$FILE\""
}

connector example

echo "harnessApiVersion: '1.0'
type: DOCKER
password: gcpkms:dockerhub
skipValidation: false
url: https://index.docker.io/v2/
username: rileysnyderharnessio" > dockerhubapi.yaml

fn_put_file "Setup/Artifact%20Servers/dockerhubapi.yaml" "$(pwd)/dockerhubapi.yaml"

environment example

echo "harnessApiVersion: '1.0'
type: ENVIRONMENT
configMapYamlByServiceTemplateName: {}
environmentType: NON_PROD" > dev.yaml

export APP_NAME=singular
export ENVIRONMENT=dev

fn_put_file "Setup/Applications/$APP_NAME/Environments/$ENVIRONMENT/Index.yaml" "$(pwd)/dev.yaml"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment