Example #1
0
def genCoreParams(table, parent_plurality, parents, resource_name,
                  is_plural=True):
    depth = len(parent_plurality)
    plural = False

    params = []
    for level in range(depth):
        if parent_plurality[level]:
            param = {}
            param["name"] = "p"*(depth-level) + "id"
            param["in"] = "path"
            param["description"] = normalizeName(parents[level], plural) + " id"
            param["required"] = True
            param["type"] = "string"
            params.append(param)

    if is_plural:
        param = {}
        param["name"] = "id"
        param["in"] = "path"
        param["description"] = normalizeName(resource_name, plural) + " id"
        param["required"] = True
        param["type"] = "string"
        params.append(param)

    return params
Example #2
0
def genAPI(paths, definitions, schema, table, resource_name, parent,
           parents, parent_plurality):
    prefix = "/system"
    depth = len(parents)
    for index, ancestor in enumerate(parents):
        prefix = prefix + "/" + ancestor
        if parent_plurality[index]:
            idname = "{" + "p"*(depth - index) + "id}"
            prefix = prefix + "/" + idname

    # Parentless resources always have multiple instances
    if resource_name is None:
        # system table
        is_plural = False
    elif parent is None:
        is_plural = True
    elif resource_name not in parent.references:
        # parent relation always have multiple children
        is_plural = True
    else:
        is_plural = parent.references[resource_name].is_plural

    if resource_name is not None:
        path = prefix + "/" + resource_name
    else:
        path = prefix

    ops = {}
    if is_plural:
        op = genGetResource(table, parent_plurality, parents,
                            resource_name, False)
        if op is not None:
            ops["get"] = op
        op = genPostResource(table, parent_plurality, parents,
                             resource_name, False)
        if op is not None:
            ops["post"] = op
    else:
        op = genGetInstance(table, parent_plurality, parents,
                            resource_name, is_plural)
        if op is not None:
            ops["get"] = op
        op = genPutInstance(table, parent_plurality, parents,
                            resource_name, is_plural)
        if op is not None:
            ops["put"] = op
    paths[path] = ops

    if is_plural:
        path = path + "/{id}"
        ops = {}
        op = genGetInstance(table, parent_plurality, parents,
                            resource_name, is_plural)
        if op is not None:
            ops["get"] = op
        op = genPutInstance(table, parent_plurality, parents,
                            resource_name, is_plural)
        if op is not None:
            ops["put"] = op
        op = genDelInstance(table, parent_plurality, parents,
                            resource_name, is_plural)
        if op is not None:
            ops["delete"] = op
        paths[path] = ops

    getDefinition(schema, table, definitions)

    # Stop for system resource
    if resource_name is None:
        return

    # Recursive into next level resources
    for col_name in table.references:
        child_name = table.references[col_name].ref_table
        child_table = schema.ovs_tables[child_name]
        if col_name in table.children:
            # True child resources
            parents.append(resource_name)
            parent_plurality.append(is_plural)
            genAPI(paths, definitions, schema, child_table, col_name,
                   table, parents, parent_plurality)
            parents.pop()
            parent_plurality.pop()
        elif table.references[col_name].relation == "parent":
            continue
        else:
            # Referenced resources (no operation exposed)
            continue

    # For child resources declared with "parent" relationship
    for col_name in table.children:
        if col_name in table.references:
            # Processed already
            continue

        # Use plural form of the resource name in URI
        child_table = schema.ovs_tables[col_name]
        child_name = normalizeName(col_name)
        parents.append(resource_name)
        parent_plurality.append(is_plural)
        genAPI(paths, definitions, schema, child_table, child_name,
               table, parents, parent_plurality)
        parents.pop()
        parent_plurality.pop()