def map_obj_to_xml_rpc(self):
        state = self._module.params['state']
        _get_filter = build_xml('banners', xmap=self._banners_meta, params=self._module.params, opcode="filter")

        running = get_config(self._module, source='running', config_filter=_get_filter)

        banner_name = None
        banner_text = None
        if etree_find(running, 'banner-text') is not None:
            banner_name = etree_find(running, 'banner-name').text
            banner_text = etree_find(running, 'banner-text').text

        opcode = None
        if state == 'absent' and banner_name == self._module.params['banner'] and len(banner_text):
            opcode = "delete"
        elif state == 'present':
            opcode = 'merge'

        self._result['xml'] = []
        if opcode:
            _edit_filter = build_xml('banners', xmap=self._banners_meta, params=self._module.params, opcode=opcode)

            if _edit_filter is not None:
                commit = not self._module.check_mode
                diff = load_config(self._module, _edit_filter, commit=commit, running=running, nc_get_filter=_get_filter)

                if diff:
                    self._result['xml'] = _edit_filter
                    if self._module._diff:
                        self._result['diff'] = dict(prepared=diff)

                    self._result['changed'] = True
Esempio n. 2
0
    def get_device_info(self):
        device_info = {}
        device_info['network_os'] = 'iosxr'
        install_meta = collections.OrderedDict()
        install_meta.update([
            ('boot-variables', {'xpath': 'install/boot-variables', 'tag': True}),
            ('boot-variable', {'xpath': 'install/boot-variables/boot-variable', 'tag': True, 'lead': True}),
            ('software', {'xpath': 'install/software', 'tag': True}),
            ('alias-devices', {'xpath': 'install/software/alias-devices', 'tag': True}),
            ('alias-device', {'xpath': 'install/software/alias-devices/alias-device', 'tag': True}),
            ('m:device-name', {'xpath': 'install/software/alias-devices/alias-device/device-name', 'value': 'disk0:'}),
        ])

        install_filter = build_xml('install', install_meta, opcode='filter')

        reply = self.get(install_filter)
        ele_boot_variable = etree_find(reply, 'boot-variable/boot-variable')
        if ele_boot_variable is not None:
            device_info['network_os_image'] = re.split('[:|,]', ele_boot_variable.text)[1]
        ele_package_name = etree_find(reply, 'package-name')
        if ele_package_name is not None:
            device_info['network_os_package'] = ele_package_name.text
            device_info['network_os_version'] = re.split('-', ele_package_name.text)[-1]

        hostname_filter = build_xml('host-names', opcode='filter')

        reply = self.get(hostname_filter)
        hostname_ele = etree_find(reply, 'host-name')
        device_info['network_os_hostname'] = hostname_ele.text if hostname_ele is not None else None

        return device_info
Esempio n. 3
0
    def get_device_info(self):
        device_info = {}
        device_info['network_os'] = 'iosxr'
        install_meta = collections.OrderedDict()
        install_meta.update([
            ('boot-variables', {'xpath': 'install/boot-variables', 'tag': True}),
            ('boot-variable', {'xpath': 'install/boot-variables/boot-variable', 'tag': True, 'lead': True}),
            ('software', {'xpath': 'install/software', 'tag': True}),
            ('alias-devices', {'xpath': 'install/software/alias-devices', 'tag': True}),
            ('alias-device', {'xpath': 'install/software/alias-devices/alias-device', 'tag': True}),
            ('m:device-name', {'xpath': 'install/software/alias-devices/alias-device/device-name', 'value': 'disk0:'}),
        ])

        install_filter = build_xml('install', install_meta, opcode='filter')

        reply = self.get(install_filter)
        ele_boot_variable = etree.fromstring(reply).find('.//boot-variable/boot-variable')
        if ele_boot_variable is not None:
            device_info['network_os_image'] = re.split('[:|,]', ele_boot_variable.text)[1]
        ele_package_name = etree.fromstring(reply).find('.//package-name')
        if ele_package_name is not None:
            device_info['network_os_package'] = ele_package_name.text
            device_info['network_os_version'] = re.split('-', ele_package_name.text)[-1]

        hostname_filter = build_xml('host-names', opcode='filter')

        reply = self.get(hostname_filter)
        hostname_ele = etree.fromstring(reply).find('.//host-name')
        device_info['network_os_hostname'] = hostname_ele.text if hostname_ele is not None else None

        return device_info
Esempio n. 4
0
    def get_device_info(self):
        device_info = {}
        device_info['network_os'] = 'iosxr'
        install_meta = collections.OrderedDict()
        install_meta.update([
            ('boot-variables', {
                'xpath': 'install/boot-variables',
                'tag': True
            }),
            ('boot-variable', {
                'xpath': 'install/boot-variables/boot-variable',
                'tag': True,
                'lead': True
            }),
            ('software', {
                'xpath': 'install/software',
                'tag': True
            }),
            ('alias-devices', {
                'xpath': 'install/software/alias-devices',
                'tag': True
            }),
            ('alias-device', {
                'xpath': 'install/software/alias-devices/alias-device',
                'tag': True
            }),
            ('m:device-name', {
                'xpath':
                'install/software/alias-devices/alias-device/device-name',
                'value': 'disk0:'
            }),
        ])

        install_filter = build_xml('install', install_meta, opcode='filter')
        try:
            reply = self.get(install_filter)
            resp = remove_namespaces(
                re.sub(r'<\?xml version="1.0" encoding="UTF-8"\?>', '', reply))
            ele_boot_variable = etree_find(resp, 'boot-variable/boot-variable')
            if ele_boot_variable is not None:
                device_info['network_os_image'] = re.split(
                    '[:|,]', ele_boot_variable.text)[1]
            ele_package_name = etree_find(reply, 'package-name')
            if ele_package_name is not None:
                device_info['network_os_package'] = ele_package_name.text
                device_info['network_os_version'] = re.split(
                    '-', ele_package_name.text)[-1]

            hostname_filter = build_xml('host-names', opcode='filter')
            reply = self.get(hostname_filter)
            resp = remove_namespaces(
                re.sub(r'<\?xml version="1.0" encoding="UTF-8"\?>', '', reply))
            hostname_ele = etree_find(resp.strip(), 'host-name')
            device_info[
                'network_os_hostname'] = hostname_ele.text if hostname_ele is not None else None
        except Exception as exc:
            self._connection.queue_message(
                'vvvv', 'Fail to retrieve device info %s' % exc)
        return device_info
Esempio n. 5
0
    def map_obj_to_commands(self):
        state = self._module.params['state']
        _get_filter = build_xml('banners',
                                xmap=self._banners_meta,
                                params=self._module.params,
                                opcode="filter")

        running = get_config(self._module,
                             source='running',
                             config_filter=_get_filter)

        banner_name = None
        banner_text = None
        if etree_find(running, 'banner-text') is not None:
            banner_name = etree_find(running, 'banner-name').text
            banner_text = etree_find(running, 'banner-text').text

        opcode = None
        if state == 'absent' and banner_name == self._module.params[
                'banner'] and len(banner_text):
            opcode = "delete"
        elif state == 'present':
            opcode = 'merge'

        self._result['commands'] = []
        if opcode:
            _edit_filter = build_xml('banners',
                                     xmap=self._banners_meta,
                                     params=self._module.params,
                                     opcode=opcode)

            if _edit_filter is not None:
                if not self._module.check_mode:
                    load_config(self._module, _edit_filter,
                                self._result['warnings'])
                    candidate = get_config(self._module,
                                           source='candidate',
                                           config_filter=_get_filter)

                    diff = get_config_diff(self._module, running, candidate)
                    if diff:
                        commit_config(self._module)
                        self._result['changed'] = True
                        self._result['commands'] = _edit_filter
                        if self._module._diff:
                            self._result['diff'] = {'prepared': diff}
                    else:
                        discard_config(self._module)
Esempio n. 6
0
    def map_obj_to_xml_rpc(self):
        self._locald_meta.update([
            ('aaa_locald', {
                'xpath': 'aaa/usernames',
                'tag': True,
                'ns': True
            }),
            ('username', {
                'xpath': 'aaa/usernames/username',
                'tag': True,
                'attrib': "operation"
            }),
            ('a:name', {
                'xpath': 'aaa/usernames/username/name'
            }),
            ('a:configured_password', {
                'xpath': 'aaa/usernames/username/secret',
                'operation': 'edit'
            }),
        ])

        self._locald_group_meta.update([
            ('aaa_locald', {
                'xpath': 'aaa/usernames',
                'tag': True,
                'ns': True
            }),
            ('username', {
                'xpath': 'aaa/usernames/username',
                'tag': True,
                'attrib': "operation"
            }),
            ('a:name', {
                'xpath': 'aaa/usernames/username/name'
            }),
            ('usergroups', {
                'xpath': 'aaa/usernames/username/usergroup-under-usernames',
                'tag': True,
                'operation': 'edit'
            }),
            ('usergroup', {
                'xpath':
                'aaa/usernames/username/usergroup-under-usernames/usergroup-under-username',
                'tag': True,
                'operation': 'edit'
            }),
            ('a:group', {
                'xpath':
                'aaa/usernames/username/usergroup-under-usernames/usergroup-under-username/name',
                'operation': 'edit'
            }),
        ])

        state = self._module.params['state']
        _get_filter = build_xml('aaa', opcode="filter")
        running = get_config(self._module,
                             source='running',
                             config_filter=_get_filter)

        elements = etree_findall(running, 'username')
        users = list()
        for element in elements:
            name_list = etree_findall(element, 'name')
            users.append(name_list[0].text)
            list_size = len(name_list)
            if list_size == 1:
                self._have.append({
                    'name': name_list[0].text,
                    'group': None,
                    'groups': None
                })
            elif list_size == 2:
                self._have.append({
                    'name': name_list[0].text,
                    'group': name_list[1].text,
                    'groups': None
                })
            elif list_size > 2:
                name_iter = iter(name_list)
                next(name_iter)
                tmp_list = list()
                for name in name_iter:
                    tmp_list.append(name.text)

                self._have.append({
                    'name': name_list[0].text,
                    'group': None,
                    'groups': tmp_list
                })

        locald_params = list()
        locald_group_params = list()
        opcode = None

        if state == 'absent':
            opcode = "delete"
            for want_item in self._want:
                if want_item['name'] in users:
                    want_item['configured_password'] = None
                    locald_params.append(want_item)
        elif state == 'present':
            opcode = "merge"
            for want_item in self._want:
                if want_item['name'] not in users:
                    want_item['configured_password'] = self.generate_md5_hash(
                        want_item['configured_password'])
                    locald_params.append(want_item)

                    if want_item['group'] is not None:
                        locald_group_params.append(want_item)
                    if want_item['groups'] is not None:
                        for group in want_item['groups']:
                            want_item['group'] = group
                            locald_group_params.append(want_item.copy())
                else:
                    if self._module.params[
                            'update_password'] == 'always' and want_item[
                                'configured_password'] is not None:
                        want_item[
                            'configured_password'] = self.generate_md5_hash(
                                want_item['configured_password'])
                        locald_params.append(want_item)
                    else:
                        want_item['configured_password'] = None

                    obj_in_have = search_obj_in_list(want_item['name'],
                                                     self._have)
                    if want_item['group'] is not None and want_item[
                            'group'] != obj_in_have['group']:
                        locald_group_params.append(want_item)
                    elif want_item['groups'] is not None:
                        for group in want_item['groups']:
                            want_item['group'] = group
                            locald_group_params.append(want_item.copy())

        purge_params = list()
        if self._module.params['purge']:
            want_users = [x['name'] for x in self._want]
            have_users = [x['name'] for x in self._have]
            for item in set(have_users).difference(set(want_users)):
                if item != 'admin':
                    purge_params.append({'name': item})

        self._result['xml'] = []
        _edit_filter_list = list()
        if opcode is not None:
            if locald_params:
                _edit_filter_list.append(
                    build_xml('aaa',
                              xmap=self._locald_meta,
                              params=locald_params,
                              opcode=opcode))

            if locald_group_params:
                _edit_filter_list.append(
                    build_xml('aaa',
                              xmap=self._locald_group_meta,
                              params=locald_group_params,
                              opcode=opcode))

            if purge_params:
                _edit_filter_list.append(
                    build_xml('aaa',
                              xmap=self._locald_meta,
                              params=purge_params,
                              opcode="delete"))

        diff = None
        if _edit_filter_list:
            commit = not self._module.check_mode
            diff = load_config(self._module,
                               _edit_filter_list,
                               commit=commit,
                               running=running,
                               nc_get_filter=_get_filter)

        if diff:
            if self._module._diff:
                self._result['diff'] = dict(prepared=diff)

            self._result['xml'] = _edit_filter_list
            self._result['changed'] = True
    def map_obj_to_xml_rpc(self):
        self._system_meta.update([
            ('vrfs', {
                'xpath': 'ip-domain/vrfs',
                'tag': True,
                'operation': 'edit'
            }),
            ('vrf', {
                'xpath': 'ip-domain/vrfs/vrf',
                'tag': True,
                'operation': 'edit'
            }),
            ('a:vrf', {
                'xpath': 'ip-domain/vrfs/vrf/vrf-name',
                'operation': 'edit'
            }),
            ('a:domain_name', {
                'xpath': 'ip-domain/vrfs/vrf/name',
                'operation': 'edit',
                'attrib': "operation"
            }),
        ])

        self._system_domain_meta.update([
            ('vrfs', {
                'xpath': 'ip-domain/vrfs',
                'tag': True,
                'operation': 'edit'
            }),
            ('vrf', {
                'xpath': 'ip-domain/vrfs/vrf',
                'tag': True,
                'operation': 'edit'
            }),
            ('a:vrf', {
                'xpath': 'ip-domain/vrfs/vrf/vrf-name',
                'operation': 'edit'
            }),
            ('lists', {
                'xpath': 'ip-domain/vrfs/vrf/lists',
                'tag': True,
                'operation': 'edit'
            }),
            ('list', {
                'xpath': 'ip-domain/vrfs/vrf/lists/list',
                'tag': True,
                'operation': 'edit',
                'attrib': "operation"
            }),
            ('a:order', {
                'xpath': 'ip-domain/vrfs/vrf/lists/list/order',
                'operation': 'edit'
            }),
            ('a:domain_search', {
                'xpath': 'ip-domain/vrfs/vrf/lists/list/list-name',
                'operation': 'edit'
            }),
        ])

        self._system_server_meta.update([
            ('vrfs', {
                'xpath': 'ip-domain/vrfs',
                'tag': True,
                'operation': 'edit'
            }),
            ('vrf', {
                'xpath': 'ip-domain/vrfs/vrf',
                'tag': True,
                'operation': 'edit'
            }),
            ('a:vrf', {
                'xpath': 'ip-domain/vrfs/vrf/vrf-name',
                'operation': 'edit'
            }),
            ('servers', {
                'xpath': 'ip-domain/vrfs/vrf/servers',
                'tag': True,
                'operation': 'edit'
            }),
            ('server', {
                'xpath': 'ip-domain/vrfs/vrf/servers/server',
                'tag': True,
                'operation': 'edit',
                'attrib': "operation"
            }),
            ('a:order', {
                'xpath': 'ip-domain/vrfs/vrf/servers/server/order',
                'operation': 'edit'
            }),
            ('a:name_servers', {
                'xpath': 'ip-domain/vrfs/vrf/servers/server/server-address',
                'operation': 'edit'
            }),
        ])

        self._hostname_meta.update([
            ('a:hostname', {
                'xpath': 'host-names/host-name',
                'operation': 'edit',
                'attrib': "operation"
            }),
        ])

        self._lookup_source_meta.update([
            ('vrfs', {
                'xpath': 'ip-domain/vrfs',
                'tag': True,
                'operation': 'edit'
            }),
            ('vrf', {
                'xpath': 'ip-domain/vrfs/vrf',
                'tag': True,
                'operation': 'edit'
            }),
            ('a:vrf', {
                'xpath': 'ip-domain/vrfs/vrf/vrf-name',
                'operation': 'edit'
            }),
            ('a:lookup_source', {
                'xpath': 'ip-domain/vrfs/vrf/source-interface',
                'operation': 'edit',
                'attrib': "operation"
            }),
        ])

        self._lookup_meta.update([
            ('vrfs', {
                'xpath': 'ip-domain/vrfs',
                'tag': True,
                'operation': 'edit'
            }),
            ('vrf', {
                'xpath': 'ip-domain/vrfs/vrf',
                'tag': True,
                'operation': 'edit'
            }),
            ('a:vrf', {
                'xpath': 'ip-domain/vrfs/vrf/vrf-name',
                'operation': 'edit'
            }),
            ('lookup', {
                'xpath': 'ip-domain/vrfs/vrf/lookup',
                'tag': True,
                'operation': 'edit',
                'attrib': "operation"
            }),
        ])

        state = self._module.params['state']
        _get_filter = build_xml('ip-domain', opcode="filter")
        running = get_config(self._module,
                             source='running',
                             config_filter=_get_filter)
        _get_filter = build_xml('host-names', opcode="filter")
        hostname_runn = get_config(self._module,
                                   source='running',
                                   config_filter=_get_filter)

        hostname_ele = etree_find(hostname_runn, 'host-name')
        hostname = hostname_ele.text if hostname_ele is not None else None

        vrf_ele = etree_findall(running, 'vrf')
        vrf_map = {}
        for vrf in vrf_ele:
            name_server_list = list()
            domain_list = list()
            vrf_name_ele = etree_find(vrf, 'vrf-name')
            vrf_name = vrf_name_ele.text if vrf_name_ele is not None else None

            domain_name_ele = etree_find(vrf, 'name')
            domain_name = domain_name_ele.text if domain_name_ele is not None else None

            domain_ele = etree_findall(vrf, 'list-name')
            for domain in domain_ele:
                domain_list.append(domain.text)

            server_ele = etree_findall(vrf, 'server-address')
            for server in server_ele:
                name_server_list.append(server.text)

            lookup_source_ele = etree_find(vrf, 'source-interface')
            lookup_source = lookup_source_ele.text if lookup_source_ele is not None else None

            lookup_enabled = False if etree_find(
                vrf, 'lookup') is not None else True

            vrf_map[vrf_name] = {
                'domain_name': domain_name,
                'domain_search': domain_list,
                'name_servers': name_server_list,
                'lookup_source': lookup_source,
                'lookup_enabled': lookup_enabled
            }

        opcode = None
        hostname_param = {}
        lookup_param = {}
        system_param = {}
        sys_server_params = list()
        sys_domain_params = list()
        add_domain_params = list()
        del_domain_params = list()
        add_server_params = list()
        del_server_params = list()
        lookup_source_params = {}

        try:
            sys_node = vrf_map[self._want['vrf']]
        except KeyError:
            sys_node = {
                'domain_name': None,
                'domain_search': [],
                'name_servers': [],
                'lookup_source': None,
                'lookup_enabled': True
            }

        if state == 'absent':
            opcode = "delete"

            def needs_update(x):
                return self._want[x] is not None and self._want[x] == sys_node[
                    x]

            if needs_update('domain_name'):
                system_param = {
                    'vrf': self._want['vrf'],
                    'domain_name': self._want['domain_name']
                }

            if needs_update('hostname'):
                hostname_param = {'hostname': hostname}

            if not self._want['lookup_enabled'] and not sys_node[
                    'lookup_enabled']:
                lookup_param['vrf'] = self._want['vrf']

            if needs_update('lookup_source'):
                lookup_source_params['vrf'] = self._want['vrf']
                lookup_source_params['lookup_source'] = self._want[
                    'lookup_source']

            if self._want['domain_search']:
                domain_param = {}
                domain_param['domain_name'] = self._want['domain_name']
                domain_param['vrf'] = self._want['vrf']
                domain_param['order'] = '0'
                for domain in self._want['domain_search']:
                    if domain in sys_node['domain_search']:
                        domain_param['domain_search'] = domain
                        sys_domain_params.append(domain_param.copy())

            if self._want['name_servers']:
                server_param = {}
                server_param['vrf'] = self._want['vrf']
                server_param['order'] = '0'
                for server in self._want['name_servers']:
                    if server in sys_node['name_servers']:
                        server_param['name_servers'] = server
                        sys_server_params.append(server_param.copy())

        elif state == 'present':
            opcode = "merge"

            def needs_update(x):
                return self._want[x] is not None and (
                    sys_node[x] is None or
                    (sys_node[x] is not None and self._want[x] != sys_node[x]))

            if needs_update('domain_name'):
                system_param = {
                    'vrf': self._want['vrf'],
                    'domain_name': self._want['domain_name']
                }

            if self._want['hostname'] is not None and self._want[
                    'hostname'] != hostname:
                hostname_param = {'hostname': self._want['hostname']}

            if not self._want['lookup_enabled'] and sys_node['lookup_enabled']:
                lookup_param['vrf'] = self._want['vrf']

            if needs_update('lookup_source'):
                lookup_source_params['vrf'] = self._want['vrf']
                lookup_source_params['lookup_source'] = self._want[
                    'lookup_source']

            if self._want['domain_search']:
                domain_adds, domain_removes = diff_list(
                    self._want['domain_search'], sys_node['domain_search'])
                domain_param = {}
                domain_param['domain_name'] = self._want['domain_name']
                domain_param['vrf'] = self._want['vrf']
                domain_param['order'] = '0'
                for domain in domain_adds:
                    if domain not in sys_node['domain_search']:
                        domain_param['domain_search'] = domain
                        add_domain_params.append(domain_param.copy())
                for domain in domain_removes:
                    if domain in sys_node['domain_search']:
                        domain_param['domain_search'] = domain
                        del_domain_params.append(domain_param.copy())

            if self._want['name_servers']:
                server_adds, server_removes = diff_list(
                    self._want['name_servers'], sys_node['name_servers'])
                server_param = {}
                server_param['vrf'] = self._want['vrf']
                server_param['order'] = '0'
                for domain in server_adds:
                    if domain not in sys_node['name_servers']:
                        server_param['name_servers'] = domain
                        add_server_params.append(server_param.copy())
                for domain in server_removes:
                    if domain in sys_node['name_servers']:
                        server_param['name_servers'] = domain
                        del_server_params.append(server_param.copy())

        self._result['xml'] = []
        _edit_filter_list = list()
        if opcode:
            if hostname_param:
                _edit_filter_list.append(
                    build_xml('host-names',
                              xmap=self._hostname_meta,
                              params=hostname_param,
                              opcode=opcode))

            if system_param:
                _edit_filter_list.append(
                    build_xml('ip-domain',
                              xmap=self._system_meta,
                              params=system_param,
                              opcode=opcode))

            if lookup_source_params:
                _edit_filter_list.append(
                    build_xml('ip-domain',
                              xmap=self._lookup_source_meta,
                              params=lookup_source_params,
                              opcode=opcode))
            if lookup_param:
                _edit_filter_list.append(
                    build_xml('ip-domain',
                              xmap=self._lookup_meta,
                              params=lookup_param,
                              opcode=opcode))

            if opcode == 'delete':
                if sys_domain_params:
                    _edit_filter_list.append(
                        build_xml('ip-domain',
                                  xmap=self._system_domain_meta,
                                  params=sys_domain_params,
                                  opcode=opcode))
                if sys_server_params:
                    _edit_filter_list.append(
                        build_xml('ip-domain',
                                  xmap=self._system_server_meta,
                                  params=sys_server_params,
                                  opcode=opcode))
                    if self._want['vrf'] != 'default':
                        self._result['warnings'] = [
                            "name-servers delete operation with non-default vrf is a success, "
                            "but with rpc-error. Recommended to use 'ignore_errors' option with the task as a workaround"
                        ]
            elif opcode == 'merge':
                if add_domain_params:
                    _edit_filter_list.append(
                        build_xml('ip-domain',
                                  xmap=self._system_domain_meta,
                                  params=add_domain_params,
                                  opcode=opcode))
                if del_domain_params:
                    _edit_filter_list.append(
                        build_xml('ip-domain',
                                  xmap=self._system_domain_meta,
                                  params=del_domain_params,
                                  opcode="delete"))

                if add_server_params:
                    _edit_filter_list.append(
                        build_xml('ip-domain',
                                  xmap=self._system_server_meta,
                                  params=add_server_params,
                                  opcode=opcode))
                if del_server_params:
                    _edit_filter_list.append(
                        build_xml('ip-domain',
                                  xmap=self._system_server_meta,
                                  params=del_server_params,
                                  opcode="delete"))

        diff = None
        if _edit_filter_list:
            commit = not self._module.check_mode
            diff = load_config(self._module,
                               _edit_filter_list,
                               commit=commit,
                               running=running,
                               nc_get_filter=_get_filter)

        if diff:
            if self._module._diff:
                self._result['diff'] = dict(prepared=diff)

            self._result['xml'] = _edit_filter_list
            self._result['changed'] = True
Esempio n. 8
0
    def map_obj_to_xml_rpc(self):
        self._system_meta.update([
            ('vrfs', {'xpath': 'ip-domain/vrfs', 'tag': True, 'operation': 'edit'}),
            ('vrf', {'xpath': 'ip-domain/vrfs/vrf', 'tag': True, 'operation': 'edit'}),
            ('a:vrf', {'xpath': 'ip-domain/vrfs/vrf/vrf-name', 'operation': 'edit'}),
            ('a:domain_name', {'xpath': 'ip-domain/vrfs/vrf/name', 'operation': 'edit', 'attrib': "operation"}),
        ])

        self._system_domain_meta.update([
            ('vrfs', {'xpath': 'ip-domain/vrfs', 'tag': True, 'operation': 'edit'}),
            ('vrf', {'xpath': 'ip-domain/vrfs/vrf', 'tag': True, 'operation': 'edit'}),
            ('a:vrf', {'xpath': 'ip-domain/vrfs/vrf/vrf-name', 'operation': 'edit'}),
            ('lists', {'xpath': 'ip-domain/vrfs/vrf/lists', 'tag': True, 'operation': 'edit'}),
            ('list', {'xpath': 'ip-domain/vrfs/vrf/lists/list', 'tag': True, 'operation': 'edit', 'attrib': "operation"}),
            ('a:order', {'xpath': 'ip-domain/vrfs/vrf/lists/list/order', 'operation': 'edit'}),
            ('a:domain_search', {'xpath': 'ip-domain/vrfs/vrf/lists/list/list-name', 'operation': 'edit'}),
        ])

        self._system_server_meta.update([
            ('vrfs', {'xpath': 'ip-domain/vrfs', 'tag': True, 'operation': 'edit'}),
            ('vrf', {'xpath': 'ip-domain/vrfs/vrf', 'tag': True, 'operation': 'edit'}),
            ('a:vrf', {'xpath': 'ip-domain/vrfs/vrf/vrf-name', 'operation': 'edit'}),
            ('servers', {'xpath': 'ip-domain/vrfs/vrf/servers', 'tag': True, 'operation': 'edit'}),
            ('server', {'xpath': 'ip-domain/vrfs/vrf/servers/server', 'tag': True, 'operation': 'edit', 'attrib': "operation"}),
            ('a:order', {'xpath': 'ip-domain/vrfs/vrf/servers/server/order', 'operation': 'edit'}),
            ('a:name_servers', {'xpath': 'ip-domain/vrfs/vrf/servers/server/server-address', 'operation': 'edit'}),
        ])

        self._hostname_meta.update([
            ('a:hostname', {'xpath': 'host-names/host-name', 'operation': 'edit', 'attrib': "operation"}),
        ])

        self._lookup_source_meta.update([
            ('vrfs', {'xpath': 'ip-domain/vrfs', 'tag': True, 'operation': 'edit'}),
            ('vrf', {'xpath': 'ip-domain/vrfs/vrf', 'tag': True, 'operation': 'edit'}),
            ('a:vrf', {'xpath': 'ip-domain/vrfs/vrf/vrf-name', 'operation': 'edit'}),
            ('a:lookup_source', {'xpath': 'ip-domain/vrfs/vrf/source-interface', 'operation': 'edit', 'attrib': "operation"}),
        ])

        self._lookup_meta.update([
            ('vrfs', {'xpath': 'ip-domain/vrfs', 'tag': True, 'operation': 'edit'}),
            ('vrf', {'xpath': 'ip-domain/vrfs/vrf', 'tag': True, 'operation': 'edit'}),
            ('a:vrf', {'xpath': 'ip-domain/vrfs/vrf/vrf-name', 'operation': 'edit'}),
            ('lookup', {'xpath': 'ip-domain/vrfs/vrf/lookup', 'tag': True, 'operation': 'edit', 'attrib': "operation"}),
        ])

        state = self._module.params['state']
        _get_filter = build_xml('ip-domain', opcode="filter")
        running = get_config(self._module, source='running', config_filter=_get_filter)
        _get_filter = build_xml('host-names', opcode="filter")
        hostname_runn = get_config(self._module, source='running', config_filter=_get_filter)

        hostname_ele = etree_find(hostname_runn, 'host-name')
        hostname = hostname_ele.text if hostname_ele is not None else None

        vrf_ele = etree_findall(running, 'vrf')
        vrf_map = {}
        for vrf in vrf_ele:
            name_server_list = list()
            domain_list = list()
            vrf_name_ele = etree_find(vrf, 'vrf-name')
            vrf_name = vrf_name_ele.text if vrf_name_ele is not None else None

            domain_name_ele = etree_find(vrf, 'name')
            domain_name = domain_name_ele.text if domain_name_ele is not None else None

            domain_ele = etree_findall(vrf, 'list-name')
            for domain in domain_ele:
                domain_list.append(domain.text)

            server_ele = etree_findall(vrf, 'server-address')
            for server in server_ele:
                name_server_list.append(server.text)

            lookup_source_ele = etree_find(vrf, 'source-interface')
            lookup_source = lookup_source_ele.text if lookup_source_ele is not None else None

            lookup_enabled = False if etree_find(vrf, 'lookup') is not None else True

            vrf_map[vrf_name] = {'domain_name': domain_name,
                                 'domain_search': domain_list,
                                 'name_servers': name_server_list,
                                 'lookup_source': lookup_source,
                                 'lookup_enabled': lookup_enabled}

        opcode = None
        hostname_param = {}
        lookup_param = {}
        system_param = {}
        sys_server_params = list()
        sys_domain_params = list()
        add_domain_params = list()
        del_domain_params = list()
        add_server_params = list()
        del_server_params = list()
        lookup_source_params = {}

        try:
            sys_node = vrf_map[self._want['vrf']]
        except KeyError:
            sys_node = {'domain_name': None,
                        'domain_search': [],
                        'name_servers': [],
                        'lookup_source': None,
                        'lookup_enabled': True}

        if state == 'absent':
            opcode = "delete"

            def needs_update(x):
                return self._want[x] is not None and self._want[x] == sys_node[x]

            if needs_update('domain_name'):
                system_param = {'vrf': self._want['vrf'], 'domain_name': self._want['domain_name']}

            if needs_update('hostname'):
                hostname_param = {'hostname': hostname}

            if not self._want['lookup_enabled'] and not sys_node['lookup_enabled']:
                lookup_param['vrf'] = self._want['vrf']

            if needs_update('lookup_source'):
                lookup_source_params['vrf'] = self._want['vrf']
                lookup_source_params['lookup_source'] = self._want['lookup_source']

            if self._want['domain_search']:
                domain_param = {}
                domain_param['domain_name'] = self._want['domain_name']
                domain_param['vrf'] = self._want['vrf']
                domain_param['order'] = '0'
                for domain in self._want['domain_search']:
                    if domain in sys_node['domain_search']:
                        domain_param['domain_search'] = domain
                        sys_domain_params.append(domain_param.copy())

            if self._want['name_servers']:
                server_param = {}
                server_param['vrf'] = self._want['vrf']
                server_param['order'] = '0'
                for server in self._want['name_servers']:
                    if server in sys_node['name_servers']:
                        server_param['name_servers'] = server
                        sys_server_params.append(server_param.copy())

        elif state == 'present':
            opcode = "merge"

            def needs_update(x):
                return self._want[x] is not None and (sys_node[x] is None or
                                                      (sys_node[x] is not None and self._want[x] != sys_node[x]))

            if needs_update('domain_name'):
                system_param = {'vrf': self._want['vrf'], 'domain_name': self._want['domain_name']}

            if self._want['hostname'] is not None and self._want['hostname'] != hostname:
                hostname_param = {'hostname': self._want['hostname']}

            if not self._want['lookup_enabled'] and sys_node['lookup_enabled']:
                lookup_param['vrf'] = self._want['vrf']

            if needs_update('lookup_source'):
                lookup_source_params['vrf'] = self._want['vrf']
                lookup_source_params['lookup_source'] = self._want['lookup_source']

            if self._want['domain_search']:
                domain_adds, domain_removes = diff_list(self._want['domain_search'], sys_node['domain_search'])
                domain_param = {}
                domain_param['domain_name'] = self._want['domain_name']
                domain_param['vrf'] = self._want['vrf']
                domain_param['order'] = '0'
                for domain in domain_adds:
                    if domain not in sys_node['domain_search']:
                        domain_param['domain_search'] = domain
                        add_domain_params.append(domain_param.copy())
                for domain in domain_removes:
                    if domain in sys_node['domain_search']:
                        domain_param['domain_search'] = domain
                        del_domain_params.append(domain_param.copy())

            if self._want['name_servers']:
                server_adds, server_removes = diff_list(self._want['name_servers'], sys_node['name_servers'])
                server_param = {}
                server_param['vrf'] = self._want['vrf']
                server_param['order'] = '0'
                for domain in server_adds:
                    if domain not in sys_node['name_servers']:
                        server_param['name_servers'] = domain
                        add_server_params.append(server_param.copy())
                for domain in server_removes:
                    if domain in sys_node['name_servers']:
                        server_param['name_servers'] = domain
                        del_server_params.append(server_param.copy())

        self._result['xml'] = []
        _edit_filter_list = list()
        if opcode:
            if hostname_param:
                _edit_filter_list.append(build_xml('host-names', xmap=self._hostname_meta,
                                                   params=hostname_param, opcode=opcode))

            if system_param:
                _edit_filter_list.append(build_xml('ip-domain', xmap=self._system_meta,
                                                   params=system_param, opcode=opcode))

            if lookup_source_params:
                _edit_filter_list.append(build_xml('ip-domain', xmap=self._lookup_source_meta,
                                                   params=lookup_source_params, opcode=opcode))
            if lookup_param:
                _edit_filter_list.append(build_xml('ip-domain', xmap=self._lookup_meta,
                                                   params=lookup_param, opcode=opcode))

            if opcode == 'delete':
                if sys_domain_params:
                    _edit_filter_list.append(build_xml('ip-domain', xmap=self._system_domain_meta,
                                                       params=sys_domain_params, opcode=opcode))
                if sys_server_params:
                    _edit_filter_list.append(build_xml('ip-domain', xmap=self._system_server_meta,
                                                       params=sys_server_params, opcode=opcode))
                    if self._want['vrf'] != 'default':
                        self._result['warnings'] = ["name-servers delete operation with non-default vrf is a success, "
                                                    "but with rpc-error. Recommended to use 'ignore_errors' option with the task as a workaround"]
            elif opcode == 'merge':
                if add_domain_params:
                    _edit_filter_list.append(build_xml('ip-domain', xmap=self._system_domain_meta,
                                                       params=add_domain_params, opcode=opcode))
                if del_domain_params:
                    _edit_filter_list.append(build_xml('ip-domain', xmap=self._system_domain_meta,
                                                       params=del_domain_params, opcode="delete"))

                if add_server_params:
                    _edit_filter_list.append(build_xml('ip-domain', xmap=self._system_server_meta,
                                                       params=add_server_params, opcode=opcode))
                if del_server_params:
                    _edit_filter_list.append(build_xml('ip-domain', xmap=self._system_server_meta,
                                                       params=del_server_params, opcode="delete"))

        diff = None
        if _edit_filter_list:
            commit = not self._module.check_mode
            diff = load_config(self._module, _edit_filter_list, commit=commit, running=running,
                               nc_get_filter=_get_filter)

        if diff:
            if self._module._diff:
                self._result['diff'] = dict(prepared=diff)

            self._result['xml'] = _edit_filter_list
            self._result['changed'] = True
    def check_declarative_intent_params(self):
        failed_conditions = []

        self._data_rate_meta.update([
            ('interfaces', {
                'xpath': 'infra-statistics/interfaces',
                'tag': True
            }),
            ('interface', {
                'xpath': 'infra-statistics/interfaces/interface',
                'tag': True
            }),
            ('a:name', {
                'xpath': 'infra-statistics/interfaces/interface/interface-name'
            }),
            ('cache', {
                'xpath': 'infra-statistics/interfaces/interface/cache',
                'tag': True
            }),
            ('data-rate', {
                'xpath':
                'infra-statistics/interfaces/interface/cache/data-rate',
                'tag': True
            }),
            ('input-data-rate', {
                'xpath':
                'infra-statistics/interfaces/interface/cache/data-rate/input-data-rate',
                'tag': True
            }),
            ('output-data-rate', {
                'xpath':
                'infra-statistics/interfaces/interface/cache/data-rate/output-data-rate',
                'tag': True
            }),
        ])

        self._line_state_meta.update([
            ('data-nodes', {
                'xpath': 'interface-properties/data-nodes',
                'tag': True
            }),
            ('data-node', {
                'xpath': 'interface-properties/data-nodes/data-node',
                'tag': True
            }),
            ('system-view', {
                'xpath':
                'interface-properties/data-nodes/data-node/system-view',
                'tag': True
            }),
            ('interfaces', {
                'xpath':
                'interface-properties/data-nodes/data-node/system-view/interfaces',
                'tag': True
            }),
            ('interface', {
                'xpath':
                'interface-properties/data-nodes/data-node/system-view/interfaces/interface',
                'tag': True
            }),
            ('a:name', {
                'xpath':
                'interface-properties/data-nodes/data-node/system-view/interfaces/interface/interface-name'
            }),
            ('line-state', {
                'xpath':
                'interface-properties/data-nodes/data-node/system-view/interfaces/interface/line-state',
                'tag': True
            }),
        ])

        _rate_filter = build_xml('infra-statistics',
                                 xmap=self._data_rate_meta,
                                 params=self._want,
                                 opcode="filter")
        out = get_oper(self._module, filter=_rate_filter)
        data_rate_list = etree_findall(out, 'interface')
        data_rate_map = dict()
        for item in data_rate_list:
            data_rate_map.update(
                {etree_find(item, 'interface-name').text: dict()})
            data_rate_map[etree_find(item, 'interface-name').text].update({
                'input-data-rate':
                etree_find(item, 'input-data-rate').text,
                'output-data-rate':
                etree_find(item, 'output-data-rate').text
            })

        _line_state_filter = build_xml('interface-properties',
                                       xmap=self._line_state_meta,
                                       params=self._want,
                                       opcode="filter")
        out = get_oper(self._module, filter=_line_state_filter)
        line_state_list = etree_findall(out, 'interface')
        line_state_map = dict()
        for item in line_state_list:
            line_state_map.update({
                etree_find(item, 'interface-name').text:
                etree_find(item, 'line-state').text
            })

        for want_item in self._want:
            want_state = want_item.get('state')
            want_tx_rate = want_item.get('tx_rate')
            want_rx_rate = want_item.get('rx_rate')
            if want_state not in (
                    'up', 'down') and not want_tx_rate and not want_rx_rate:
                continue

            if self._result['changed']:
                sleep(want_item['delay'])

            if want_state in ('up', 'down'):
                if want_state not in line_state_map[want_item['name']]:
                    failed_conditions.append('state ' +
                                             'eq({!s})'.format(want_state))

            if want_tx_rate:
                if want_tx_rate != data_rate_map[
                        want_item['name']]['output-data-rate']:
                    failed_conditions.append('tx_rate ' + want_tx_rate)

            if want_rx_rate:
                if want_rx_rate != data_rate_map[
                        want_item['name']]['input-data-rate']:
                    failed_conditions.append('rx_rate ' + want_rx_rate)

        if failed_conditions:
            msg = 'One or more conditional statements have not been satisfied'
            self._module.fail_json(msg=msg,
                                   failed_conditions=failed_conditions)
    def map_obj_to_xml_rpc(self):
        self._intf_meta.update([
            ('interface-configuration', {
                'xpath': 'interface-configurations/interface-configuration',
                'tag': True,
                'attrib': 'operation'
            }),
            ('a:active', {
                'xpath':
                'interface-configurations/interface-configuration/active',
                'operation': 'edit'
            }),
            ('a:name', {
                'xpath':
                'interface-configurations/interface-configuration/interface-name'
            }),
            ('a:description', {
                'xpath':
                'interface-configurations/interface-configuration/description',
                'operation': 'edit'
            }),
            ('mtus', {
                'xpath':
                'interface-configurations/interface-configuration/mtus',
                'tag': True,
                'operation': 'edit'
            }),
            ('mtu', {
                'xpath':
                'interface-configurations/interface-configuration/mtus/mtu',
                'tag': True,
                'operation': 'edit'
            }),
            ('a:owner', {
                'xpath':
                'interface-configurations/interface-configuration/mtus/mtu/owner',
                'operation': 'edit'
            }),
            ('a:mtu', {
                'xpath':
                'interface-configurations/interface-configuration/mtus/mtu/mtu',
                'operation': 'edit'
            }),
            ('CEthernet', {
                'xpath':
                'interface-configurations/interface-configuration/ethernet',
                'tag': True,
                'operation': 'edit',
                'ns': True
            }),
            ('a:speed', {
                'xpath':
                'interface-configurations/interface-configuration/ethernet/speed',
                'operation': 'edit'
            }),
            ('a:duplex', {
                'xpath':
                'interface-configurations/interface-configuration/ethernet/duplex',
                'operation': 'edit'
            }),
        ])

        self._shut_meta.update([
            ('interface-configuration', {
                'xpath': 'interface-configurations/interface-configuration',
                'tag': True
            }),
            ('a:active', {
                'xpath':
                'interface-configurations/interface-configuration/active',
                'operation': 'edit'
            }),
            ('a:name', {
                'xpath':
                'interface-configurations/interface-configuration/interface-name'
            }),
            ('shutdown', {
                'xpath':
                'interface-configurations/interface-configuration/shutdown',
                'tag': True,
                'operation': 'edit',
                'attrib': 'operation'
            }),
        ])
        state = self._module.params['state']

        _get_filter = build_xml('interface-configurations',
                                xmap=self._intf_meta,
                                params=self._want,
                                opcode="filter")

        running = get_config(self._module,
                             source='running',
                             config_filter=_get_filter)
        intfcfg_nodes = etree_findall(running, 'interface-configuration')

        intf_list = set()
        shut_list = set()
        for item in intfcfg_nodes:
            intf_name = etree_find(item, 'interface-name').text
            if intf_name is not None:
                intf_list.add(intf_name)

                if etree_find(item, 'shutdown') is not None:
                    shut_list.add(intf_name)

        intf_params = list()
        shut_params = list()
        noshut_params = list()
        for index, item in enumerate(self._want):
            if item['name'] in intf_list:
                intf_params.append(item)
            if not item['enabled']:
                shut_params.append(item)
            if item['name'] in shut_list and item['enabled']:
                noshut_params.append(item)

        opcode = None
        if state == 'absent':
            if intf_params:
                opcode = "delete"
        elif state in ('present', 'up', 'down'):
            intf_params = self._want
            opcode = 'merge'

        self._result['xml'] = []
        _edit_filter_list = list()
        if opcode:
            _edit_filter_list.append(
                build_xml('interface-configurations',
                          xmap=self._intf_meta,
                          params=intf_params,
                          opcode=opcode))

            if opcode == 'merge':
                if len(shut_params):
                    _edit_filter_list.append(
                        build_xml('interface-configurations',
                                  xmap=self._shut_meta,
                                  params=shut_params,
                                  opcode='merge'))
                if len(noshut_params):
                    _edit_filter_list.append(
                        build_xml('interface-configurations',
                                  xmap=self._shut_meta,
                                  params=noshut_params,
                                  opcode='delete'))
            diff = None
            if len(_edit_filter_list):
                commit = not self._module.check_mode
                diff = load_config(self._module,
                                   _edit_filter_list,
                                   commit=commit,
                                   running=running,
                                   nc_get_filter=_get_filter)

            if diff:
                if self._module._diff:
                    self._result['diff'] = dict(prepared=diff)

                self._result['xml'] = _edit_filter_list
                self._result['changed'] = True
Esempio n. 11
0
    def map_obj_to_xml_rpc(self):
        self._locald_meta.update([
            ('aaa_locald', {'xpath': 'aaa/usernames', 'tag': True, 'ns': True}),
            ('username', {'xpath': 'aaa/usernames/username', 'tag': True, 'attrib': "operation"}),
            ('a:name', {'xpath': 'aaa/usernames/username/name'}),
            ('a:configured_password', {'xpath': 'aaa/usernames/username/secret', 'operation': 'edit'}),
        ])

        self._locald_group_meta.update([
            ('aaa_locald', {'xpath': 'aaa/usernames', 'tag': True, 'ns': True}),
            ('username', {'xpath': 'aaa/usernames/username', 'tag': True, 'attrib': "operation"}),
            ('a:name', {'xpath': 'aaa/usernames/username/name'}),
            ('usergroups', {'xpath': 'aaa/usernames/username/usergroup-under-usernames', 'tag': True, 'operation': 'edit'}),
            ('usergroup', {'xpath': 'aaa/usernames/username/usergroup-under-usernames/usergroup-under-username', 'tag': True, 'operation': 'edit'}),
            ('a:group', {'xpath': 'aaa/usernames/username/usergroup-under-usernames/usergroup-under-username/name', 'operation': 'edit'}),
        ])

        state = self._module.params['state']
        _get_filter = build_xml('aaa', opcode="filter")
        running = get_config(self._module, source='running', config_filter=_get_filter)

        elements = etree_findall(running, 'username')
        users = list()
        for element in elements:
            name_list = etree_findall(element, 'name')
            users.append(name_list[0].text)
            list_size = len(name_list)
            if list_size == 1:
                self._have.append({'name': name_list[0].text, 'group': None, 'groups': None})
            elif list_size == 2:
                self._have.append({'name': name_list[0].text, 'group': name_list[1].text, 'groups': None})
            elif list_size > 2:
                name_iter = iter(name_list)
                next(name_iter)
                tmp_list = list()
                for name in name_iter:
                    tmp_list.append(name.text)

                self._have.append({'name': name_list[0].text, 'group': None, 'groups': tmp_list})

        locald_params = list()
        locald_group_params = list()
        opcode = None

        if state == 'absent':
            opcode = "delete"
            for want_item in self._want:
                if want_item['name'] in users:
                    want_item['configured_password'] = None
                    locald_params.append(want_item)
        elif state == 'present':
            opcode = "merge"
            for want_item in self._want:
                if want_item['name'] not in users:
                    want_item['configured_password'] = self.generate_md5_hash(want_item['configured_password'])
                    locald_params.append(want_item)

                    if want_item['group'] is not None:
                        locald_group_params.append(want_item)
                    if want_item['groups'] is not None:
                        for group in want_item['groups']:
                            want_item['group'] = group
                            locald_group_params.append(want_item.copy())
                else:
                    if self._module.params['update_password'] == 'always' and want_item['configured_password'] is not None:
                        want_item['configured_password'] = self.generate_md5_hash(want_item['configured_password'])
                        locald_params.append(want_item)
                    else:
                        want_item['configured_password'] = None

                    obj_in_have = search_obj_in_list(want_item['name'], self._have)
                    if want_item['group'] is not None and want_item['group'] != obj_in_have['group']:
                        locald_group_params.append(want_item)
                    elif want_item['groups'] is not None:
                        for group in want_item['groups']:
                            want_item['group'] = group
                            locald_group_params.append(want_item.copy())

        purge_params = list()
        if self._module.params['purge']:
            want_users = [x['name'] for x in self._want]
            have_users = [x['name'] for x in self._have]
            for item in set(have_users).difference(set(want_users)):
                if item != 'admin':
                    purge_params.append({'name': item})

        self._result['xml'] = []
        _edit_filter_list = list()
        if opcode is not None:
            if locald_params:
                _edit_filter_list.append(build_xml('aaa', xmap=self._locald_meta,
                                                   params=locald_params, opcode=opcode))

            if locald_group_params:
                _edit_filter_list.append(build_xml('aaa', xmap=self._locald_group_meta,
                                                   params=locald_group_params, opcode=opcode))

            if purge_params:
                _edit_filter_list.append(build_xml('aaa', xmap=self._locald_meta,
                                                   params=purge_params, opcode="delete"))

        diff = None
        if _edit_filter_list:
            commit = not self._module.check_mode
            diff = load_config(self._module, _edit_filter_list, commit=commit, running=running,
                               nc_get_filter=_get_filter)

        if diff:
            if self._module._diff:
                self._result['diff'] = dict(prepared=diff)

            self._result['xml'] = _edit_filter_list
            self._result['changed'] = True
Esempio n. 12
0
    def map_obj_to_xml_rpc(self):
        self._log_file_meta.update([
            ('files', {
                'xpath': 'syslog/files',
                'tag': True,
                'operation': 'edit'
            }),
            ('file', {
                'xpath': 'syslog/files/file',
                'tag': True,
                'operation': 'edit',
                'attrib': "operation"
            }),
            ('a:name', {
                'xpath': 'syslog/files/file/file-name',
                'operation': 'edit'
            }),
            ('file-attrib', {
                'xpath': 'syslog/files/file/file-log-attributes',
                'tag': True,
                'operation': 'edit'
            }),
            ('a:size', {
                'xpath': 'syslog/files/file/file-log-attributes/max-file-size',
                'operation': 'edit'
            }),
            ('a:level', {
                'xpath': 'syslog/files/file/file-log-attributes/severity',
                'operation': 'edit'
            }),
        ])
        self._log_host_meta.update([
            ('host-server', {
                'xpath': 'syslog/host-server',
                'tag': True,
                'operation': 'edit'
            }),
            ('vrfs', {
                'xpath': 'syslog/host-server/vrfs',
                'tag': True,
                'operation': 'edit'
            }),
            ('vrf', {
                'xpath': 'syslog/host-server/vrfs/vrf',
                'tag': True,
                'operation': 'edit'
            }),
            ('a:vrf', {
                'xpath': 'syslog/host-server/vrfs/vrf/vrf-name',
                'operation': 'edit'
            }),
            ('ipv4s', {
                'xpath': 'syslog/host-server/vrfs/vrf/ipv4s',
                'tag': True,
                'operation': 'edit'
            }),
            ('ipv4', {
                'xpath': 'syslog/host-server/vrfs/vrf/ipv4s/ipv4',
                'tag': True,
                'operation': 'edit',
                'attrib': "operation"
            }),
            ('a:name', {
                'xpath': 'syslog/host-server/vrfs/vrf/ipv4s/ipv4/address',
                'operation': 'edit'
            }),
            ('ipv4-sev', {
                'xpath':
                'syslog/host-server/vrfs/vrf/ipv4s/ipv4/ipv4-severity-port',
                'tag': True,
                'operation': 'edit'
            }),
            ('a:level', {
                'xpath':
                'syslog/host-server/vrfs/vrf/ipv4s/ipv4/ipv4-severity-port/severity',
                'operation': 'edit'
            }),
        ])
        self._log_console_meta.update([
            ('a:enable-console', {
                'xpath': 'syslog/enable-console-logging',
                'operation': 'edit',
                'attrib': "operation"
            }),
            ('console', {
                'xpath': 'syslog/console-logging',
                'tag': True,
                'operation': 'edit',
                'attrib': "operation"
            }),
            ('a:console-level', {
                'xpath': 'syslog/console-logging/logging-level',
                'operation': 'edit'
            }),
        ])
        self._log_monitor_meta.update([
            ('monitor', {
                'xpath': 'syslog/monitor-logging',
                'tag': True,
                'operation': 'edit',
                'attrib': "operation"
            }),
            ('a:monitor-level', {
                'xpath': 'syslog/monitor-logging/logging-level',
                'operation': 'edit'
            }),
        ])
        self._log_buffered_size_meta.update([
            ('buffered', {
                'xpath': 'syslog/buffered-logging',
                'tag': True,
                'operation': 'edit',
                'attrib': "operation"
            }),
            ('a:size', {
                'xpath': 'syslog/buffered-logging/buffer-size',
                'operation': 'edit'
            }),
        ])
        self._log_buffered_level_meta.update([
            ('buffered', {
                'xpath': 'syslog/buffered-logging',
                'tag': True,
                'operation': 'edit',
                'attrib': "operation"
            }),
            ('a:level', {
                'xpath': 'syslog/buffered-logging/logging-level',
                'operation': 'edit'
            }),
        ])
        self._log_facility_meta.update([
            ('facility', {
                'xpath': 'syslog/logging-facilities',
                'tag': True,
                'operation': 'edit',
                'attrib': "operation"
            }),
            ('a:facility', {
                'xpath': 'syslog/logging-facilities/facility-level',
                'operation': 'edit'
            }),
        ])
        self._log_prefix_meta.update([
            ('a:hostnameprefix', {
                'xpath': 'syslog/host-name-prefix',
                'operation': 'edit',
                'attrib': "operation"
            }),
        ])

        state = self._module.params['state']

        _get_filter = build_xml('syslog', opcode="filter")
        running = get_config(self._module,
                             source='running',
                             config_filter=_get_filter)

        file_ele = etree_findall(running, 'file')
        file_list = list()
        if len(file_ele):
            for file in file_ele:
                file_name = etree_find(file, 'file-name')
                file_list.append(
                    file_name.text if file_name is not None else None)
        vrf_ele = etree_findall(running, 'vrf')
        host_list = list()
        for vrf in vrf_ele:
            host_ele = etree_findall(vrf, 'ipv4')
            for host in host_ele:
                host_name = etree_find(host, 'address')
                host_list.append(
                    host_name.text if host_name is not None else None)

        console_ele = etree_find(running, 'console-logging')
        console_level = etree_find(
            console_ele, 'logging-level') if console_ele is not None else None
        have_console = console_level.text if console_level is not None else None

        monitor_ele = etree_find(running, 'monitor-logging')
        monitor_level = etree_find(
            monitor_ele, 'logging-level') if monitor_ele is not None else None
        have_monitor = monitor_level.text if monitor_level is not None else None

        buffered_ele = etree_find(running, 'buffered-logging')
        buffered_size = etree_find(
            buffered_ele, 'buffer-size') if buffered_ele is not None else None
        have_buffered = buffered_size.text if buffered_size is not None else None

        facility_ele = etree_find(running, 'logging-facilities')
        facility_level = etree_find(
            facility_ele,
            'facility-level') if facility_ele is not None else None
        have_facility = facility_level.text if facility_level is not None else None

        prefix_ele = etree_find(running, 'host-name-prefix')
        have_prefix = prefix_ele.text if prefix_ele is not None else None

        console_enable_ele = etree_find(running, 'enable-console-logging')
        have_console_enable = console_enable_ele.text if console_enable_ele is not None else None

        file_params = list()
        host_params = list()
        console_params = dict()
        monitor_params = dict()
        buffered_params = dict()
        facility_params = dict()
        prefix_params = dict()

        opcode = None
        if state == 'absent':
            opcode = "delete"
            for item in self._want:
                if item['dest'] == 'file' and item['name'] in file_list:
                    item['level'] = severity_level[item['level']]
                    file_params.append(item)
                elif item['dest'] == 'host' and item['name'] in host_list:
                    item['level'] = severity_level[item['level']]
                    host_params.append(item)
                elif item['dest'] == 'console' and have_console:
                    console_params.update({'console-level': item['level']})
                elif item['dest'] == 'monitor' and have_monitor:
                    monitor_params.update({'monitor-level': item['level']})
                elif item['dest'] == 'buffered' and have_buffered:
                    buffered_params['size'] = str(
                        item['size']) if item['size'] else None
                    buffered_params[
                        'level'] = item['level'] if item['level'] else None
                elif item['dest'] is None and item['hostnameprefix'] is None and \
                        item['facility'] is not None and have_facility:
                    facility_params.update({'facility': item['facility']})
                elif item['dest'] is None and item[
                        'hostnameprefix'] is not None and have_prefix:
                    prefix_params.update(
                        {'hostnameprefix': item['hostnameprefix']})
        elif state == 'present':
            opcode = 'merge'
            for item in self._want:
                if item['dest'] == 'file':
                    item['level'] = severity_level[item['level']]
                    file_params.append(item)
                elif item['dest'] == 'host':
                    item['level'] = severity_level[item['level']]
                    host_params.append(item)
                elif item['dest'] == 'console':
                    console_params.update({'console-level': item['level']})
                elif item['dest'] == 'monitor':
                    monitor_params.update({'monitor-level': item['level']})
                elif item['dest'] == 'buffered':
                    buffered_params['size'] = str(
                        item['size']) if item['size'] else None
                    buffered_params[
                        'level'] = item['level'] if item['level'] else None
                elif item['dest'] is None and item['hostnameprefix'] is None and \
                        item['facility'] is not None:
                    facility_params.update({'facility': item['facility']})
                elif item['dest'] is None and item[
                        'hostnameprefix'] is not None:
                    prefix_params.update(
                        {'hostnameprefix': item['hostnameprefix']})

        self._result['xml'] = []
        _edit_filter_list = list()
        if opcode:
            if len(file_params):
                _edit_filter_list.append(
                    build_xml('syslog',
                              xmap=self._log_file_meta,
                              params=file_params,
                              opcode=opcode))
            if len(host_params):
                _edit_filter_list.append(
                    build_xml('syslog',
                              xmap=self._log_host_meta,
                              params=host_params,
                              opcode=opcode))
            if len(console_params):
                _edit_filter_list.append(
                    build_xml('syslog',
                              xmap=self._log_console_meta,
                              params=console_params,
                              opcode=opcode))
            if len(monitor_params):
                _edit_filter_list.append(
                    build_xml('syslog',
                              xmap=self._log_monitor_meta,
                              params=monitor_params,
                              opcode=opcode))
            if len(buffered_params):
                _edit_filter_list.append(
                    build_xml('syslog',
                              xmap=self._log_buffered_size_meta,
                              params=buffered_params,
                              opcode=opcode))
                _edit_filter_list.append(
                    build_xml('syslog',
                              xmap=self._log_buffered_level_meta,
                              params=buffered_params,
                              opcode=opcode))
            if len(facility_params):
                _edit_filter_list.append(
                    build_xml('syslog',
                              xmap=self._log_facility_meta,
                              params=facility_params,
                              opcode=opcode))
            if len(prefix_params):
                _edit_filter_list.append(
                    build_xml('syslog',
                              xmap=self._log_prefix_meta,
                              params=prefix_params,
                              opcode=opcode))

            diff = None
            if len(_edit_filter_list):
                commit = not self._module.check_mode
                diff = load_config(self._module,
                                   _edit_filter_list,
                                   commit=commit,
                                   running=running,
                                   nc_get_filter=_get_filter)

            if diff:
                if self._module._diff:
                    self._result['diff'] = dict(prepared=diff)

                self._result['xml'] = _edit_filter_list
                self._result['changed'] = True
Esempio n. 13
0
    def check_declarative_intent_params(self):
        failed_conditions = []

        self._data_rate_meta.update([
            ('interfaces', {'xpath': 'infra-statistics/interfaces', 'tag': True}),
            ('interface', {'xpath': 'infra-statistics/interfaces/interface', 'tag': True}),
            ('a:name', {'xpath': 'infra-statistics/interfaces/interface/interface-name'}),
            ('cache', {'xpath': 'infra-statistics/interfaces/interface/cache', 'tag': True}),
            ('data-rate', {'xpath': 'infra-statistics/interfaces/interface/cache/data-rate', 'tag': True}),
            ('input-data-rate', {'xpath': 'infra-statistics/interfaces/interface/cache/data-rate/input-data-rate', 'tag': True}),
            ('output-data-rate', {'xpath': 'infra-statistics/interfaces/interface/cache/data-rate/output-data-rate', 'tag': True}),
        ])

        self._line_state_meta.update([
            ('data-nodes', {'xpath': 'interface-properties/data-nodes', 'tag': True}),
            ('data-node', {'xpath': 'interface-properties/data-nodes/data-node', 'tag': True}),
            ('system-view', {'xpath': 'interface-properties/data-nodes/data-node/system-view', 'tag': True}),
            ('interfaces', {'xpath': 'interface-properties/data-nodes/data-node/system-view/interfaces', 'tag': True}),
            ('interface', {'xpath': 'interface-properties/data-nodes/data-node/system-view/interfaces/interface', 'tag': True}),
            ('a:name', {'xpath': 'interface-properties/data-nodes/data-node/system-view/interfaces/interface/interface-name'}),
            ('line-state', {'xpath': 'interface-properties/data-nodes/data-node/system-view/interfaces/interface/line-state', 'tag': True}),
        ])

        _rate_filter = build_xml('infra-statistics', xmap=self._data_rate_meta, params=self._want, opcode="filter")
        out = get_oper(self._module, filter=_rate_filter)
        data_rate_list = etree_findall(out, 'interface')
        data_rate_map = dict()
        for item in data_rate_list:
            data_rate_map.update({etree_find(item, 'interface-name').text: dict()})
            data_rate_map[etree_find(item, 'interface-name').text].update({'input-data-rate': etree_find(item, 'input-data-rate').text,
                                                                           'output-data-rate': etree_find(item, 'output-data-rate').text})

        _line_state_filter = build_xml('interface-properties', xmap=self._line_state_meta, params=self._want, opcode="filter")
        out = get_oper(self._module, filter=_line_state_filter)
        line_state_list = etree_findall(out, 'interface')
        line_state_map = dict()
        for item in line_state_list:
            line_state_map.update({etree_find(item, 'interface-name').text: etree_find(item, 'line-state').text})

        for want_item in self._want:
            want_state = want_item.get('state')
            want_tx_rate = want_item.get('tx_rate')
            want_rx_rate = want_item.get('rx_rate')
            if want_state not in ('up', 'down') and not want_tx_rate and not want_rx_rate:
                continue

            if self._result['changed']:
                sleep(want_item['delay'])

            if want_state in ('up', 'down'):
                if want_state not in line_state_map[want_item['name']]:
                    failed_conditions.append('state ' + 'eq({!s})'.format(want_state))

            if want_tx_rate:
                if want_tx_rate != data_rate_map[want_item['name']]['output-data-rate']:
                    failed_conditions.append('tx_rate ' + want_tx_rate)

            if want_rx_rate:
                if want_rx_rate != data_rate_map[want_item['name']]['input-data-rate']:
                    failed_conditions.append('rx_rate ' + want_rx_rate)

        if failed_conditions:
            msg = 'One or more conditional statements have not been satisfied'
            self._module.fail_json(msg=msg, failed_conditions=failed_conditions)
Esempio n. 14
0
    def map_obj_to_xml_rpc(self):
        self._intf_meta.update([
            ('interface-configuration', {'xpath': 'interface-configurations/interface-configuration', 'tag': True, 'attrib': 'operation'}),
            ('a:active', {'xpath': 'interface-configurations/interface-configuration/active', 'operation': 'edit'}),
            ('a:name', {'xpath': 'interface-configurations/interface-configuration/interface-name'}),
            ('a:description', {'xpath': 'interface-configurations/interface-configuration/description', 'operation': 'edit'}),
            ('mtus', {'xpath': 'interface-configurations/interface-configuration/mtus', 'tag': True, 'operation': 'edit'}),
            ('mtu', {'xpath': 'interface-configurations/interface-configuration/mtus/mtu', 'tag': True, 'operation': 'edit'}),
            ('a:owner', {'xpath': 'interface-configurations/interface-configuration/mtus/mtu/owner', 'operation': 'edit'}),
            ('a:mtu', {'xpath': 'interface-configurations/interface-configuration/mtus/mtu/mtu', 'operation': 'edit'}),
            ('CEthernet', {'xpath': 'interface-configurations/interface-configuration/ethernet', 'tag': True, 'operation': 'edit', 'ns': True}),
            ('a:speed', {'xpath': 'interface-configurations/interface-configuration/ethernet/speed', 'operation': 'edit'}),
            ('a:duplex', {'xpath': 'interface-configurations/interface-configuration/ethernet/duplex', 'operation': 'edit'}),
        ])

        self._shut_meta.update([
            ('interface-configuration', {'xpath': 'interface-configurations/interface-configuration', 'tag': True}),
            ('a:active', {'xpath': 'interface-configurations/interface-configuration/active', 'operation': 'edit'}),
            ('a:name', {'xpath': 'interface-configurations/interface-configuration/interface-name'}),
            ('shutdown', {'xpath': 'interface-configurations/interface-configuration/shutdown', 'tag': True, 'operation': 'edit', 'attrib': 'operation'}),
        ])
        state = self._module.params['state']

        _get_filter = build_xml('interface-configurations', xmap=self._intf_meta, params=self._want, opcode="filter")

        running = get_config(self._module, source='running', config_filter=_get_filter)
        intfcfg_nodes = etree_findall(running, 'interface-configuration')

        intf_list = set()
        shut_list = set()
        for item in intfcfg_nodes:
            intf_name = etree_find(item, 'interface-name').text
            if intf_name is not None:
                intf_list.add(intf_name)

                if etree_find(item, 'shutdown') is not None:
                    shut_list.add(intf_name)

        intf_params = list()
        shut_params = list()
        noshut_params = list()
        for index, item in enumerate(self._want):
            if item['name'] in intf_list:
                intf_params.append(item)
            if not item['enabled']:
                shut_params.append(item)
            if item['name'] in shut_list and item['enabled']:
                noshut_params.append(item)

        opcode = None
        if state == 'absent':
            if intf_params:
                opcode = "delete"
        elif state in ('present', 'up', 'down'):
            intf_params = self._want
            opcode = 'merge'

        self._result['xml'] = []
        _edit_filter_list = list()
        if opcode:
            _edit_filter_list.append(build_xml('interface-configurations', xmap=self._intf_meta,
                                               params=intf_params, opcode=opcode))

            if opcode == 'merge':
                if len(shut_params):
                    _edit_filter_list.append(build_xml('interface-configurations', xmap=self._shut_meta,
                                                       params=shut_params, opcode='merge'))
                if len(noshut_params):
                    _edit_filter_list.append(build_xml('interface-configurations', xmap=self._shut_meta,
                                                       params=noshut_params, opcode='delete'))
            diff = None
            if len(_edit_filter_list):
                commit = not self._module.check_mode
                diff = load_config(self._module, _edit_filter_list, commit=commit, running=running,
                                   nc_get_filter=_get_filter)

            if diff:
                if self._module._diff:
                    self._result['diff'] = dict(prepared=diff)

                self._result['xml'] = _edit_filter_list
                self._result['changed'] = True