-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathpatch_federation.py
More file actions
64 lines (45 loc) · 2.05 KB
/
patch_federation.py
File metadata and controls
64 lines (45 loc) · 2.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import re
from pathlib import Path
NETBOX_ROOT = Path("/opt/netbox/netbox")
resolve_reference = '''\
@classmethod
def resolve_reference(cls, id: strawberry.ID) -> '{type_name}':
"""Required for resolving this class through GraphQL Federation."""
return models.{model_name}.objects.get(pk=id)
'''
type_directives = "directives=[Key(fields=key, resolvable=UNSET) for key in ['id']],"
def insert_import(text: str, import_stmt: str) -> str:
# Find the first import line and add our line above that
return re.sub(r"(^(?:from .+|import .+)$)", rf"{import_stmt}\n\1", text, count=1, flags=re.MULTILINE)
def patch_types():
file = NETBOX_ROOT / "dcim/graphql/types.py"
text = file.open().read()
text = insert_import(text, "from strawberry.federation.schema_directives import Key")
text = insert_import(text, "from strawberry import UNSET")
types_to_patch = [
("Device", "DeviceType"),
]
for model_name, type_name in types_to_patch:
# Add resolve_reference() method to the class
method = resolve_reference.format(type_name=type_name, model_name=model_name)
regex_find_class = rf"(class {type_name}\(.+?\):\n)"
text = re.sub(regex_find_class, rf"\1{method}\n", text, count=1, flags=re.DOTALL)
# Add directives to the class decorator
regex_find_decorator = rf"(@strawberry_django.type\(\n\s+models\.{model_name},)"
text = re.sub(regex_find_decorator, rf"\1 {type_directives}", text, count=1)
with file.open(mode="w") as f:
f.write(text)
def patch_subscription_query():
file = NETBOX_ROOT / "extras/graphql/schema.py"
text = file.open().read()
text = re.sub("subscription:", "nb_subscription:", text, count=1)
text = re.sub("subscription_list:", "nb_subscription_list:", text, count=1)
with file.open(mode="w") as f:
f.write(text)
def main():
if not NETBOX_ROOT.is_dir():
raise Exception(f"Netbox root {NETBOX_ROOT} not found")
patch_types()
patch_subscription_query()
if __name__ == "__main__":
main()