使用Azure REST API获取虚拟网络(VNet)的网络输入/网络输出数据

时间:2017-02-24 04:01:13

标签: api azure

我正在尝试聚合Microsoft.Insights Network In&由虚拟网络分组的虚拟机的网络输出数据,并且由于虚拟机仅返回其订阅,因此无法确定如何执行此操作。通过API查询时的资源组。

我想知道是否有办法让单个API调用返回:

a)VNET的虚拟机

OR

b)具有VNET信息的虚拟机

如果没有,那么获取每个VNet虚拟机列表的最佳方法是什么?

从我读过的所有数据来看,似乎获得此关联的唯一方法是通过/ networkinterfaces。这是对的吗?

VNET位于:properties / ipConfigurations / properties / subnet / ip

VM位于:properties / virtualMachine / id

我在2014年发现了一个关于MS的旧问题,表明当时没有这个问题:

https://social.msdn.microsoft.com/Forums/en-US/95b7f1bd-c557-4866-ae4e-57b58d802a31/how-do-i-use-powershell-to-get-virtual-machine-settings-dns-vnet-etc?forum=WAVirtualMachinesforWindows

真的希望他们在过去2年多的时间内改进了一些东西。让VNet成为POST上VM的要求有点令人沮丧,但不是GET响应的一部分。

1 个答案:

答案 0 :(得分:0)

好的,所以这已经过时了,所以我会给出一个答案,万一有其他人来这里看。

首先关闭 - 不,没有办法直接获得此关联。

如问题中所示,NIC似乎是解决方案。

我请求返回所有NIC数据(跨所有订阅以确保完全覆盖所有VNETS)。 (https://docs.microsoft.com/en-us/rest/api/network/list-network-interface-cards-within-a-resource-group)'

在此数据中,您可以通过读取子网数据来确定父vnet ...

这是我在Python中放在一起似乎可以完成任务的一个小测试:

Azure的数据响应(显示2个NICS): { "value": [ { "name": "REDACTED", "id": "REDACTED", "etag": "REDACTED", "location": "eastus", "type": "Microsoft.Network/networkInterfaces", "properties": { "provisioningState": "Succeeded", "macAddress": "REDACTED", "primary": true, "virtualMachine": { "id": "/subscriptions/REDACTED/resourceGroups/REDACTED/providers/Microsoft.Compute/virtualMachines/MyVM1" }, "dnsSettings": { "internalDomainNameSuffix": "REDACTED", "dnsServers": [], "appliedDnsServers": [] }, "enableIPForwarding": false, "resourceGuid": "REDACTED", "networkSecurityGroup": { "id": "/subscriptions/REDACTED/resourceGroups/REDACTED/providers/Microsoft.Network/networkSecurityGroups/REDACTED" }, "ipConfigurations": [ { "etag": "W/\"REDACTED\"", "id": "/subscriptions/REDACTED/resourceGroups/REDACTED/providers/Microsoft.Network/networkInterfaces/REDACTED/ipConfigurations/ipconfig1", "name": "ipconfig1", "properties": { "subnet": { "id": "/subscriptions/REDACTED/resourceGroups/REDACTED/providers/Microsoft.Network/virtualNetworks/MyVNET1/subnets/REDACTED" }, "primary": true, "privateIPAddressVersion": "IPv4", "publicIPAddress": { "id": "/subscriptions/REDACTED/resourceGroups/REDACTED/providers/Microsoft.Network/publicIPAddresses/REDACTED" }, "privateIPAllocationMethod": "Dynamic", "privateIPAddress": "10.0.0.4", "provisioningState": "Succeeded" } } ], "enableAcceleratedNetworking": false } }, { "name": "REDACTED", "id": "REDACTED", "etag": "REDACTED", "location": "eastus", "type": "Microsoft.Network/networkInterfaces", "properties": { "provisioningState": "Succeeded", "macAddress": "REDACTED", "primary": true, "virtualMachine": { "id": "/subscriptions/REDACTED/resourceGroups/REDACTED/providers/Microsoft.Compute/virtualMachines/MyVM2" }, "dnsSettings": { "internalDomainNameSuffix": "REDACTED", "dnsServers": [], "appliedDnsServers": [] }, "enableIPForwarding": false, "resourceGuid": "REDACTED", "networkSecurityGroup": { "id": "/subscriptions/REDACTED/resourceGroups/REDACTED/providers/Microsoft.Network/networkSecurityGroups/REDACTED" }, "ipConfigurations": [ { "etag": "W/\"REDACTED\"", "id": "/subscriptions/REDACTED/resourceGroups/REDACTED/providers/Microsoft.Network/networkInterfaces/REDACTED/ipConfigurations/ipconfig1", "name": "ipconfig1", "properties": { "subnet": { "id": "/subscriptions/REDACTED/resourceGroups/REDACTED/providers/Microsoft.Network/virtualNetworks/MyVNET1/subnets/REDACTED" }, "primary": true, "privateIPAddressVersion": "IPv4", "publicIPAddress": { "id": "/subscriptions/REDACTED/resourceGroups/REDACTED/providers/Microsoft.Network/publicIPAddresses/REDACTED" }, "privateIPAllocationMethod": "Dynamic", "privateIPAddress": "10.0.0.4", "provisioningState": "Succeeded" } } ], "enableAcceleratedNetworking": false } } } ] }

处理数据(注意下面的输出中有缩进问题):

def build_vms_by_net(data):
data = [data]
returnData = []
for i,obj in enumerate(data):
    if 'value' in obj:
        inner = obj['value']
        for j, obj2 in enumerate(inner):
            config = obj2['properties']['ipConfigurations']
            for k, obj3 in enumerate(config):
                if '/virtualNetworks/' in obj3['properties']['subnet']['id']:
                    vnetvals = obj3['properties']['subnet']['id'].split('/')
                    vnet = vnetvals[vnetvals.index('virtualNetworks')+1]
                else:
                    vnet = ''
            try:
                vmvals = obj2['properties']['virtualMachine']['id'].split('/')
                vm = vmvals[-1]
                subscriptionId = vmvals[vmvals.index('subscriptions')+1]
                resourceGroupName = vmvals[vmvals.index('resourceGroups')+1]
                if (vnet != '' and vm != ''):
                    returnData.append({
                        "nicId" : obj2['name'],
                        "vm"  : vm,
                        "vnet"    : vnet,
                        "subscriptionId" : subscriptionId,
                        "resourceGroupName" : resourceGroupName
                    })
            except Exception:
                # Azure has a habit of orphaning resources, in the event that
                # a nic is not associated with a virtual machine, we just don't
                # append its info and continue w/ the rest of the processing
                pass
                # as it likely means the VM was deleted, but not the NIC
            vnet = ''
            vm = ''
return returnData

outpur如下:[ { "subscriptionId": "REDACTED", "vnet": "REDACTED", "nicId": "REDACTED", "vm": "REDACTED", "resourceGroupName": "REDACTED" }, { "subscriptionId": "REDACTED", "vnet": "REDACTED", "nicId": "REDACTED", "vm": "REDACTED", "resourceGroupName": "REDACTED" } ]

循环遍历所有资源组,您将构建一个完整列表,然后您可以进一步处理该列表,以便在您选择的情况下通过VNET创建VM列表。

请注意,您可能需要加强错误处理,但是这个测试很快就可以开始了......它也可以作为在Azure中具有“资源组”的许多其他资源之间建立连接的基础。他们的父母(另一个更简单的例子是从它的数据响应中获取Gateway的父VNet)。