magpie.api.management.resource.resource_utils

Functions

check_valid_service_or_resource_permission(...)

Checks if a permission is valid to be applied to a specific service or a resource under a root service.

check_valid_service_resource(→ magpie.models.Service)

Checks if a new Resource can be contained under a parent Resource given the requested type and the corresponding

check_unique_child_resource_name(→ None)

Verify that resource will be unique amongst other resources at the same target position.

crop_tree_with_permission(...)

Recursively prunes all children resources from the tree hierarchy except listed ones matched by ID.

get_resource_path(→ magpie.typedefs.Str)

Obtains the full path representation of the specified resource ID from the root service it resides under using all

get_service_or_resource_types(...)

Obtain the service or resource class and a corresponding "service" or "resource" type identifier.

get_resource_parents(...)

Obtains the parent resource nodes of the input service or resource.

get_resource_children(...)

Obtains the children resource node structure of the input service or resource.

get_resource_permissions(...)

Obtains the applicable permissions on the service or resource, accordingly to what was provided.

get_resource_root_service(...)

Retrieves the service-specialized resource corresponding to the top-level resource in the tree hierarchy.

get_resource_root_service_by_id(...)

Retrieves the service-specialized resource corresponding to the top-level resource in the tree hierarchy.

get_resource_root_service_impl(...)

Obtain the root service implementation.

create_resource(→ pyramid.httpexceptions.HTTPException)

delete_resource(request)

Module Contents

magpie.api.management.resource.resource_utils.check_valid_service_or_resource_permission(permission_name: magpie.typedefs.Str | magpie.permissions.Permission, service_or_resource: magpie.typedefs.ServiceOrResourceType, db_session: sqlalchemy.orm.session.Session) magpie.permissions.Permission | None[source]

Checks if a permission is valid to be applied to a specific service or a resource under a root service.

Parameters:
  • permission_name – permission name to be validated

  • service_or_resource – resource item corresponding to either a Service or a Resource

  • db_session – db connection

Returns:

valid Permission if allowed by the service/resource

Raises:

HTTPBadRequest – if the permission is not valid for the targeted service/resource

magpie.api.management.resource.resource_utils.check_valid_service_resource(parent_resource: magpie.typedefs.ServiceOrResourceType, resource_type: magpie.typedefs.Str, db_session: sqlalchemy.orm.session.Session) magpie.models.Service[source]

Checks if a new Resource can be contained under a parent Resource given the requested type and the corresponding Service under which the parent Resource is already assigned.

Parameters:
  • parent_resource – Resource under which the new resource of resource_type must be placed

  • resource_type – desired resource type

  • db_session

Returns:

root Service if all checks were successful

magpie.api.management.resource.resource_utils.check_unique_child_resource_name(resource_name: magpie.typedefs.Str, parent_id: int, error_message: magpie.typedefs.Str, db_session: sqlalchemy.orm.session.Session) None[source]

Verify that resource will be unique amongst other resources at the same target position.

Verifies that the provided resource_name does not already exist amongst other children resources at the level immediately under the parent, for the specified parent resource.

Returns:

nothing if no conflict detected

Raises:

HTTPConflict – if the resource_name conflict with another existing resource

magpie.api.management.resource.resource_utils.crop_tree_with_permission(children: magpie.typedefs.NestedResourceNodes, resource_id_list: List[int]) Tuple[magpie.typedefs.NestedResourceNodes, List[int]][source]

Recursively prunes all children resources from the tree hierarchy except listed ones matched by ID.

Input children is expected to be a dictionary of resource nodes and children resources with their ID as keys:

{
    <res-id>: {
        "node": <res>,
        "children": {
            <res-id>: {
                "node": <res>,
                "children": { <...> }
            },
            <...>
    },
    <...>
}
Parameters:
  • children – full hierarchy of children resource nodes.

  • resource_id_list – resource IDs of nodes to preserve.

Returns:

pruned hierarchy of resource nodes.

magpie.api.management.resource.resource_utils.get_resource_path(resource_id: int, db_session: sqlalchemy.orm.session.Session) magpie.typedefs.Str[source]

Obtains the full path representation of the specified resource ID from the root service it resides under using all respective names of the intermediate resources.

For example, the following hierarchy:

<service-1> (id: 1)
    <resource-1> (id: 2)
        <resource-2> (id: 3)

Will return the following path: /service-1/resource-1/resource-2.

This is the same representation of the resource field within startup permissions configuration file.

magpie.api.management.resource.resource_utils.get_service_or_resource_types(service_or_resource: magpie.typedefs.ServiceOrResourceType) Tuple[Type[magpie.services.ServiceInterface], magpie.typedefs.Str][source]

Obtain the service or resource class and a corresponding "service" or "resource" type identifier.

magpie.api.management.resource.resource_utils.get_resource_parents(resource: magpie.typedefs.ServiceOrResourceType, db_session: sqlalchemy.orm.session.Session, tree_service_builder: ziggurat_foundations.models.services.resource_tree.ResourceTreeService | None = None) List[magpie.typedefs.ServiceOrResourceType][source]

Obtains the parent resource nodes of the input service or resource.

Parameters:
  • resource – Initial resource where to start building the list from.

  • db_session – Database connection to retrieve resources.

  • tree_service_builder – Utility that build the tree (default: models.RESOURCE_TREE_SERVICE).

Returns:

List of resources starting at input resource going all the way down to the root service.

magpie.api.management.resource.resource_utils.get_resource_children(resource: magpie.typedefs.ServiceOrResourceType, db_session: sqlalchemy.orm.session.Session, tree_service_builder: ziggurat_foundations.models.services.resource_tree.ResourceTreeService | None = None, limit_depth: int | None = None) magpie.typedefs.NestedResourceNodes[source]

Obtains the children resource node structure of the input service or resource.

Parameters:
  • resource – Initial resource where to start building the tree from.

  • db_session – Database connection to retrieve resources.

  • tree_service_builder – Utility that build the tree (default: models.RESOURCE_TREE_SERVICE).

  • limit_depth – Maximum depth to look for children resources (very deep if not specified, could be slow).

Returns:

{node: Resource, children: {node_id: <recursive>}}

magpie.api.management.resource.resource_utils.get_resource_permissions(resource: magpie.typedefs.ServiceOrResourceType, db_session: sqlalchemy.orm.session.Session) List[magpie.permissions.Permission][source]

Obtains the applicable permissions on the service or resource, accordingly to what was provided.

When parsing a resource, rewinds the hierarchy up to the top-most service in order to find the context under which the resource resides, and therefore which permissions this resource is allowed to have under that service.

magpie.api.management.resource.resource_utils.get_resource_root_service(resource: magpie.typedefs.ServiceOrResourceType, db_session: sqlalchemy.orm.session.Session) magpie.models.Service | None[source]

Retrieves the service-specialized resource corresponding to the top-level resource in the tree hierarchy.

See also

magpie.api.management.resource.resource_utils.get_resource_root_service_by_id(resource_id: magpie.typedefs.ServiceOrResourceType, db_session: sqlalchemy.orm.session.Session) magpie.models.Service | None[source]

Retrieves the service-specialized resource corresponding to the top-level resource in the tree hierarchy.

See also

magpie.api.management.resource.resource_utils.get_resource_root_service_impl(resource: magpie.typedefs.ServiceOrResourceType, request: pyramid.request.Request) magpie.services.ServiceInterface[source]

Obtain the root service implementation.

Retrieves the root-resource from the provided resource within a tree hierarchy and generates the corresponding top-level service’s implementation from the service_factory().

See also

get_resource_root_service() to retrieve only the service flavored resource model

magpie.api.management.resource.resource_utils.create_resource(resource_name: magpie.typedefs.Str, resource_display_name: magpie.typedefs.Str | None, resource_type: magpie.typedefs.Str, parent_id: int, db_session: sqlalchemy.orm.session.Session) pyramid.httpexceptions.HTTPException[source]
magpie.api.management.resource.resource_utils.delete_resource(request)[source]