Ejemplo n.º 1
0
 def render_resource(cls, resource_path):
     resource_class = cls.resolve(resource_path)
     provider_name, resource_name = resource_path.split('.', 1)
     return cls._render('resource.rst',
         variables=dict(
             provider_name=provider_name,
             resource_name="%s.%s" % (provider_name, resource_class.type),
             filters=ElementSchema.elements(resource_class.filter_registry),
             actions=ElementSchema.elements(resource_class.action_registry),
             resource=resource_class))
Ejemplo n.º 2
0
def _main(provider, output_dir, group_by):
    """Generate RST docs for a given cloud provider's resources
    """
    env = init()

    logging.basicConfig(level=logging.INFO)
    output_dir = os.path.abspath(output_dir)
    provider_class = clouds[provider]

    # group by will be provider specific, supports nested attributes
    group_by = operator.attrgetter(group_by or "type")

    files = []

    groups = {}

    for r in provider_class.resources.values():
        group = group_by(r)
        if not isinstance(group, list):
            group = [group]
        for g in group:
            groups.setdefault(g, []).append(r)

    # Create individual resources pages
    for r in provider_class.resources.values():
        rpath = resource_file_name(output_dir, r)
        with open(rpath, 'w') as fh:
            t = env.get_template('provider-resource.rst')
            fh.write(t.render(
                provider_name=provider,
                resource=r))

    # Create files for all groups
    for key, group in sorted(groups.items()):
        group = sorted(group, key=operator.attrgetter('type'))
        rpath = os.path.join(output_dir, "group-%s.rst" % key).replace(' ', '-').lower()
        with open(rpath, 'w') as fh:
            t = env.get_template('provider-group.rst')
            fh.write(t.render(
                provider_name=provider,
                key=key,
                resource_files=[os.path.basename(resource_file_name(output_dir, r)) for r in group],
                resources=group))
        files.append(os.path.basename(rpath))

    # Write out common provider filters & actions
    common_actions = {}
    common_filters = {}
    for r in provider_class.resources.values():
        for f in ElementSchema.elements(r.filter_registry):
            if not f.schema_alias:
                continue
            common_filters[ElementSchema.name(f)] = (f, r)

        for a in ElementSchema.elements(r.action_registry):
            if not a.schema_alias:
                continue
            common_actions[ElementSchema.name(a)] = (a, r)

    fpath = os.path.join(
        output_dir, "%s-common-filters.rst" % provider_class.type.lower())
    with open(fpath, 'w') as fh:
        t = env.get_template('provider-common-elements.rst')
        fh.write(t.render(
            provider_name=provider_class.display_name,
            element_type='filters',
            elements=[common_filters[k] for k in sorted(common_filters)]))
        files.insert(0, os.path.basename(fpath))

    fpath = os.path.join(
        output_dir, "%s-common-actions.rst" % provider_class.type.lower())
    with open(fpath, 'w') as fh:
        t = env.get_template('provider-common-elements.rst')
        fh.write(t.render(
            provider_name=provider_class.display_name,
            element_type='actions',
            elements=[common_actions[k] for k in sorted(common_actions)]))
        files.insert(0, os.path.basename(fpath))

    log.info("%s Wrote %d resources groups", provider.title(), len(files))

    # Write out the provider index
    provider_path = os.path.join(output_dir, 'index.rst')
    with open(provider_path, 'w') as fh:
        log.info("Writing Provider Index to %s", provider_path)
        t = env.get_template('provider-index.rst')
        fh.write(t.render(provider_name=provider_class.display_name, files=files))
Ejemplo n.º 3
0
def _main(provider, output_dir, group_by):
    """Generate RST docs for a given cloud provider's resources
    """
    env = init(provider)

    logging.basicConfig(level=logging.INFO)
    output_dir = os.path.abspath(output_dir)
    provider_class = clouds[provider]

    # group by will be provider specific, supports nested attributes
    group_by = operator.attrgetter(group_by or "type")

    files = []
    written = 0
    groups = {}

    for r in provider_class.resources.values():
        group = group_by(r)
        if not isinstance(group, list):
            group = [group]
        for g in group:
            groups.setdefault(g, []).append(r)

    # Create individual resources pages
    for r in provider_class.resources.values():
        # FIXME / TODO: temporary work arounds for a few types that have recursion
        # in jsonschema on these types.
        if provider == 'awscc' and r.type in (
                'wafv2_rulegroup', 'wafv2_webacl', 'amplifyuibuilder_theme',
                'amplifyuibuilder_component', 'amplifybuilder_component'):
            continue
        rpath = resource_file_name(output_dir, r)
        t = env.get_template('provider-resource.rst')
        written += write_modified_file(rpath,
                                       t.render(provider_name=provider,
                                                resource=r),
                                       diff_changes=not written)

    # Create files for all groups
    for key, group in sorted(groups.items()):
        group = sorted(group, key=operator.attrgetter('type'))
        rpath = os.path.join(output_dir,
                             ("group-%s.rst" % key).replace(' ', '-').lower())
        t = env.get_template('provider-group.rst')
        written += write_modified_file(
            rpath,
            t.render(provider_name=provider,
                     key=key,
                     resource_files=[
                         os.path.basename(resource_file_name(output_dir, r))
                         for r in group
                     ],
                     resources=group))
        files.append(os.path.basename(rpath))

    # Write out common provider filters & actions
    common_actions = {}
    common_filters = {}
    for _, r in sorted(provider_class.resources.items()):
        for f in ElementSchema.elements(r.filter_registry):
            if not f.schema_alias:
                continue
            common_filters[ElementSchema.name(f)] = (f, r)

        for a in ElementSchema.elements(r.action_registry):
            if not a.schema_alias:
                continue
            common_actions[ElementSchema.name(a)] = (a, r)

    fpath = os.path.join(
        output_dir, ("%s-common-filters.rst" % provider_class.type.lower()))

    t = env.get_template('provider-common-elements.rst')
    written += write_modified_file(
        fpath,
        t.render(provider_name=provider_class.display_name,
                 element_type='filters',
                 elements=[common_filters[k] for k in sorted(common_filters)]))
    files.insert(0, os.path.basename(fpath))

    fpath = os.path.join(
        output_dir, ("%s-common-actions.rst" % provider_class.type.lower()))
    t = env.get_template('provider-common-elements.rst')
    written += write_modified_file(
        fpath,
        t.render(provider_name=provider_class.display_name,
                 element_type='actions',
                 elements=[common_actions[k] for k in sorted(common_actions)]))
    files.insert(0, os.path.basename(fpath))

    # Write out provider modes
    modes = get_provider_modes(provider)
    mode_path = os.path.join(output_dir,
                             '%s-modes.rst' % provider_class.type.lower())
    t = env.get_template('provider-mode.rst')
    written += write_modified_file(
        mode_path,
        t.render(provider_name=provider_class.display_name, modes=modes))
    files.insert(0, os.path.basename(mode_path))

    # Write out the provider index
    provider_path = os.path.join(output_dir, 'index.rst')
    t = env.get_template('provider-index.rst')
    written += write_modified_file(
        provider_path,
        t.render(provider_name=provider_class.display_name, files=files))

    if written:
        log.info("%s Wrote %d files", provider.title(), written)