首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Tring返回订阅中所有资源的IP地址,如果没有,则返回null,在Azure中。使用python

Tring返回订阅中所有资源的IP地址,如果没有,则返回null,在Azure中。使用python
EN

Stack Overflow用户
提问于 2022-06-08 10:33:32
回答 1查看 135关注 0票数 0

当我尝试运行这段代码时,它会将null作为每个资源的IP地址,但不确定原因。尝试返回每个资源的IP地址,如果资源没有IP地址,也将返回null。

代码语言:javascript
复制
from ipaddress import ip_address
import logging
import json
import os

from azure.mgmt.resource import ResourceManagementClient

此函数返回资源组中的资源。这里是我需要返回IP地址的地方。

代码语言:javascript
复制
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,
    
    
        
}

此函数返回订阅中的资源组。

代码语言:javascript
复制
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的资源组列表,并返回该列表。

代码语言:javascript
复制
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)
EN

回答 1

Stack Overflow用户

发布于 2022-06-08 12:18:40

其中一个解决办法,

要使用python返回IP地址,您可能需要在代码中使用regex模式到,如下面的示例所示:

Example Of Code:-

代码语言:javascript
复制
pattern = re.compile(r'(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})')

有关更多信息,请参考此所以线程_。

要从使用Python的Azure函数获得IP地址,请参考这个解决方案。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72544187

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档