How to check is subnet allocated with another resource in azure using Fluent API or management API ?

Dnyaneshwar Surywanshi 156 Reputation points
2021-02-18T11:31:15.527+00:00

I am creating Application gateway using ARM templete now I want to check after select the virtual network subnet should not be allocated any other resource if allocated then show the message like Subnet must have only have Application Gateway.

69497-subnetappgw.png

Azure Virtual Network
Azure Virtual Network
An Azure networking service that is used to provision private networks and optionally to connect to on-premises datacenters.
2,548 questions
Azure Application Gateway
Azure Application Gateway
An Azure service that provides a platform-managed, scalable, and highly available application delivery controller as a service.
1,088 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. KalyanChanumolu-MSFT 8,341 Reputation points
    2021-02-24T11:03:57.067+00:00

    @Dnyaneshwar Surywanshi Thank you for reaching out.
    We sincerely apologize for the delay in getting back to you.

    You can use the IpConfigurations property to check if the subnet is already associated with with any other resource.

    var network = await azure.Networks.GetByIdAsync("/subscriptions/XXXXX-c5b6-44fb-b5ba-XXXXX/resourceGroups/XXXX-testing/providers/Microsoft.Network/virtualNetworks/vnet90e998400c370f");  
    foreach (var subnet in network.Inner.Subnets)  
    {  
        if (subnet.IpConfigurations == null)  
        {  
            Console.WriteLine("Subnet is not associated");  
        }  
        else  
        {  
            Console.WriteLine("Subnet is already associated");  
        }  
    }  
    

    Please let us know if you have any further questions.

    ----------

    If an answer is helpful, please "Accept answer" or "Up-Vote" for the same which might be beneficial to other community members reading this thread.

    0 comments No comments

  2. Venkata Suri 1 Reputation point
    2022-04-29T20:45:19.647+00:00

    @KalyanChanumolu-MSFT Do we run this on azure shell? By passing with respective arguments?

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.