Tech Me More

To quench our thirst of sharing knowledge about our day to day experience & solution to techincal problems we face in our projects.

Advertise with us !
Send us an email at diehardtechy@gmail.com

Wednesday, May 27, 2020

Create MySQL server in microsoft azure using java SDK


Microsoft Azure is a leading cloud computing service provider. We can deploy, test, manage our applications in the azure. Azure offers a lot of managed services. MySql server is also one of the services provided by Azure. 

as a software developer we need to programmatically manage the Azure instance. Starting from creating a service, deploying new virtual machines, load balancers, etc. 

Azure provides a Java SDK to manage Azure instances programmatically. 

You can find more details about AZURE-JAVA-SDK  here.

Azure Java SDK provides many utilities to manage resources in Azure. however, as of today (27/5/20) there is no client provided in azure SDK for creating MySQL server. 

So, how to create a MySQL server instance in azure? 

Azure provides a separate maven dependency which is still WIP under the below maven repository. 

Please add the above dependency in POM.XML of your project. 

Steps to create MySQL server using the above maven dependency. 
Step 1: Add the below maven dependency in POM.xml 

Step 2: Entry class for MySQL server is MySQLManager. 
Below is the code example for creating a MySQL server using the above SDK.

public Server createMySQLServer(AzureTokenCredentials credential,
                                    AzureMySqlModel model) {

        if (credential == null || model == null)
            return null;

        Server server = null;
        if (model.validate()) {
            try {
                ServerPropertiesForDefaultCreate defaultProp = new ServerPropertiesForDefaultCreate();

                ServerPropertiesForCreate withVersion = defaultProp.withAdministratorLogin(
                            model.getAdministratorLogin()).withAdministratorLoginPassword(
                                        model.getAdministratorPassword()).withVersion(
                                                    model.getServerVersion());

                server = MySQLManager.configure().withLogLevel(
                            LogLevel.BODY).authenticate(credential,
                                        credential.defaultSubscriptionId()).servers().define(
                                                    model.getServerName()).withRegion(
                                                                model.getRegion()).withExistingResourceGroup(
                                                                            model.getResourceGroup()).withProperties(
                                                                                        withVersion).create();
            } catch (Exception ex) {
                log.error("Error creating MySQL server {}", ex.getMessage());
            }
        }

        return server;
    }


public AzureTokenCredentials getAzureTokenCredentials(String azureClientId,
                                                          String azureTenantId,
                                                          String azureSecret,
                                                          String azureSubscriptionId) {

        AzureTokenCredentials credentials = new ApplicationTokenCredentials(
                    azureClientId, azureTenantId, azureSecret,
                    AzureEnvironment.AZURE).withDefaultSubscriptionId(
                                azureSubscriptionId);

        return credentials;

}

AzureMySQLModel is a simple java POJO as defined below. 

import com.microsoft.azure.management.mysql.v2017_12_01.ServerVersion;

import lombok.AllArgsConstructor;
import lombok.Data;

@AllArgsConstructor
@Data
public class AzureMySqlModel {

    private String serverName;
    private String administratorLogin;
    private String administratorPassword;
    private String region;
    private String resourceGroup;
    private ServerVersion serverVersion;

    public boolean validate() {
        return (serverName == null | administratorLogin == null
                    | administratorPassword == null | serverVersion == null
                    | region == null | resourceGroup == null) ? false : true;

    }

}

You can get the AzureTokenCredentials via passing the azure client id, subscription id, tenant id, and azure secret in getAzureTokenCredentials() method. 

As of today the ServerVersion class only provides MySQL version 5.6 & 5.7. However we can pass the Azure supported MySQL server version by providing the custom implementation for the fromString method of ServerVersion class. 


No comments: