2

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.

Arjun
  • 3,248
  • 18
  • 35

2 Answers2

1

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"));
            });
}
Arjun
  • 3,248
  • 18
  • 35
0

I test the code , but not gradle, mine is maven. And I used az ad sp create-for-rbac --sdk-auth > my.azureauth to get the my.azureauth file , then get the clientID,clientScrect,SunscribtionID and tenantID. Here is my result, it works well.

enter image description here

And here is my code.

import com.microsoft.azure.AzureEnvironment;
import com.microsoft.azure.PagedList;
import com.microsoft.azure.credentials.ApplicationTokenCredentials;
import com.microsoft.azure.management.Azure;
import com.microsoft.azure.management.appservice.WebApp;


public class App 
{
public static void main( String[] args )
{

    String client="*******";
    String tenant="*******";
    String key="********";
    String subscriptionId="********";
    ApplicationTokenCredentials credentials = new ApplicationTokenCredentials(
            client, tenant, key, AzureEnvironment.AZURE);
    Azure azure = Azure.authenticate(credentials).withSubscription(subscriptionId);

    System.out.println("asd");
    listWebApps(azure);
    System.out.println("asd");
    listWebAppsUsingAppServicePlan(azure);
}



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());
    }
}

}

Note: it will cost a lot of time while authenticate and run the methods to get the web lists. If you still have other questions, please let me know.

Community
  • 1
  • 1
George Chen
  • 13,703
  • 2
  • 11
  • 26