azure-sdk-for-net
azure-sdk-for-net copied to clipboard
[QUERY] How to manage child resource with GenericResource
Library name and version
1.1.0
Query/Question
We want to leverage the SDK to delete resources based on resource id. However, it seems child resource cannot be supported.
Particularly in our case, we use the SDK to delete privateDnsZoneGroup. We firstly try to query the resource to check if it exists.
var resourceId = "/subscriptions/7e9a9569-4189-41ca-9bf5-48e49c67c5e0/resourceGroups/rg-workloadinfra-vnetbef6d5fcae4042af/providers/Microsoft.Network/privateEndpoints/pe8e7a3ba6efc14c5e/privateDnsZoneGroups/default";
var resource = armClient.GetGenericResource(new ResourceIdentifier(resourceId));
await resource.GetAsync();
while in await resource.GetAsync() , it will give error: System.InvalidOperationException: 'Invalid resource type Microsoft.Network/privateEndpoints/privateDnsZoneGroups'
at Azure.ResourceManager.Resources.ResourceProviderCollection.<GetApiVersionAsync>d__3.MoveNext()
at System.Threading.Tasks.ValueTask`1.get_Result()
at System.Runtime.CompilerServices.ConfiguredValueTaskAwaitable`1.ConfiguredValueTaskAwaiter.GetResult()
at Azure.ResourceManager.Resources.GenericResource.<GetApiVersionAsync>d__26.MoveNext()
at Azure.ResourceManager.Resources.GenericResource.<GetAsync>d__13.MoveNext()
It throws the error when it tries to get api version based on resource type. And ResourceIdentifier would return Microsoft.Network/privateEndpoints/privateDnsZoneGroup based on resource id. If go to the subscription in Azure portal, this resource type cannot be found under "Resource Providers" blade. So I suppose this is why the library say it is an invalid resource type. However, it should be a valid scenario to use this SDK to manage child resource. Appreciated on any suggestion. Thanks.
Environment
No response
Thank you for your feedback. Tagging and routing to the team member best able to assist.
Hi @Zoe-ms, you are right about the reason of the exception, we can't find the API version corresponding to the resource Microsoft.Network/privateEndpoints/privateDnsZoneGroups. I have two options here:
- Explicitly set the API version that you need. Here is the sample code.
ArmClientOptions options = new ArmClientOptions();
options.SetApiVersion("Microsoft.Network/privateEndpoints/privateDnsZoneGroups", "2021-02-01");
var client = GetArmClient(options);
var resourceId = "/subscriptions/7e9a9569-4189-41ca-9bf5-48e49c67c5e0/resourceGroups/rg-workloadinfra-vnetbef6d5fcae4042af/providers/Microsoft.Network/privateEndpoints/pe8e7a3ba6efc14c5e/privateDnsZoneGroups/default";
var resource = client.GetGenericResource(new ResourceIdentifier(resourceId));
await resource.GetAsync();
- Use Azure.ResourceManager.Network instead of the generic resource methods in Azure.ResourceManager, this is the get method for PrivateDnsZoneGroupResource.
Please let me know if it could help.
@Yao725, thanks for reply, the options are helpful. But I'm wondering if the library can automatically get the api-version based on parent resource type, which would be Microsoft.Network/privateEndpoints in this case. Not sure if it is a valid feature ask. If this can be supported, that would be more helpful, since we are building a generic service to handle multiple types of arm resources, not limit to network resource, and it would be perfect if we do not need have special handing based on resource type. Thanks.
Hi @Zoe-ms, I'm glad to know that it could help.
For the feature request that you mentioned above, we can't support it for now, I'm sorry about that. Take this issue as an example to explain why we don't want to do it. In this case, if we check the parent resource type Microsoft.Network/privateEndpoints, we could get the API version such as 1500-10-10 and we will use it for Microsoft.Network/privateEndpoints/privateDnsZoneGroups. But the truth may be that the Microsoft.Network/privateEndpoints/privateDnsZoneGroups is not supported at all in this version. Then the SDK will send bad request, this is not allowed from the SDK perspective.
@Yao725 , thanks for your quick response. But I'm wondering child resource should have same version with parent resource, since it is built by same team, even in the same service. But I cannot find any arm guidelines to guarantee this. Do you know if there is any documentation which defines the related behavior?
@Zoe-ms, sorry, I have no idea about the documentation that you mentioned. But from the perspective of SDK, it is allowed to use different API version for different resources in one service. Here is one example in Azure.ResourceManager.Synapse. SynapseWorkspaceResource is parent of SynapseBigDataPoolInfoResource. But the API version of SynapseWorkspaceResource is 2021-06-01 when the API version of SynapseBigDataPoolInfoResource is 2021-06-01-preview.
Remark:
- Request path of SynapseWorkspaceResource:
/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Synapse/workspaces/{workspaceName} - Request path of SynapseBigDataPoolInfoResource:
/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Synapse/workspaces/{workspaceName}/bigDataPools/{bigDataPoolName}
@Yao725 , thanks for having this example, which proves using parent api-version may result in incorrect behavior. We will have workaround in our service instead. Great thanks for all your help.
Np, I'm going to close this issue, feel free to reopen it if you still have questions. And if you have any other feedback to our Azure SDK, please let us know in this survey.