magpie.api.management.resource.resource_utils

Module Contents

Functions

check_valid_service_or_resource_permission(permission_name: Str, service_or_resource: ServiceOrResourceType, db_session: Session) → Optional[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(parent_resource, resource_type, db_session)

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

check_unique_child_resource_name(resource_name: Str, parent_id: int, error_message: Str, db_session: Session) → None

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

crop_tree_with_permission(children: ChildrenResourceNodes, resource_id_list: List[int]) → Tuple[ChildrenResourceNodes, List[int]]

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

get_resource_path(resource_id: int, db_session: Session) → 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(service_or_resource: ServiceOrResourceType) → Tuple[Type[ServiceInterface], Str]

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

get_resource_children(resource: ServiceOrResourceType, db_session: Session, tree_service_builder: Optional[ResourceTreeService] = None) → ChildrenResourceNodes

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

get_resource_permissions(resource: ServiceOrResourceType, db_session: Session) → List[Permission]

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

get_resource_root_service(resource: ServiceOrResourceType, db_session: Session) → Optional[models.Service]

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

get_resource_root_service_by_id(resource_id: ServiceOrResourceType, db_session: Session) → Optional[models.Service]

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

get_resource_root_service_impl(resource: ServiceOrResourceType, request: Request) → ServiceInterface

Obtain the root service implementation.

create_resource(resource_name: Str, resource_display_name: Optional[Str], resource_type: Str, parent_id: int, db_session: Session) → HTTPException

delete_resource(request)

magpie.api.management.resource.resource_utils.check_valid_service_or_resource_permission(permission_name: Str, service_or_resource: ServiceOrResourceType, db_session: Session) → Optional[Permission][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

magpie.api.management.resource.resource_utils.check_valid_service_resource(parent_resource, resource_type, db_session)[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: Str, parent_id: int, error_message: Str, db_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: ChildrenResourceNodes, resource_id_list: List[int]) → Tuple[ChildrenResourceNodes, 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: Session) → 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: ServiceOrResourceType) → Tuple[Type[ServiceInterface], Str][source]

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

magpie.api.management.resource.resource_utils.get_resource_children(resource: ServiceOrResourceType, db_session: Session, tree_service_builder: Optional[ResourceTreeService] = None) → ChildrenResourceNodes[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 – service that build the tree (default: RESOURCE_TREE_SERVICE)

Returns

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

magpie.api.management.resource.resource_utils.get_resource_permissions(resource: ServiceOrResourceType, db_session: Session) → List[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: ServiceOrResourceType, db_session: Session) → Optional[models.Service][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: ServiceOrResourceType, db_session: Session) → Optional[models.Service][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: ServiceOrResourceType, request: Request) → 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: Str, resource_display_name: Optional[Str], resource_type: Str, parent_id: int, db_session: Session) → HTTPException[source]
magpie.api.management.resource.resource_utils.delete_resource(request)[source]