Posts

Showing posts from 2023

GitLab CI/CD Pipeline for CloudHub 2.0 Deployment (with Auto Incrementing Version)

Image
This document covers GitLab CI/CD Pipeline Script for deploying Mule App to CloudHub 2.0. If you are experienced in setting up CI/CD Pipeline for CloudHub 1.0 and have tried to re-use the same script for CloudHub 2.0, then you must have found that it doesn't work. The fact is that Mulesoft's CloudHub 2.0 deployments are tightly coupled with Anypoint Exchange, viz., every Mule App that is deployed in Runtime Manager must be linked with a corresponding instance in Anypoint Exchange. One common hurdle that Mule Developers face when setting up CloudHub 2.0 specific pipelines is how Mule App's version can be automatically incremented whenever a pipeline is started. This is again due to CloudHub 2.0 being tightly coupled with Anypoint Exchange. This document also covers logic to handle that in POM file. Let’s begin... Pre-requisites Mule App GitLab Account with available Runners & configured Deployment/Environment Variables Connected App in Anypoint Platform with Correct Perm...

Role-based AWS Setup for Mulesoft S3 Connector's On New Object Component (Including Local Run Setup)

Image
This document covers setting up AWS Services when using Mulesoft S3 Connector's On New Object component. We'll cover the minimum permissions required (Least-Privilege Access) by this component. The S3 Connector here is configured to use AWS IAM Role-based authentication which is only applicable for CloudHub 2.0 deployments targeted inside a Private Space. The role-based authentication is only supported by latest versions of this connector viz. 6.2.0+. The On New Object component is an event source which triggers Mule flow whenever a new file is uploaded in a S3 Bucket. It sends metadata of the uploaded file as payload. For this component to work, the S3 Bucket must have a notification configuration attached to it with destination set to an SQS Queue. In the end, we'll look over how to deploy and run such Mulesoft Application locally. Let’s begin... Pre-requisites Anypoint Studio (S3 Connector V6.2.0+) Mulesoft Anypoint Platform Account Private Space in Anypoint Platform AWS...

Dataweave Code to Validate All Key Values of an Object in JSON Array

Image
This document covers Dataweave code to validate all JSON key values in a JSON array. For this demonstration, we are using Mulesoft's Online Dataweave Playground - Link as an IDE. Let’s begin... Solution Preview Code Snippet %dw 2.0 output application/json import * from dw::Runtime import * from dw::core::Strings fun valRequiredAlphanum(value, key, len = 30) =  if (isEmpty(value)) fail(key ++ " is a required field; ")  else if (!(value matches /[A-Za-z0-9]+/)) fail(key ++ " can only contain alphanumeric values; ")  else "" fun valNum(value, key, len = 10) =  if (!(value matches /[0-9]+/)) fail(key ++ " can only contain number; ")  else if (!(sizeOf(value) <= len)) fail(key ++ " must be less than " ++ (len as String) ++ " digits; ")  else "" fun valList(value, key, list) =  if (isEmpty(list find value)) fail(key ++ " field has invalid selection; ")  else "" var validatedRecords = payload map...

Dataweave Code to Identify Duplicate Key Values in JSON Array

Image
This document covers Dataweave code to identify and flag duplicate field or key values in a JSON array. This code will treat the first copy as original and mark the remaining copies as duplicate. For this demonstration, we are using Mulesoft's Online Dataweave Playground - Link as an IDE. Let’s begin... Solution Preview Code Snippet %dw 2.0 output application/json fun getDistinctRecords(records, field) = flatten((records groupBy ((item, index) -> item[field])) pluck ((value, key, index) -> value) map ((item, index) -> item distinctBy ((item, index) -> item[field]))) var distincValues = getDistinctRecords(payload, "Username") var duplicates = payload -- distincValues --- (distincValues) ++ (duplicates map ((item, index) -> item ++ {"Duplicate Flag": "Yes"})) Brief Explanation The main logic for this problem revolves around the distinctBy function. But you are here because you al so found out about this function's limitation when dea...

Securing API with Salesforce as OAuth Provider on Anypoint Platform

Image
This document covers steps for securing an API with Salesforce as OAuth 2.0 Provider on Anypoint Platform. We'll create a Connected App on Salesforce account for this demo. Then on the Anypoint Platform we will setup Salesforce as a Client Provider of type - OpenID Connect Dynamic Client Registration. Next, we'll configure default Client provider in the environment where our API lives. After that, we will deploy our API to Cloudhub and then secure it using OpenId Connect access token enforcement Policy. Let’s begin... Pre-requisites Salesforce Account Mulesoft Anypoint Platform Account API ready for Cloudhub Deployment For this demo, we're using the following Non-MuleAPI - Coffee SampleAPI RAML File -  Link . You can import it in Design Center and then publish it to your Anypoint Exchange. We'll later deploy it as a Proxy Application as mentioned in the steps below. Do not deploy the API until Client Provider is registered on Anypoint Platform (Just follow the events as...