Azure SDK Python,我想将多个端口分配给 NSG 安全规则

Azure SDK Python,I want to assign multiple ports to an NSG Security Rule

我正试图在 Python 中操纵 Azure 安全组。 全局有效。

但是,尝试向现有安全组添加规​​则。

network_client.security_rules.create_or_update('resourcesname',"nsg-name","secure-name",SecurityRule(
        protocol='Tcp', 
        source_address_prefix='*', 
        destination_address_prefix='*', 
        access='Allow', 
        direction='Inbound', description=name+' use rules',source_port_range='*', 
        #destination_port_range="1000,2000",
        #destination_port_range=["1000","2000"],
        destination_port_range=[1000,2000],
        priority=100, name="secure-name"))

当我指定多个端口时出现以下错误

msrestazure.azure_exceptions.CloudError: Azure Error: SecurityRuleInvalidPortRange
Message: Security rule has invalid Port range. Value provided: [1000,2000]. Value should be an integer OR integer range with '-' delimiter. Valid range 0-65535.

我也尝试过字符串数组和简单字符串。 但它失败了。 谁能解决这个问题?

其实很简单

如果要添加一系列端口,您应该使用 属性 destination_port_ranges 而不是 destination_port_range(注意 " s" 在 2 个属性的末尾)。

这是我的代码:

from azure.common.credentials import ServicePrincipalCredentials
from azure.mgmt.network import NetworkManagementClient
from azure.mgmt.network.v2020_04_01.models import NetworkSecurityGroup, SecurityRule

subscription_id = 'xxx'
credentials = ServicePrincipalCredentials(
    client_id = 'xxx',
    secret = 'xxx',
    tenant = 'xxx'
)

network_client = NetworkManagementClient(
    credentials,
    subscription_id
)

network_client.security_rules.create_or_update('xxx',"yysecurityGroup","my_Port_8080",SecurityRule(
        protocol='Tcp', 
        source_address_prefix='*', 
        destination_address_prefix='*', 
        access='Allow', 
        direction='Inbound', description='my_Port_8080 use rules',source_port_range='*', 
        #destination_port_range="1000,2000",
        destination_port_ranges=["1000","1005","2005","2020"],     
        priority=100, name="my_Port_8080"))


print("**complete**")

测试结果: