Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

azure_rm_networkinterface - Set parimary key when the ip_configuration length is greater than 2 #1679

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 16 additions & 18 deletions plugins/modules/azure_rm_networkinterface.py
Original file line number Diff line number Diff line change
Expand Up @@ -479,8 +479,8 @@
# This is handled in azure_rm_common
pass

from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import (AzureRMModuleBase,
azure_id_to_dict,
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common_ext import AzureRMModuleBaseExt
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import (azure_id_to_dict,
normalize_location_name,
format_resource_id
)
Expand Down Expand Up @@ -560,7 +560,7 @@ def nic_to_dict(nic):
)


class AzureRMNetworkInterface(AzureRMModuleBase):
class AzureRMNetworkInterface(AzureRMModuleBaseExt):

def __init__(self):

Expand Down Expand Up @@ -634,7 +634,10 @@ def exec_module(self, **kwargs):

# if application security groups set, convert to resource id format
if self.ip_configurations:
primary_flag = False
for config in self.ip_configurations:
if config.get('primary'):
primary_flag = True
if config.get('application_security_groups'):
asgs = []
for asg in config['application_security_groups']:
Expand All @@ -650,10 +653,14 @@ def exec_module(self, **kwargs):
asgs.append(asg_resource_id)
if len(asgs) > 0:
config['application_security_groups'] = asgs
if not primary_flag:
self.ip_configurations[0]['primary'] = True

# If ip_confiurations is not specified then provide the default
# private interface
skip_compare = False
if self.state == 'present' and not self.ip_configurations:
skip_compare = True
self.ip_configurations = [
dict(
name='default',
Expand Down Expand Up @@ -728,19 +735,8 @@ def exec_module(self, **kwargs):
# name, private_ip_address, public_ip_address_name, private_ip_allocation_method, subnet_name
ip_configuration_result = self.construct_ip_configuration_set(results['ip_configurations'])
ip_configuration_request = self.construct_ip_configuration_set(self.ip_configurations)
ip_configuration_result_name = [item['name'] for item in ip_configuration_result]
for item_request in ip_configuration_request:
if item_request['name'] not in ip_configuration_result_name:
changed = True
break
else:
for item_result in ip_configuration_result:
if len(ip_configuration_request) == 1 and len(ip_configuration_result) == 1:
item_request['primary'] = True
if item_request['name'] == item_result['name'] and item_request != item_result:
changed = True
break

if not skip_compare and not self.default_compare({}, ip_configuration_request, ip_configuration_result, '', dict(compare=[])):
changed = True
elif self.state == 'absent':
self.log("CHANGED: network interface {0} exists but requested state is 'absent'".format(self.name))
changed = True
Expand Down Expand Up @@ -898,7 +894,6 @@ def construct_ip_configuration_set(self, raw):
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'),
load_balancer_backend_address_pools=(set([to_native(self.backend_addr_pool_id(id))
for id in item.get('load_balancer_backend_address_pools')])
if item.get('load_balancer_backend_address_pools') else None),
Expand All @@ -907,7 +902,10 @@ def construct_ip_configuration_set(self, raw):
if item.get('application_gateway_backend_address_pools') else None),
application_security_groups=(set([to_native(asg_id) for asg_id in item.get('application_security_groups')])
if item.get('application_security_groups') else None),
name=to_native(item.get('name'))
name=to_native(item.get('name')),
private_ip_address=to_native(item.get('private_ip_address')),
private_ip_address_version=to_native(item.get('private_ip_address_version')),
public_ip_allocation_method=to_native(item.get('public_ip_allocation_method', 'Dynamic'))
) for item in raw]
return configurations

Expand Down
18 changes: 12 additions & 6 deletions tests/integration/targets/azure_rm_networkinterface/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -307,19 +307,21 @@
private_ip_address: "{{ facts.networkinterfaces[0].ip_configurations[0].private_ip_address }}"
private_ip_allocation_method: "{{ facts.networkinterfaces[0].ip_configurations[0].private_ip_allocation_method }}"
primary: "{{ facts.networkinterfaces[0].ip_configurations[0].primary }}"
- name: ipconfig1
public_ip_name: "tn{{ rpfx }}"
load_balancer_backend_address_pools:
- "{{ lb.state.backend_address_pools[0].id }}"
- name: backendaddrpool1
load_balancer: "lb{{ rpfx }}"
- name: ipconfig2
private_ip_address: "{{ output.state.ip_configurations[1].private_ip_address }}"
public_ip_name: "tn{{ rpfx }}v6"
private_ip_address_version: 'IPv6'
load_balancer_backend_address_pools:
- "{{ lb.state.backend_address_pools[2].id }}"
- name: backendaddrpool2
load_balancer: "lb{{ rpfx }}"
- name: ipconfig1
public_ip_name: "tn{{ rpfx }}"
private_ip_address: "{{ output.state.ip_configurations[2].private_ip_address }}"
load_balancer_backend_address_pools:
- "{{ lb.state.backend_address_pools[0].id }}"
- name: backendaddrpool1
load_balancer: "lb{{ rpfx }}"
register: output

- name: Assert the NIC no change
Expand Down Expand Up @@ -481,6 +483,8 @@
create_with_security_group: true
ip_configurations:
- name: ipconfig1
private_ip_address: "{{ output.state.ip_configurations[0].private_ip_address }}"
private_ip_allocation_method: "{{ output.state.ip_configurations[0].private_ip_allocation_method }}"
application_security_groups:
- "{{ asg.id }}"
- "{{ applicationsecuritygroup_name1 }}"
Expand Down Expand Up @@ -615,6 +619,8 @@
ip_configurations:
- name: "default"
primary: true
private_ip_address: "{{ output.state.ip_configurations[0].private_ip_address }}"
private_ip_allocation_method: "{{ output.state.ip_configurations[0].private_ip_allocation_method }}"
application_gateway_backend_address_pools:
- name: "test_backend_address_pool"
application_gateway: "appgateway{{ rpfx }}"
Expand Down