Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Using Mule CloudHub APIs

DZone's Guide to

Using Mule CloudHub APIs

Learn about how to access Mule ESB functions programmatically by deploying applications in the CloudHub API environment.

· Integration Zone
Free Resource

Share, secure, distribute, control, and monetize your APIs with the platform built with performance, time-to-value, and growth in mind. Free 90 day trial 3Scale by Red Hat

CloudHub APIs provide a way to access Mule ESB functions programmatically. CloudHub provides many APIs to use in external applications. In this article, some of the important APIs will be discussed.

Where To Get The APIs

All the APIs exposed by Anypoint Platform can be found here. The URL of the Cloudhub APIs can be accessed here. Also, the Mule documentation contains some important information on how to use the Cloudhub APIs. It can be found here .

Deploy Using Cloudhub API

Here in this section, the important and necessary steps to deploy a Mule application  using all those APIs in the above section will be discussed.

Login API

First, you will need to get the access token. Here is how to get it:

API Endpoint 

https://anypoint.mulesoft.com/accounts/login

Method

POST
Input
{
"username" : "max" ,
"password" : "mule"
}


Output
{
"access_token" : "b33e50a1-14a9-4f42-a92d-65940e02324b",
"token_type" : "bearer",
"redirectUrl" : "/home/"
}



Get Anypoint Platform Account Information

After getting the token, let's get the information about our Anypoint Platform account.

API Endpoint

https://anypoint.mulesoft.com/accounts/api/me

Method GET

Content-Type

application/json

Header

Authorization → token we retrieved at Login step

Output
{
"access_token" : {
"expires_in" : 3278
},
"user" : {
"id" : "5e2b163d-f28a-4cdd-a199-3e2588354bba",
"createdAt" : "2017-05-18T21:49:27.916Z",
"updatedAt" : "2017-06-07T13:23:39.030Z",
"firstName" : "Anupam",
"lastName" : "Gogoi",
"email" : "a@gmail.com",
"phoneNumber" : "1111111111",
"idprovider_id" : "mulesoft",
"username" : "dgogoi",
"enabled" : true,
"deleted" : false,
"organizationPreferences" : {
"deedf975-d1a6-4191-acf5-ed203ecd7970" : {
"defaultEnvironment" : "264aabf7-3e56-435b-a775-c6cf5c15f9c0"
}
},
"organization" : {
"name" : "AC",
"id" : " deedf975-d1a6-4191-acf5-ed203ecd7970 ",
"createdAt" : "2017-05-18T21:49:27.955Z",
"updatedAt" : "2017-05-30T19:22:03.697Z",
"ownerId" : "5e2b163d-f28a-4cdd-a199-3e2588354bba",
"clientId" : "0c19a09185334e9b977c4c4790e908fa",
"domain" : "ac-9"
}
}


In the output, the value of our concern is the Organization ID.

Get the Environment ID

The Environment (Development, Production, Quality) ID can be obtained in two ways:

  • Using CLI tools.

  • Using the API.

Using CLI tools

Please check this link to install and configure Anypoint CLI. After installing, please log into Anypoint Platform.

Screen Shot 2017-06-06 at 6.13.21 PM.png

After login, execute the following command:  account environment list 

Screen Shot 2017-06-06 at 6.16.55 PM.png

It will show the environments and their respective IDs.

Using the API

API Endpoint

https://anypoint.mulesoft.com/accounts/api/organizations/{ORGANIZATION_ID}/environments

Header

Authorization → token we retrieved at Login step

URI Param

ORGANIZATION_ID → It can be found in the step where we retrieved the

Anypoint Platform account information.

Method GET
Content-Type

application/json

Output
{
"data" : [
{
"id" : " 264aabf7-3e56-435b-a775-c6cf5c15f9c0 ",
"name" : "Production",
"organizationId" : "deedf975-d1a6-4191-acf5-ed203ecd7970",
"isProduction" : true,
"type" : "production"
}
],
"total" : 1
}



Deploy to the Environment

API Endpoint

https://anypoint.mulesoft.com/cloudhub/api/v2/applications

Authentication

Basic Authentication → Anypoint Platform Login and Password

Header

X-ANYPNT-ENV-ID → The environment ID we have retrieved in the previous step

Method POST
Content-Type

multipart/form-data

Required parameters are,

file → Browse to the zip archive to be deployed

appInfoJson

{
"domain" : "maven-test",
"muleVersion" : {
"version" : "3.8.2"
},
"region" : "us-east-1",
"monitoringEnabled" : true,
"monitoringAutoRestart" : true,
"workers" : {
"amount" : 1,
"type" : {
"name" : "Micro",
"weight" : 1,
"cpu" : "0.1 vCore",
"memory" : "500 MB memory"
}
},
"loggingNgEnabled" : true,
"persistentQueues" : true
}


Output
{
"versionId" : "593805c0e4b0a6bb61298834",
"domain" : "maven-test",
"fullDomain" : "maven-test.cloudhub.io",
"properties" : {},
"propertiesOptions" : {},
"status" : " UNDEPLOYED ",
"workers" : {
"type" : {
"name" : "Micro",
"weight" : 0.1,
"cpu" : "0.1 vCores",
"memory" : "500 MB memory"
},
"amount" : 1,
"remainingOrgWorkers" : 0.8,
"totalOrgWorkers" : 1
},
"lastUpdateTime" : 1496843712690,
"fileName" : "maven-test-1.0.0.zip",
"muleVersion" : {
"version" : "3.8.2",
"updateId" : "589baf5be4b0126ff1b986d5",
"endOfSupportDate" : 1633417200000
},
"userName" : "",
"persistentQueues" : true,
"persistentQueuesEncryptionEnabled" : false,
"persistentQueuesEncrypted" : false,
"monitoringEnabled" : true,
"monitoringAutoRestart" : true,
"staticIPsEnabled" : false,
"hasFile" : true,
"secureDataGatewayEnabled" : false,
"loggingNgEnabled" : true,
"loggingCustomLog4JEnabled" : false,
"trackingSettings" : {
"trackingLevel" : "DISABLED"
},
"logLevels" : [],
"ipAddresses" : []
}


At this step, the application is uploaded to CloudHub but is not yet deployed. To deploy, please use the following API.

Start the Application

API Endpoint

https://anypoint.mulesoft.com/cloudhub/api/applications/{APP_NAME}/status

Authentication

Basic Authentication → Anypoint Platform Login and Password

Header

X-ANYPNT-ENV-ID → Environment ID we have extracted

URI param

APP_NAME → Name of the app to be started

Method POST
Content-Type

application/json

Input
{
"status" : " 'stop' or 'start' "
}


Output

{}


Conclusion

In this article, I have discussed some of the important APIs exposed by Cloudhub. For a detailed explanation of all the APIs exposed by CloudHub, please have a look at the developer portal exposed by Anypoint Platform. Interestingly, the mule-maven-plugin is created using all these APIs.

N.B.: All the above APIs have been tested with Postman only.

Discover how you can achielve enterpriese agility with microservices and API management

Topics:
cloudhub ,mule ,integration ,api

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}