Skip to content

Commit

Permalink
Add filters to os_router and os_subnet Fixes #37921 (#47220)
Browse files Browse the repository at this point in the history
  • Loading branch information
racciari authored and ansibot committed Nov 24, 2018
1 parent 704d564 commit 4b1b0bc
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 19 deletions.
22 changes: 11 additions & 11 deletions lib/ansible/modules/cloud/openstack/os_router.py
Expand Up @@ -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']:
Expand All @@ -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
Expand Down Expand Up @@ -283,15 +283,15 @@ 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:
return True
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


Expand Down Expand Up @@ -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 = []
Expand All @@ -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'])
Expand Down Expand Up @@ -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':
Expand All @@ -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)

Expand All @@ -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
Expand Down
16 changes: 8 additions & 8 deletions lib/ansible/modules/cloud/openstack/os_subnet.py
Expand Up @@ -145,15 +145,15 @@
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'])
ipv6_ra_mode = module.params['ipv6_ra_mode']
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:
Expand All @@ -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']
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand All @@ -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,
Expand Down

0 comments on commit 4b1b0bc

Please sign in to comment.