network models

This commit is contained in:
Zhijun Zhao 2017-12-28 16:27:13 +08:00 committed by Matt Davis
parent 7d77ce6866
commit 11c5d7b670
9 changed files with 71 additions and 85 deletions

View file

@ -113,8 +113,6 @@ try:
from msrestazure.azure_exceptions import CloudError
from msrestazure.tools import resource_id, is_valid_resource_id
from msrestazure import azure_cloud
from azure.mgmt.network.models import PublicIPAddress, NetworkSecurityGroup, SecurityRule, NetworkInterface, \
NetworkInterfaceIPConfiguration, Subnet
from azure.common.credentials import ServicePrincipalCredentials, UserPassCredentials
from azure.mgmt.network.version import VERSION as network_client_version
from azure.mgmt.storage.version import VERSION as storage_client_version
@ -638,7 +636,7 @@ class AzureRMModuleBase(object):
self.check_provisioning_state(pip)
return pip
params = PublicIPAddress(
params = self.network_models.PublicIPAddress(
location=location,
public_ip_allocation_method=allocation_method,
)
@ -678,7 +676,7 @@ class AzureRMModuleBase(object):
self.check_provisioning_state(group)
return group
parameters = NetworkSecurityGroup()
parameters = self.network_models.NetworkSecurityGroup()
parameters.location = location
if not open_ports:
@ -686,16 +684,16 @@ class AzureRMModuleBase(object):
if os_type == 'Linux':
# add an inbound SSH rule
parameters.security_rules = [
SecurityRule('Tcp', '*', '*', 'Allow', 'Inbound', description='Allow SSH Access',
self.network_models.SecurityRule('Tcp', '*', '*', 'Allow', 'Inbound', description='Allow SSH Access',
source_port_range='*', destination_port_range='22', priority=100, name='SSH')
]
parameters.location = location
else:
# for windows add inbound RDP and WinRM rules
parameters.security_rules = [
SecurityRule('Tcp', '*', '*', 'Allow', 'Inbound', description='Allow RDP port 3389',
self.network_models.SecurityRule('Tcp', '*', '*', 'Allow', 'Inbound', description='Allow RDP port 3389',
source_port_range='*', destination_port_range='3389', priority=100, name='RDP01'),
SecurityRule('Tcp', '*', '*', 'Allow', 'Inbound', description='Allow WinRM HTTPS port 5986',
self.network_models.SecurityRule('Tcp', '*', '*', 'Allow', 'Inbound', description='Allow WinRM HTTPS port 5986',
source_port_range='*', destination_port_range='5986', priority=101, name='WinRM01'),
]
else:
@ -706,7 +704,7 @@ class AzureRMModuleBase(object):
priority += 1
rule_name = "Rule_{0}".format(priority)
parameters.security_rules.append(
SecurityRule('Tcp', '*', '*', 'Allow', 'Inbound', source_port_range='*',
self.network_models.SecurityRule('Tcp', '*', '*', 'Allow', 'Inbound', source_port_range='*',
destination_port_range=str(port), priority=priority, name=rule_name)
)
@ -767,6 +765,11 @@ class AzureRMModuleBase(object):
api_version='2017-06-01')
return self._network_client
@property
def network_models(self):
self.log("Getting network models...")
return NetworkManagementClient.models("2017-06-01")
@property
def rm_client(self):
self.log('Getting resource manager client')
@ -776,6 +779,11 @@ class AzureRMModuleBase(object):
api_version='2017-05-10')
return self._resource_client
@property
def rm_models(self):
self.log("Getting resource manager models")
return ResourceManagementClient.models("2017-05-10")
@property
def compute_client(self):
self.log('Getting compute client')
@ -785,6 +793,11 @@ class AzureRMModuleBase(object):
api_version='2017-03-30')
return self._compute_client
@property
def compute_models(self):
self.log("Getting compute models")
return ComputeManagementClient.models("2017-03-30")
@property
def dns_client(self):
self.log('Getting dns client')

View file

@ -184,16 +184,6 @@ from ansible.module_utils.azure_rm_common import AzureRMModuleBase
try:
from msrestazure.azure_exceptions import CloudError
from azure.mgmt.network.models import (
LoadBalancer,
FrontendIPConfiguration,
BackendAddressPool,
Probe,
LoadBalancingRule,
SubResource,
InboundNatPool,
Subnet
)
except ImportError:
# This is handled in azure_rm_common
pass
@ -342,7 +332,7 @@ class AzureRMLoadBalancer(AzureRMModuleBase):
if self.public_ip_address_name:
pip = self.get_public_ip_address(self.public_ip_address_name)
load_balancer_props['frontend_ip_configurations'] = [
FrontendIPConfiguration(
self.network_models.FrontendIPConfiguration(
name=frontend_ip_config_name,
public_ip_address=pip
)
@ -392,7 +382,7 @@ class AzureRMLoadBalancer(AzureRMModuleBase):
load_balancer_name=self.name,
name=backend_address_pool_name
)
load_balancer_props['backend_address_pools'] = [BackendAddressPool(name=backend_address_pool_name)]
load_balancer_props['backend_address_pools'] = [self.network_models.BackendAddressPool(name=backend_address_pool_name)]
probe_name = random_name('probe')
prb_id = probe_id(
@ -404,7 +394,7 @@ class AzureRMLoadBalancer(AzureRMModuleBase):
if self.probe_protocol:
load_balancer_props['probes'] = [
Probe(
self.network_models.Probe(
name=probe_name,
protocol=self.probe_protocol,
port=self.probe_port,
@ -417,11 +407,11 @@ class AzureRMLoadBalancer(AzureRMModuleBase):
load_balancing_rule_name = random_name('lbr')
if self.protocol:
load_balancer_props['load_balancing_rules'] = [
LoadBalancingRule(
self.network_models.LoadBalancingRule(
name=load_balancing_rule_name,
frontend_ip_configuration=SubResource(id=frontend_ip_config_id),
backend_address_pool=SubResource(id=backend_addr_pool_id),
probe=SubResource(id=prb_id),
frontend_ip_configuration=self.network_models.SubResource(id=frontend_ip_config_id),
backend_address_pool=self.network_models.SubResource(id=backend_addr_pool_id),
probe=self.network_models.SubResource(id=prb_id),
protocol=self.protocol,
load_distribution=self.load_distribution,
frontend_port=self.frontend_port,
@ -434,7 +424,7 @@ class AzureRMLoadBalancer(AzureRMModuleBase):
inbound_nat_pool_name = random_name('inp')
if frontend_ip_config_id and self.natpool_protocol:
load_balancer_props['inbound_nat_pools'] = [
InboundNatPool(
self.network_models.InboundNatPool(
name=inbound_nat_pool_name,
frontend_ip_configuration=Subnet(id=frontend_ip_config_id),
protocol=self.natpool_protocol,
@ -447,7 +437,7 @@ class AzureRMLoadBalancer(AzureRMModuleBase):
self.results['changed'] = changed
self.results['state'] = (
results if results
else load_balancer_to_dict(LoadBalancer(**load_balancer_props))
else load_balancer_to_dict(self.network_models.LoadBalancer(**load_balancer_props))
)
if self.check_mode:
@ -457,7 +447,7 @@ class AzureRMLoadBalancer(AzureRMModuleBase):
self.network_client.load_balancers.create_or_update(
resource_group_name=self.resource_group,
load_balancer_name=self.name,
parameters=LoadBalancer(**load_balancer_props)
parameters=self.network_models.LoadBalancer(**load_balancer_props)
).wait()
except CloudError as err:
self.fail('Error creating load balancer {}'.format(err))

View file

@ -218,8 +218,6 @@ state:
try:
from msrestazure.azure_exceptions import CloudError
from azure.mgmt.network.models import NetworkInterface, NetworkInterfaceIPConfiguration, Subnet, \
PublicIPAddress, NetworkSecurityGroup
except ImportError:
# This is handled in azure_rm_common
pass
@ -444,44 +442,44 @@ class AzureRMNetworkInterface(AzureRMModuleBase):
pip = self.create_default_pip(self.resource_group, self.location, self.name,
self.public_ip_allocation_method)
nic = NetworkInterface(
nic = self.network_models.NetworkInterface(
location=self.location,
tags=self.tags,
ip_configurations=[
NetworkInterfaceIPConfiguration(
self.network_models.NetworkInterfaceIPConfiguration(
private_ip_allocation_method=self.private_ip_allocation_method,
)
]
)
# nic.name = self.name
nic.ip_configurations[0].subnet = Subnet(id=subnet.id)
nic.ip_configurations[0].subnet = self.network_models.Subnet(id=subnet.id)
nic.ip_configurations[0].name = 'default'
nic.network_security_group = NetworkSecurityGroup(id=nsg.id,
nic.network_security_group = self.network_models.NetworkSecurityGroup(id=nsg.id,
location=nsg.location,
resource_guid=nsg.resource_guid)
if self.private_ip_address:
nic.ip_configurations[0].private_ip_address = self.private_ip_address
if pip:
nic.ip_configurations[0].public_ip_address = PublicIPAddress(
nic.ip_configurations[0].public_ip_address = self.network_models.PublicIPAddress(
id=pip.id,
location=pip.location,
resource_guid=pip.resource_guid)
else:
self.log("Updating network interface {0}.".format(self.name))
nic = NetworkInterface(
nic = self.network_models.NetworkInterface(
id=results['id'],
location=results['location'],
tags=results['tags'],
ip_configurations=[
NetworkInterfaceIPConfiguration(
self.network_models.NetworkInterfaceIPConfiguration(
private_ip_allocation_method=results['ip_configuration']['private_ip_allocation_method']
)
]
)
subnet = self.get_subnet(results['ip_configuration']['subnet']['virtual_network_name'],
results['ip_configuration']['subnet']['name'])
nic.ip_configurations[0].subnet = Subnet(id=subnet.id)
nic.ip_configurations[0].subnet = self.network_models.Subnet(id=subnet.id)
nic.ip_configurations[0].name = results['ip_configuration']['name']
# nic.name = name=results['name'],
@ -491,7 +489,7 @@ class AzureRMNetworkInterface(AzureRMModuleBase):
if results['ip_configuration']['public_ip_address'].get('id'):
pip = \
self.get_public_ip_address(results['ip_configuration']['public_ip_address']['name'])
nic.ip_configurations[0].public_ip_address = PublicIPAddress(
nic.ip_configurations[0].public_ip_address = self.network_models.PublicIPAddress(
id=pip.id,
location=pip.location,
resource_guid=pip.resource_guid)
@ -499,7 +497,7 @@ class AzureRMNetworkInterface(AzureRMModuleBase):
if results['network_security_group'].get('id'):
nsg = self.get_security_group(results['network_security_group']['name'])
nic.network_security_group = NetworkSecurityGroup(id=nsg.id,
nic.network_security_group = self.network_models.NetworkSecurityGroup(id=nsg.id,
location=nsg.location,
resource_guid=nsg.resource_guid)

View file

@ -114,7 +114,6 @@ from ansible.module_utils.azure_rm_common import AzureRMModuleBase
try:
from msrestazure.azure_exceptions import CloudError
from azure.mgmt.network.models import PublicIPAddress, PublicIPAddressDnsSettings
except ImportError:
# This is handled in azure_rm_common
pass
@ -223,25 +222,25 @@ class AzureRMPublicIPAddress(AzureRMModuleBase):
if self.state == 'present':
if not pip:
self.log("Create new Public IP {0}".format(self.name))
pip = PublicIPAddress(
pip = self.network_models.PublicIPAddress(
location=self.location,
public_ip_allocation_method=self.allocation_method,
)
if self.tags:
pip.tags = self.tags
if self.domain_name:
pip.dns_settings = PublicIPAddressDnsSettings(
pip.dns_settings = self.network_models.PublicIPAddressDnsSettings(
domain_name_label=self.domain_name
)
else:
self.log("Update Public IP {0}".format(self.name))
pip = PublicIPAddress(
pip = self.network_models.PublicIPAddress(
location=results['location'],
public_ip_allocation_method=results['public_ip_allocation_method'],
tags=results['tags']
)
if self.domain_name:
pip.dns_settings = PublicIPAddressDnsSettings(
pip.dns_settings = self.network_models.PublicIPAddressDnsSettings(
domain_name_label=self.domain_name
)
self.results['state'] = self.create_or_update_pip(pip)

View file

@ -334,12 +334,6 @@ state:
try:
from msrestazure.azure_exceptions import CloudError
from azure.mgmt.network.models import NetworkSecurityGroup, SecurityRule
from azure.mgmt.network.models import (
SecurityRuleAccess,
SecurityRuleDirection,
SecurityRuleProtocol
)
except ImportError:
# This is handled in azure_rm_common
pass
@ -348,7 +342,7 @@ from ansible.module_utils.azure_rm_common import AzureRMModuleBase
from ansible.module_utils.six import integer_types
def validate_rule(rule, rule_type=None):
def validate_rule(self, rule, rule_type=None):
'''
Apply defaults to a rule dictionary and check that all values are valid.
@ -375,7 +369,7 @@ def validate_rule(rule, rule_type=None):
if not rule.get('access'):
rule['access'] = 'Allow'
access_names = [member.value for member in SecurityRuleAccess]
access_names = [member.value for member in self.network_models.SecurityRuleAccess]
if rule['access'] not in access_names:
raise Exception("Rule access must be one of [{0}]".format(', '.join(access_names)))
@ -388,14 +382,14 @@ def validate_rule(rule, rule_type=None):
if not rule.get('protocol'):
rule['protocol'] = '*'
protocol_names = [member.value for member in SecurityRuleProtocol]
protocol_names = [member.value for member in self.network_models.SecurityRuleProtocol]
if rule['protocol'] not in protocol_names:
raise Exception("Rule protocol must be one of [{0}]".format(', '.join(protocol_names)))
if not rule.get('direction'):
rule['direction'] = 'Inbound'
direction_names = [member.value for member in SecurityRuleDirection]
direction_names = [member.value for member in self.network_models.SecurityRuleDirection]
if rule['direction'] not in direction_names:
raise Exception("Rule direction must be one of [{0}]".format(', '.join(direction_names)))
@ -438,14 +432,14 @@ def compare_rules(r, rule):
return matched, changed
def create_rule_instance(rule):
def create_rule_instance(self, rule):
'''
Create an instance of SecurityRule from a dict.
:param rule: dict
:return: SecurityRule
'''
return SecurityRule(
return self.network_models.SecurityRule(
protocol=rule['protocol'],
source_address_prefix=rule['source_address_prefix'],
destination_address_prefix=rule['destination_address_prefix'],
@ -566,14 +560,14 @@ class AzureRMSecurityGroup(AzureRMModuleBase):
if self.rules:
for rule in self.rules:
try:
validate_rule(rule)
validate_rule(self, rule)
except Exception as exc:
self.fail("Error validating rule {0} - {1}".format(rule, str(exc)))
if self.default_rules:
for rule in self.default_rules:
try:
validate_rule(rule, 'default')
validate_rule(self, rule, 'default')
except Exception as exc:
self.fail("Error validating default rule {0} - {1}".format(rule, str(exc)))
@ -687,15 +681,15 @@ class AzureRMSecurityGroup(AzureRMModuleBase):
return self.results
def create_or_update(self, results):
parameters = NetworkSecurityGroup()
parameters = self.network_models.NetworkSecurityGroup()
if results.get('rules'):
parameters.security_rules = []
for rule in results.get('rules'):
parameters.security_rules.append(create_rule_instance(rule))
parameters.security_rules.append(create_rule_instance(self, rule))
if results.get('default_rules'):
parameters.default_security_rules = []
for rule in results.get('default_rules'):
parameters.default_security_rules.append(create_rule_instance(rule))
parameters.default_security_rules.append(create_rule_instance(self, rule))
parameters.tags = results.get('tags')
parameters.location = results.get('location')

View file

@ -126,7 +126,6 @@ from ansible.module_utils.azure_rm_common import AzureRMModuleBase, CIDR_PATTERN
try:
from msrestazure.azure_exceptions import CloudError
from azure.mgmt.network.models import Subnet, NetworkSecurityGroup
except ImportError:
# This is handled in azure_rm_common
pass
@ -234,23 +233,23 @@ class AzureRMSubnet(AzureRMModuleBase):
if not subnet:
# create new subnet
self.log('Creating subnet {0}'.format(self.name))
subnet = Subnet(
subnet = self.network_models.Subnet(
address_prefix=self.address_prefix_cidr
)
if nsg:
subnet.network_security_group = NetworkSecurityGroup(id=nsg.id,
subnet.network_security_group = self.network_models.NetworkSecurityGroup(id=nsg.id,
location=nsg.location,
resource_guid=nsg.resource_guid)
else:
# update subnet
self.log('Updating subnet {0}'.format(self.name))
subnet = Subnet(
subnet = self.network_models.Subnet(
address_prefix=results['address_prefix']
)
if results['network_security_group'].get('id'):
nsg = self.get_security_group(results['network_security_group']['name'])
subnet.network_security_group = NetworkSecurityGroup(id=nsg.id,
subnet.network_security_group = self.network_models.NetworkSecurityGroup(id=nsg.id,
location=nsg.location,
resource_guid=nsg.resource_guid)

View file

@ -604,8 +604,6 @@ try:
ImageReference, NetworkProfile, LinuxConfiguration, \
SshConfiguration, SshPublicKey, VirtualMachineSizeTypes, \
DiskCreateOptionTypes, Plan, SubResource
from azure.mgmt.network.models import PublicIPAddress, NetworkSecurityGroup, NetworkInterface, \
NetworkInterfaceIPConfiguration, Subnet
except ImportError:
# This is handled in azure_rm_common
pass
@ -1643,20 +1641,20 @@ class AzureRMVirtualMachine(AzureRMModuleBase):
group = self.create_default_securitygroup(self.resource_group, self.location, self.name, self.os_type,
self.open_ports)
parameters = NetworkInterface(
parameters = self.network_models.NetworkInterface(
location=self.location,
ip_configurations=[
NetworkInterfaceIPConfiguration(
self.network_models.NetworkInterfaceIPConfiguration(
private_ip_allocation_method='Dynamic',
)
]
)
parameters.ip_configurations[0].subnet = Subnet(id=subnet_id)
parameters.ip_configurations[0].subnet = self.network_models.Subnet(id=subnet_id)
parameters.ip_configurations[0].name = 'default'
parameters.network_security_group = NetworkSecurityGroup(id=group.id,
parameters.network_security_group = self.network_models.NetworkSecurityGroup(id=group.id,
location=group.location,
resource_guid=group.resource_guid)
parameters.ip_configurations[0].public_ip_address = PublicIPAddress(id=pip.id,
parameters.ip_configurations[0].public_ip_address = self.network_models.PublicIPAddress(id=pip.id,
location=pip.location,
resource_guid=pip.resource_guid)

View file

@ -354,10 +354,6 @@ try:
UpgradePolicy, VirtualMachineScaleSetNetworkConfiguration, \
ApiEntityReference, ImageReference, SubResource
from azure.mgmt.network.models import PublicIPAddress, \
NetworkSecurityGroup, NetworkInterface, \
NetworkInterfaceIPConfiguration, Subnet, VirtualNetwork
except ImportError:
# This is handled in azure_rm_common
pass

View file

@ -129,7 +129,6 @@ state:
try:
from msrestazure.azure_exceptions import CloudError
from azure.mgmt.network.models import VirtualNetwork, AddressSpace, DhcpOptions
except ImportError:
# This is handled in azure_rm_common
pass
@ -295,14 +294,14 @@ class AzureRMVirtualNetwork(AzureRMModuleBase):
self.log("Create virtual network {0}".format(self.name))
if not self.address_prefixes_cidr:
self.fail('Parameter error: address_prefixes_cidr required when creating a virtual network')
vnet = VirtualNetwork(
vnet = self.network_models.VirtualNetwork(
location=self.location,
address_space=AddressSpace(
address_space=self.network_models.AddressSpace(
address_prefixes=self.address_prefixes_cidr
)
)
if self.dns_servers:
vnet.dhcp_options = DhcpOptions(
vnet.dhcp_options = self.network_models.DhcpOptions(
dns_servers=self.dns_servers
)
if self.tags:
@ -311,15 +310,15 @@ class AzureRMVirtualNetwork(AzureRMModuleBase):
else:
# update existing virtual network
self.log("Update virtual network {0}".format(self.name))
vnet = VirtualNetwork(
vnet = self.network_models.VirtualNetwork(
location=results['location'],
address_space=AddressSpace(
address_space=self.network_models.AddressSpace(
address_prefixes=results['address_prefixes']
),
tags=results['tags']
)
if results.get('dns_servers'):
vnet.dhcp_options = DhcpOptions(
vnet.dhcp_options = self.network_models.DhcpOptions(
dns_servers=results['dns_servers']
)
self.results['state'] = self.create_or_update_vnet(vnet)