Hot questions for Using Azure in azure management api

Top Java Programmings / Azure / azure management api

Question:

I'm trying to list my deployed spring boot application in appservices in one of my azure subscription using azure management libraries for java but not able to do so.

Everything works fine from azure cli.

azure java sdk version 1.18.0 (latest) jdk version 1.8.0_172

build.gradle

dependencies {
    compile group: 'com.microsoft.azure', name: 'azure', version: '1.18.0'
    compile group: 'ch.qos.logback', name: 'logback-classic', version: '1.2.3'
    testCompile group: 'junit', name: 'junit', version: '4.12'
}

snippets

try {
    Azure azure = Azure
                   .configure()
                   .authenticate(applicationTokenCredentials)
                   .withDefaultSubscription();

            listWebApps(azure);
            listWebAppsUsingAppServicePlan(azure);

        } catch (IOException e) {
            e.printStackTrace();
        }

private static void listWebAppsUsingAppServicePlan(Azure azure){
    PagedList<WebApp> webAppPagedList = azure.appServices().webApps().list();
    System.out.printf("There are %d web apps when searched via azure.appServices().webApps()\n", webAppPagedList.size());
    for (WebApp app : webAppPagedList) {
        System.out.printf("App: %s, Deployment slots: %d", app.name(), app.deploymentSlots().list().size());
    }

}

private static void listWebApps(Azure azure){
    PagedList<WebApp> webAppPagedList = azure.webApps().list();
    System.out.printf("There are %d web apps when searched via azure.webApps()\n", webAppPagedList.size());
    for (WebApp app : webAppPagedList) {
        System.out.printf("App: %s, Deployment slots: %d", app.name(), app.deploymentSlots().list().size());
    }
}

Output

There are 0 web apps when searched via azure.webApps() There are 0 web apps when searched via azure.appServices().webApps()

Am I missing something or if there are some prerequisites let me know.

Thanks a ton times.


Answer:

somehow azure.webApps().list() approach was returning an empty list but switching to azure.webapps.listAsync() solved my problem.

The new Snippet

    azure.webApps().listAsync()
            .subscribe(webApp -> {
                int capacity = webApp.manager().appServicePlans().getById(webApp.appServicePlanId()).capacity();
                System.out.println(webApp.name() + ": " + capacity + (capacity == 1 ? " instance" : " instances"));
            });
}

Question:

I am using the Java sdk to try and automate some azure tasks like start a server and shutdown a server. I was using version 0.9.0 of the java sdk from the maven

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

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

This code compiled and ran successfully in eclipse

 package com.services.servers.operations.azure;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

import com.microsoft.windowsazure.Configuration;
import com.microsoft.windowsazure.core.utils.KeyStoreType;
import com.microsoft.windowsazure.exception.ServiceException;
import com.microsoft.windowsazure.management.compute.ComputeManagementClient;
import com.microsoft.windowsazure.management.compute.ComputeManagementService;
import com.microsoft.windowsazure.management.compute.VirtualMachineOperations;
import com.microsoft.windowsazure.management.configuration.ManagementConfiguration;

public class AzureTest {

    String uri = "https://management.core.windows.net/";
    String subscriptionId = "dasdas9-86da-4343-a1f4-24c20864e166";
    String keyStoreLocation = "C:\\Users\\test\\Desktop\\azure\\testKeystore.jks";
    String keyStorePassword = "password";

    public boolean startVirtualMachine(String serviceName, String deploymentName, String virtualMachineName){

        boolean isSuccess = true;

        try {            

            VirtualMachineOperations virtualMachineOperations = null;

            Configuration config = ManagementConfiguration.configure(
                        new URI(uri), 
                          subscriptionId,
                          keyStoreLocation, 
                          keyStorePassword, 
                          KeyStoreType.jks 
                      );

            ComputeManagementClient computeManagementClient = ComputeManagementService.create(config);

            virtualMachineOperations = computeManagementClient.getVirtualMachinesOperations();

            virtualMachineOperations.beginStarting(serviceName, deploymentName, virtualMachineName);

        } catch (IOException e) {
            System.out.println("An IOException has occured. Exception: " +e);
            isSuccess = false;
        }  catch (ServiceException e) {
            System.out.println("A ServiceException has occured. Exception: " + e);
            isSuccess = false;
        } catch (URISyntaxException e) {
            System.out.println("A URISyntaxException has occured. Exception: " + e);
            isSuccess = false;
        }         


        return isSuccess;
    }

}

When I upgrade to the latest version of the sdk - 0.9.1 - the following classes dont exist any longer

import com.microsoft.windowsazure.Configuration;
import com.microsoft.windowsazure.core.utils.KeyStoreType;
import com.microsoft.windowsazure.exception.ServiceException;
import com.microsoft.windowsazure.management.configuration.ManagementConfiguration;

I couldnt find anything online to state where these classes have gone to - whether they have been deprecated or more to another lib

If anyone has any idea what classes I should instead or what libs they may have moved to- that would be great Or if anyone can suggest any improvements to the above code for starting a server that would be much appreciated

Thanks Damien


Answer:

I tried to reproduce the issue, I got the error Failed to read artifact descriptor for com.microsoft.azure:azure-svc-mgmt...jar:0.9.0.

It seems that the issue is caused by the maven repository for downloading the dependencies of the version 0.9.1 of Microsoft Azure SDK for Management.

I suggest you can use the version 0.9.0 for the present.

If you have to use the version 0.9.1, you can add the complete maven list for the libraries and their dependencies manually in the pom.xml file, or you can download & add all library files into the project classpath manually.

Question:

I am having a doubt that what could be the difference between creating azure redis cache using jedin client vs creating it using resource management API(Azure object). How can we effectively manage the redis cache using management API? And if we are using the mamagement API, then how it should be created and maintained in the whole application such as the Azure object gets created only once and is accessed over the whole application when required. I am using Java/Maven project. Thanks


Answer:

Per my experience, Azure Management API for Redis Cache and Jedis client are totally different SDKs.

Azure Redis Cache is created in the form of resource on azure, including Dns name,Subscription,Resource group,Location,Pricing tier and Virtual Network etc. Azure Management API for Redis Cache is to manage these resources about Azure Redis Cache Service. You could create a Redis Cache and get the hostname or get the access keys or delete these resources etc.It is IaaS level thing.

On the other side, Jedis client is just one of the recommanded clients for Redis Cache .It is to use it ,not manage it. For example ,you could connect to the cache or add something to the cache and retrieve it etc. But you cannot modify the resource group or location etc. via Jedis client. It is SaaS level thing.

Any concern , please let me know.

Question:

I deployed cloud service in azure portal. Deployment is successful. And I made some azure java sdk codes like below,

ComputeManagementClient cmclient = ComputeManagementService.create(config); 

 VirtualMachineOperations vmoperat = cmclient.getVirtualMachinesOperations(); 
 VirtualMachineGetResponse vmresp = vmoperat.get("AzurecloudDNS", AzurecloudDNSStaging20160613193121","WorkerRole1_IN_0");

But it throws the following exception,

Exception in thread "main" com.microsoft.windowsazure.exception.ServiceException: ResourceNotFound: Role WorkerRole1_IN_0 was not found in the currently deployed service.
 at com.microsoft.windowsazure.exception.ServiceException.createFromXml(ServiceException.java:206)
 at com.microsoft.windowsazure.management.compute.VirtualMachineOperationsImpl.get(VirtualMachineOperationsImpl.java:6517)
 at com.aaa.azure.JavaClient.main(JavaClient.java:81)

In azure portal, I can see WorkerRole1_IN_0 is working in this cloud service. But it throws exception.

Any idea? Thank in advanced.


Answer:

Explaining for the difference between Azure classic portal and Azure new portal, which using different model to create cloud service and virtual machine.

On Azure classic portal, using ASM mode (Azure Service Management) to create Azure services. But using ARM mode (Azure Resource Management) for Azure new portal.

For example, you can refer to the REST APIs of different modes to know the difference for getting information of a existing cloud service, please see below.

  1. Using Get Role classic REST API on ASM mode, it relate to the Java package com.microsoft.windowsazure.management.compute.
  2. Using Get information about a virtual machine REST API on ARM mode, it relate to the Java package com.microsoft.azure.management.compute.

So according to your code, you are using the Service Management SDK to get information for a cloud service created on classic portal, not for a cloud service created on new portal, because the cloud services created on different modes have different model structures.