Add filters to os_router and os_subnet Fixes #37921 (#47220)

This commit is contained in:
Romain Acciari 2018-11-24 14:07:37 +01:00 committed by ansibot
parent 704d5642f3
commit 4b1b0bcbb6
2 changed files with 19 additions and 19 deletions

View file

@ -221,7 +221,7 @@ def _router_internal_interfaces(cloud, router):
yield port
def _needs_update(cloud, module, router, network, internal_subnet_ids, internal_port_ids):
def _needs_update(cloud, module, router, network, internal_subnet_ids, internal_port_ids, filters=None):
"""Decide if the given router needs an update.
"""
if router['admin_state_up'] != module.params['admin_state_up']:
@ -240,7 +240,7 @@ def _needs_update(cloud, module, router, network, internal_subnet_ids, internal_
# check external interfaces
if module.params['external_fixed_ips']:
for new_iface in module.params['external_fixed_ips']:
subnet = cloud.get_subnet(new_iface['subnet'])
subnet = cloud.get_subnet(new_iface['subnet'], filters)
exists = False
# compare the requested interface with existing, looking for an existing match
@ -283,7 +283,7 @@ def _needs_update(cloud, module, router, network, internal_subnet_ids, internal_
return False
def _system_state_change(cloud, module, router, network, internal_ids, internal_portids):
def _system_state_change(cloud, module, router, network, internal_ids, internal_portids, filters=None):
"""Check if the system state would be changed."""
state = module.params['state']
if state == 'absent' and router:
@ -291,7 +291,7 @@ def _system_state_change(cloud, module, router, network, internal_ids, internal_
if state == 'present':
if not router:
return True
return _needs_update(cloud, module, router, network, internal_ids, internal_portids)
return _needs_update(cloud, module, router, network, internal_ids, internal_portids, filters)
return False
@ -323,7 +323,7 @@ def _build_kwargs(cloud, module, router, network):
return kwargs
def _validate_subnets(module, cloud):
def _validate_subnets(module, cloud, filters=None):
external_subnet_ids = []
internal_subnet_ids = []
internal_port_ids = []
@ -339,12 +339,12 @@ def _validate_subnets(module, cloud):
if module.params['interfaces']:
for iface in module.params['interfaces']:
if isinstance(iface, str):
subnet = cloud.get_subnet(iface)
subnet = cloud.get_subnet(iface, filters)
if not subnet:
module.fail_json(msg='subnet %s not found' % iface)
internal_subnet_ids.append(subnet['id'])
elif isinstance(iface, dict):
subnet = cloud.get_subnet(iface['subnet'])
subnet = cloud.get_subnet(iface['subnet'], filters)
if not subnet:
module.fail_json(msg='subnet %s not found' % iface['subnet'])
net = cloud.get_network(iface['net'])
@ -414,10 +414,10 @@ def main():
# Validate and cache the subnet IDs so we can avoid duplicate checks
# and expensive API calls.
external_ids, subnet_internal_ids, internal_portids = _validate_subnets(module, cloud)
external_ids, subnet_internal_ids, internal_portids = _validate_subnets(module, cloud, filters)
if module.check_mode:
module.exit_json(
changed=_system_state_change(cloud, module, router, net, subnet_internal_ids, internal_portids)
changed=_system_state_change(cloud, module, router, net, subnet_internal_ids, internal_portids, filters)
)
if state == 'present':
@ -436,7 +436,7 @@ def main():
cloud.add_router_interface(router, port_id=int_p_id)
changed = True
else:
if _needs_update(cloud, module, router, net, subnet_internal_ids, internal_portids):
if _needs_update(cloud, module, router, net, subnet_internal_ids, internal_portids, filters):
kwargs = _build_kwargs(cloud, module, router, net)
updated_router = cloud.update_router(**kwargs)
@ -453,7 +453,7 @@ def main():
for port in ports:
cloud.remove_router_interface(router, port_id=port['id'])
if internal_portids:
external_ids, subnet_internal_ids, internal_portids = _validate_subnets(module, cloud)
external_ids, subnet_internal_ids, internal_portids = _validate_subnets(module, cloud, filters)
for int_p_id in internal_portids:
cloud.add_router_interface(router, port_id=int_p_id)
changed = True

View file

@ -145,7 +145,7 @@ from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.openstack import openstack_full_argument_spec, openstack_module_kwargs, openstack_cloud_from_module
def _can_update(subnet, module, cloud):
def _can_update(subnet, module, cloud, filters=None):
"""Check for differences in non-updatable values"""
network_name = module.params['network_name']
ip_version = int(module.params['ip_version'])
@ -153,7 +153,7 @@ def _can_update(subnet, module, cloud):
ipv6_a_mode = module.params['ipv6_address_mode']
if network_name:
network = cloud.get_network(network_name)
network = cloud.get_network(network_name, filters)
if network:
netid = network['id']
else:
@ -170,11 +170,11 @@ def _can_update(subnet, module, cloud):
subnet')
def _needs_update(subnet, module, cloud):
def _needs_update(subnet, module, cloud, filters=None):
"""Check for differences in the updatable values."""
# First check if we are trying to update something we're not allowed to
_can_update(subnet, module, cloud)
_can_update(subnet, module, cloud, filters)
# now check for the things we are allowed to update
enable_dhcp = module.params['enable_dhcp']
@ -209,12 +209,12 @@ def _needs_update(subnet, module, cloud):
return False
def _system_state_change(module, subnet, cloud):
def _system_state_change(module, subnet, cloud, filters=None):
state = module.params['state']
if state == 'present':
if not subnet:
return True
return _needs_update(subnet, module, cloud)
return _needs_update(subnet, module, cloud, filters)
if state == 'absent' and subnet:
return True
return False
@ -299,7 +299,7 @@ def main():
if module.check_mode:
module.exit_json(changed=_system_state_change(module, subnet,
cloud))
cloud, filters))
if state == 'present':
if not subnet:
@ -326,7 +326,7 @@ def main():
subnet = cloud.create_subnet(network_name, **kwargs)
changed = True
else:
if _needs_update(subnet, module, cloud):
if _needs_update(subnet, module, cloud, filters):
cloud.update_subnet(subnet['id'],
subnet_name=subnet_name,
enable_dhcp=enable_dhcp,