使用 python 的网络安全组部署问题:NetworkSecurityGroup' 对象没有属性 'lower'
Issues with Network security group deployment using python : NetworkSecurityGroup' object has no attribute 'lower'
提前致谢,我正在尝试使用 python 创建 NSG 并遇到
的问题
Message=Unable to build a model: Unable to deserialize to object:
type, AttributeError: 'NetworkSecurityGroup' object has no attribute
'lower', DeserializationError: Unable to deserialize to object: type,
AttributeError: 'NetworkSecurityGroup' object has no attribute 'lower'
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 = 'XXXXX'
GROUP_NAME = 'AQRG'
LOCATION = 'westus'
VM_NAME = 'myVM'
def get_credentials():
credentials = ServicePrincipalCredentials(
client_id = 'xxxx',
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='rdp rule',
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 = network_client.network_security_groups.create_or_update(
GROUP_NAME,
'nsg-vm',
params_create,
)
return result_create.result()
# creation_result = create_network_security_group(network_client)
# print("------------------------------------------------------")
# print(creation_result)
# input('Press enter to continue...')
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 = create_network_security_group(network_client)
print("------------------------------------------------------")
print(creation_result)
input('Press enter to continue...')
我是 python 的新手,几个小时后就创建了这段代码。我在部署 NSG 时遇到此错误,必须处理将 NSG 链接到子网
def attach_network_security_group(network_client):
params_create = azure.mgmt.network.models.Subnet(
network_security_group='nsg-vm',
)
result_create = network_client.subnets.create_or_update(
GROUP_NAME,
VNET,
SUBNET,
params_create,
)
return result_create.result()
对于你的问题,只是一个小错误。您只需要删除 ,
然后代码将如下所示:
params_create = azure.mgmt.network.models.NetworkSecurityGroup(
location=LOCATION,
security_rules=[
azure.mgmt.network.models.SecurityRule(
name='rdp rule',
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='*',
),
],
)
提前致谢,我正在尝试使用 python 创建 NSG 并遇到
的问题Message=Unable to build a model: Unable to deserialize to object: type, AttributeError: 'NetworkSecurityGroup' object has no attribute 'lower', DeserializationError: Unable to deserialize to object: type, AttributeError: 'NetworkSecurityGroup' object has no attribute 'lower'
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 = 'XXXXX'
GROUP_NAME = 'AQRG'
LOCATION = 'westus'
VM_NAME = 'myVM'
def get_credentials():
credentials = ServicePrincipalCredentials(
client_id = 'xxxx',
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='rdp rule',
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 = network_client.network_security_groups.create_or_update(
GROUP_NAME,
'nsg-vm',
params_create,
)
return result_create.result()
# creation_result = create_network_security_group(network_client)
# print("------------------------------------------------------")
# print(creation_result)
# input('Press enter to continue...')
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 = create_network_security_group(network_client)
print("------------------------------------------------------")
print(creation_result)
input('Press enter to continue...')
我是 python 的新手,几个小时后就创建了这段代码。我在部署 NSG 时遇到此错误,必须处理将 NSG 链接到子网
def attach_network_security_group(network_client):
params_create = azure.mgmt.network.models.Subnet(
network_security_group='nsg-vm',
)
result_create = network_client.subnets.create_or_update(
GROUP_NAME,
VNET,
SUBNET,
params_create,
)
return result_create.result()
对于你的问题,只是一个小错误。您只需要删除 ,
然后代码将如下所示:
params_create = azure.mgmt.network.models.NetworkSecurityGroup(
location=LOCATION,
security_rules=[
azure.mgmt.network.models.SecurityRule(
name='rdp rule',
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='*',
),
],
)