Deployment of Mule application to Cloudhub using a connected app

Shyam Raj Prasad
4 min readApr 15, 2022

--

In this article, we will create a mule application and deploy the same to the cloud hub using Connected App.

Connected APP : The Connected Apps feature provides a framework that enables an external application to integrate with Anypoint Platform using APIs through OAuth 2.0 and OpenID Connect. Connected apps help users delegate their access without sharing sensitive credentials or giving full control of their accounts to third parties.

Create a Mule Application in Anypoint studio

Lets first create a mule application and below are the steps:

  • Download a RAML zip file from https://anypoint.mulesoft.com/exchange/68ef9520-24e9-4cf2-b2f5-620025690913/covid19-data-tracking-api/
  • Go to the design center and import this RAML file for creating API specifications. Give name as COVID-19 Data Tracking API
  • Publish the same RAML file to exchange with version 1.0.0
  • Go to Anypoint studio and create a new project as covid-data-tracking-api . Choose import a published API. It will prompt a new window and enter your Anypoint credentials. Search Covid and import the RAML which you have published from your organization account.
  • Run the application in Anypoint studio and hope it will be successfully deployed.

Configure Maven Settings using Connected App

We will create a global Maven settings.xml that allows a Maven build outside of Studio to retrieve artifacts — such as the COVID-19 Data Tracking API specification — from your Anypoint Platform organization’s Exchange.

  • Maven-build: In a command-line interface, navigate to the base directory of covid-data-tracking-api and run a Maven build of this Mule app; this should succeed: mvn clean verify
  • Delete local Maven repo: Now let’s go to directory \.m2\repository and run the below command to remove the delete all artifacts downloaded from any exchange.
rm -rf ~/.m2/repository/*-*-*-*-*
  • Maven-build: Re-run the Maven build from a command-line interface; this should now fail due to unresolvable dependencies
[ERROR] Failed to execute goal on project covid-data-tracking-api: Could not resolve dependencies for project com.mycompany:covid-data-tracking-api:mule-application:1.0.0-SNAPSHOT: Failed to collect dependencies at 4dfec539-dc02-40fe-8618-8747d32cfc66:covid-19-data-tracking-api:zip:raml:1.0.0: Failed to read artifact descriptor for 4dfec539-dc02-40fe-8618-8747d32cfc66:covid-19-data-tracking-api:zip:raml:1.0.0: Could not transfer artifact 4dfec539-dc02-40fe-8618-8747d32cfc66:covid-19-data-tracking-api:pom:1.0.0 from/to anypoint-exchange-v2 (https://maven.anypoint.mulesoft.com/api/v2/maven): authentication failed for https://maven.anypoint.mulesoft.com/api/v2/maven/4dfec539-dc02-40fe-8618-8747d32cfc66/covid-19-data-tracking-api/1.0.0/covid-19-data-tracking-api-1.0.0.pom, status: 401 Unauthorized -> [Help 1]
Create Exchange Viewer Connected App: In Anypoint Access Management, create a new Connected App that acts on its own behalf (client credentials), for reading assets from Exchange, adding the Exchange Viewer scope, and retrieve its client ID and secret. client ID and secret will be used in password section of settings.xml
  • Create settings.xml: In a text editor, create settings.xml in ~/.m2/ with credentials for reading from your Anypoint Platform organization’s Exchange as a Maven repository:
<settings>
<servers>
<server>
<id>anypoint-exchange-v2</id>
<username>~~~Client~~~</username>
<password>clientID~?~clientSecret</password>
</server>
</servers>
</settings>
  • To use Connected App authentication, provide basic authentication and define the username as ~~~Client~~~ and the password as clientID~?~clientSecret. Replace clientID with the client ID and clientSecret with the client secret from Connected App.
  • Maven-build: In a command-line interface, re-run a Maven build of covid-data-tracking-api as before; this should now succeed

Deploy to CloudHub using the Mule Maven plugin

In this section, we will use the existing Maven build configuration to perform automated deployments to CloudHub. We need to create a Connected App for authentication for deployment. we will deploy the mule application covid-data-tracking-api to the CloudHub prod environment from the Maven command-line interface, passing the client ID and secret.

  • Configure CloudHub deployment: Add below configuration to Mule Maven plugin configuration for a simple deployment to CloudHub, utilizing Object Store v2, and using your initials as the hostname prefix as before, such as shyam2603-:
<configuration>
<cloudHubDeployment>
<businessGroup/>
<environment>dev</environment>
<region>us-east-2</region>
<muleVersion>4.4.0</muleVersion>
<applyLatestRuntimePatch>true</applyLatestRuntimePatch>
<workers>1</workers>
<workerType>MICRO</workerType>
<objectStoreV2>true</objectStoreV2>
<applicationName>shyam2603-${project.artifactId}</applicationName>
<deploymentTimeout>600000</deploymentTimeout>
<connectedAppClientId>${ap.ca.client_id}</connectedAppClientId>
<connectedAppClientSecret> ${ap.ca.client_secret}</connectedAppClientSecret>
<connectedAppGrantType>client_credentials</connectedAppGrantType>
<properties>
<anypoint.platform.client_id> ${ap.client_id}</anypoint.platform.client_id>
<anypoint.platform.client_secret> ${ap.client_secret}</anypoint.platform.client_secret>
</properties>
</cloudHubDeployment>
</configuration>
  • Create CloudHub Deployment Connected App: In Anypoint Access Management, create a new Connected App that acts on its own behalf (client credentials), for deployment to CH, adding the Cloudhub Organization Admin, Create Applications, Delete Applications, Read Applications, View Environment and View Organization scopes and applying to all environments and retrieve its client ID and secret. These client Id and secret will be used in the above configuration as ap.ca.client_id and ap.ca.client_secret
  • Create Environment: In Anypoint Access Management, create a new environment dev in the sandbox and retrieves its client ID and secret. These client ID and secret will be used in the above configuration as ap.client_id and ap.client_secret
  • Maven-deploy: In a command-line interface, deploy to CloudHub using the Mule Maven plugin, supplying the same client ID and secret of your Anypoint Platform organization as previously and the client ID and secret of your Connected App; this should result in a successful build:
mvn -DmuleDeploy deploy -Dap.client_id=<insert-envirnoment-client-id> -
Dap.client_secret=<insert-environment-client-secret>
-Dap.ca.client_id=<insert-connected-app-client-id> -Dap.ca.client_secret
=<insert-connected-app-client-secret>
  • Wait and observe: In Runtime Manager, verify the successful deployment of covid-data-tracking-api with your chosen hostname prefix to the dev environment, resulting in the same fully qualified domain name and endpoint URL as before.
  • Invoke: Invoke the API using this endpoint URL; this should return an HTTP 200 OK response with body:
curl --location --request GET 'http://shyam2603-covid-data-tracking-api.us-e2.cloudhub.io/api/covid19/v1/locations?zipCode=01011'
  • Note: The hostname will be different for you. The above endpoint URL will not be accessible after some time, as I am using a trial account.

--

--

Shyam Raj Prasad
Shyam Raj Prasad

Written by Shyam Raj Prasad

Engineering Leader at Tricon Infotech Private Limited | Mulesoft Certified Developer and Architect

No responses yet