Ejemplo n.º 1
0
class LogicalExprSchema(BaseSchema):
    """Expression combining multiple other query expressions."""

    op = base.String(description="The operator.")
    # many=True does not work here for some reason.
    expr = base.List(
        base.Nested(
            lambda *a, **kw: ExprSchema(*a, **kw),  # pylint: disable=unnecessary-lambda
            description="A list of query expressions to combine.",
        ))
Ejemplo n.º 2
0
def attributes_field(
    object_type: ObjectType,
    object_context: ObjectContext,
    description: Optional[str] = None,
    example: Optional[Any] = None,
    required: bool = False,
    load_default: Any = utils.missing,
    many: bool = False,
    names_only: bool = False,
) -> _fields.Field:
    """Build an Attribute Field

    Args:
        object_type:
            May be one of 'folder', 'host' or 'cluster'.

        object_context:
            May be 'create' or 'update'. Deletion is considered as 'update'.

        description:
            A descriptive text of this field. Required.

        example:
            An example for the OpenAPI documentation. Required.

        required:
            Whether the field must be sent by the client or is option.

        load_default:
        many:

        names_only:
            When set to True, the field will be a List of Strings which validate the tag names only.

    Returns:

    """
    if description is None:
        # SPEC won't validate without description, though the error message is very obscure, so we
        # clarify this here by force.
        raise ValueError("description is necessary.")

    custom_schema = {
        "host": CustomHostAttributes,
        "cluster": CustomHostAttributes,
        "folder": CustomFolderAttributes,
    }
    if not names_only:
        return MultiNested(
            [
                attr_openapi_schema(object_type, object_context),
                custom_schema[object_type],
            ],
            metadata={"context": {
                "object_context": object_context
            }},
            merged=True,  # to unify both models
            description=description,
            example=example,
            many=many,
            load_default=dict
            if load_default is utils.missing else utils.missing,
            required=required,
        )

    attrs = {
        attr.name
        for attr in collect_attributes(object_type, object_context)
    }

    def validate(value):
        if value not in attrs:
            raise ValidationError(f"Unknown attribute: {value!r}")

    return base.List(
        base.String(validate=validate),
        description=description,
        example=example,
        load_default=load_default,
        required=required,
    )