2

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

Community
  • 1
  • 1
Damien
  • 4,081
  • 12
  • 75
  • 126

2 Answers2

1

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/).

Gaurav Mantri
  • 128,066
  • 12
  • 206
  • 241
  • Hi Guarav - these are the same docs and classes more or less. The only diference is the package name . Your link is /com/microsoft/azure/management/compute/VirtualMachineOperations.html and mine is com/microsoft/windowsazure/management/compute/VirtualMachineOperations.html – Damien Jan 09 '16 at 15:55
  • 1
    If you see the method signature, they are different. Under new world, you just have to provide resource group name and virtual machine name to start it when using `beginStarting` method. – Gaurav Mantri Jan 09 '16 at 16:01
  • Apologies - I see that now. So 1 set of methods for classic and 1 set of methods for new virtual machines. Thanks for noticing this. I need to import both jars then if that is the case. I dont know about you but the documentation is confusing. Do you know if they have a link to the "latest" version of their api? – Damien Jan 09 '16 at 16:21
  • 1
    No worries! Glad we got this one cleared. When you say API, do you mean REST API? – Gaurav Mantri Jan 09 '16 at 17:10
  • Yeah their api docs - be it the rest api or for the Java SDK. Like for the Java SDK - when you look at the API Docs here http://azure.github.io/azure-sdk-for-java/index.html - there are 2 version of the VirtualMachineOperations class listed. I think this is what led to my confusion sorry. I know for other apis/sdk's such as spring - the docs are versioned and there is a link that will always bring you to the latest version – Damien Jan 09 '16 at 18:13
0

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.

Rishi Anand
  • 280
  • 1
  • 4
  • 15