使用Azure python SDK创建NSG不使用安全规则 [英] Creating NSG using Azure python sdk does not use the security rule
本文介绍了使用Azure python SDK创建NSG不使用安全规则的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用
λpip显示天蓝色名称:天蓝色版本:2.0.0
我想创建一个具有特定安全规则的NSG.我有以下代码.
I want to create a NSG with a specific security rule. I have the following code.
```
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罚款,但无法创建适当的规则.
This does create the NSG fine but fails to create the proper rules.
我想念什么?
推荐答案
我们可以使用此脚本来实现它:
We can use this script to achieve it:
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
仅具有三个值,资源组, security_group_name 和参数.
有关 network_client.network_security_groups.create_or_update
的更多信息,请参阅此
More information about network_client.network_security_groups.create_or_update
, please refer to this link.
这篇关于使用Azure python SDK创建NSG不使用安全规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文