Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get Public IP Address ResourceId from IP Address #13

Open
rajdhandus opened this issue Feb 2, 2022 · 1 comment
Open

Get Public IP Address ResourceId from IP Address #13

rajdhandus opened this issue Feb 2, 2022 · 1 comment

Comments

@rajdhandus
Copy link

Is there a way to get the Azure Resource ID of a given Public IP if we know the IP address?

Input:- 40.81.207.37
Output:- /subscription/xxxx/resourceGroups/xxxx/providers/Microsoft.Network/publicIPAddresses/

Azure.ResourceManager.Network.PublicIPAddressCollection doesn't seem to have the necessary method..

@mcgallan
Copy link
Contributor

mcgallan commented Feb 1, 2024

Hi @rajdhandus , thank you for your feedback. If you only have the IpAddress, there is no such method in the SDK to obtain the corresponding ResourceID. However, if you know the subscription and resourceName you are using, you can try the following method to obtain the ID:
var sub = await ArmClient.GetSubscriptionResourceAsync(subId);
var rg = await subscription.GetResourceGroupAsync(resourceGroupName);
var publicIPAddressCollection = rg.GetPublicIPAddresses();
string yourIPAddress = "";
AsyncPageable<PublicIPAddressResource> getPublicIpAddressListResponseAP = publicIPAddressCollection.GetAllAsync();
List<PublicIPAddressResource> getPublicIpAddressListResponse = await getPublicIpAddressListResponseAP.ToEnumerableAsync();
foreach (PublicIPAddressResource resource in getPublicIpAddressListResponse)
{
if (resource.Data.IPAddress != null)
{
if (resource.Data.IPAddress == yourIPAddress)
{
Console.WriteLine($"Azure Resource ID of the public IP address {yourIPAddress} is {resource.Id}");
}
}
else
{
Console.WriteLine($"Public IP address {yourIPAddress} not found in your subscription.");
}
}
Output: /subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Network/publicIPAddresses/{publicIpAddressName}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants