Fixes some NIC bugs (#39213)

* add loadbalancer

* dict check nullable

* add default vallue when get list

* create backend addr pool

* fix the set

* fix to dict

* fix ideponement

* use param security group name when create

* nic can has no nsg

* add test

* fix

* fix

* fix

* fix idemponet

* add document

* fix test

* add configuration

* fix

* fix

* remove all resources

* fix

* fix test

* add version added

* fix lint

* fix lint

* fix lint

* remove new feature and only submit bugfix

* remove useless test

* fix
This commit is contained in:
Yuwei Zhou 2018-04-25 09:54:19 +08:00 committed by Zim Kalinowski
parent 72456711c3
commit 39ca41eb1b
3 changed files with 66 additions and 26 deletions

View file

@ -814,8 +814,15 @@ class AzureRMModuleBase(object):
priority += 1 priority += 1
rule_name = "Rule_{0}".format(priority) rule_name = "Rule_{0}".format(priority)
parameters.security_rules.append( parameters.security_rules.append(
self.network_models.SecurityRule('Tcp', '*', '*', 'Allow', 'Inbound', source_port_range='*', self.network_models.SecurityRule(protocol='Tcp',
destination_port_range=str(port), priority=priority, name=rule_name) source_address_prefix='*',
destination_address_prefix='*',
access='Allow',
direction='Inbound',
source_port_range='*',
destination_port_range=str(port),
priority=priority,
name=rule_name)
) )
self.log('Creating default security group {0}'.format(security_group_name)) self.log('Creating default security group {0}'.format(security_group_name))

View file

@ -2,6 +2,7 @@
# #
# Copyright (c) 2016 Matt Davis, <mdavis@ansible.com> # Copyright (c) 2016 Matt Davis, <mdavis@ansible.com>
# Chris Houseknecht, <house@redhat.com> # Chris Houseknecht, <house@redhat.com>
# Yuwei ZHou, <yuwzho@microsoft.com>
# #
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
@ -174,6 +175,7 @@ extends_documentation_fragment:
author: author:
- "Chris Houseknecht (@chouseknecht)" - "Chris Houseknecht (@chouseknecht)"
- "Matt Davis (@nitzmahone)" - "Matt Davis (@nitzmahone)"
- "Yuwei Zhou (@yuwzho)"
''' '''
EXAMPLES = ''' EXAMPLES = '''
@ -266,7 +268,8 @@ state:
"id": "/subscriptions/XXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXX/resourceGroups/Testing/providers/Microsoft.Network/publicIPAddresses/publicip001", "id": "/subscriptions/XXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXX/resourceGroups/Testing/providers/Microsoft.Network/publicIPAddresses/publicip001",
"name": "publicip001" "name": "publicip001"
}, },
"subnet": {} "subnet": {},
"load_balancer_backend_address_pools": []
}], }],
"location": "eastus2", "location": "eastus2",
"mac_address": null, "mac_address": null,
@ -340,16 +343,6 @@ def nic_to_dict(nic):
) )
def construct_ip_configuration_set(raw):
configurations = [str(dict(
private_ip_allocation_method=to_native(item.get('private_ip_allocation_method')),
public_ip_address_name=(to_native(item.get('public_ip_address').get('name'))
if item.get('public_ip_address') else to_native(item.get('public_ip_address_name'))),
primary=item.get('primary'),
name=to_native(item.get('name'))
)) for item in raw]
return set(configurations)
ip_configuration_spec = dict( ip_configuration_spec = dict(
name=dict(type='str', required=True), name=dict(type='str', required=True),
private_ip_address=dict(type='str'), private_ip_address=dict(type='str'),
@ -438,6 +431,9 @@ class AzureRMNetworkInterface(AzureRMModuleBase):
if virtual_network_resource_group is None: if virtual_network_resource_group is None:
virtual_network_resource_group = self.resource_group virtual_network_resource_group = self.resource_group
# if not set the security group name, use nic name for default
self.security_group_name = self.security_group_name or self.name
if self.state == 'present' and not self.ip_configurations: if self.state == 'present' and not self.ip_configurations:
# construct the ip_configurations array for compatiable # construct the ip_configurations array for compatiable
self.deprecate('Setting ip_configuration flatten is deprecated and will be removed.' self.deprecate('Setting ip_configuration flatten is deprecated and will be removed.'
@ -448,7 +444,8 @@ class AzureRMNetworkInterface(AzureRMModuleBase):
private_ip_allocation_method=self.private_ip_allocation_method, private_ip_allocation_method=self.private_ip_allocation_method,
public_ip_address_name=self.public_ip_address_name if self.public_ip else None, public_ip_address_name=self.public_ip_address_name if self.public_ip else None,
public_ip_allocation_method=self.public_ip_allocation_method, public_ip_allocation_method=self.public_ip_allocation_method,
name='default' name='default',
primary=True
) )
] ]
@ -468,11 +465,10 @@ class AzureRMNetworkInterface(AzureRMModuleBase):
if update_tags: if update_tags:
changed = True changed = True
if self.security_group_name: nsg = self.get_security_group(self.security_group_name)
nsg = self.get_security_group(self.security_group_name) if nsg and results.get('network_security_group') and results['network_security_group'].get('id') != nsg.id:
if nsg and results['network_security_group'].get('id') != nsg.id: self.log("CHANGED: network interface {0} network security group".format(self.name))
self.log("CHANGED: network interface {0} network security group".format(self.name)) changed = True
changed = True
if results['ip_configurations'][0]['subnet']['virtual_network_name'] != virtual_network_name: if results['ip_configurations'][0]['subnet']['virtual_network_name'] != virtual_network_name:
self.log("CHANGED: network interface {0} virtual network name".format(self.name)) self.log("CHANGED: network interface {0} virtual network name".format(self.name))
@ -490,8 +486,8 @@ class AzureRMNetworkInterface(AzureRMModuleBase):
# construct two set with the same structure and then compare # construct two set with the same structure and then compare
# the list should contains: # the list should contains:
# name, private_ip_address, public_ip_address_name, private_ip_allocation_method, subnet_name # name, private_ip_address, public_ip_address_name, private_ip_allocation_method, subnet_name
ip_configuration_result = construct_ip_configuration_set(results['ip_configurations']) ip_configuration_result = self.construct_ip_configuration_set(results['ip_configurations'])
ip_configuration_request = construct_ip_configuration_set(self.ip_configurations) ip_configuration_request = self.construct_ip_configuration_set(self.ip_configurations)
if ip_configuration_result != ip_configuration_request: if ip_configuration_result != ip_configuration_request:
self.log("CHANGED: network interface {0} ip configurations".format(self.name)) self.log("CHANGED: network interface {0} ip configurations".format(self.name))
changed = True changed = True
@ -531,7 +527,11 @@ class AzureRMNetworkInterface(AzureRMModuleBase):
) for ip_config in self.ip_configurations ) for ip_config in self.ip_configurations
] ]
nsg = nsg or self.create_default_securitygroup(self.resource_group, self.location, self.name, self.os_type, self.open_ports) nsg = self.create_default_securitygroup(self.resource_group,
self.location,
self.security_group_name,
self.os_type,
self.open_ports)
self.log('Creating or updating network interface {0}'.format(self.name)) self.log('Creating or updating network interface {0}'.format(self.name))
nic = self.network_models.NetworkInterface( nic = self.network_models.NetworkInterface(
id=results['id'] if results else None, id=results['id'] if results else None,
@ -598,6 +598,16 @@ class AzureRMNetworkInterface(AzureRMModuleBase):
except Exception as exc: except Exception as exc:
return None return None
def construct_ip_configuration_set(self, raw):
configurations = [str(dict(
private_ip_allocation_method=to_native(item.get('private_ip_allocation_method')),
public_ip_address_name=(to_native(item.get('public_ip_address').get('name'))
if item.get('public_ip_address') else to_native(item.get('public_ip_address_name'))),
primary=item.get('primary'),
name=to_native(item.get('name'))
)) for item in raw]
return set(configurations)
def main(): def main():
AzureRMNetworkInterface() AzureRMNetworkInterface()

View file

@ -12,6 +12,11 @@
address_prefix: "10.10.0.0/24" address_prefix: "10.10.0.0/24"
virtual_network: testnic001 virtual_network: testnic001
- name: create public ip
azure_rm_publicipaddress:
name: ansiblepip3
resource_group: '{{ resource_group }}'
- name: Create NIC (check mode) - name: Create NIC (check mode)
azure_rm_networkinterface: azure_rm_networkinterface:
resource_group: "{{ resource_group }}" resource_group: "{{ resource_group }}"
@ -40,6 +45,22 @@
security_group: testnic001 security_group: testnic001
register: output register: output
- name: Create NIC using virtual_network_resource_group parameter (idempotent)
azure_rm_networkinterface:
resource_group: "{{ resource_group }}"
name: testnic001rg
virtual_network: testnic001
virtual_network_resource_group: "{{ resource_group_secondary }}"
subnet: testnic001
public_ip_name: testnic001
public_ip_allocation_method: Static
security_group: testnic001
register: output
- assert:
that:
- not output.changed
- name: Delete NIC - name: Delete NIC
azure_rm_networkinterface: azure_rm_networkinterface:
resource_group: "{{ resource_group }}" resource_group: "{{ resource_group }}"
@ -139,8 +160,8 @@
virtual_network: "{{ vn.state.id }}" virtual_network: "{{ vn.state.id }}"
subnet: testnic001 subnet: testnic001
ip_configurations: ip_configurations:
- name: ipconfig-add - name: ipconfig1
public_ip_name: testnic002 public_ip_name: testnic003
- name: default - name: default
public_ip_name: testnic001 public_ip_name: testnic001
public_ip_allocation_method: Static public_ip_allocation_method: Static
@ -156,7 +177,6 @@
azure_rm_networkinterface: azure_rm_networkinterface:
resource_group: "{{ resource_group }}" resource_group: "{{ resource_group }}"
name: testnic001noip name: testnic001noip
security_group: testnic001
virtual_network: "{{ vn.state.id }}" virtual_network: "{{ vn.state.id }}"
subnet: testnic001 subnet: testnic001
ip_configurations: ip_configurations:
@ -183,8 +203,11 @@
- name: Delete the NIC - name: Delete the NIC
azure_rm_networkinterface: azure_rm_networkinterface:
resource_group: "{{ resource_group }}" resource_group: "{{ resource_group }}"
name: testnic001 name: "{{ item }}"
state: absent state: absent
with_items:
- testnic001
- testnic001noip
register: output register: output
- assert: - assert: