python - 使用 python 将 NSG 附加到子网
问题描述
我正在尝试创建 NSG,然后将其附加到现有子网。我已经成功地创建了 NSG,但是在将其附加到子网时会引发错误。说明地址前缀不能为空。我们是否也必须传递地址前缀?在下面的功能?
params_create = azure.mgmt.network.models.Subnet(
下面是完整的代码片段。
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...')
解决方案
这意味着您没有将它应该使用的地址前缀传递给它。根据文档,您需要传入address_prefix
参数。所以将它添加到您的 params_create 中,如下所示:
params_create = Subnet(
address_prefix = "10.0.0.0/24",
network_security_group = azure.mgmt.network.models.NetworkSecurityGroup(xxx)
)
推荐阅读
- xml - 如何确定 xml 在代号中的格式是否正确?
- sql - 是否与空值定义的行为进行比较?
- java - HTML 音频标签在 localhost 中无法使用 apache tomcat
- postgresql - 错误:YAML 解析错误 - 将 YAML 转换为 JSON 时出错:yaml: control characters are not allowed
- java - 如何通过热敏打印机在卷筒上正确对齐打印多个标签
- python - 使用 multiprocessing.pool() 遍历列表
- windows - 是否可以在 windows iis 上运行 Nest.js 应用程序?
- c++ - 如何使用 C++17 的文件系统检查路径是否引用 Windows 中的卷?
- python - Python:加载txt文件并形成数据框
- node.js - 实体管理器和存储库 typeorm 之间的区别