Using the API

IBM Hybrid Cloud Mesh This document provides an overview of the IBM Hybrid Cloud Mesh (Mesh) APIs and how to use them. Examples in curl and Python are shown.

Prerequisites

Using the API with curl

The API output is in JSON format. It is easier to view it if you install the jq JSON processor.

Get the cloud resources:

curl -sSLf -w %{http_code} -H "Authorization: $MESH_API_KEY" $MESH_API_URL/nets/cloud | jq

Get the cluster resources in the cloud named IBM-Cloud:

cloud_id="$(curl -sSLf -H "Authorization: $MESH_API_KEY" $MESH_API_URL/nets/cloud | jq -r '.clouds[] | select(.name == "IBM-Cloud") | .resource_id')"
curl -sSLf -w %{http_code} -H "Authorization: $MESH_API_KEY" $MESH_API_URL/nets/cloud/$cloud_id/cluster | jq

Register a cloud resource by sending the cloud fields to the stdin of curl:

cat << EOM | curl -sSLf -w %{http_code} -H "Authorization: $MESH_API_KEY" -X POST -H Content-Type:application/json -d @- $MESH_API_URL/nets/cloud | jq
{
  "name": "mycloud",
  "type": "other",
  "is_private": true
}
EOM

Get the application resources:

curl -sSLf -w %{http_code} -H "Authorization: $MESH_API_KEY" $MESH_API_URL/apps/application | jq

Register an application resource by sending the application fields to the stdin of curl:

cat << EOM | curl -sSLf -w %{http_code} -H "Authorization: $MESH_API_KEY" -X POST -H Content-Type:application/json -d @- $MESH_API_URL/apps/application | jq
{
  "name": "another-app",
  "app_identity": "app.my.domain.example",
  "labels": [
    "app:exampleWeb",
    "cloud:aws"
    ]
}
EOM

Get the policy resources:

curl -sSLf -w %{http_code} -H "Authorization: $MESH_API_KEY" $MESH_API_URL/apps/policy | jq

Get the identity (user) resources:

curl -sSLf -w %{http_code} -H "Authorization: $MESH_API_KEY" $MESH_API_URL/auth/identity | jq

Get the event resources:

curl -sSLf -w %{http_code} -H "Authorization: $MESH_API_KEY" $MESH_API_URL/events/event | jq

Get the resource topology:

curl -sSLf -w %{http_code} -H "Authorization: $MESH_API_KEY" "$MESH_API_URL/topology/topology?visibility=all" | jq

Using the API with Python

To use the Mesh API from Python, use the Python Requests package. Install the package with pip:

pip install requests

Use the Requests package to make calls to an API endpoint. The following example shows a simple get applications call in Python (replace <console-url> with https://app.hybridcloudmesh.ibm.com):

import requests 
MESH_API_URL = "<console-url>/api/v1"
MESH_API_KEY = "<your-api-key>"

# get applications 
r = requests.get(MESH_API_URL + "apps/application", headers={'Authorization': MESH_API_KEY})

print(r.status_code)
print(r.json())