Using the API

Learn the prerequisites for using the IBM® Hybrid Cloud Mesh (Mesh) API, review the API specification, and see examples of how to use it in curl and Python.

Prerequisites

Mesh API specification

Review the IBM Hybrid Cloud Mesh API specification on IBM API Hub.

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 connection 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())

Querying resources

When you query for a specific resource, you might receive one of the following responses:

  • The current state of that resource.
  • A no permission error, which indicates that you don't have sufficient permission to access the resource.

When querying for a list of resources, you might receive one of the following responses:

  • An empty list, if no resources that are matching the query.
  • A list of resources that match the query and to which you have access.
  • A no permission error, which indicates that you don't have sufficient permission to access any of the matching resources.