Azure / Azure/azure-sdk-for-java

[QUERY] How to retrieve Availability Zone information directly from `GenericResource`?

Open
#48,574 6 comments 0 reactions 1 assignee Claimed by @weidongxu-microsoft View on GitHub
ARM customer-reported Mgmt needs-team-attention question
Dominant language
Java
Stars
2.6k
Forks
2.2k
Avg merge
2d 8h
Merged PRs (30d)
178

Description

**Query/Question**
We are using `azure.genericResources().listByResourceGroup()` to generate a complete inventory of all resources within a Resource Group regardless of their type.

Our requirement is to capture the Availability Zone ("zonal") information for resources that support it (VMs, Disks, etc.). In the raw REST API and Azure CLI, the `"zones"` array is returned as a top-level property next to `"type"` and `"properties"`:

```json
"type": "Microsoft.Compute/virtualMachines",
"zones": [ "2" ],
"properties": { ... }
```

However, when inspecting the `GenericResource` object returned by the Java SDK, the top-level `zones` array does not appear to be exposed. It is not mapped into the `resource.properties()` map, nor is it accessible directly on the `GenericResource` interface.

**Our Current Workaround:**
When we iterate through the generic resources, we check the type and dynamically fetch the strongly-typed object to access the zones:
```java
if (resource.type().equalsIgnoreCase("Microsoft.Compute/virtualMachines")) {
VirtualMachine vm = azure.virtualMachines().getById(resource.id());
Set zones = vm.zones();
}
```
**The Problem:** This introduces a severe N+1 API call issue. If a resource group has 100 VMs and Disks, we are forced to make 101 API calls just to retrieve the zone metadata, which heavily impacts performance.

**The Ask:** Is there a supported way to extract the raw `"zones"` array directly from the `GenericResource` (or its `innerModel()`) returned by `listByResourceGroup()` to avoid making individual `.getById()` calls for every resource?

***Why is this not a Bug or a feature Request?***
This is not a bug because `GenericResource` is behaving as designed by wrapping standard ARM properties and dropping unmapped top-level keys. It is also not a feature request (yet), as we are inquiring if there is an existing best practice, hidden property bag, or specific SDK deserialization setting we might have missed that allows access to raw top-level ARM JSON properties within the `GenericResource` wrapper.

**Setup (please complete the following information if applicable):**
- OS: macOS
- IDE: IntelliJ IDEA / VS Code
- Library/Libraries: `com.azure.resourcemanager:azure-resourcemanager:2.36.0`

**Information Checklist**
Kindly make sure that you have added all the following information above and checkoff the required fields otherwise we will treat the issuer as an incomplete report
- [x] Query Added
- [x] Setup information Added

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.