【问题标题】:Creating NSG using Azure python sdk does not use the security rule使用 Azure python sdk 创建 NSG 不使用安全规则
【发布时间】:2018-05-22 22:26:37
【问题描述】:

我正在使用

λ pip show azure Name: azure Version: 2.0.0

我想创建一个具有特定安全规则的 NSG。我有以下代码。

```

from azure.mgmt.compute import ComputeManagementClient
from azure.mgmt.network import NetworkManagementClient
from azure.common.credentials import ServicePrincipalCredentials
from azure.mgmt.network.v2017_03_01.models import NetworkSecurityGroup
from azure.mgmt.network.v2017_03_01.models import SecurityRule
subscription_id = 'my-id'
credentials = ...

compute_client = ComputeManagementClient(
    credentials,
    subscription_id
)

network_client = NetworkManagementClient(
    credentials,
    subscription_id
)
from azure.mgmt.resource.resources import ResourceManagementClient

resource_client = ResourceManagementClient(
    credentials,
    subscription_id
)
resource_client.providers.register('Microsoft.Compute')
resource_client.providers.register('Microsoft.Network')

resource_group_name = 'test-rg'

security_rule = SecurityRule( protocol='Tcp', source_address_prefix='Internet', 
                              source_port_range="*", destination_port_range="3389", priority=100,
                              destination_address_prefix='*', access='Allow', direction='Inbound')
nsg_params = NetworkSecurityGroup(id='test-nsg', location='UK South', tags={ 'name' : 'testnsg' })
network_client.network_security_groups.create_or_update(resource_group_name, "test-nsg", parameters=nsg_params, security_rules=[security_rule])

这确实创建了 NSG 罚款,但未能创建正确的规则。

我错过了什么?

【问题讨论】:

    标签: azure azure-virtual-network azure-sdk-python


    【解决方案1】:

    我们可以使用这个脚本来实现它:

    from azure.common.credentials import ServicePrincipalCredentials
    from azure.mgmt.compute import ComputeManagementClient
    from azure.mgmt.network import NetworkManagementClient
    from azure.mgmt.network.v2017_03_01.models import NetworkSecurityGroup
    from azure.mgmt.network.v2017_03_01.models import SecurityRule
    from azure.mgmt.resource.resources import ResourceManagementClient
    
    subscription_id = 'xxxxxxxxx-xxxxxxxxxxxxxxxxxxxx'
    credentials = ServicePrincipalCredentials(
        client_id = 'xxxxxx-xxxx-xxx-xxxx-xxxxxxx',
        secret = 'xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxx',
        tenant = 'xxxxxx-xxxxxxx'
    )
    
    compute_client = ComputeManagementClient(
        credentials,
        subscription_id
    )
    
    network_client = NetworkManagementClient(
        credentials,
        subscription_id
    )
    
    resource_client = ResourceManagementClient(
        credentials,
        subscription_id
    )
    resource_client.providers.register('Microsoft.Compute')
    resource_client.providers.register('Microsoft.Network')
    
    resource_group_name = 'test-rg'
    
    
    parameters = NetworkSecurityGroup()
    parameters.location = 'UK South'
    
    parameters.security_rules = [SecurityRule('Tcp', '*', '*', 'Allow', 'Inbound', description='Allow RDP port 3389',
                                     source_port_range='*', destination_port_range='3389', priority=100, name='RDP01')]   
    
    
    network_client.network_security_groups.create_or_update(resource_group_name, "test-nsg", parameters)
    

    network_client.network_security_groups.create_or_update 只有三个值,resource_groupsecurity_group_nameparametes

    更多关于network_client.network_security_groups.create_or_update的信息,请参考这个link

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-07-11
      • 2019-09-03
      • 1970-01-01
      • 2016-02-26
      • 1970-01-01
      • 1970-01-01
      • 2020-02-23
      相关资源
      最近更新 更多