Пример #1
0
 def build_cmdref_objects(td):
     cmd_ref[td['type']]['ref'] = []
     saved_ids = []
     if want.get(td['name']):
         for playvals in want[td['name']]:
             valiate_input(playvals, td['name'], self._module)
             if playvals['id'] in saved_ids:
                 continue
             saved_ids.append(playvals['id'])
             resource_key = td['cmd'].format(playvals['id'])
             # Only build the NxosCmdRef object for the td['name'] module parameters.
             self._module.params[
                 'config'] = get_module_params_subsection(
                     ALL_MP, td['type'], playvals['id'])
             cmd_ref[td['type']]['ref'].append(
                 NxosCmdRef(self._module, td['obj']))
             ref = cmd_ref[td['type']]['ref'][-1]
             ref.set_context([resource_key])
             if td['type'] == 'TMS_SENSORGROUP' and get_setval_path(
                     self._module):
                 # Sensor group path setting can contain optional values.
                 # Call get_setval_path helper function to process any
                 # optional setval keys.
                 ref._ref['path']['setval'] = get_setval_path(
                     self._module)
             ref.get_existing(device_cache)
             ref.get_playvals()
             if td['type'] == 'TMS_DESTGROUP':
                 normalize_data(ref)
Пример #2
0
    def set_state(self, want, have):
        """ Select the appropriate function based on the state provided
        :param want: the desired configuration as a dictionary
        :param have: the current configuration as a dictionary
        :rtype: A list
        :returns: the commands necessary to migrate the current configuration
                  to the desired configuration
        """
        state = self._module.params['state']

        # The deleted case is very simple since we purge all telemetry config
        # and does not require any processing using NxosCmdRef objects.
        if state == 'deleted':
            return self._state_deleted(want, have)

        # Save off module params
        ALL_MP = self._module.params['config']

        cmd_ref = {}
        cmd_ref['TMS_GLOBAL'] = {}
        cmd_ref['TMS_DESTGROUP'] = {}
        cmd_ref['TMS_SENSORGROUP'] = {}
        cmd_ref['TMS_SUBSCRIPTION'] = {}

        # Get Telemetry Global Data
        cmd_ref['TMS_GLOBAL']['ref'] = []
        self._module.params['config'] = get_module_params_subsection(
            ALL_MP, 'TMS_GLOBAL')
        cmd_ref['TMS_GLOBAL']['ref'].append(
            NxosCmdRef(self._module, TMS_GLOBAL))
        ref = cmd_ref['TMS_GLOBAL']['ref'][0]
        ref.set_context()
        ref.get_existing()
        ref.get_playvals()
        device_cache = ref.cache_existing

        # Get Telemetry Destination Group Data
        if want.get('destination_groups'):
            td = {
                'name': 'destination_groups',
                'type': 'TMS_DESTGROUP',
                'obj': TMS_DESTGROUP,
                'cmd': 'destination-group {0}'
            }
            cmd_ref[td['type']]['ref'] = []
            saved_ids = []
            for playvals in want[td['name']]:
                valiate_input(playvals, td['name'], self._module)
                if playvals['id'] in saved_ids:
                    continue
                saved_ids.append(playvals['id'])
                resource_key = td['cmd'].format(playvals['id'])
                # Only build the NxosCmdRef object for the destination group module parameters.
                self._module.params['config'] = get_module_params_subsection(
                    ALL_MP, td['type'], playvals['id'])
                cmd_ref[td['type']]['ref'].append(
                    NxosCmdRef(self._module, td['obj']))
                ref = cmd_ref[td['type']]['ref'][-1]
                ref.set_context([resource_key])
                ref.get_existing(device_cache)
                ref.get_playvals()
                normalize_data(ref)

        # Get Telemetry Sensor Group Data
        if want.get('sensor_groups'):
            td = {
                'name': 'sensor_groups',
                'type': 'TMS_SENSORGROUP',
                'obj': TMS_SENSORGROUP,
                'cmd': 'sensor-group {0}'
            }
            cmd_ref[td['type']]['ref'] = []
            saved_ids = []
            for playvals in want[td['name']]:
                valiate_input(playvals, td['name'], self._module)
                if playvals['id'] in saved_ids:
                    continue
                saved_ids.append(playvals['id'])
                resource_key = td['cmd'].format(playvals['id'])
                # Only build the NxosCmdRef object for the sensor group module parameters.
                self._module.params['config'] = get_module_params_subsection(
                    ALL_MP, td['type'], playvals['id'])
                cmd_ref[td['type']]['ref'].append(
                    NxosCmdRef(self._module, td['obj']))
                ref = cmd_ref[td['type']]['ref'][-1]
                ref.set_context([resource_key])
                if get_setval_path(self._module):
                    # Sensor group path setting can contain optional values.
                    # Call get_setval_path helper function to process any
                    # optional setval keys.
                    ref._ref['path']['setval'] = get_setval_path(self._module)
                ref.get_existing(device_cache)
                ref.get_playvals()

        # Get Telemetry Subscription Data
        if want.get('subscriptions'):
            td = {
                'name': 'subscriptions',
                'type': 'TMS_SUBSCRIPTION',
                'obj': TMS_SUBSCRIPTION,
                'cmd': 'subscription {0}'
            }
            cmd_ref[td['type']]['ref'] = []
            saved_ids = []
            for playvals in want[td['name']]:
                valiate_input(playvals, td['name'], self._module)
                if playvals['id'] in saved_ids:
                    continue
                saved_ids.append(playvals['id'])
                resource_key = td['cmd'].format(playvals['id'])
                # Only build the NxosCmdRef object for the subscription module parameters.
                self._module.params['config'] = get_module_params_subsection(
                    ALL_MP, td['type'], playvals['id'])
                cmd_ref[td['type']]['ref'].append(
                    NxosCmdRef(self._module, td['obj']))
                ref = cmd_ref[td['type']]['ref'][-1]
                ref.set_context([resource_key])
                ref.get_existing(device_cache)
                ref.get_playvals()

        if state == 'overridden':
            if want == have:
                return []
            commands = self._state_overridden(cmd_ref, want, have)
        elif state == 'merged':
            if want == have:
                return []
            commands = self._state_merged(cmd_ref)
        elif state == 'replaced':
            if want == have:
                return []
            commands = self._state_replaced(cmd_ref)
        return commands
Пример #3
0
    def populate_facts(self, connection, ansible_facts, data=None):
        """ Populate the facts for telemetry
        :param connection: the device connection
        :param ansible_facts: Facts dictionary
        :param data: previously collected conf
        :rtype: dictionary
        :returns: facts
        """
        if connection:  # just for linting purposes, remove
            pass

        cmd_ref = {}
        cmd_ref['TMS_GLOBAL'] = {}
        cmd_ref['TMS_DESTGROUP'] = {}
        cmd_ref['TMS_SENSORGROUP'] = {}
        cmd_ref['TMS_SUBSCRIPTION'] = {}

        # For fact gathering, module state should be 'present' when using
        # NxosCmdRef to query state
        if self._module.params.get('state'):
            saved_module_state = self._module.params['state']
            self._module.params['state'] = 'present'

        # Get Telemetry Global Data
        cmd_ref['TMS_GLOBAL']['ref'] = []
        cmd_ref['TMS_GLOBAL']['ref'].append(NxosCmdRef(self._module, TMS_GLOBAL))
        ref = cmd_ref['TMS_GLOBAL']['ref'][0]
        ref.set_context()
        ref.get_existing()
        device_cache = ref.cache_existing

        if device_cache is None:
            device_cache_lines = []
        else:
            device_cache_lines = device_cache.split("\n")

        # Get Telemetry Destination Group Data
        cmd_ref['TMS_DESTGROUP']['ref'] = []
        for line in device_cache_lines:
            if re.search(r'destination-group', line):
                resource_key = line.strip()
                cmd_ref['TMS_DESTGROUP']['ref'].append(NxosCmdRef(self._module, TMS_DESTGROUP))
                ref = cmd_ref['TMS_DESTGROUP']['ref'][-1]
                ref.set_context([resource_key])
                ref.get_existing(device_cache)
                normalize_data(ref)

        # Get Telemetry Sensorgroup Group Data
        cmd_ref['TMS_SENSORGROUP']['ref'] = []
        for line in device_cache_lines:
            if re.search(r'sensor-group', line):
                resource_key = line.strip()
                cmd_ref['TMS_SENSORGROUP']['ref'].append(NxosCmdRef(self._module, TMS_SENSORGROUP))
                ref = cmd_ref['TMS_SENSORGROUP']['ref'][-1]
                ref.set_context([resource_key])
                ref.get_existing(device_cache)

        # Get Telemetry Subscription Data
        cmd_ref['TMS_SUBSCRIPTION']['ref'] = []
        for line in device_cache_lines:
            if re.search(r'subscription', line):
                resource_key = line.strip()
                cmd_ref['TMS_SUBSCRIPTION']['ref'].append(NxosCmdRef(self._module, TMS_SUBSCRIPTION))
                ref = cmd_ref['TMS_SUBSCRIPTION']['ref'][-1]
                ref.set_context([resource_key])
                ref.get_existing(device_cache)

        objs = []
        objs = self.render_config(self.generated_spec, cmd_ref)
        facts = {'telemetry': {}}
        if objs:
            # params = utils.validate_config(self.argument_spec, {'config': objs})
            facts['telemetry'] = objs

        ansible_facts['ansible_network_resources'].update(facts)
        if self._module.params.get('state'):
            self._module.params['state'] = saved_module_state
        return ansible_facts