azure-java-sdk

Azure SDK for Java lists some VMs with empty fields


I'm trying to enumerate VMs of Azure for a certain Resource Group. On some tenants the SDK returns part of the VMs with zeroed fields (like computerName() == 'null').

Code sample:

var azureResourceManager = AzureResourceManager.authenticate(tokenCredential, azureProfile).withSubscription("subscription");

azureResourceManager.resourceGroups().list().forEach(resourceGroup -> {
    azureResourceManager.virtualMachines().listByResourceGroup(resourceGroup.name()).forEach(vm -> {
        var computerName = vm.computerName();
        if (computerName == null) {
            // VM lost
            return;
        }

        // VM is OK
    });
});

On a certain tenant I see ~30% of VMs have no computerName(), but the Azure Portal presents every VM the same proper way. About 70 VMs are lost from a total of 250.

On a different account with a smaller number of VMs there is no such issue.


Solution

  • It could be due to the issues with syncing data from the underlying system which leads to missing fields. Also, possible that VMs are stopped or deallocated which makes the properties temporarily unavailable.

    Check the VM's power state and provisioning state using Azure SDK.

    I have tried with below code and able to list VMs.

    package com.example.demo;
    
    import org.springframework.boot.CommandLineRunner;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.web.bind.annotation.GetMapping;
    
    import com.azure.core.credential.TokenCredential;
    import com.azure.core.management.AzureEnvironment;
    import com.azure.core.management.profile.AzureProfile;
    import com.azure.identity.ClientSecretCredentialBuilder;
    import com.azure.resourcemanager.AzureResourceManager;
    
    @SpringBootApplication
    public class DemoApplication implements CommandLineRunner {
    
        private static final String clientId = "<Client_ID>";
        private static final String tenantId = "<Tenant_ID>";
        private static final String clientSecret = "<Client_Secret>";
        private static final String subscriptionId = "<Subscription_ID>";
    
        public static void main(String[] args) {
            SpringApplication.run(DemoApplication.class, args);
        }
    
        @Override
        @GetMapping("/")
        public void run(String... args) throws Exception {
            TokenCredential tokenCredential = new ClientSecretCredentialBuilder()
                    .clientId(clientId)
                    .tenantId(tenantId)
                    .clientSecret(clientSecret)
                    .build();
    
            AzureProfile azureProfile = new AzureProfile(tenantId, subscriptionId, AzureEnvironment.AZURE);
    
            AzureResourceManager azureResourceManager = AzureResourceManager
                    .authenticate(tokenCredential, azureProfile)
                    .withSubscription(subscriptionId);
                    
            azureResourceManager.resourceGroups().list().forEach(resourceGroup -> {
                System.out.println("Resource Group: " + resourceGroup.name());
                azureResourceManager.virtualMachines().listByResourceGroup(resourceGroup.name()).forEach(vm -> {
                    String computerName = vm.computerName();
                    
                    if (computerName == null) {
                        System.out.println("VM " + vm.name() + " is lost (computer name is null).");
                    } else {
                        System.out.println("VM " + vm.name() + " is operational with computer name: " + computerName);
                    }
                });
            });
        }
    }
    

    Output:

    mvn spring-boot:run
    
    [INFO] Scanning for projects...
    [INFO] 
    [INFO] --------------------------< com.example:demo >--------------------------
    [INFO] Building demo 0.0.1-SNAPSHOT
    [INFO]   from pom.xml
    [INFO] --------------------------------[ jar ]---------------------------------
    [INFO] 
    [INFO] >>> spring-boot:3.4.4:run (default-cli) > test-compile @ demo >>>
    [INFO] 
    [INFO] --- resources:3.3.1:resources (default-resources) @ demo ---
    [INFO] Copying 1 resource from src\main\resources to target\classes
    [INFO] Copying 0 resource from src\main\resources to target\classes
    //Removed few logs
    2025-04-04T14:02:20.595+05:30  INFO 23896 --- [demo] [  restartedMain] com.example.demo.DemoApplication         : Starting DemoApplication using Java 21.0.4 with PID 23896 (C:\Users\uname\Downloads\demo (1)\demo\target\classes started by v-pkothaveer in C:\Users\uname\Downloads\demo (1)\demo)
    2025-04-04T14:02:20.595+05:30  INFO 23896 --- [demo] [  restartedMain] com.example.demo.DemoApplication         : No active profile set, falling back to 1 default profile: "default"
    2025-04-04T14:02:20.685+05:30  INFO 23896 --- [demo] [  restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : Devtools property defaults active! Set 'spring.devtools.add-properties' to 'false' to disable
    2025-04-04T14:02:20.685+05:30  INFO 23896 --- [demo] [  restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : For additional web related logging consider setting the 'logging.level.web' property to 'DEBUG'
    2025-04-04T14:02:22.551+05:30  INFO 23896 --- [demo] [  restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port 8080 (http)
    2025-04-04T14:02:22.571+05:30  INFO 23896 --- [demo] [  restartedMain] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
    2025-04-04T14:02:22.571+05:30  INFO 23896 --- [demo] [  restartedMain] o.apache.catalina.core.StandardEngine    : Starting Servlet engine: [Apache Tomcat/10.1.39]
    2025-04-04T14:02:22.654+05:30  INFO 23896 --- [demo] [  restartedMain] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
    2025-04-04T14:02:22.656+05:30  INFO 23896 --- [demo] [  restartedMain] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1969 ms
    2025-04-04T14:02:23.453+05:30  INFO 23896 --- [demo] [  restartedMain] o.s.b.d.a.OptionalLiveReloadServer       : LiveReload server is running on port 35729
    2025-04-04T14:02:23.520+05:30  INFO 23896 --- [demo] [  restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port 8080 (http) with context path '/'
    2025-04-04T14:02:23.540+05:30  INFO 23896 --- [demo] [  restartedMain] com.example.demo.DemoApplication         : Started DemoApplication in 3.851 seconds (process running for 4.878)
    
    // Removed few logs
    
    Resource Group: DefaultResourceGroup-CCAN
    Resource Group: pravrg
    VM Name: vm-kp
    Computer Name: vm-kp
    Power State: PowerState/running
    Provisioning State: Succeeded
    VM vm-kp is operational with computer name: vm-kp
    Resource Group: NetworkWatcherRG
    Resource Group: DefaultResourceGroup-EUS2