问题
I'm trying to create NSG and then attach it to a existing subnet. I've successfully able to create the NSG but it throws an error while attaching it to subnet. Stating that the address prefix cannot be null. Do we have to pass the address prefix as well? in below function?
params_create = azure.mgmt.network.models.Subnet(
Below is the full code snippet.
from azure.common.credentials import ServicePrincipalCredentials
from azure.mgmt.resource import ResourceManagementClient
from azure.mgmt.compute import ComputeManagementClient
from azure.mgmt.network import NetworkManagementClient
from azure.mgmt.compute.models import DiskCreateOption
from azure.mgmt.network.v2017_03_01.models import NetworkSecurityGroup
from azure.mgmt.network.v2017_03_01.models import SecurityRule
import azure.mgmt.network.models
SUBSCRIPTION_ID = 'xxx'
GROUP_NAME = 'xxxx'
LOCATION = 'xxxx'
VM_NAME = 'myVM'
VNET = 'existingvnet'
SUBNET = 'default'
def get_credentials():
credentials = ServicePrincipalCredentials(
client_id = 'xxx',
secret = 'xxxx',
tenant = 'xxxx'
)
return credentials
def create_network_security_group(network_client):
params_create = azure.mgmt.network.models.NetworkSecurityGroup(
location=LOCATION,
security_rules=[
azure.mgmt.network.models.SecurityRule(
name='rdprule',
access=azure.mgmt.network.models.SecurityRuleAccess.allow,
description='test security rule',
destination_address_prefix='*',
destination_port_range='3389',
direction=azure.mgmt.network.models.SecurityRuleDirection.inbound,
priority=500,
protocol=azure.mgmt.network.models.SecurityRuleProtocol.tcp,
source_address_prefix='*',
source_port_range='*',
),
],
)
result_create_NSG = network_client.network_security_groups.create_or_update(
GROUP_NAME,
'nsg-vm',
params_create,
)
return result_create_NSG.result()
def attach_network_security_group(network_client,creation_result_nsg):
params_create = azure.mgmt.network.models.Subnet(
network_security_group= creation_result_nsg,
)
result_create = network_client.subnets.create_or_update(
GROUP_NAME,
VNET,
SUBNET,
params_create,
)
return result_create.result()
if __name__ == "__main__":
credentials = get_credentials()
resource_group_client = ResourceManagementClient(
credentials,
SUBSCRIPTION_ID
)
network_client = NetworkManagementClient(
credentials,
SUBSCRIPTION_ID
)
compute_client = ComputeManagementClient(
credentials,
SUBSCRIPTION_ID
)
creation_result_nsg = create_network_security_group(network_client)
print("------------------------------------------------------")
print(creation_result_nsg)
input('Press enter to continue...')
creation_result = attach_network_security_group(network_client,creation_result_nsg)
print("------------------------------------------------------")
print(creation_result)
input('Press enter to continue...')
回答1:
that means you are not passing it the address prefix it should use. According to the docs you need to pass in address_prefix
parameter. so add it to your params_create, something like this:
params_create = Subnet(
address_prefix = "10.0.0.0/24",
network_security_group = azure.mgmt.network.models.NetworkSecurityGroup(xxx)
)
来源:https://stackoverflow.com/questions/55980556/attaching-nsg-to-subnet-using-python