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(ip): '''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]): return NIOS_IPV4_NETWORK elif validate_ip_v6_address(check_ip[0]): return NIOS_IPV6_NETWORK
def destination(self): if self.address is None: return None if self.port is None: return None if validate_ip_v6_address(self.address): result = '{0}.{1}'.format(self.address, self.port) else: result = '{0}:{1}'.format(self.address, self.port) return result
def update(self): self.have = self.read_current_from_device() if not self.should_update(): return False if self.want.reverse == 'enabled': if not self.want.receive and not self.have.receive: raise F5ModuleError( "A 'receive' string must be specified when setting 'reverse'." ) if self.want.time_until_up != 0 and self.have.time_until_up != 0: raise F5ModuleError( "Monitors with the 'reverse' attribute are not currently compatible with 'time_until_up'." ) if ((self.want.receive is not None and validate_ip_v6_address( self.want.receive) or self.have.receive is not None and validate_ip_v6_address(self.have.receive)) and (self.want.query_type == 'a' and self.have.query_type == 'a')): raise F5ModuleError( "Monitor has a IPv6 address. Only a 'query_type' of 'aaaa' is supported for IPv6." ) elif ((self.want.receive is not None and validate_ip_address( self.want.receive) or self.have.receive is not None and validate_ip_address(self.have.receive)) and (self.want.query_type == 'aaaa' and self.have.query_type == 'aaaa')): raise F5ModuleError( "Monitor has a IPv4 address. Only a 'query_type' of 'a' is supported for IPv4." ) if self.want.accept_rcode == 'anything': if self.want.receive is not None and is_valid_ip( self.want.receive) and self.have.receive is not None: raise F5ModuleError( "No 'receive' string may be specified, or exist, when 'accept_rcode' is 'anything'." ) elif self.want.receive is None and self.have.receive is not None: self.want.update({'receive': ''}) if self.module.check_mode: return True self.update_on_device() return True
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 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 _get_rdns(self): ip_address = self.module.params.get("ip_address") if utils.validate_ip_address(ip_address): if self.hcloud_server.public_net.ipv4.ip == ip_address: self.hcloud_rdns = { "ip_address": self.hcloud_server.public_net.ipv4.ip, "dns_ptr": self.hcloud_server.public_net.ipv4.dns_ptr, } else: self.module.fail_json(msg="The selected server does not have this IP address") elif utils.validate_ip_v6_address(ip_address): for ipv6_address_dns_ptr in self.hcloud_server.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"], } else: self.module.fail_json(msg="The given IP address is not valid")
def create(self): self._set_changed_options() if self.want.reverse == 'enabled': if self.want.time_until_up != 0: raise F5ModuleError( "Monitors with the 'reverse' attribute are not currently compatible with 'time_until_up'." ) if not self.want.receive: raise F5ModuleError( "A 'receive' string must be specified when setting 'reverse'." ) if (self.want.receive is not None and validate_ip_v6_address(self.want.receive) and self.want.query_type == 'a'): raise F5ModuleError( "Monitor has a IPv6 address. Only a 'query_type' of 'aaaa' is supported for IPv6." ) elif (self.want.receive is not None and validate_ip_address(self.want.receive) and self.want.query_type == 'aaaa'): raise F5ModuleError( "Monitor has a IPv4 address. Only a 'query_type' of 'a' is supported for IPv4." ) if self.want.accept_rcode == 'anything': if self.want.receive is not None and is_valid_ip( self.want.receive): raise F5ModuleError( "No 'receive' string may be specified, or exist, when 'accept_rcode' is 'anything'." ) elif self.want.receive is None: self.want.update({'receive': ''}) if self.want.query_name is None: raise F5ModuleError( "'query_name' is required when creating a new DNS monitor.") if self.module.check_mode: return True self.create_on_device() return True
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
def full_name(self): delimiter = ':' if validate_ip_v6_address(self.full_name_dict['name']): delimiter = '.' return '{0}{1}{2}'.format(self.full_name_dict['name'], delimiter, self.port)