Skip to content

Commit

Permalink
refactor(graphql): Simplify GraphQL schema structure
Browse files Browse the repository at this point in the history
Replaced multiple query classes with a unified `NetBoxACIQuery` class.
This change consolidates all ACI-related queries for easier maintenance
and ensures compatibility with NetBox v4.0.10 and v4.0.11.
  • Loading branch information
pheus committed Oct 1, 2024
1 parent 4502f09 commit 5208f05
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 87 deletions.
4 changes: 2 additions & 2 deletions netbox_aci_plugin/graphql/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from .schema import schema
from .schema import NetBoxACIQuery

__all__ = ["schema"]
schema = [NetBoxACIQuery]
80 changes: 11 additions & 69 deletions netbox_aci_plugin/graphql/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
]
69 changes: 53 additions & 16 deletions netbox_aci_plugin/graphql/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(
Expand All @@ -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]]


Expand All @@ -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]
Expand All @@ -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(
Expand All @@ -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
)
18 changes: 18 additions & 0 deletions netbox_aci_plugin/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ class ACIAppProfileAPIViewTestCase(APIViewTestCases.APIViewTestCase):
"nb_tenant",
"url",
]
user_permissions = ("netbox_aci_plugin.view_acitenant",)

@classmethod
def setUpTestData(cls) -> None:
Expand Down Expand Up @@ -187,6 +188,7 @@ class ACIVRFAPIViewTestCase(APIViewTestCases.APIViewTestCase):
"nb_vrf",
"url",
]
user_permissions = ("netbox_aci_plugin.view_acitenant",)

@classmethod
def setUpTestData(cls) -> None:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 5208f05

Please sign in to comment.