def _return_formatted(self, kwargs): if 'invocation' not in kwargs: kwargs['invocation'] = {'module_args': self.params} if 'warnings' in kwargs: if isinstance(kwargs['warnings'], list): for w in kwargs['warnings']: self.warn(w) else: self.warn(kwargs['warnings']) if self.__warnings: kwargs['warnings'] = self.__warnings if 'deprecations' in kwargs: if isinstance(kwargs['deprecations'], list): for d in kwargs['deprecations']: if isinstance(d, SEQUENCETYPE) and len(d) == 2: self.deprecate(d[0], version=d[1]) elif isinstance(d, Mapping): self.deprecate(d['msg'], version=d.get('version'), date=d.get('date'), collection_name=d.get('collection_name')) else: self.deprecate(d) # pylint: disable=ansible-deprecated-no-version else: self.deprecate(kwargs['deprecations']) # pylint: disable=ansible-deprecated-no-version if self.__deprecations: kwargs['deprecations'] = self.__deprecations kwargs = remove_values(kwargs, self.no_log_values) raise _ModuleExitException(kwargs)
def test_hit_recursion_limit(self): """ Check that we do not hit a recursion limit""" data_list = [] inner_list = data_list for i in range(0, 10000): new_list = [] inner_list.append(new_list) inner_list = new_list inner_list.append('secret') # Check that this does not hit a recursion limit actual_data_list = remove_values(data_list, frozenset(('secret',))) levels = 0 inner_list = actual_data_list while inner_list: if isinstance(inner_list, list): self.assertEquals(len(inner_list), 1) else: levels -= 1 break inner_list = inner_list[0] levels += 1 self.assertEquals(inner_list, self.OMIT) self.assertEquals(levels, 10000)
def test_hit_recursion_limit(self): """ Check that we do not hit a recursion limit""" data_list = [] inner_list = data_list for i in range(0, 10000): new_list = [] inner_list.append(new_list) inner_list = new_list inner_list.append('secret') # Check that this does not hit a recursion limit actual_data_list = remove_values(data_list, frozenset(('secret', ))) levels = 0 inner_list = actual_data_list while inner_list: if isinstance(inner_list, list): self.assertEqual(len(inner_list), 1) else: levels -= 1 break inner_list = inner_list[0] levels += 1 self.assertEqual(inner_list, self.OMIT) self.assertEqual(levels, 10000)
def fail_json(self, **kwargs): ''' return from the module, with an error message ''' assert 'msg' in kwargs, "implementation error -- msg to explain the error is required" kwargs['failed'] = True if 'invocation' not in kwargs: kwargs['invocation'] = {'module_args': self.params} kwargs = remove_values(kwargs, self.no_log_values) raise AnsibleModuleExit(kwargs)
def exit_json(self, **kwargs): ''' return from the module, without error ''' if not 'changed' in kwargs: kwargs['changed'] = False if 'invocation' not in kwargs: kwargs['invocation'] = {'module_args': self.params} kwargs = remove_values(kwargs, self.no_log_values) raise AnsibleModuleExit(kwargs)
def config_fail(module, **result): """ return from the module, with an error message """ if HAS_ANSIBLE_MODULE_EXIT: assert 'msg' in result, "implementation error -- msg to explain the error is required" result['failed'] = True if 'invocation' not in result: result['invocation'] = {'module_args': module.params} result = remove_values(result, module.no_log_values) raise AnsibleModuleExit(result) else: module.fail_json(**result)
def config_exit(module, **result): """ return from the module, without error """ if HAS_ANSIBLE_MODULE_EXIT: if 'changed' not in result: result['changed'] = False if 'invocation' not in result: result['invocation'] = {'module_args': module.params} result = remove_values(result, module.no_log_values) raise AnsibleModuleExit(result) else: module.exit_json(**result)
def test_strings_to_remove(self): for value, no_log_strings, expected in self.dataset_remove: self.assertEquals(remove_values(value, no_log_strings), expected)
def test_no_removal(self): for value, no_log_strings in self.dataset_no_remove: self.assertEquals(remove_values(value, no_log_strings), value)
def test_strings_to_remove(self): for value, no_log_strings, expected in self.dataset_remove: self.assertEqual(remove_values(value, no_log_strings), expected)
def test_no_removal(self): for value, no_log_strings in self.dataset_no_remove: self.assertEqual(remove_values(value, no_log_strings), value)
def main(): argument_spec = openstack_full_argument_spec( server=dict(required=True), state=dict(default='present', choices=['absent', 'present']), network=dict(required=False, default=None), floating_ip_address=dict(required=False, default=None), reuse=dict(required=False, type='bool', default=False), fixed_address=dict(required=False, default=None), nat_destination=dict(required=False, default=None, aliases=['fixed_network', 'internal_network']), wait=dict(required=False, type='bool', default=False), timeout=dict(required=False, type='int', default=60), purge=dict(required=False, type='bool', default=False), ) module_kwargs = openstack_module_kwargs() module = AnsibleModule(argument_spec, **module_kwargs) server_name_or_id = module.params['server'] state = module.params['state'] network = module.params['network'] floating_ip_address = module.params['floating_ip_address'] reuse = module.params['reuse'] fixed_address = module.params['fixed_address'] nat_destination = module.params['nat_destination'] wait = module.params['wait'] timeout = module.params['timeout'] purge = module.params['purge'] sdk, cloud = openstack_cloud_from_module(module) try: server = cloud.get_server(server_name_or_id) if server is None: module.fail_json( msg="server {0} not found".format(server_name_or_id)) if state == 'present': # If f_ip already assigned to server, check that it matches # requirements. public_ip = cloud.get_server_public_ip(server) f_ip = _get_floating_ip(cloud, public_ip) if public_ip else public_ip if f_ip: if network: network_id = cloud.get_network(name_or_id=network)["id"] else: network_id = None # check if we have floating ip on given nat_destination network if nat_destination: nat_floating_addrs = [addr for addr in server.addresses.get( cloud.get_network(nat_destination)['name'], []) if addr['addr'] == public_ip and addr['OS-EXT-IPS:type'] == 'floating'] if len(nat_floating_addrs) == 0: module.fail_json(msg="server {server} already has a " "floating-ip on a different " "nat-destination than '{nat_destination}'" .format(server=server_name_or_id, nat_destination=nat_destination)) if all([fixed_address, f_ip.fixed_ip_address == fixed_address, network, f_ip.network != network_id]): # Current state definitely conflicts with requirements module.fail_json(msg="server {server} already has a " "floating-ip on requested " "interface but it doesn't match " "requested network {network}: {fip}" .format(server=server_name_or_id, network=network, fip=remove_values(f_ip, module.no_log_values))) if not network or f_ip.network == network_id: # Requirements are met module.exit_json(changed=False, floating_ip=f_ip) # Requirements are vague enough to ignore existing f_ip and try # to create a new f_ip to the server. server = cloud.add_ips_to_server( server=server, ips=floating_ip_address, ip_pool=network, reuse=reuse, fixed_address=fixed_address, wait=wait, timeout=timeout, nat_destination=nat_destination) fip_address = cloud.get_server_public_ip(server) # Update the floating IP status f_ip = _get_floating_ip(cloud, fip_address) module.exit_json(changed=True, floating_ip=f_ip) elif state == 'absent': if floating_ip_address is None: if not server_name_or_id: module.fail_json(msg="either server or floating_ip_address are required") server = cloud.get_server(server_name_or_id) floating_ip_address = cloud.get_server_public_ip(server) f_ip = _get_floating_ip(cloud, floating_ip_address) if not f_ip: # Nothing to detach module.exit_json(changed=False) changed = False if f_ip["fixed_ip_address"]: cloud.detach_ip_from_server( server_id=server['id'], floating_ip_id=f_ip['id']) # Update the floating IP status f_ip = cloud.get_floating_ip(id=f_ip['id']) changed = True if purge: cloud.delete_floating_ip(f_ip['id']) module.exit_json(changed=True) module.exit_json(changed=changed, floating_ip=f_ip) except sdk.exceptions.OpenStackCloudException as e: module.fail_json(msg=str(e), extra_data=e.extra_data)
def main(): argument_spec = openstack_full_argument_spec( server=dict(required=True), state=dict(default='present', choices=['absent', 'present']), network=dict(required=False, default=None), floating_ip_address=dict(required=False, default=None), reuse=dict(required=False, type='bool', default=False), fixed_address=dict(required=False, default=None), nat_destination=dict(required=False, default=None, aliases=['fixed_network', 'internal_network']), wait=dict(required=False, type='bool', default=False), timeout=dict(required=False, type='int', default=60), purge=dict(required=False, type='bool', default=False), ) module_kwargs = openstack_module_kwargs() module = AnsibleModule(argument_spec, **module_kwargs) if module.params['nat_destination']: min_version = '1.8.0' else: min_version = None server_name_or_id = module.params['server'] state = module.params['state'] network = module.params['network'] floating_ip_address = module.params['floating_ip_address'] reuse = module.params['reuse'] fixed_address = module.params['fixed_address'] nat_destination = module.params['nat_destination'] wait = module.params['wait'] timeout = module.params['timeout'] purge = module.params['purge'] shade, cloud = openstack_cloud_from_module(module, min_version=min_version) try: server = cloud.get_server(server_name_or_id) if server is None: module.fail_json( msg="server {0} not found".format(server_name_or_id)) if state == 'present': # If f_ip already assigned to server, check that it matches # requirements. public_ip = cloud.get_server_public_ip(server) f_ip = _get_floating_ip(cloud, public_ip) if public_ip else public_ip if f_ip: if network: network_id = cloud.get_network(name_or_id=network)["id"] else: network_id = None # check if we have floting ip on given nat_destination network if nat_destination: nat_floating_addrs = [addr for addr in server.addresses.get( cloud.get_network(nat_destination)['name'], []) if addr.addr == public_ip and addr['OS-EXT-IPS:type'] == 'floating'] if len(nat_floating_addrs) == 0: module.fail_json(msg="server {server} already has a " "floating-ip on a different " "nat-destination than '{nat_destination}'" .format(server=server_name_or_id, nat_destination=nat_destination)) if all([fixed_address, f_ip.fixed_ip_address == fixed_address, network, f_ip.network != network_id]): # Current state definitely conflicts with requirements module.fail_json(msg="server {server} already has a " "floating-ip on requested " "interface but it doesn't match " "requested network {network}: {fip}" .format(server=server_name_or_id, network=network, fip=remove_values(f_ip, module.no_log_values))) if not network or f_ip.network == network_id: # Requirements are met module.exit_json(changed=False, floating_ip=f_ip) # Requirements are vague enough to ignore existing f_ip and try # to create a new f_ip to the server. server = cloud.add_ips_to_server( server=server, ips=floating_ip_address, ip_pool=network, reuse=reuse, fixed_address=fixed_address, wait=wait, timeout=timeout, nat_destination=nat_destination) fip_address = cloud.get_server_public_ip(server) # Update the floating IP status f_ip = _get_floating_ip(cloud, fip_address) module.exit_json(changed=True, floating_ip=f_ip) elif state == 'absent': if floating_ip_address is None: if not server_name_or_id: module.fail_json(msg="either server or floating_ip_address are required") server = cloud.get_server(server_name_or_id) floating_ip_address = cloud.get_server_public_ip(server) f_ip = _get_floating_ip(cloud, floating_ip_address) if not f_ip: # Nothing to detach module.exit_json(changed=False) changed = False if f_ip["fixed_ip_address"]: cloud.detach_ip_from_server( server_id=server['id'], floating_ip_id=f_ip['id']) # Update the floating IP status f_ip = cloud.get_floating_ip(id=f_ip['id']) changed = True if purge: cloud.delete_floating_ip(f_ip['id']) module.exit_json(changed=True) module.exit_json(changed=changed, floating_ip=f_ip) except shade.OpenStackCloudException as e: module.fail_json(msg=str(e), extra_data=e.extra_data)
def main(): argument_spec = openstack_full_argument_spec( server=dict(required=True), state=dict(default='present', choices=['absent', 'present']), network=dict(required=False, default=None), floating_ip_address=dict(required=False, default=None), reuse=dict(required=False, type='bool', default=False), fixed_address=dict(required=False, default=None), nat_destination=dict(required=False, default=None, aliases=['fixed_network', 'internal_network']), wait=dict(required=False, type='bool', default=False), timeout=dict(required=False, type='int', default=60), purge=dict(required=False, type='bool', default=False), ) module_kwargs = openstack_module_kwargs() module = AnsibleModule(argument_spec, **module_kwargs) if not HAS_SHADE: module.fail_json(msg='shade is required for this module') if (module.params['nat_destination'] and StrictVersion(shade.__version__) < StrictVersion('1.8.0')): module.fail_json( msg="To utilize nat_destination, the installed version of" "the shade library MUST be >= 1.8.0") server_name_or_id = module.params['server'] state = module.params['state'] network = module.params['network'] floating_ip_address = module.params['floating_ip_address'] reuse = module.params['reuse'] fixed_address = module.params['fixed_address'] nat_destination = module.params['nat_destination'] wait = module.params['wait'] timeout = module.params['timeout'] purge = module.params['purge'] cloud = shade.openstack_cloud(**module.params) try: server = cloud.get_server(server_name_or_id) if server is None: module.fail_json( msg="server {0} not found".format(server_name_or_id)) if state == 'present': # If f_ip already assigned to server, check that it matches # requirements. public_ip = cloud.get_server_public_ip(server) f_ip = _get_floating_ip(cloud, public_ip) if public_ip else public_ip if f_ip: if network: network_id = cloud.get_network(name_or_id=network)["id"] else: network_id = None if all([ (fixed_address and f_ip.fixed_ip_address == fixed_address) or (nat_destination and f_ip.internal_network == fixed_address), network, f_ip.network != network_id ]): # Current state definitely conflicts with requirements module.fail_json( msg="server {server} already has a " "floating-ip on requested " "interface but it doesn't match " "requested network {network}: {fip}".format( server=server_name_or_id, network=network, fip=remove_values(f_ip, module.no_log_values))) if not network or f_ip.network == network_id: # Requirements are met module.exit_json(changed=False, floating_ip=f_ip) # Requirements are vague enough to ignore existing f_ip and try # to create a new f_ip to the server. server = cloud.add_ips_to_server(server=server, ips=floating_ip_address, ip_pool=network, reuse=reuse, fixed_address=fixed_address, wait=wait, timeout=timeout, nat_destination=nat_destination) fip_address = cloud.get_server_public_ip(server) # Update the floating IP status f_ip = _get_floating_ip(cloud, fip_address) module.exit_json(changed=True, floating_ip=f_ip) elif state == 'absent': if floating_ip_address is None: if not server_name_or_id: module.fail_json( msg="either server or floating_ip_address are required" ) server = cloud.get_server(server_name_or_id) floating_ip_address = cloud.get_server_public_ip(server) f_ip = _get_floating_ip(cloud, floating_ip_address) if not f_ip: # Nothing to detach module.exit_json(changed=False) changed = False if f_ip["fixed_ip_address"]: cloud.detach_ip_from_server(server_id=server['id'], floating_ip_id=f_ip['id']) # Update the floating IP status f_ip = cloud.get_floating_ip(id=f_ip['id']) changed = True if purge: cloud.delete_floating_ip(f_ip['id']) module.exit_json(changed=True) module.exit_json(changed=changed, floating_ip=f_ip) except shade.OpenStackCloudException as e: module.fail_json(msg=str(e), extra_data=e.extra_data)