Esempio n. 1
0
def filename_type(name_str):
    # Also allow . on filename, on top of what's allowed by filename_safe
    if re.sub(r'\.', '_', name_str) != filename_safe(name_str):
        raise argparse.ArgumentTypeError(
            'Invalid name "{name}". Only alphanumeric characters, "-", "_", and "." are allowed.'.format(name=name_str)
        )
    return name_str
Esempio n. 2
0
    def device_template_values(cls, api, show_args):
        def item_matches(item_name, item_id):
            if show_args.id is not None:
                return item_id == show_args.id
            if show_args.name is not None:
                return item_name == show_args.name
            return regex_search(show_args.regex, item_name)

        def template_values(ext_name, template_name, template_id):
            if show_args.workdir is None:
                # Load from vManage via API
                devices_attached = DeviceTemplateAttached.get(api, template_id)
                if devices_attached is None:
                    cls.log_error('Failed to retrieve %s attached devices',
                                  template_name)
                    return None

                try:
                    uuid_list = [uuid for uuid, _ in devices_attached]
                    values = DeviceTemplateValues(
                        api.post(
                            DeviceTemplateValues.api_params(
                                template_id, uuid_list),
                            DeviceTemplateValues.api_path.post))
                except RestAPIException:
                    cls.log_error('Failed to retrieve %s values',
                                  template_name)
                    return None
            else:
                # Load from local backup
                values = DeviceTemplateValues.load(show_args.workdir, ext_name,
                                                   template_name, template_id)
                if values is None:
                    cls.log_debug('Skipped %s. No template values file found.',
                                  template_name)

            return values

        print_buffer = []
        backend = show_args.workdir if show_args.workdir is not None else api
        matched_item_iter = ((index.need_extended_name, item_name, item_id,
                              tag, info)
                             for tag, info, index, item_cls in cls.index_iter(
                                 backend, catalog_entries('template_device'))
                             for item_id, item_name in index
                             if item_matches(item_name, item_id)
                             and issubclass(item_cls, DeviceTemplate))
        for use_ext_name, item_name, item_id, tag, info in matched_item_iter:
            attached_values = template_values(use_ext_name, item_name, item_id)
            if attached_values is None:
                continue

            cls.log_info('Inspecting %s %s values', info, item_name)
            var_names = attached_values.title_dict()
            for csv_id, csv_name, entry in attached_values:
                print_grp = [
                    'Template {name}, device {device}:'.format(name=item_name,
                                                               device=csv_name
                                                               or csv_id)
                ]
                results = Table('Name', 'Value', 'Variable')
                results.extend((var_names.get(var, '<not found>'), value, var)
                               for var, value in entry.items())
                if len(results) > 0:
                    if show_args.csv is not None:
                        filename = 'template_values_{name}_{id}.csv'.format(
                            name=filename_safe(item_name, lower=True),
                            id=csv_name or csv_id)
                        results.save(Path(show_args.csv, filename))
                    print_grp.extend(results.pretty_iter())
                print_buffer.append('\n'.join(print_grp))

        if len(print_buffer) > 0:
            if show_args.csv is not None:
                cls.log_info('Files saved under directory %s', show_args.csv)
            else:
                print('\n\n'.join(print_buffer))
        else:
            match_type = 'ID' if show_args.id is not None else 'name' if show_args.name is not None else 'regex'
            cls.log_warning('No items found with the %s provided', match_type)