Beginning with the 0.5.0 release of the Microsoft Azure SDK for Java, Microsoft has added support for service management in the Java SDK. Service management is an area already rich in the Azure SDK for .NET and Azure SDK for Node.js but it was a new area for our Java SDK we were excited to release. Like the .NET and Node.js SDKs and the Storage team’s Java SDK, the Java SDK for service management is also open-source on GitHub. In this post, I’ll introduce you to the management libraries for Java and walk you through the process of getting an Eclipse project up and running that you can extend and use to create and manage your own Azure subscription and resources within it.
What are the Management Libraries for Java?
Simply put, these libraries provide your Java applications the ability to automate the setup, teardown, provisioning, and routine management tasks for Azure resources. Our team’s mantra is pretty simple; we want to enable a developer the ability to execute operations on their Azure subscriptions in their code that they’d otherwise have to do using the management portal. The libraries enable automation of Azure resources (and in fact, our libraries enable Microsoft's own PowerShell and XPlat CLI experiences today). You can use any of the libraries to do things like:
- Create, delete, and update settings for resources like web sites, SQL databases, cloud services, scheduler job collections, virtual machines, and storage
- Start and stop web sites
- Back up databases to storage accounts
- Automate the creation of virtual machines
The management libraries for Java are available on Maven, so they’re easily available in most modern Java development tools. This post will focus on developing using Eclipse.
Creating an Eclipse Maven Project
This area of the post will discuss this process assuming the audience has little or no experience using Eclipse to develop using Maven and the Azure libraries and will summarize at a very basic level how to get going from ground zero, so if you’re truly experienced in the arts of Java/Maven/Eclipse, some of this may seem like child’s play to you. I’m relatively new to Java, having been a long-time .NET developer, but my interest in extending the Azure SDK has given me the opportunity to get reacquainted with the language. To put it bluntly, I’m a great candidate for a getting started post in this area (the team’s sure to have a joke here).
I’ll create a new workspace to get started collecting these tutorials, so I’ll make a new folder here:
Once your workspace has been created and have got the Eclipse IDE set up the way you want, create a new Maven project.
Since we are actually creating an Eclipse Maven project here (you can create any type of project you want so long as you can pull in the Azure Maven packages) we need to provide some information about the app so that when its published to a repository the consumer will see what the package does. Provide some basic information here to flag this to consumers that this is demo code.
Now that you’ve got an Eclipse project set up with support for Maven packages, find the Azure packages and install them.
Installing the Management Libraries using Maven
Click the pom.xml file to see the list of packages the project has installed in it currently. There shouldn’t be any, as this is a new project. We need to add a Maven package to my project, so click the Add button here.
Now, searching for the Azure Maven packages results with success you can see the packages that we are after, so go ahead and select the base management package for now.
Now the base Azure management package is selected as a dependency.
Writing Java Code to Access the Azure APIs
Now that you've referenced the Azure SDK lets write some code. To start, add a new Java class file to your project.
Authenticating to the Azure API Using Certificates
The Java SDK makes it pretty simple to call out to the API. There are a few pieces of information you need to provide the API for it to work against your subscriptions. One of the attributes needed to provide the API is a management certificate. The CER file should already uploaded into your Azure subscription and retrievable from the Portal.
Calling the Azure API to Get a List of Regions
Next lets add some code to set the value of the subscriptionId field in our code and author some additional Java code that will actually make the API call to Azure to get the list of geographic regions. I’ll simply output the names of the regions to the console in the following code. This will demonstrate that I’ve successfully authenticated to the Azure Management API and done some work.
public class Program { static String uri = "https://management.core.windows.net/"; static String subscriptionId = "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX"; static String keyStoreLocation = "c:\\certificates\\AzureJavaDemo.jks"; static String keyStorePassword = "my-cert-password"; public static void main(String[] args) throws IOException, URISyntaxException, ServiceException, ParserConfigurationException, SAXException { Configuration config = ManagementConfiguration.configure( new URI(uri), subscriptionId, keyStoreLocation, // the file path to the JKS keyStorePassword, // the password for the JKS KeyStoreType.jks // flags that I'm using a JKS keystore ); // create a management client to call the API ManagementClient client = ManagementService.create(config); // get the list of regions LocationsListResponse response = client.getLocationsOperations().list(); ArrayList locations = response.getLocations(); // write them out for( int i=0; i<locations.size(); i++){ System.out.println(locations.get(i).getDisplayName()); } } }
When I debug the code in Eclipse I can see the expected output, confirming that everything’s in working order and functioning as desired. This proves that I was able to get my management certificate from my subscription successfully converted to JKS format, loaded it into my application’s runtime, and used the management libraries to call out to the API.
Summary and Next Steps
Hopefully this post demonstrates the ease of getting the Java SDK via Maven and of using it to authenticate up to Azure’s API and do some simple work. The SDK can do a lot more than just list regions – create cloud services you could then use the Eclipse Java Toolkit to deploy code to, virtual machines to use as developer workstations or self-maintained servers, Websites that can run your Java code, or storage accounts to house all your stuff. I’ll be investigating some of the options available in the Java SDK on the Azure blog in coming weeks so stay tuned – the 0.6.0. SDK release has a huge swath of new functionality useful for Azure service automation and provisioning.
Along with the Storage SDK for Java and a series of other SDKs available on the Azure home page, it’s easy to see that the opportunities for Java development in Azure are continuing to evolve.