I am using
λ pip show azure
Name: azure
Version: 2.0.0
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])
This does create the NSG fine but fails to create the proper rules.
What am I missing?
We can use this script to achieve it:
network_client.network_security_groups.create_or_update
only have three values, resource_group, security_group_name and parametes.More information about
network_client.network_security_groups.create_or_update
, please refer to this link.