diff --git a/netbox_aci_plugin/graphql/__init__.py b/netbox_aci_plugin/graphql/__init__.py index 2376ea8..3d63509 100644 --- a/netbox_aci_plugin/graphql/__init__.py +++ b/netbox_aci_plugin/graphql/__init__.py @@ -1,3 +1,3 @@ -from .schema import schema +from .schema import NetBoxACIQuery -__all__ = ["schema"] +schema = [NetBoxACIQuery] diff --git a/netbox_aci_plugin/graphql/schema.py b/netbox_aci_plugin/graphql/schema.py index 1102cba..1e6dbbe 100644 --- a/netbox_aci_plugin/graphql/schema.py +++ b/netbox_aci_plugin/graphql/schema.py @@ -6,13 +6,6 @@ import strawberry import strawberry_django -from ..models.tenant_app_profiles import ACIAppProfile, ACIEndpointGroup -from ..models.tenant_networks import ( - ACIVRF, - ACIBridgeDomain, - ACIBridgeDomainSubnet, -) -from ..models.tenants import ACITenant from .types import ( ACIAppProfileType, ACIBridgeDomainSubnetType, @@ -23,85 +16,34 @@ ) -@strawberry.type -class ACITenantsQuery: - """GraphQL query definition for ACITenant model.""" +@strawberry.type(name="Query") +class NetBoxACIQuery: + """GraphQL query definition for the NetBox ACI Plugin.""" + aci_tenant: ACITenantType = strawberry_django.field() aci_tenant_list: List[ACITenantType] = strawberry_django.field() - @strawberry.field - def aci_tenant(self, id: int) -> ACITenantType: - return ACITenant.objects.get(pk=id) - - -@strawberry.type -class ACIAppProfilesQuery: - """GraphQL query definition for ACIAppProfile model.""" - + aci_application_profile: ACIAppProfileType = strawberry_django.field() aci_application_profile_list: List[ACIAppProfileType] = ( strawberry_django.field() ) - @strawberry.field - def aci_application_profile(self, id: int) -> ACIAppProfileType: - return ACIAppProfile.objects.get(pk=id) - - -@strawberry.type -class ACIVRFQuery: - """GraphQL query definition for ACIVRF model.""" - + aci_vrf: ACIVRFType = strawberry_django.field() aci_vrf_list: List[ACIVRFType] = strawberry_django.field() - @strawberry.field - def aci_vrf(self, id: int) -> ACIVRFType: - return ACIVRF.objects.get(pk=id) - - -@strawberry.type -class ACIBridgeDomainQuery: - """GraphQL query definition for ACIBridgeDomain model.""" - + aci_bridge_domain: ACIBridgeDomainType = strawberry_django.field() aci_bridge_domain_list: List[ACIBridgeDomainType] = ( strawberry_django.field() ) - @strawberry.field - def aci_bridge_domain(self, id: int) -> ACIBridgeDomainType: - return ACIBridgeDomain.objects.get(pk=id) - - -@strawberry.type -class ACIBridgeDomainSubnetQuery: - """GraphQL query definition for ACIBridgeDomainSubnet model.""" - + aci_bridge_domain_subnet: ACIBridgeDomainSubnetType = ( + strawberry_django.field() + ) aci_bridge_domain_subnet_list: List[ACIBridgeDomainSubnetType] = ( strawberry_django.field() ) - @strawberry.field - def aci_bridge_domain_subnet(self, id: int) -> ACIBridgeDomainSubnetType: - return ACIBridgeDomainSubnet.objects.get(pk=id) - - -@strawberry.type -class ACIEndpointGroupQuery: - """GraphQL query definition for ACIEndpointGroup model.""" - + aci_endpoint_group: ACIEndpointGroupType = strawberry_django.field() aci_endpoint_group_list: List[ACIEndpointGroupType] = ( strawberry_django.field() ) - - @strawberry.field - def aci_endpoint_group(self, id: int) -> ACIEndpointGroupType: - return ACIEndpointGroup.objects.get(pk=id) - - -schema: list = [ - ACITenantsQuery, - ACIAppProfilesQuery, - ACIBridgeDomainQuery, - ACIBridgeDomainSubnetQuery, - ACIEndpointGroupQuery, - ACIVRFQuery, -] diff --git a/netbox_aci_plugin/graphql/types.py b/netbox_aci_plugin/graphql/types.py index 514cdb4..5a1ab6e 100644 --- a/netbox_aci_plugin/graphql/types.py +++ b/netbox_aci_plugin/graphql/types.py @@ -2,8 +2,9 @@ # # SPDX-License-Identifier: GPL-3.0-or-later -from typing import List, Optional +from typing import Annotated, List, Optional +import strawberry import strawberry_django from ipam.graphql.types import IPAddressType, VRFType from netbox.graphql.types import NetBoxObjectType @@ -30,7 +31,10 @@ class ACITenantType(NetBoxObjectType): """GraphQL type definition for ACITenant model.""" - nb_tenant: Optional[TenantType] + nb_tenant: ( + Annotated["TenantType", strawberry.lazy("tenancy.graphql.types")] + | None + ) @strawberry_django.type( @@ -39,17 +43,27 @@ class ACITenantType(NetBoxObjectType): class ACIAppProfileType(NetBoxObjectType): """GraphQL type definition for ACIAppProfile model.""" - aci_tenant: ACITenantType - nb_tenant: Optional[TenantType] + aci_tenant: Annotated[ + "ACITenantType", strawberry.lazy("netbox_aci_plugin.graphql.types") + ] + nb_tenant: ( + Annotated["TenantType", strawberry.lazy("tenancy.graphql.types")] + | None + ) @strawberry_django.type(ACIVRF, fields="__all__", filters=ACIVRFFilter) class ACIVRFType(NetBoxObjectType): """GraphQL type definition for ACIVRF model.""" - aci_tenant: ACITenantType - nb_tenant: Optional[TenantType] - nb_vrf: Optional[VRFType] + aci_tenant: Annotated[ + "ACITenantType", strawberry.lazy("netbox_aci_plugin.graphql.types") + ] + nb_tenant: ( + Annotated["TenantType", strawberry.lazy("tenancy.graphql.types")] + | None + ) + nb_vrf: Annotated["VRFType", strawberry.lazy("ipam.graphql.types")] | None dns_labels: Optional[List[str]] @@ -59,9 +73,16 @@ class ACIVRFType(NetBoxObjectType): class ACIBridgeDomainType(NetBoxObjectType): """GraphQL type definition for ACIBridgeDomain model.""" - aci_tenant: ACITenantType - aci_vrf: ACIVRFType - nb_tenant: Optional[TenantType] + aci_tenant: Annotated[ + "ACITenantType", strawberry.lazy("netbox_aci_plugin.graphql.types") + ] + aci_vrf: Annotated[ + "ACIVRFType", strawberry.lazy("netbox_aci_plugin.graphql.types") + ] + nb_tenant: ( + Annotated["TenantType", strawberry.lazy("tenancy.graphql.types")] + | None + ) dhcp_labels: Optional[List[str]] mac_address: Optional[str] virtual_mac_address: Optional[str] @@ -75,9 +96,17 @@ class ACIBridgeDomainType(NetBoxObjectType): class ACIBridgeDomainSubnetType(NetBoxObjectType): """GraphQL type definition for ACIBridgeDomainSubnet model.""" - aci_bridge_domain: ACIBridgeDomainType - gateway_ip_address: IPAddressType - nb_tenant: Optional[TenantType] + aci_bridge_domain: Annotated[ + "ACIBridgeDomainType", + strawberry.lazy("netbox_aci_plugin.graphql.types"), + ] + gateway_ip_address: Annotated[ + "IPAddressType", strawberry.lazy("ipam.graphql.types") + ] + nb_tenant: ( + Annotated["TenantType", strawberry.lazy("tenancy.graphql.types")] + | None + ) @strawberry_django.type( @@ -88,6 +117,14 @@ class ACIBridgeDomainSubnetType(NetBoxObjectType): class ACIEndpointGroupType(NetBoxObjectType): """GraphQL type definition for ACIEndpointGroup model.""" - aci_app_profile: ACIAppProfileType - aci_bridge_domain: ACIBridgeDomainType - nb_tenant: Optional[TenantType] + aci_app_profile: Annotated[ + "ACIAppProfileType", strawberry.lazy("netbox_aci_plugin.graphql.types") + ] + aci_bridge_domain: Annotated[ + "ACIBridgeDomainType", + strawberry.lazy("netbox_aci_plugin.graphql.types"), + ] + nb_tenant: ( + Annotated["TenantType", strawberry.lazy("tenancy.graphql.types")] + | None + ) diff --git a/netbox_aci_plugin/tests/test_api.py b/netbox_aci_plugin/tests/test_api.py index d7a0859..2df0c83 100644 --- a/netbox_aci_plugin/tests/test_api.py +++ b/netbox_aci_plugin/tests/test_api.py @@ -110,6 +110,7 @@ class ACIAppProfileAPIViewTestCase(APIViewTestCases.APIViewTestCase): "nb_tenant", "url", ] + user_permissions = ("netbox_aci_plugin.view_acitenant",) @classmethod def setUpTestData(cls) -> None: @@ -187,6 +188,7 @@ class ACIVRFAPIViewTestCase(APIViewTestCases.APIViewTestCase): "nb_vrf", "url", ] + user_permissions = ("netbox_aci_plugin.view_acitenant",) @classmethod def setUpTestData(cls) -> None: @@ -300,6 +302,10 @@ class ACIBridgeDomainAPIViewTestCase(APIViewTestCases.APIViewTestCase): "nb_tenant", "url", ] + user_permissions = ( + "netbox_aci_plugin.view_acitenant", + "netbox_aci_plugin.view_acivrf", + ) @classmethod def setUpTestData(cls) -> None: @@ -472,6 +478,12 @@ class ACIBridgeDomainSubnetAPIViewTestCase(APIViewTestCases.APIViewTestCase): "nb_tenant", "url", ] + user_permissions = ( + "netbox_aci_plugin.view_acitenant", + "netbox_aci_plugin.view_acivrf", + "netbox_aci_plugin.view_acibridgedomain", + "ipam.view_ipaddress", + ) @classmethod def setUpTestData(cls) -> None: @@ -623,6 +635,12 @@ class ACIEndpointGroupAPIViewTestCase(APIViewTestCases.APIViewTestCase): "nb_tenant", "url", ] + user_permissions = ( + "netbox_aci_plugin.view_acitenant", + "netbox_aci_plugin.view_aciappprofile", + "netbox_aci_plugin.view_acivrf", + "netbox_aci_plugin.view_acibridgedomain", + ) @classmethod def setUpTestData(cls) -> None: