当我尝试运行这段代码时,它会将null作为每个资源的IP地址,但不确定原因。尝试返回每个资源的IP地址,如果资源没有IP地址,也将返回null。
from ipaddress import ip_address
import logging
import json
import os
from azure.mgmt.resource import ResourceManagementClient此函数返回资源组中的资源。这里是我需要返回IP地址的地方。
def process_r_instance(resource):
"""
Get the resources from a ResourceGroup instance.
"""
return {
"Name": resource.name,
"Type": resource.type,
"Location": resource.location,
"Tags": resource.tags,
"IP Address: ": resource.properties.ipAddress \
if resource.properties and resource.properties.ipAddress else None,
}此函数返回订阅中的资源组。
def process_rg_instance(group):
"""
Get the relevant pieces of information from a ResourceGroup
instance.
"""
return {
"Name": group.name,
"Id": group.id,
"Location": group.location,
"Type": group.type,
"Tags": group.tags,
"Properties: Provisioning State: ": group.properties.provisioning_state \
if group.properties and group.properties.provisioning_state else None,
}此函数获取传递的订阅ID的资源组列表,并返回该列表。
async def list_rgs(credentials, subscription_id):
"""
Get list of resource groups for the subscription id passed.
"""
list_of_resource_groups = []
with ResourceManagementClient(credentials, subscription_id) as
rg_client:
try:
for i in rg_client.resource_groups.list():
list_of_resource_groups.append(process_rg_instance(i)),
for i in rg_client.resources.list():
list_of_resource_groups.append(process_r_instance(i)),
except Exception as e:
logging.error("encountered: {0}".format(str(e)))
return json.dumps(list_of_resource_groups)发布于 2022-06-08 12:18:40
其中一个解决办法,
要使用python返回IP地址,您可能需要在代码中使用regex模式到,如下面的示例所示:
Example Of Code:-
pattern = re.compile(r'(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})')有关更多信息,请参考此所以线程_。
要从使用Python的Azure函数获得IP地址,请参考这个解决方案。
https://stackoverflow.com/questions/72544187
复制相似问题