問題描述
如何通過Azure REST API 擷取到虛拟機(VM)所使用的公共IP位址資訊

問題解答
由于直接擷取到的虛拟機資訊(Virtual Machines - Get)中,并不會包含虛拟機的公共IP資訊,但是可以根據查詢出的虛拟機網卡資訊,來找到對應的關聯的IP。
驗證步驟:
1) 通過REST API擷取虛拟機資訊:
GET https://management.chinacloudapi.cn/{虛拟機Resource ID}?api-version=2021-03-01
注:中國區 Azure REST API的終結點為:https://management.chinacloudapi.cn
2) 從傳回的消息體中,找到NetworkInterfaces中的ID資訊
3) 根據第二步中的 NetworkInterfaces id,進行進一步查詢Network Interfaces 資訊:
GET https://management.chinacloudapi.cn/{networkInterfaces id}?api-version=2021-02-01&$expand=ipConfigurations/publicIPAddress,networkSecurityGroup
4)從第三步的傳回消息體中, properties 中的 ipAddress 就是需要的公共IP位址
參考資料
Virtual Machines - Get :https://docs.microsoft.com/en-us/rest/api/compute/virtual-machines/get
Network Interfaces - Get :https://docs.microsoft.com/en-us/rest/api/virtualnetwork/network-interfaces/get
當在複雜的環境中面臨問題,格物之道需:濁而靜之徐清,安以動之徐生。 雲中,恰是如此!