On-Demand-Schedulers With MuleSoft CloudHub APIs
In this article, learn how to use MuleSoft's CloudHub APIs to run schedulers of an application on demand as well as how to access payload in HTML.
Join the DZone community and get the full member experience.
Join For FreeIn some projects, there might be many scheduler-based applications, and the testing team from either source or target systems doesn't have access to the Anypoint platform to run schedulers whenever required; thus, having to rely on MuleSoft developers to run the schedulers for them. In this article, you will learn how to create a simple web page using HTML for fetching the user's input and use CloudHub APIs to run the schedulers based on the user's inputs provided.
This will save a lot of time for the testing team to test the integration in the lower environments. I would recommend not using this for testing in production as if someone misuses it by running the schedulers more frequently, then integration might break.
Implementation
Step 1
Go to Anypoint platform and create a Connected App in Access Management for using CloudHub APIs. Limit the scope of the Connected App to Runtime Manager only. You can go through this MuleSoft document to learn how to create a Connected App in Access Management.
Copy the Client ID and Secret, which will be used for fetching the access_token
to authenticate requests for CloudHub APIs.
Step 2
Create a new Mule application in Anypoint Studio and create the following HTML files inside src/main/resources:
- applications.html
- homepage.html
- invalidCredentials.html
- notfound.html
- schedulers.html
- signin.html
- thankyou.html
First, create the flow as shown below. This is the entry point of the application which will open the Signin web page using the Parse Template component.
signin.html
<html>
<body>
<h1>On Demand Schedulers</h1>
<form action="http://run-scheduler-app.us-e2.cloudhub.io/environments" method="POST" >
<!-- for running in local <form action="http://localhost:8081/environments" method="POST"> -->
<label for="environment">Enter the Username and Password to signin</label>
<br><br>
<label for="username">Username: </label>
<input type="text" id="username" name="username"><br><br>
<label for="password">Password: </label>
<input type="password" id="password" name="password"><br><br>
<br><br>
<input type="submit" value="Signin">
</form>
</body>
</html>
In the above HTML, the form
tag's action
attribute should call the endpoint of the mule flow "run-scheduler-environments" (listening to the endpoint /environments) as shown in the Mule image below. The method should be POST
so that the credentials do not get displayed in the URL.
On the web page above, you will have to enter the username and password for the non-prod/prod environment in which you want to run the scheduler. The credentials for non-prod and prod environments are managed in the properties file. The Mule flow, as shown below, will validate the credentials and proceed further to get the environment details. If the credentials are invalid, then it won't proceed further, and you will get an error message.
invalidCredentials.html
<html>
<body>
<h1>On Demand Schedulers</h1>
<h2>Invalid Username or Password !!</h2>
<br><br>
<form action="http://run-scheduler-app.us-e2.cloudhub.io/onDemandSchedulers">
<br><br>
<input type="submit" value="Go Back">
</form>
</body>
</html>
Here, in the above HTML, the form tag's action attribute should call the Mule flow "run-scheduler-onDemandSchedulers
," listening to the endpoint /onDemandSchedulers
. If you click on the Go Back button, it will take you to the sign-in page again.
Step 3
To send a request to CloudHub APIs, you must generate the access token by using the following curl
:
curl --location 'https://anypoint.mulesoft.com/accounts/api/v2/oauth2/token' \
--header 'Content-type: application/json' \
--data '{
"client_id": "{{client_id}}",
"client_secret": "{{client_secret}}",
"grant_type": "client_credentials"
}
Here, the client_id
and secret
s need to be passed from Step 1. The response that you will get is shown below. Store the access_token
in the object store and set the Entry TTL property to 3600
(same as the expires_in property
in the response). This will ensure that the key expires after 3600 seconds.
Implement the above logic in the Mule flow as shown below.
Step 4
After credentials are validated on the Sign-in page, you need to call the endpoint (https://anypoint.mulesoft.com/accounts/api/organizations/{{orgID}}/environments)
to get the list of environments. You will get the response as shown below. You will get the organization ID in the URL of the Anypoint Platform Home Page.
Implement the above logic using Mule as shown below.
Step 5
Non-Prod Environments
When the user signs in with non-prod environment credentials, the non-prod environment names and IDs are filtered out of the payload, and the values are passed to the homepage.html
.
homepage.html
<html>
<body>
<h1>On Demand Schedulers</h1>
<form action="http://run-scheduler-app.us-e2.cloudhub.io/getApplications" method="POST">
<label for="environment">Select the Non Prod Environment:</label>
<br><br>
<select name="environment" id="environment">
#[%dw 2.0
output application/java
---
payload map ((item,index) ->
"<option value='" ++ (item.id ++ "_" ++ item.name default "") ++ "'>" ++ (item.name default "") ++ "</option>"
)
joinBy ""]
</select>
<br><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
Using the select
tag, you can display the environment names in the dropdown box in the webpage. The number of non-prod environments that were retrieved in Step 4 is passed as a payload to homepage.html, where you can use dataweave expressions to map through the different environments and display on the webpage. Set the html option
tag's value
attribute as environment ID ++ "_"
++ environment name
, the same value that the user will select from the dropdown. The Mule flow (run-scheduler-getApplications
) listening to the endpoint (/getApplications ) in the html form tag will be triggered once the user clicks on the Submit button.
Prod Environment
When the user signs in using Prod environment credentials, then directly send the production environment ID and call the flow listening to the endpoint /getApplications
.
Considering there is only one production environment, there is no need to call the homepage.html to display the list of production environments.
Step 6
Store the environment ID and name in variables which will come as a payload from the environment selected in the previous step.
Now, call the below endpoint to get the list of applications from the environment selected in Step 5. Pass the environment ID, retrieved in the previous step as a header "X-ANYPNT-ENV-ID
" (https://anypoint.mulesoft.com/cloudhub/api/v2/applications?retrieveStatistics=false&period=3600000&retrieveLogLevels=true&retrieveTrackingSettings=true&retrieveIpAddresses=true
).
Implement the above in Mule flow as shown below.
Set the payload with environment ID, name, and list of applications payload and pass it to the next webpage.
Step 7
Applications Not Found
Suppose an environment has no applications. Then the list of applications (payload retrieved in the previous step) would be empty. In this case, call the notfound.html
.
notfound.html
This HTML is a common HTML for both applications not found and schedulers not found. Using the DataWeave expression, it can be determined whether the notfound.html
is called for an application not found or schedulers not found. In both cases, the user needs to Exit and go back to the Sign-in page. Hence, the form
tag's action
attribute is calling the Mule flow listening to the endpoint /onDemandSchedulers
.
<html>
<body>
<h1>On Demand Schedulers</h1>
<br><br>
<form action="http://run-scheduler-app.us-e2.cloudhub.io/onDemandSchedulers" >
<label for="scheduler">#[%dw 2.0 output application/java --- if(isEmpty(payload.application)) ("No applications found in the environment: " ++ ((payload.environment default "") replace "\'" with "") ++ ". Please select another environment") else if(isEmpty(payload.scheduler)) ("No schedulers found for the application: " ++ ((payload.application default "") replace "\'" with "") ++ ". Please select another application") else ""]</label> <br>
<br><br>
<input type="submit" value="Exit">
</form>
</body>
</html>
Applications Found
In case of a non-empty payload from the Get Applications HTTP Request in the previous step, pass the environment ID, name, and list of applications (shown in Step 6) and pass it to the webpage applications.html
. This will display the environment selected and the list of applications in the dropdown box on the webpage.
applications.html
Here, the environment selected in the previous step will be displayed on the webpage using the HTML tag <input type="radio">
. Set the value
attribute as environment ID ++ "_" ++ environment name
, so that it can be stored in a variable in the Mule flow and used afterwards. Similarly, the list of applications retrieved from the previous step will be displayed in the dropdown box. Using DataWeave expressions, the payload from the previous step can be used in the HTML. Set the value
attribute as the application name so that it can be used afterwards in the Mule flow. Once, the user selects the application from the dropdown box and clicks on the submit button, the mule flow "run-scheduler-getSchedulers
" listening to the endpoint /getSchedulers
will be triggered.
<html>
<body>
<h1>On Demand Schedulers</h1>
<form action="http://run-scheduler-app.us-e2.cloudhub.io/getSchedulers" method="POST">
<br>
<label for="env">Environment selected:</label> <br>
#[%dw 2.0 output application/java ---
"<input type='radio' id='environment' name='environment' value='" ++ (payload.environmentId ++ "_" ++ payload.environment default "") ++ "' checked='checked'>"]
<label for="environment">#[%dw 2.0 output application/java ---(payload.environment default "") replace "\'" with ""]</label> <br>
<br><br>
<label for="application">Select the Application:</label> <br><br>
<select name="application" id="application">
#[%dw 2.0
output application/java
---
payload.application map ((item,index) ->
"<option value='" ++ (item.domain default "no applications found") ++ "'>" ++ (item.domain default "") ++ "</option>"
)
joinBy ""]
</select>
<br><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
Step 8
Store the environment ID, name, and application name, retrieved from the previous step in the variables, and call the sub-flow for fetching the list of schedulers.
Call the below endpoint of CloudHub API to get the list of schedulers for the application and environment selected and stored in variables (https://anypoint.mulesoft.com/cloudhub/api/applications/{{application_name}}/schedules
).
Implement this using Mule as shown below.
Set the payload with environment ID, name, application name, and list of schedulers payload and pass it to the next webpage.
Step 9
Schedulers Not Found
Suppose an application has no schedulers. Then the list of schedulers (payload retrieved in the previous step) would be empty. In this case, call the notfound.html
.
Refer to Step 7 for notfound.html
.
Scheduler Found
In case of non-empty payload from the Get Schedulers HTTP Request in the previous step, pass the environment id, name, application name, and list of schedulers payload (shown in Step 8) and pass it to the webpage schedulers.html
. This will display the environment and application selected and the list of schedulers in the dropdown box on the webpage.
scheduers.html
Here, the environment and application selected in the previous step will be displayed on the webpage. Set the value
attribute for the application as payload.application
so that it can be stored in a variable in the Mule flow and used afterward. Similarly, the list of schedulers retrieved from the previous step will be displayed in the dropdown box. Using DataWeave expressions, the payload from the previous step can be used in the HTML. Set the value
attribute for the scheduler as scheduler ID, so that it can be used later in the Mule flow. Once, the user selects the scheduler from the dropdown box and clicks on the submit button, the Mule flow "run-scheduler-runSchedulers"
listening to the endpoint /runSchedulers
will be triggered.
<html>
<body>
<h1>On Demand Schedulers</h1>
<form action="http://run-scheduler-app.us-e2.cloudhub.io/runSchedulers" method="POST" >
<br>
<label for="env">Environment selected:</label> <br>
#[%dw 2.0 output application/java ---
"<input type='radio' id='environment' name='environment' value='" ++ (payload.environmentId ++ "_" ++ payload.environment default "") ++ "' checked='checked'>"]
<label for="environment">#[%dw 2.0 output application/java ---(payload.environment default "") replace "\'" with ""]</label> <br>
<br><br>
<label for="app">Application selected:</label> <br>
#[%dw 2.0 output application/java ---
"<input type='radio' id='application' name='application' value='" ++ (payload.application default "") ++ "' checked='checked'>"]
<label for="application">#[%dw 2.0 output application/java ---(payload.application default "") replace "\'" with ""]</label> <br>
<br><br>
<label for="schedulers">Select the Scheduler:</label><br><br>
<select name="scheduler" id="scheduler">
#[%dw 2.0
output application/java
---
payload.scheduler map ((item,index) ->
"<option value='" ++ (item.id default "no schedulers found") ++ "'>" ++ (item.name default "") ++ "</option>"
)
joinBy ""]
</select>
<br><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
Step 10
Store the environment ID, name, application name, and scheduler ID, retrieved from the previous step in variables, and call the flow for running the scheduler.
Call the below endpoint of CloudHub API to run the scheduler for the application, environment, and scheduler selected and stored in variables. Pass the access token and environment ID as header and scheduler ID in the payload (https://anypoint.mulesoft.com/cloudhub/api/applications/{{application_name}}/schedules
).
Implement the same in the Mule as shown below.
Now, call the thankyou.html
flow as shown below.
thankyou.html
On clicking the Exit button, the user will go back to the sign-in page. As you can see below, the Mule flow listening to the endpoint /onDemandSchedulers
is called in the HTML form tag's action
attribute.
<html>
<body>
<h1>On Demand Schedulers</h1>
<h2>Scheduler is queued to run. Thank You !!</h2>
<br><br>
<form action="http://run-scheduler-app.us-e2.cloudhub.io/onDemandSchedulers" >
<br><br>
<input type="submit" value="Exit">
</form>
</body>
</html>
Source Code
interface.xml
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core" xmlns:http="http://www.mulesoft.org/schema/mule/http"
xmlns="http://www.mulesoft.org/schema/mule/core"
xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/ee/core http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd">
<flow name="run-scheduler-onDemandSchedulers" doc:id="da96cf57-5e69-4169-9235-83c52ff43cf4" >
<http:listener doc:name="Listener for onDemandSchedulers" doc:id="0bcb157a-306b-4693-83cc-b8611193a5cd" config-ref="HTTP_Listener_config" path="/onDemandSchedulers" allowedMethods="GET"/>
<logger level="INFO" doc:name="Logger" doc:id="8e7f1a2a-6f9b-4cb1-8be7-1a34d8219728" message="Loading HTML..."/>
<parse-template doc:name="Signin" doc:id="d34e573f-5787-47f1-a9d1-91db19538a3e" location="${mule.home}/apps/${app.name}/html/signin.html"/>
</flow>
<flow name="run-scheduler-environments" doc:id="2720db03-70b6-46d5-a2a8-e09499c7e474">
<http:listener doc:name="Listener for environments" doc:id="f5f50b50-9b95-4d7a-bd6d-d4d9122ecd9c" config-ref="HTTP_Listener_config" path="/environments" allowedMethods="POST" />
<choice doc:name="Choice" doc:id="56539418-0e5e-4af3-9461-b7908d90c1b2" >
<when expression="#[payload.username == p('non-prod.user') and payload.password == p('non-prod.password')]">
<logger level="INFO" doc:name="Logger" doc:id="68dcce6c-c420-45cf-a143-8096081d1eda" message="Username and password validation succesful for non prod"/>
<flow-ref doc:name="get-environments-flow" doc:id="e2305e2a-8cea-4ba4-a9fe-25450e899962" name="get-environments-flow" />
<ee:transform doc:name="filter non prod environments" doc:id="7081f5b4-19d5-42c1-945e-ff35cda10b85">
<ee:message>
<ee:set-payload><![CDATA[%dw 2.0
output application/json
---
payload filter($.isProduction == false)]]></ee:set-payload>
</ee:message>
</ee:transform>
<parse-template doc:name="Homepage" doc:id="256a4519-76f5-44b5-9719-f4e4fb3e1d4d" location="${mule.home}/apps/${app.name}/html/homepage.html" />
</when>
<when expression="#[payload.username == p('prod.user') and payload.password == p('prod.password')]">
<logger level="INFO" doc:name="Logger" doc:id="767498f3-1f4e-485e-9efe-2944601dadc3" message="Username Password validation successful for Prod"/>
<flow-ref doc:name="get-environments-flow" doc:id="5e56eb0a-769a-4897-b9b1-39615d5651cd" name="get-environments-flow"/>
<ee:transform doc:name="filter prod environments" doc:id="f9ddac2f-5daa-45a1-aa67-7fff3302130a">
<ee:message>
<ee:set-payload><![CDATA[%dw 2.0
output application/json
var environment= if(!isEmpty(payload filter($.isProduction == true))) (payload filter($.isProduction == true))[0] else ""
---
{
"environment": if(!isEmpty(environment)) (environment.id ++ "_" ++ environment.name) else "_PROD"
}]]></ee:set-payload>
</ee:message>
</ee:transform>
<http:request method="POST" doc:name="Call Get Applications" doc:id="39c3356b-3f5c-44d1-a2cf-bda32c038bb5" url='#[//"http://localhost:8081/getApplications"
"http://run-scheduler-app.us-e2.cloudhub.io/getApplications"]' responseTimeout="60000"/>
</when>
<otherwise >
<logger level="INFO" doc:name="Logger" doc:id="a7d3dda8-8eca-432d-b0c5-090a4f876da3" message="Invalid Username and Password"/>
<parse-template doc:name="Invalid Credentials" doc:id="cf39c3a9-5638-434f-8d87-1f5785dc9381" location="${mule.home}/apps/${app.name}/html/invalidCredentials.html"/>
</otherwise>
</choice>
</flow>
<flow name="run-scheduler-getApplications" doc:id="b5cc3c19-8cc3-4500-a832-d05eab14b17f" >
<http:listener doc:name="Listener for getApplications" doc:id="95fa060f-3ed6-4be6-ab1a-81a7e9da442f" config-ref="HTTP_Listener_config" path="/getApplications" allowedMethods="POST"/>
<ee:transform doc:name="environment" doc:id="555e6cba-1b83-4b42-9188-e778b42f5c6e" >
<ee:message >
</ee:message>
<ee:variables >
<ee:set-variable variableName="environmentId" ><![CDATA[%dw 2.0
output application/java
---
(payload.environment splitBy "_")[0] default ""]]></ee:set-variable>
<ee:set-variable variableName="environment" ><![CDATA[%dw 2.0
output application/java
---
(payload.environment splitBy "_")[1] default ""]]></ee:set-variable>
</ee:variables>
</ee:transform>
<flow-ref doc:name="on-demand-scheduler-get-applications-flow" doc:id="bbb1bd01-927f-4486-87a3-a0fc9e1f9833" name="get-applications-flow" />
<choice doc:name="Choice" doc:id="bfa12b0f-8be6-4f2e-8347-6aa72b09d3a7" >
<when expression="#[not isEmpty(payload.application)]">
<parse-template doc:name="Applications" doc:id="2a55c836-f8c3-4996-ad21-73cd1605d4c8" location='${mule.home}/apps/${app.name}/html/applications.html' />
</when>
<otherwise >
<parse-template doc:name="Application not found" doc:id="44c64643-ffb9-421f-bae0-1fe5357a72f5" location="${mule.home}/apps/${app.name}/html/notfound.html" />
</otherwise>
</choice>
</flow>
<flow name="run-scheduler-getSchedulers" doc:id="a100ab9f-cb87-484b-9820-745ba00e18a8" >
<http:listener doc:name="Listener for getSchedulers" doc:id="5039f345-03fb-4525-bb5b-4d44ac482e5f" config-ref="HTTP_Listener_config" path="/getSchedulers" allowedMethods="POST"/>
<ee:transform doc:name="application and environment" doc:id="236f1b71-67ca-44b0-ba7f-15ae0c36c654" >
<ee:message >
</ee:message>
<ee:variables >
<ee:set-variable variableName="application" ><![CDATA[%dw 2.0
output application/java
---
payload.application]]></ee:set-variable>
<ee:set-variable variableName="environment" ><![CDATA[%dw 2.0
output application/java
---
(payload.environment splitBy "_")[1]]]></ee:set-variable>
<ee:set-variable variableName="environmentId" ><![CDATA[%dw 2.0
output application/java
---
(payload.environment splitBy "_")[0]]]></ee:set-variable>
</ee:variables>
</ee:transform>
<flow-ref doc:name="on-demand-scheduler-get-schedulers-flow" doc:id="3a83b9bc-aef8-4608-bd60-d2260615874a" name="get-schedulers-flow"/>
<choice doc:name="Choice" doc:id="158d0033-b457-4cb8-8b08-d530d0ff66f4" >
<when expression="#[not isEmpty(payload.scheduler)]">
<parse-template doc:name="Schedulers" doc:id="e4851faf-9eed-4f1e-a9a5-62d3896c9715" location="${mule.home}/apps/${app.name}/html/schedulers.html" />
</when>
<otherwise >
<parse-template doc:name="Scheduler not found" doc:id="a5017eba-bd7a-4f91-a3c1-9cb2d146835d" location="${mule.home}/apps/${app.name}/html/notfound.html"/>
</otherwise>
</choice>
</flow>
<flow name="run-scheduler-runSchedulers" doc:id="7bb4ddd7-10fb-46b8-8290-62d53920cb2c" >
<http:listener doc:name="Listener for runSchedulers" doc:id="b3c92c64-944a-4cd2-8ed3-dc17acca7dc3" config-ref="HTTP_Listener_config" path="/runSchedulers" allowedMethods="POST"/>
<ee:transform doc:name="envrionment, scheduler and application" doc:id="03875982-8431-420a-9320-7c1a1165bf50" >
<ee:message />
<ee:variables >
<ee:set-variable variableName="scheduler" ><![CDATA[%dw 2.0
output application/java
---
payload.scheduler]]></ee:set-variable>
<ee:set-variable variableName="application" ><![CDATA[%dw 2.0
output application/java
---
payload.application]]></ee:set-variable>
<ee:set-variable variableName="environment" ><![CDATA[%dw 2.0
output application/java
---
(payload.environment splitBy "_")[1]]]></ee:set-variable>
<ee:set-variable variableName="environmentId" ><![CDATA[%dw 2.0
output application/java
---
(payload.environment splitBy "_")[0]]]></ee:set-variable>
</ee:variables>
</ee:transform>
<flow-ref doc:name="on-demand-scheduler-run-schedulers-flow" doc:id="2c029af8-2bab-493f-b9a5-0fd9189cb6b9" name="run-schedulers-flow" />
<parse-template doc:name="Thankyou" doc:id="c5df384e-a503-4df6-856b-a54bf0d2208e" location="${mule.home}/apps/${app.name}/html/thankyou.html" />
</flow>
</mule>
implementation.xml
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core"
xmlns:os="http://www.mulesoft.org/schema/mule/os"
xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/os http://www.mulesoft.org/schema/mule/os/current/mule-os.xsd
http://www.mulesoft.org/schema/mule/ee/core http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd">
<sub-flow name="get-environments-flow" doc:id="43a66535-3033-4e89-92c8-016f906ba8d8" >
<flow-ref doc:name="retrieve-auth-token" doc:id="d368153d-3e67-4cda-aeb6-b22d2e5149fe" name="retrieve-auth-token" />
<logger level="INFO" doc:name="Logger" doc:id="770201b8-f77a-4a21-9f70-75998c697b48" message='#["Get all non-prod environments"]' />
<http:request method="GET" doc:name="Get Environments" doc:id="918c9406-8e7f-422d-b067-e06f7d6334c4" config-ref="HTTP_Request_configuration" path="accounts/api/organizations/{orgId}/environments" >
<http:headers ><![CDATA[#[output application/java
---
{
"Authorization": "Bearer " ++ (vars.oauthToken default "")
}]]]></http:headers>
<http:uri-params ><![CDATA[#[output application/java
---
{
"orgId" : p('orgId')
}]]]></http:uri-params>
</http:request>
<ee:transform doc:name="Environments" doc:id="9c0ad057-1f55-458a-a290-3165f09e68e3" >
<ee:message >
<ee:set-payload ><![CDATA[%dw 2.0
output application/json
---
payload.data]]></ee:set-payload>
</ee:message>
</ee:transform>
<logger level="INFO" doc:name="Logger" doc:id="a38ca30d-2762-4ffa-85f4-532fad63d490" message='#["Environments retrieved successfully"]' />
</sub-flow>
<sub-flow name="get-applications-flow" doc:id="61c48468-09b5-4fc4-880b-094852cf2f49" >
<flow-ref doc:name="retrieve-auth-token" doc:id="99220047-f110-4b61-bd99-b20b12548fd7" name="retrieve-auth-token"/>
<logger level="INFO" doc:name="Logger" doc:id="e901b2bc-b636-4506-a69f-b7641ba60010" message='#["Retrieving applications for environment " ++ vars.environment]'/>
<http:request method="GET" doc:name="Get Applications" doc:id="afcf6757-1dff-40a5-b562-a74b97166e02" config-ref="HTTP_Request_configuration" path="/cloudhub/api/v2/applications" >
<http:headers ><![CDATA[#[output application/java
---
{
"X-ANYPNT-ENV-ID" : vars.environmentId,
"Authorization": "Bearer " ++ (vars.oauthToken default "")
}]]]></http:headers>
<http:query-params ><![CDATA[#[output application/java
---
{
"retrieveStatistics" : false,
"period": "3600000",
"retrieveLogLevels": true,
"retrieveTrackingSettings": true,
"retrieveIpAddresses": true
}]]]></http:query-params>
</http:request>
<ee:transform doc:name="Applications" doc:id="48e3de3c-8e2e-4068-bbc0-9d803ed43caa" >
<ee:message >
<ee:set-payload ><![CDATA[%dw 2.0
output application/json
---
{
environment: vars.environment,
environmentId: vars.environmentId,
application: payload
}]]></ee:set-payload>
</ee:message>
</ee:transform>
<logger level="INFO" doc:name="Logger" doc:id="fb3f9af5-4464-497f-baae-ed115869f86c" message='#["Applications retrieved successfully"]' />
</sub-flow>
<sub-flow name="get-schedulers-flow" doc:id="b80bacc8-6d67-4fbf-89a7-0cd968f2c342" >
<logger level="INFO" doc:name="Logger" doc:id="e8584814-3a23-4f5e-a828-4a3cd68bd893" message="Retrieving scheduler details for application scheduler-demo-poc" />
<flow-ref doc:name="retrieve-auth-token" doc:id="833ee32d-1665-4032-8fe1-f74ae490e5cd" name="retrieve-auth-token" />
<http:request method="GET" doc:name="get schedulers" doc:id="1e10e000-6ea4-4205-af5a-6f29ce217f60" config-ref="HTTP_Request_configuration" path='#["/cloudhub/api/applications/" ++ (vars.application default "") ++ "/schedules"]'>
<http:headers><![CDATA[#[output application/java
---
{
"X-ANYPNT-ENV-ID" : vars.environmentId,
"Authorization": "Bearer " ++ (vars.oauthToken default "")
}]]]></http:headers>
</http:request>
<ee:transform doc:name="schedulers" doc:id="0e578a6e-d2ff-45bb-9440-4d3bb0967bce" >
<ee:message >
<ee:set-payload ><![CDATA[%dw 2.0
output application/json
---
{
environment: vars.environment,
environmentId: vars.environmentId,
application: vars.application,
scheduler: payload
}]]></ee:set-payload>
</ee:message>
</ee:transform>
<logger level="INFO" doc:name="scheduler details retrieved" doc:id="2fda395e-73b0-4695-b9b8-f2627414f4c5" message="scheduler details retrieved successfully" />
</sub-flow>
<sub-flow name="run-schedulers-flow" doc:id="69eecbab-2180-4353-81d5-5d53788c4300" >
<logger level="INFO" doc:name="Running scheduler" doc:id="73c9a387-c579-4751-9978-e2c71588a05a" message='#["Running scheduler " ++ (vars.scheduler splitBy "_")[1] default ""]' />
<flow-ref doc:name="retrieve-auth-token" doc:id="7b7b9f32-7e27-463a-900d-756fa0fe0605" name="retrieve-auth-token" />
<ee:transform doc:name="Prepare request" doc:id="595550fb-e070-43cd-9705-4033178ca5b4">
<ee:message>
<ee:set-payload><![CDATA[%dw 2.0
output application/json
---
[
{
"id" : vars.scheduler default "",
"runNow" : true
}
]]]></ee:set-payload>
</ee:message>
</ee:transform>
<http:request method="PUT" doc:name="Run Scheduler" doc:id="07515567-5745-4a00-befa-b9f898aef5d7" config-ref="HTTP_Request_configuration" path='#["/cloudhub/api/applications/" ++ (vars.application default "") ++ "/schedules"]'>
<http:headers><![CDATA[#[output application/java
---
{
"X-ANYPNT-ENV-ID" : vars.environmentId,
"Authorization": "Bearer " ++ (vars.oauthToken default "")
}]]]></http:headers>
</http:request>
<logger level="INFO" doc:name="Scheduler ran successfully" doc:id="76c4bbb4-a16c-453f-8b77-a3a71cb9919a" message='#["Scheduler " ++ (vars.scheduler splitBy "_")[1] default "" ++ "ran successfully"]' />
</sub-flow>
<sub-flow name="retrieve-auth-token" doc:id="7e68b07d-ac01-4bf2-8668-2b9166e11aac" >
<logger level="INFO" doc:name="Retrieving oauth token" doc:id="6d483293-4177-4e53-9b3d-6d7e66965792" message="Retrieving oauth token" />
<os:contains doc:name="tokenExists?" doc:id="08319212-dac6-4577-8cbb-524a41ebe803" key="state" objectStore="Object_store" target="tokenExist" />
<choice doc:name="Choice" doc:id="38c7afe6-a6a3-44b7-973f-01b284b41114">
<when expression="#[vars.tokenExist]">
<os:retrieve doc:name="Retrieve token" doc:id="04006a2d-c873-435a-8bff-3d555a4ae3ae" key="state" objectStore="Object_store" target="oauthToken">
<os:default-value><![CDATA[""]]></os:default-value>
</os:retrieve>
</when>
<otherwise>
<flow-ref doc:name="get-oauth-token" doc:id="0b98c709-dda5-462c-853b-b17ab9fbb1f8" name="get-oauth-token" />
<set-variable value='#[vars.oauthResponse.access_token default "0"]' doc:name="oauthToken" doc:id="6ccad6af-9a85-45d3-946c-3f4ce346aaec" variableName="oauthToken" />
</otherwise>
</choice>
<logger level="INFO" doc:name="oauth token retrieved" doc:id="9927f11e-ac37-4124-b56b-47d8adaf5a44" message="oauth token retrieved." />
</sub-flow>
<sub-flow name="get-oauth-token" doc:id="18d4390d-09d0-4832-bd29-409c9c7c3eae" >
<ee:transform doc:name="requestPayload" doc:id="ba0f83ae-1709-4f6e-a638-dc1c3d8d3c42" >
<ee:message >
</ee:message>
<ee:variables >
<ee:set-variable variableName="requestPayload" ><![CDATA[%dw 2.0
output application/json
---
{
"client_id" : p('client_id'),
"client_secret": p('client_secret'),
"grant_type" : "client_credentials"
}]]></ee:set-variable>
</ee:variables>
</ee:transform>
<http:request method="POST" doc:name="Request token" doc:id="5842a3e3-c4a5-4edf-9cde-1aecbc9771b6" target="oauthResponse" config-ref="HTTP_Request_configuration" path="/accounts/api/v2/oauth2/token">
<http:body ><![CDATA[#[vars.requestPayload]]]></http:body>
<http:headers ><![CDATA[#[output application/java
---
{
"Content-type" : "application/json"
}]]]></http:headers>
</http:request>
<os:store doc:name="Store token" doc:id="9974e56d-49bc-4035-b8f5-35e01ddfda5d" key="state" objectStore="Object_store">
<os:value ><![CDATA[#[vars.oauthResponse.access_token default "0"]]]></os:value>
</os:store>
</sub-flow>
</mule>
Conclusion
Here, I wanted to show you how to use CloudHub APIs and Mule expressions in HTML.
Thanks for reading the article and if you have any questions please feel free to write it down in the comments section.
Opinions expressed by DZone contributors are their own.
Comments