def map_params_to_obj(module, required_if=None): obj = [] addr6 = False aggregate = module.params.get('aggregate') if aggregate: for item in aggregate: if item['name'] is not None and validate_ip_v6_address( item['name']): addr6 = True for key in item: if item.get(key) is None: item[key] = module.params[key] check_required_if(module, required_if, item) item.update({'addr6': addr6}) d = item.copy() d['level'] = set(d['level']) if d['level'] is not None else None if d['dest'] != 'host': d['name'] = None d['udp_port'] = None if d['dest'] != 'buffered': d['level'] = None del d['check_running_config'] obj.append(d) else: if module.params['name'] is not None and validate_ip_v6_address( module.params['name']): addr6 = True if module.params['dest'] != 'host': module.params['name'] = None module.params['udp_port'] = None if module.params['dest'] != 'buffered': module.params['level'] = None obj.append({ 'dest': module.params['dest'], 'name': module.params['name'], 'udp_port': module.params['udp_port'], 'level': set(module.params['level']) if module.params['level'] else None, 'facility': module.params['facility'], 'state': module.params['state'], 'addr6': addr6 }) return obj
def is_valid_ip(addr, type='all'): if type in ['all', 'ipv4']: if validate_ip_address(addr): return True if type in ['all', 'ipv6']: if validate_ip_v6_address(addr): return True return False
def check_ip_addr_type(obj_filter, ib_spec): '''This function will check if the argument ip is type v4/v6 and return appropriate infoblox network/networkcontainer type ''' ip = obj_filter['network'] if 'container' in obj_filter and obj_filter['container']: check_ip = ip.split('/') del ib_spec['container'] # removing the container key from post arguments del ib_spec['options'] # removing option argument as for network container it's not supported if validate_ip_address(check_ip[0]): return NIOS_IPV4_NETWORK_CONTAINER, ib_spec elif validate_ip_v6_address(check_ip[0]): return NIOS_IPV6_NETWORK_CONTAINER, ib_spec else: check_ip = ip.split('/') del ib_spec['container'] # removing the container key from post arguments if validate_ip_address(check_ip[0]): return NIOS_IPV4_NETWORK, ib_spec elif validate_ip_v6_address(check_ip[0]): return NIOS_IPV6_NETWORK, ib_spec
def _set_config(self, want, have): # Set the interface config based on the want and have config commands = [] interface = 'interface ' + want['name'] if want == have: return if want.get('ipv4'): haddr = have.get('ipv4') if not haddr: # if interface has no address, have empty list haddr = [] for waddr in want.get('ipv4'): waddr = remove_empties(waddr) if waddr not in haddr: # if wanted address not yet configured, add if waddr.get('address') != 'dhcp': ipv4_addr = waddr['address'].split('/') if len(ipv4_addr) != 2: # validate ip address self._module.fail_json(msg='Invalid format') if not validate_ip_address( ipv4_addr[0]) or not is_masklen(ipv4_addr[1]): self._module.fail_json(msg='Invalid IP address') cmd = 'ip address {0}'.format(waddr['address']) if waddr.get('secondary'): cmd += ' secondary' elif waddr.get('address') == 'dhcp': cmd = 'ip address dhcp' if waddr.get('dhcp_client'): cmd += ' client-id vlan{0}'.format( waddr.get('dhcp_client')) if waddr.get('dhcp_hostname'): cmd += ' hostname {0}'.format( waddr.get('dhcp_hostname')) add_command_to_config_list(interface, cmd, commands) if want.get('ipv6'): haddr = have.get('ipv6') if not haddr: haddr = [] for waddr in want.get('ipv6'): waddr = remove_empties(waddr) if waddr not in haddr: ipv6_addr = waddr['address'].split('/') if len(ipv6_addr) != 2: # validate ipv6 address self._module.fail_json(msg='Invalid format') if not validate_ip_v6_address( ipv6_addr[0]) or int(ipv6_addr[1]) > 64: self._module.fail_json(msg='Invalid IP address') cmd = 'ipv6 address {0}'.format(waddr['address']) add_command_to_config_list(interface, cmd, commands) return commands
def validate_ip_addr_type(ip, arg_spec, module): '''This function will check if the argument ip is type v4/v6 and return appropriate infoblox network type ''' check_ip = ip.split('/') if validate_ip_address(check_ip[0]) and 'ipaddr' in arg_spec: arg_spec['ipv4addr'] = arg_spec.pop('ipaddr') module.params['ipv4addr'] = module.params.pop('ipaddr') return NIOS_IPV4_FIXED_ADDRESS, arg_spec, module elif validate_ip_v6_address(check_ip[0]) and 'ipaddr' in arg_spec: arg_spec['ipv6addr'] = arg_spec.pop('ipaddr') module.params['ipv6addr'] = module.params.pop('ipaddr') return NIOS_IPV6_FIXED_ADDRESS, arg_spec, module
def __init__(self, argument_spec): self.spec = argument_spec self.module = None self.init_module() # file copy parameters self.local_file = self.module.params['local_file'] self.remote_file = self.module.params['remote_file'] self.file_system = self.module.params['file_system'] self.host_is_ipv6 = validate_ip_v6_address(self.module.params['provider']['host']) # state self.transfer_result = None self.changed = False
def _get_rdns(self): ip_address = self.module.params.get("ip_address") if utils.validate_ip_address(ip_address): if self.module.params.get("server"): if self.hcloud_resource.public_net.ipv4.ip == ip_address: self.hcloud_rdns = { "ip_address": self.hcloud_resource.public_net.ipv4.ip, "dns_ptr": self.hcloud_resource.public_net.ipv4.dns_ptr, } else: self.module.fail_json( msg="The selected server does not have this IP address" ) elif self.module.params.get("floating_ip"): if self.hcloud_resource.ip == ip_address: self.hcloud_rdns = { "ip_address": self.hcloud_resource.ip, "dns_ptr": self.hcloud_resource.dns_ptr[0]["dns_ptr"], } else: self.module.fail_json( msg= "The selected Floating IP does not have this IP address" ) elif utils.validate_ip_v6_address(ip_address): if self.module.params.get("server"): for ipv6_address_dns_ptr in self.hcloud_resource.public_net.ipv6.dns_ptr: if ipv6_address_dns_ptr["ip"] == ip_address: self.hcloud_rdns = { "ip_address": ipv6_address_dns_ptr["ip"], "dns_ptr": ipv6_address_dns_ptr["dns_ptr"], } elif self.module.params.get("floating_ip"): for ipv6_address_dns_ptr in self.hcloud_resource.dns_ptr: if ipv6_address_dns_ptr["ip"] == ip_address: self.hcloud_rdns = { "ip_address": ipv6_address_dns_ptr["ip"], "dns_ptr": ipv6_address_dns_ptr["dns_ptr"], } else: self.module.fail_json(msg="The given IP address is not valid")
def map_obj_to_commands(want, have, module): commands = list() state = module.params['state'] def needs_update(x): return want.get(x) is not None and (want.get(x) != have.get(x)) if state == 'absent': if have['name_servers'] == [] and have['aaa_servers'] == [] and have[ 'domain_search'] == [] and have['hostname'] is None: if want['hostname']: commands.append('no hostname') if want['domain_search']: for item in want['domain_search']: commands.append('no ip dns domain-list %s' % item) if want['name_servers']: for item in want['name_servers']: commands.append('no ip dns server-address %s' % item) if want['aaa_servers']: want_servers = [] want_server = want['aaa_servers'] if want_server: want_list = deepcopy(want_server) for items in want_list: items['auth_key'] = None want_servers.append(items) for item in want_servers: ipv6addr = validate_ip_v6_address(item['hostname']) if ipv6addr: commands.append('no ' + item['type'] + '-server host ipv6 ' + item['hostname']) else: commands.append('no ' + item['type'] + '-server host ' + item['hostname']) if want['hostname']: if have['hostname'] == want['hostname']: commands.append('no hostname') if want['domain_search']: for item in want['domain_search']: if item in have['domain_search']: commands.append('no ip dns domain-list %s' % item) if want['name_servers']: for item in want['name_servers']: if item in have['name_servers']: commands.append('no ip dns server-address %s' % item) if want['aaa_servers']: want_servers = [] want_server = want['aaa_servers'] have_server = have['aaa_servers'] if want_server: want_list = deepcopy(want_server) for items in want_list: items['auth_key'] = None want_servers.append(items) for item in want_servers: if item in have_server: ipv6addr = validate_ip_v6_address(item['hostname']) if ipv6addr: commands.append('no ' + item['type'] + '-server host ipv6 ' + item['hostname']) else: commands.append('no ' + item['type'] + '-server host ' + item['hostname']) elif state == 'present': if needs_update('hostname'): commands.append('hostname %s' % want['hostname']) if want['domain_search']: adds, removes = diff_list(want['domain_search'], have['domain_search']) for item in removes: commands.append('no ip dns domain-list %s' % item) for item in adds: commands.append('ip dns domain-list %s' % item) if want['name_servers']: adds, removes = diff_list(want['name_servers'], have['name_servers']) for item in removes: commands.append('no ip dns server-address %s' % item) for item in adds: commands.append('ip dns server-address %s' % item) if want['aaa_servers']: want_servers = [] want_server = want['aaa_servers'] have_server = have['aaa_servers'] want_list = deepcopy(want_server) for items in want_list: items['auth_key'] = None want_servers.append(items) adds, removes = diff_list(want_servers, have_server) for item in removes: ip6addr = validate_ip_v6_address(item['hostname']) if ip6addr: cmd = 'no ' + item['type'] + '-server host ipv6 ' + item[ 'hostname'] else: cmd = 'no ' + item['type'] + '-server host ' + item[ 'hostname'] commands.append(cmd) for w_item in adds: for item in want_server: if item['hostname'] == w_item['hostname'] and item[ 'type'] == w_item['type']: auth_key = item['auth_key'] ip6addr = validate_ip_v6_address(w_item['hostname']) if ip6addr: cmd = w_item['type'] + '-server host ipv6 ' + w_item[ 'hostname'] else: cmd = w_item['type'] + '-server host ' + w_item['hostname'] if w_item['auth_port_type']: cmd += ' ' + w_item['auth_port_type'] + ' ' + w_item[ 'auth_port_num'] if w_item['acct_port_num'] and w_item['type'] == 'radius': cmd += ' acct-port ' + w_item['acct_port_num'] if w_item['type'] == 'tacacs': if any((w_item['acct_port_num'], w_item['auth_key_type'])): module.fail_json( msg= 'acct_port and auth_key_type is not applicable for tacacs server' ) if w_item['acct_type']: cmd += ' ' + w_item['acct_type'] if auth_key is not None: cmd += ' key ' + auth_key if w_item['auth_key_type'] and w_item['type'] == 'radius': val = '' for y in w_item['auth_key_type']: val = val + ' ' + y cmd += val commands.append(cmd) return commands
def map_obj_to_commands(updates): dest_group = ('host', 'console', 'persistence', 'enable') commands = list() want, have = updates for w in want: dest = w['dest'] name = w['name'] level = w['level'] state = w['state'] udp_port = w['udp_port'] facility = w['facility'] del w['state'] del w['facility'] facility_name = '' facility_level = '' if name is not None and validate_ip_v6_address(name): name = 'ipv6 ' + name if facility: for item in have: if item['dest'] == 'facility': facility_name = item['dest'] facility_level = item['facility'] if state == 'absent': if have == []: if facility: commands.append('no logging facility') if dest == 'buffered': for item in have: if item['dest'] == 'buffered': want_level = level have_level = item['level'] for item in want_level: commands.append('no logging buffered {0}'.format(item)) if dest == 'host': if name and udp_port: commands.append('no logging host {0} udp-port {1}'.format(name, udp_port)) elif name: commands.append('no logging host {0}'.format(name)) else: if dest == 'rfc5424': commands.append('no logging enable {0}'.format(dest)) else: if dest != 'buffered': commands.append('no logging {0}'.format(dest)) if facility: if facility_name == 'facility' and facility_level != 'user': commands.append('no logging facility') if dest == 'buffered': for item in have: if item['dest'] == 'buffered': want_level = level have_level = item['level'] for item in want_level: if item in have_level: commands.append('no logging buffered {0}'.format(item)) if w in have: if dest == 'host': if name and udp_port: commands.append('no logging host {0} udp-port {1}'.format(name, udp_port)) elif name: commands.append('no logging host {0}'.format(name)) else: if dest == 'rfc5424': commands.append('no logging enable {0}'.format(dest)) else: if dest != 'buffered': commands.append('no logging {0}'.format(dest)) if state == 'present': if facility: if facility != facility_level: commands.append('logging facility {0}'.format(facility)) if w not in have: if dest == 'host': if name and udp_port: commands.append('logging host {0} udp-port {1}'.format(name, udp_port)) elif name: commands.append('logging host {0}'.format(name)) elif dest == 'buffered': adds, removes = diff_in_list(want, have) for item in adds: commands.append('logging buffered {0}'.format(item)) for item in removes: commands.append('no logging buffered {0}'.format(item)) elif dest == 'rfc5424': commands.append('logging enable {0}'.format(dest)) else: commands.append('logging {0}'.format(dest)) return commands