2
votes

I am trying to use the Azure Java SDK to automate tasks for my azure virtual machines such as starting and stopping them at various stages of the day

I was looking at the azure documentation for start virtual machine here

The method signature in question is as follows

OperationResponse beginStarting(java.lang.String serviceName,
                            java.lang.String deploymentName,
                            java.lang.String virtualMachineName)
                     throws java.io.IOException,
                            ServiceException

I was wondering - where can I get the values for serviceName and deploymentName on the azure console for my virtual machine?

I tried looking on the old portal and the new portal but to date I have been unable to find these values

This question looks like a duplicate to the question at the below url but it is not Azure find deployment name

Indeed when you look at the old portal (https://manage.windowsazure.com) - the above link gives the correct answer for getting a deployment name

However, if you create a VirtualMachine in the old portal and view it in the new portal (https://portal.azure.com) - it shows under the Virtual Machine Classic option. With the help of the above link I was able to perform operations on the classic virtual machines using the Java SDK

If I create a Virtual Machine in the new portal under the Virtual Machine option (not Virtual Machine (classic)) I am unable to find the deploymentName or serviceName

So to update the question - how does one find the deploymentName and serviceName for a Virtual machine in the new portal

Also - one other thing I noticed with the SDK - If you have the wrong serviceName entered - it logs that the deployment name is wrong - that got me for a while on the classic virtual machines

Just to close out this question To be able to perform operations on non classic virtual machines - use this maven dependency

<dependency>
    <groupId>com.microsoft.azure</groupId>
    <artifactId>azure-mgmt-compute</artifactId>
    <version>0.9.0</version>
</dependency>

For operations on non classic virtual machines - you need to use Active Directory Security - see this link - https://azure.microsoft.com/en-us/documentation/articles/resource-group-create-service-principal-portal/ To perform operations on classic virtual machines - use this maven dependency

<dependency>
    <groupId>com.microsoft.azure</groupId>
    <artifactId>azure-svc-mgmt-compute</artifactId>
    <version>0.9.0</version>
</dependency>

For operations on classic virtual machines - you need to use certs

2

2 Answers

1
votes

I haven't worked with Java SDK (so I may be totally off base here) but I don't think you can use this library to perform operations on a non-classic Virtual Machines. These VMs are deployed through Azure Resource Manager and they have entirely different mechanism to manage resources.

Looking at the source code here: https://github.com/azure/azure-sdk-for-java, I believe this is where you will find the methods to manage Virtual Machines: https://github.com/Azure/azure-sdk-for-java/tree/master/resource-management/azure-mgmt-compute/src/main/java/com/microsoft/azure/management/compute.

For your specific query, please see the documentation here: http://azure.github.io/azure-sdk-for-java/com/microsoft/azure/management/compute/VirtualMachineOperations.html#beginStarting-java.lang.String-java.lang.String- (This is the place where you will find entire documentation - http://azure.github.io/azure-sdk-for-java/).

0
votes

Recently azure has released Java SDK 1.0.0 as LTS version. Use that SDk.

<dependency>
    <groupId>com.microsoft.azure</groupId>
    <artifactId>azure</artifactId>
    <version>1.0.0</version>
</dependency>

To start and stop a virtual machine

    ApplicationTokenCredentials credentials = new ApplicationTokenCredentials(clientId, tenantId, clientKey, AzureEnvironment.AZURE);
    Azure azure =  Azure.authenticate(credentials).withSubscription(subscriptionId);

azure.virtualMachines().start("resourceGroupName", "vmName");
azure.virtualMachines().powerOff("resourceGroupName", "vmName");

    azure.virtualMachines().getByResourceGroup("resourceGroupName", "vmName").start();
    azure.virtualMachines().getByResourceGroup("resourceGroupName", "vmName").powerOff();
        
        

But these are blocking calls. You can use startAsync() method to start it in an async way.