Thursday, July 29, 2021

Azure Lighthouse—Using Java programming to Deploy Azure Lighthouse templates

Last time we talked about how to onboard Azure lighthouse from Azure portal in this Blog Azure Lighthouse - Step by step guidance - Onboard customer to Lighthouse using sample template, now discuss about how to use Azure Java SDK to deploy Lighthouse templates.

Pre-requirements:

You may follow my previous blog mentioned in above and please read this document about what is Azure lighthouse. Here we will not repeat the definitions and how to gather values.

Please also read this document Azure authentication with Java and Azure Identity and Authentication in Azure Management Libraries for Java(github.com), they explain the methods to login Azure through Java program. We will not explain about the authentication in details.

I used ClientSecretCredential in below simple code:

final AzureProfile profile = new AzureProfile(tenantId,

subscriptionId, AzureEnvironment.AZURE);

 

ClientSecretCredential clientSecretCredential = new ClientSecretCredentialBuilder()

.clientId(clientId).clientSecret(clientSecret)

.tenantId(tenantId).build();

 

// Azure SDK client builders accept the credential as a parameter

AzureResourceManager azureResourceManager = AzureResourceManager.configure()

.withLogLevel(HttpLogDetailLevel.BASIC).authenticate(clientSecretCredential, profile)

.withSubscription(subId);

 

Now, let’s start for the functions of Azure Lighthouse in Java programing.

Deploy Azure Lighthouse templates

From last blog you may know, we use ARM templates to onboard Azure lighthouse, there are types of several templates depends on the scope. Here we are talking about “Azure Lighthouse-Subscription deployment”, “Azure Lighthouse-Resource Group Deployment” and “Azure Lighthouse-Multiple Resource Group Deployment” referred from Azure-Lighthouse-samples templates.

Scarlett_liu_0-1627549489609.png

Deploy to resource group:

  • If you want to deploy resources to a resource group or using “Azure Lighthouse-Resource Group Deployment” to onboard Azure Lighthouse in resource group. we have a sample code from DeployUsingARMTemplate.java (github.com). Here I extract the key code in below.

//Deploy resource in resource group “rgName”

azureResourceManager.deployments().define(deploymentName)

.withExistingResourceGroup(rgName)

.withTemplate(templateJson)

.withParameters(prameterJson)

.withMode(DeploymentMode.INCREMENTAL)

.create();

 

Deploy to subscription

  • And if you want to deploy on Azure Lighthouse to Subscription or Multiple resource groups. You can use this code in below. But deploying other resources require a resource group, this code may not be suitable for deployment of other resources.

//A deployment name which shows in deployments.

final String deploymentName = "sample-resourcegroup5";

 

//Get the template in local and convert to JsonNode

JsonNode jsonNode = DeployUsingARMTemplate.getTemplateJson(azureResourceManager, "/multipleRgDelegatedResourceManagement.json");

 

//Covert parameter template to JsonNode.

JsonNode jsonNodeParameter = DeployUsingARMTemplate.getTemplateJson(azureResourceManager,

"/multipleRgDelegatedResourceManagement.parameters2.json");

 

//Deploy template to Subscription, it needs to use Incremental mode to deploy resources.

azureResourceManager.deployments().manager().serviceClient().getDeployments()

.createOrUpdateAtScope("/subscriptions/"+subId, deploymentName,

new DeploymentInner().withLocation("eastus").withProperties(new DeploymentProperties().withTemplate(jsonNode)

.withParameters(jsonNodeParameter)

.withMode(DeploymentMode.INCREMENTAL)));

Explanation to above code:

  1. Deployment name is a name when you deploy to Azure. You can follow the default deployment name in portal with resource type and datetime. Or a unique name for test. Please check the reference from here Deploy resources with Azure portal

Scarlett_liu_1-1627549489614.png

 

  1. My templates are in my Java project, so they need to covert Json file to JsonNode before adding in azureResourceManager. deployment.

Scarlett_liu_2-1627549489619.png

  1. azureResourceManager is from previous step that authenticated with clientSecretCredential.
  2. DeploymentProperties  from this Java SDK DeploymentProperties Class Java SDK. You can find all the SDK classes and methods there.
  3. If deploying templates from a link, it implement by. withTemplateLink(templateUri, templateContentVersion)

For example:

azureResourceManager.deployments(rgName)

.define(deploymentName)

.withNewResourceGroup()

.withTemplateLink(templateUri, templateContentVersion)

.withParameters(params).withMode(DeploymentMode.INCREMENTAL).create()

 

  1. DeploymentMode.INCREMENTAL. There are two types of DeploymentMode when deploy templates.
    1. Incremental mode, keep the exist resources and deploy resources in the template.
    2. Complete mode, deletes the resources that exist in the resource group but aren't specified in the template. You can check from here Deployment modes - Azure Resource Manager | Microsoft Docs

 

In short, Azure Lighthouse is onboard based on the ARM template, so we can use deployment scripts or Java SDK to deploy it. But please confirm the scope and find the correct method in the SDK.

Tell me which language SDK you are interested in. we may discuss it next time. :smiling_face_with_smiling_eyes:

Posted at https://sl.advdat.com/3rEiOr0