How to check Public IP addresses already associated with the other resources using Fluent API

Dnyaneshwar Surywanshi 156 Reputation points
2021-02-19T05:45:32.71+00:00

I want to find out weather selected IP address is associated with other resources or not suing Fluent API .

Below is my code right now :

var publicIp = _azureClient.PublicIPAddresses.ListByResourceGroup(requestObject.ResourceGroupName);
var publicIpList = new List<PublicIpObject>();
foreach (var ip in publicIp)
{
var pubIpObj = new PublicIpObject();
if (ip != null && ip.Sku.Equals("Standard") && ip.IPAllocationMethod.Equals("Static"))
{
pubIpObj.PublicIpAddressName = ip.Name;
pubIpObj.PublicIpSku = ip.Sku.ToString();
pubIpObj.IsAttachedToNic = ip.HasAssignedNetworkInterface;
pubIpObj.IsAttachedToLoadBalancer = ip.HasAssignedLoadBalancer;
pubIpObj.PublicIpAssignment = ip.IPAllocationMethod.ToString();
publicIpList.Add(pubIpObj);
}
}
return publicIpList;

69898-publicipassociated.png

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,014 questions
0 comments No comments
{count} votes

Accepted answer
  1. KalyanChanumolu-MSFT 8,321 Reputation points
    2021-02-24T10:14:56.273+00:00

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

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

        var publicip1 = await azure.PublicIPAddresses.GetByIdAsync("/subscriptions/XXXXX-c5b6-44fb-b5ba-XXXXX/resourceGroups/XXXX-testing/providers/Microsoft.Network/publicIPAddresses/pip1-intlb1-22899143b");  
    
        if (publicip1.Inner.IpConfiguration == null)  
        {  
            Console.WriteLine("IP Address is not associated");  
        }  
        else  
        {  
            Console.WriteLine("IP Address 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.

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful