|
| 1 | +from typing import TYPE_CHECKING, Any, Optional |
| 2 | + |
| 3 | +from graphene import InputObjectType, Mutation |
| 4 | +from graphql import GraphQLResolveInfo |
| 5 | +from typing_extensions import Self |
| 6 | + |
| 7 | +from infrahub.core.branch import Branch |
| 8 | +from infrahub.core.constants import RESTRICTED_NAMESPACES |
| 9 | +from infrahub.core.manager import NodeManager |
| 10 | +from infrahub.core.node import Node |
| 11 | +from infrahub.core.protocols import CoreMenuItem |
| 12 | +from infrahub.core.schema import NodeSchema |
| 13 | +from infrahub.database import InfrahubDatabase |
| 14 | +from infrahub.exceptions import ValidationError |
| 15 | +from infrahub.graphql.mutations.main import InfrahubMutationMixin |
| 16 | + |
| 17 | +from .main import InfrahubMutationOptions |
| 18 | + |
| 19 | +if TYPE_CHECKING: |
| 20 | + from infrahub.graphql.initialization import GraphqlContext |
| 21 | + |
| 22 | +EXTENDED_RESTRICTED_NAMESPACES = RESTRICTED_NAMESPACES + ["Builtin"] |
| 23 | + |
| 24 | + |
| 25 | +def validate_namespace(data: InputObjectType) -> None: |
| 26 | + namespace = data.get("namespace") |
| 27 | + if isinstance(namespace, dict) and "value" in namespace: |
| 28 | + namespace_value = str(namespace.get("value")) |
| 29 | + if namespace_value in EXTENDED_RESTRICTED_NAMESPACES: |
| 30 | + raise ValidationError( |
| 31 | + input_value={"namespace": f"{namespace_value} is not valid, it's a restricted namespace"} |
| 32 | + ) |
| 33 | + |
| 34 | + |
| 35 | +class InfrahubCoreMenuMutation(InfrahubMutationMixin, Mutation): |
| 36 | + @classmethod |
| 37 | + def __init_subclass_with_meta__( # pylint: disable=arguments-differ |
| 38 | + cls, schema: NodeSchema, _meta: Optional[Any] = None, **options: dict[str, Any] |
| 39 | + ) -> None: |
| 40 | + # Make sure schema is a valid NodeSchema Node Class |
| 41 | + if not isinstance(schema, NodeSchema): |
| 42 | + raise ValueError(f"You need to pass a valid NodeSchema in '{cls.__name__}.Meta', received '{schema}'") |
| 43 | + |
| 44 | + if not _meta: |
| 45 | + _meta = InfrahubMutationOptions(cls) |
| 46 | + _meta.schema = schema |
| 47 | + |
| 48 | + super().__init_subclass_with_meta__(_meta=_meta, **options) |
| 49 | + |
| 50 | + @classmethod |
| 51 | + async def mutate_create( |
| 52 | + cls, |
| 53 | + info: GraphQLResolveInfo, |
| 54 | + data: InputObjectType, |
| 55 | + branch: Branch, |
| 56 | + at: str, |
| 57 | + database: Optional[InfrahubDatabase] = None, |
| 58 | + ) -> tuple[Node, Self]: |
| 59 | + validate_namespace(data=data) |
| 60 | + |
| 61 | + obj, result = await super().mutate_create(info=info, data=data, branch=branch, at=at) |
| 62 | + |
| 63 | + return obj, result |
| 64 | + |
| 65 | + @classmethod |
| 66 | + async def mutate_update( |
| 67 | + cls, |
| 68 | + info: GraphQLResolveInfo, |
| 69 | + data: InputObjectType, |
| 70 | + branch: Branch, |
| 71 | + at: str, |
| 72 | + database: Optional[InfrahubDatabase] = None, |
| 73 | + node: Optional[Node] = None, |
| 74 | + ) -> tuple[Node, Self]: |
| 75 | + context: GraphqlContext = info.context |
| 76 | + |
| 77 | + obj = await NodeManager.find_object( |
| 78 | + db=context.db, kind=CoreMenuItem, id=data.get("id"), hfid=data.get("hfid"), branch=branch, at=at |
| 79 | + ) |
| 80 | + validate_namespace(data=data) |
| 81 | + |
| 82 | + if obj.protected.value: |
| 83 | + raise ValidationError(input_value="This object is protected, it can't be modified.") |
| 84 | + |
| 85 | + obj, result = await super().mutate_update(info=info, data=data, branch=branch, at=at, node=obj) # type: ignore[assignment] |
| 86 | + return obj, result # type: ignore[return-value] |
| 87 | + |
| 88 | + @classmethod |
| 89 | + async def mutate_delete( |
| 90 | + cls, |
| 91 | + info: GraphQLResolveInfo, |
| 92 | + data: InputObjectType, |
| 93 | + branch: Branch, |
| 94 | + at: str, |
| 95 | + ) -> tuple[Node, Self]: |
| 96 | + context: GraphqlContext = info.context |
| 97 | + obj = await NodeManager.find_object( |
| 98 | + db=context.db, kind=CoreMenuItem, id=data.get("id"), hfid=data.get("hfid"), branch=branch, at=at |
| 99 | + ) |
| 100 | + if obj.protected.value: |
| 101 | + raise ValidationError(input_value="This object is protected, it can't be deleted.") |
| 102 | + |
| 103 | + return await super().mutate_delete(info=info, data=data, branch=branch, at=at) |
0 commit comments