Azure / azure-sdk-for-java

This repository is for active development of the Azure SDK for Java. For consumers of the SDK we recommend visiting our public developer docs at https://docs.microsoft.com/java/azure/ or our versioned developer docs at https://azure.github.io/azure-sdk-for-java.
MIT License
2.35k stars 1.99k forks source link

How can I know if a resource is of hidden type? #34337

Closed maheshn99 closed 1 year ago

maheshn99 commented 1 year ago

I need to know if any of the resources returned by the API: azureResourceManager.genericResources().list() is of hidden type. Can you please let me know how can I know that?

Many thanks!

ghost commented 1 year ago

Thanks for the feedback! We are routing this to the appropriate team for follow-up. cc @weidongxu-microsoft, @XiaofeiCao

weidongxu-microsoft commented 1 year ago

@maheshn99

Would you elaborate what do you mean by a "hidden type"?

maheshn99 commented 1 year ago

Please see attached image HiddenResExample

maheshn99 commented 1 year ago

Hello! Is there any update on this? Please prioritise implementing in the Java API a flag to denote that the resource being returned is of hidden type.

weidongxu-microsoft commented 1 year ago

Portal uses a huge ARG query.

Here is what you can do equivalent via Java SDK.

lib: ARG https://learn.microsoft.com/java/api/overview/azure/resourcemanager-resourcegraph-readme?view=azure-java-stable

code

ResourceGraphManager argManager = ResourceGraphManager.authenticate(
        new DefaultAzureCredentialBuilder().build(),
        new AzureProfile(AzureEnvironment.AZURE));
var subscriptionId1 = "<subscription-id>";
var rgName1 = "<resource-group>";
// load the query from resource, its too big to fit in String constant pool
var query = new Scanner(Main.class.getResourceAsStream("/queryAll.txt"), StandardCharsets.UTF_8)
        .useDelimiter("\\A").next().trim()
        .replace("{rgName}", rgName1);
// query ARG
var ret = argManager.resourceProviders().resources(
        new QueryRequest()
                .withSubscriptions(Collections.singletonList(subscriptionId1))
                .withQuery(query));

Below is 2 query file: queryNoHidden.txt queryAll.txt

maheshn99 commented 1 year ago

Thank you very much! I will give this a try.

maheshn99 commented 1 year ago

Can you please throw some light on how can I get the list of GenericResources from the variable ret (QueryResponse) ? I need to iterate through each of the resources returned by the query.

Many thanks!

weidongxu-microsoft commented 1 year ago

@maheshn99

I think the recommendation is to stick to ARG for related queries.

In the end of the query, there is project name,typeDisplayName,locationDisplayName,id,type,kind,location,subscriptionId,resourceGroup,tags,extendedLocation|sort by (tolower(tostring(name))) asc, which you can modify to return more rich data of the resource.

If you had to query via GenericResource, I only know how to query one by one, hence the performance would be pretty bad, if you have lots of resources.

You can try following code to query one item in the response of ARG query (assume ret is the response of argManager.resourceProviders().resources(..) API in the previous comment).

var item1 = (Map<String, String>) ((List) ret.data()).get(0);
var resourceName = (String) item1.get("name");
var resourceId = (String) item1.get("id");
// percentage encode
var encodedResourceName = URLEncoder.encode(resourceName, StandardCharsets.UTF_8)
        .replace("+", "%20");
if (!Objects.equals(resourceName, encodedResourceName)) {
    resourceId = resourceId.replace("/" + resourceName, "/" + encodedResourceName);
}
GenericResource resource = azure.genericResources().getById(resourceId);

Note the encoding part is pretty tricky and likely incomplete (Azure resource name should be simple, without space, but some are not).

maheshn99 commented 1 year ago

Fantastic! I was able to use the projected tags themselves being returned as part of QueryResponse. Truly appreciate your prompt help. Thank you very much. Closing this issue now.