-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathload_bootstrap_data.py
More file actions
978 lines (805 loc) · 38.1 KB
/
load_bootstrap_data.py
File metadata and controls
978 lines (805 loc) · 38.1 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
# SPDX-FileCopyrightText: Copyright (c) 2024-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Nautobot job to load baseline NVIDIA Config Manager data for external customers."""
import os
from pathlib import Path
import yaml
from django.contrib.contenttypes.models import ContentType
from nautobot.apps.jobs import Job, register_jobs
from nautobot.core.models.fields import slugify_dashes_to_underscores
from nautobot.dcim.models import (
DeviceType,
LocationType,
Manufacturer,
Platform,
)
from nautobot.extras.models import (
ConfigContext,
ConfigContextSchema,
Relationship,
Role,
Status,
Tag,
)
from nautobot.ipam.models import Namespace
from nautobot.tenancy.models import Tenant
name = "Bootstrap"
class LoadBootstrapData(Job):
"""Load bootstrap data for NVIDIA Config Manager external customers from YAML templates."""
class Meta:
"""Job metadata."""
name = "Load Bootstrap Data"
description = (
"Load manufacturers, roles, tags, platforms, device types, tenants, location types, "
"namespaces, statuses, relationships, config context schemas, and config contexts "
"from YAML templates"
)
has_sensitive_variables = False
approval_required = False
def __init__(self):
"""Initialize the job."""
super().__init__()
# Path to data templates relative to the job file
self.data_path = Path(__file__).parent.parent / "data"
# Get deployment type from environment variable (e.g., 'superpod', 'dgxc', 'azure', 'all')
self.deployment_type = os.getenv("NV_CONFIG_MANAGER_DEPLOYMENT_TYPE", "all").lower()
def should_load_item(self, item_data, item_name="item"):
"""Check if an item should be loaded based on deployment_types.
Args:
item_data: Dictionary containing item data with optional 'deployment_types' field
item_name: Name of the item for logging purposes
Returns:
bool: True if item should be loaded, False otherwise
"""
deployment_types = item_data.get("deployment_types", ["all"])
# If deployment_types is a string, convert to list
if isinstance(deployment_types, str):
deployment_types = [deployment_types]
should_load = self.deployment_type in deployment_types or "all" in deployment_types
if not should_load:
self.logger.debug(f"Skipping {item_name} (not in deployment type: {self.deployment_type})")
return should_load
def get_content_types(self, content_type_strings):
"""Convert content type strings to ContentType objects.
Args:
content_type_strings: List of strings like ['dcim.device', 'dcim.interface']
Returns:
list: List of ContentType objects
"""
content_types = []
for ct_string in content_type_strings:
try:
app_label, model = ct_string.split(".")
ct = ContentType.objects.get(app_label=app_label, model=model)
content_types.append(ct)
except (ValueError, ContentType.DoesNotExist):
self.logger.warning(f"Could not find content type: {ct_string}")
return content_types
def add_content_types(self, obj, content_type_strings):
"""Add content type memberships without removing existing memberships."""
content_types = self.get_content_types(content_type_strings)
if content_types:
obj.content_types.add(*content_types)
def run(self):
"""Execute the job to load bootstrap data.
Returns:
str: Success message
"""
self.logger.info(
f"Starting NVIDIA Config Manager Bootstrap Data Load (deployment_type: {self.deployment_type})",
extra={"grouping": "bootstrap"},
)
# Load in dependency order
self.load_manufacturers()
self.load_tenants()
self.load_location_types()
self.load_namespaces()
self.load_statuses()
self.load_roles()
self.load_tags()
self.load_platforms()
self.load_device_types()
self.load_relationships()
self.load_config_context_schemas()
self.load_config_contexts()
self.logger.info("Bootstrap Data Load Complete!", extra={"grouping": "bootstrap"})
return "Bootstrap data load completed successfully"
def load_manufacturers(self):
"""Load manufacturers from YAML template."""
self.logger.info("Loading Manufacturers", extra={"grouping": "manufacturers"})
manufacturers_file = self.data_path / "manufacturers.yaml"
if not manufacturers_file.exists():
self.logger.failure(f"Manufacturers file not found: {manufacturers_file}")
return
try:
with open(manufacturers_file) as f:
manufacturers = yaml.safe_load(f)
if not manufacturers:
self.logger.warning("No manufacturers found in file")
return
for mfg_data in manufacturers:
try:
name = mfg_data.get("name")
if not name:
self.logger.warning("Skipping manufacturer with no name")
continue
# Check deployment type filtering
if not self.should_load_item(mfg_data, f"manufacturer '{name}'"):
continue
mfg, created = Manufacturer.objects.get_or_create(
name=name,
defaults={"description": mfg_data.get("description", "")},
)
if created:
self.logger.success(
f"Created manufacturer: {name}",
extra={"grouping": "manufacturers", "object": mfg},
)
else:
self.logger.info(
f"Manufacturer already exists: {name}",
extra={"grouping": "manufacturers", "object": mfg},
)
except Exception as e:
self.logger.error(
f"Error processing manufacturer: {mfg_data.get('name', 'unknown')}",
extra={"grouping": "manufacturers"},
)
self.logger.debug(str(e))
except Exception as e:
self.logger.failure("Error reading manufacturers file", extra={"grouping": "manufacturers"})
self.logger.debug(str(e))
def _load_single_device_type(self, device_type_file, manufacturer):
"""Load a single device type from a YAML file.
Args:
device_type_file: Path to the device type YAML file.
manufacturer: The Manufacturer instance to associate with.
"""
with open(device_type_file) as f:
dt_data = yaml.safe_load(f)
if not dt_data or "model" not in dt_data:
self.logger.warning(
f"Invalid device type file: {device_type_file.name}",
extra={"grouping": "device_types"},
)
return
if not self.should_load_item(dt_data, f"device type '{dt_data['model']}'"):
return
device_type, created = DeviceType.objects.get_or_create(
manufacturer=manufacturer,
model=dt_data["model"],
defaults={
"part_number": dt_data.get("part_number", ""),
"u_height": dt_data.get("u_height", 1),
"is_full_depth": dt_data.get("is_full_depth", True),
},
)
manufacturer_name = manufacturer.name
if created:
self.logger.success(
f"Created device type: {manufacturer_name} {dt_data['model']}",
extra={"grouping": "device_types", "object": device_type},
)
else:
self.logger.info(
f"Device type already exists: {manufacturer_name} {dt_data['model']}",
extra={"grouping": "device_types", "object": device_type},
)
def _load_manufacturer_device_types(self, manufacturer_dir):
"""Load all device types for a single manufacturer directory.
Args:
manufacturer_dir: Path to the manufacturer subdirectory.
"""
manufacturer_name = manufacturer_dir.name
try:
manufacturer = Manufacturer.objects.get(name=manufacturer_name)
except Manufacturer.DoesNotExist:
self.logger.warning(
f"Manufacturer not found: {manufacturer_name}, skipping device types",
extra={"grouping": "device_types"},
)
return
for device_type_file in manufacturer_dir.glob("*.yaml"):
try:
self._load_single_device_type(device_type_file, manufacturer)
except Exception as e:
self.logger.error(
f"Error processing device type {device_type_file.name}",
extra={"grouping": "device_types"},
)
self.logger.debug(str(e))
def load_device_types(self):
"""Load device types from YAML templates organized by manufacturer."""
self.logger.info("Loading Device Types", extra={"grouping": "device_types"})
device_types_path = self.data_path / "device_types"
if not device_types_path.exists():
self.logger.failure(f"Device types directory not found: {device_types_path}")
return
try:
for manufacturer_dir in device_types_path.iterdir():
if manufacturer_dir.is_dir():
self._load_manufacturer_device_types(manufacturer_dir)
except Exception as e:
self.logger.failure("Error reading device types", extra={"grouping": "device_types"})
self.logger.debug(str(e))
def load_roles(self):
"""Load roles from YAML template with optional deployment type filtering."""
self.logger.info("Loading Roles", extra={"grouping": "roles"})
roles_file = self.data_path / "roles.yaml"
if not roles_file.exists():
self.logger.failure(f"Roles file not found: {roles_file}")
return
try:
with open(roles_file) as f:
roles = yaml.safe_load(f)
if not roles:
self.logger.warning("No roles found in file")
return
for role_data in roles:
try:
name = role_data.get("name")
if not name:
self.logger.warning("Skipping role with no name")
continue
# Check deployment type filtering
if not self.should_load_item(role_data, f"role '{name}'"):
continue
role, created = Role.objects.get_or_create(
name=name,
defaults={
"color": role_data.get("color", "grey"),
"weight": role_data.get("weight"),
},
)
# Add content_types for both new and existing roles without
# removing memberships created by other jobs.
if "content_types" in role_data:
self.add_content_types(role, role_data["content_types"])
role.validated_save()
if created:
self.logger.success(
f"Created role: {name}",
extra={"grouping": "roles", "object": role},
)
else:
self.logger.info(
f"Role already exists: {name}",
extra={"grouping": "roles", "object": role},
)
except Exception as e:
self.logger.error(
f"Error processing role: {role_data.get('name', 'unknown')}",
extra={"grouping": "roles"},
)
self.logger.debug(str(e))
except Exception as e:
self.logger.failure("Error reading roles file", extra={"grouping": "roles"})
self.logger.debug(str(e))
def load_tags(self):
"""Load tags from YAML template."""
self.logger.info("Loading Tags", extra={"grouping": "tags"})
tags_file = self.data_path / "tags.yaml"
if not tags_file.exists():
self.logger.failure(f"Tags file not found: {tags_file}")
return
try:
with open(tags_file) as f:
tags = yaml.safe_load(f)
if not tags:
self.logger.warning("No tags found in file")
return
for tag_data in tags:
try:
name = tag_data.get("name")
if not name:
self.logger.warning("Skipping tag with no name")
continue
# Check deployment type filtering
if not self.should_load_item(tag_data, f"tag '{name}'"):
continue
# Use update_or_create to always set color/description
tag, created = Tag.objects.update_or_create(
name=name,
defaults={
"description": tag_data.get("description", ""),
"color": tag_data.get("color", "9e9e9e"),
},
)
# Add content_types without removing memberships created by other jobs.
if "content_types" in tag_data:
self.add_content_types(tag, tag_data["content_types"])
if created:
self.logger.success(
f"Created tag: {name}",
extra={"grouping": "tags", "object": tag},
)
else:
self.logger.info(
f"Tag already exists: {name}",
extra={"grouping": "tags", "object": tag},
)
except Exception as e:
self.logger.error(
f"Error processing tag: {tag_data.get('name', 'unknown')}",
extra={"grouping": "tags"},
)
self.logger.debug(str(e))
except Exception as e:
self.logger.failure("Error reading tags file", extra={"grouping": "tags"})
self.logger.debug(str(e))
def load_platforms(self):
"""Load platforms from YAML template."""
self.logger.info("Loading Platforms", extra={"grouping": "platforms"})
platforms_file = self.data_path / "platforms.yaml"
if not platforms_file.exists():
self.logger.failure(f"Platforms file not found: {platforms_file}")
return
try:
with open(platforms_file) as f:
platforms = yaml.safe_load(f)
if not platforms:
self.logger.warning("No platforms found in file")
return
for platform_data in platforms:
try:
name = platform_data.get("name")
manufacturer_name = platform_data.get("manufacturer")
if not name:
self.logger.warning("Skipping platform with no name")
continue
# Check deployment type filtering
if not self.should_load_item(platform_data, f"platform '{name}'"):
continue
# Get manufacturer if specified
manufacturer = None
if manufacturer_name:
try:
manufacturer = Manufacturer.objects.get(name=manufacturer_name)
except Manufacturer.DoesNotExist:
self.logger.warning(
f"Manufacturer not found for platform {name}: {manufacturer_name}",
extra={"grouping": "platforms"},
)
platform, created = Platform.objects.get_or_create(
name=name,
defaults={
"manufacturer": manufacturer,
"description": platform_data.get("description", ""),
"napalm_driver": platform_data.get("napalm_driver", ""),
},
)
if created:
self.logger.success(
f"Created platform: {name}",
extra={"grouping": "platforms", "object": platform},
)
else:
self.logger.info(
f"Platform already exists: {name}",
extra={"grouping": "platforms", "object": platform},
)
except Exception as e:
self.logger.error(
f"Error processing platform: {platform_data.get('name', 'unknown')}",
extra={"grouping": "platforms"},
)
self.logger.debug(str(e))
except Exception as e:
self.logger.failure("Error reading platforms file", extra={"grouping": "platforms"})
self.logger.debug(str(e))
def load_tenants(self):
"""Load tenants from YAML template."""
self.logger.info("Loading Tenants", extra={"grouping": "tenants"})
tenants_file = self.data_path / "tenants.yaml"
if not tenants_file.exists():
self.logger.warning(f"Tenants file not found: {tenants_file}")
return
try:
with open(tenants_file) as f:
tenants = yaml.safe_load(f)
if not tenants:
self.logger.warning("No tenants found in file")
return
for tenant_data in tenants:
try:
name = tenant_data.get("name")
if not name:
self.logger.warning("Skipping tenant with no name")
continue
# Check deployment type filtering
if not self.should_load_item(tenant_data, f"tenant '{name}'"):
continue
tenant, created = Tenant.objects.get_or_create(
name=name,
defaults={"description": tenant_data.get("description", "")},
)
if created:
self.logger.success(
f"Created tenant: {name}",
extra={"grouping": "tenants", "object": tenant},
)
else:
self.logger.info(
f"Tenant already exists: {name}",
extra={"grouping": "tenants", "object": tenant},
)
except Exception as e:
self.logger.error(
f"Error processing tenant: {tenant_data.get('name', 'unknown')}",
extra={"grouping": "tenants"},
)
self.logger.debug(str(e))
except Exception as e:
self.logger.failure("Error reading tenants file", extra={"grouping": "tenants"})
self.logger.debug(str(e))
def load_location_types(self):
"""Load location types from YAML template."""
self.logger.info("Loading Location Types", extra={"grouping": "location_types"})
location_types_file = self.data_path / "location_types.yaml"
if not location_types_file.exists():
self.logger.warning(f"Location types file not found: {location_types_file}")
return
try:
with open(location_types_file) as f:
location_types = yaml.safe_load(f)
if not location_types:
self.logger.warning("No location types found in file")
return
for lt_data in location_types:
try:
name = lt_data.get("name")
if not name:
self.logger.warning("Skipping location type with no name")
continue
# Check deployment type filtering
if not self.should_load_item(lt_data, f"location type '{name}'"):
continue
# Get parent location type if specified
parent = None
if "parent" in lt_data and lt_data["parent"]:
try:
parent = LocationType.objects.get(name=lt_data["parent"])
except LocationType.DoesNotExist:
self.logger.warning(
f"Parent location type not found: {lt_data['parent']}",
extra={"grouping": "location_types"},
)
lt, created = LocationType.objects.get_or_create(
name=name,
defaults={
"description": lt_data.get("description", ""),
"nestable": lt_data.get("nestable", True),
"parent": parent,
},
)
# Add content_types without removing memberships created by other jobs.
if "content_types" in lt_data:
self.add_content_types(lt, lt_data["content_types"])
if created:
self.logger.success(
f"Created location type: {name}",
extra={"grouping": "location_types", "object": lt},
)
else:
self.logger.info(
f"Location type already exists: {name}",
extra={"grouping": "location_types", "object": lt},
)
except Exception as e:
self.logger.error(
f"Error processing location type: {lt_data.get('name', 'unknown')}",
extra={"grouping": "location_types"},
)
self.logger.debug(str(e))
except Exception as e:
self.logger.failure(
"Error reading location types file",
extra={"grouping": "location_types"},
)
self.logger.debug(str(e))
def load_namespaces(self):
"""Load namespaces from YAML template."""
self.logger.info("Loading Namespaces", extra={"grouping": "namespaces"})
namespaces_file = self.data_path / "namespaces.yaml"
if not namespaces_file.exists():
self.logger.warning(f"Namespaces file not found: {namespaces_file}")
return
try:
with open(namespaces_file) as f:
namespaces = yaml.safe_load(f)
if not namespaces:
self.logger.warning("No namespaces found in file")
return
for ns_data in namespaces:
try:
name = ns_data.get("name")
if not name:
self.logger.warning("Skipping namespace with no name")
continue
# Check deployment type filtering
if not self.should_load_item(ns_data, f"namespace '{name}'"):
continue
ns, created = Namespace.objects.get_or_create(
name=name,
defaults={"description": ns_data.get("description", "")},
)
if created:
self.logger.success(
f"Created namespace: {name}",
extra={"grouping": "namespaces", "object": ns},
)
else:
self.logger.info(
f"Namespace already exists: {name}",
extra={"grouping": "namespaces", "object": ns},
)
except Exception as e:
self.logger.error(
f"Error processing namespace: {ns_data.get('name', 'unknown')}",
extra={"grouping": "namespaces"},
)
self.logger.debug(str(e))
except Exception as e:
self.logger.failure("Error reading namespaces file", extra={"grouping": "namespaces"})
self.logger.debug(str(e))
def load_statuses(self):
"""Load statuses from YAML template."""
self.logger.info("Loading Statuses", extra={"grouping": "statuses"})
statuses_file = self.data_path / "statuses.yaml"
if not statuses_file.exists():
self.logger.warning(f"Statuses file not found: {statuses_file}")
return
try:
with open(statuses_file) as f:
statuses = yaml.safe_load(f)
if not statuses:
self.logger.warning("No statuses found in file")
return
for status_data in statuses:
try:
name = status_data.get("name")
if not name:
self.logger.warning("Skipping status with no name")
continue
# Check deployment type filtering
if not self.should_load_item(status_data, f"status '{name}'"):
continue
# Use update_or_create to handle color updates
status, created = Status.objects.update_or_create(
name=name,
defaults={
"description": status_data.get("description", ""),
"color": status_data.get("color", "9e9e9e"),
},
)
# Add content_types without removing memberships created by other jobs.
if "content_types" in status_data:
self.add_content_types(status, status_data["content_types"])
if created:
self.logger.success(
f"Created status: {name}",
extra={"grouping": "statuses", "object": status},
)
else:
self.logger.info(
f"Status already exists: {name}",
extra={"grouping": "statuses", "object": status},
)
except Exception as e:
self.logger.error(
f"Error processing status: {status_data.get('name', 'unknown')}",
extra={"grouping": "statuses"},
)
self.logger.debug(str(e))
except Exception as e:
self.logger.failure("Error reading statuses file", extra={"grouping": "statuses"})
self.logger.debug(str(e))
def _resolve_content_type(self, ct_string):
"""Resolve a 'app_label.model' string to a ContentType."""
app_label, model = ct_string.split(".")
return ContentType.objects.get(app_label=app_label, model=model)
def load_relationships(self):
"""Load relationships from YAML template."""
self.logger.info("Loading Relationships", extra={"grouping": "relationships"})
relationships_file = self.data_path / "relationships.yaml"
if not relationships_file.exists():
self.logger.warning(f"Relationships file not found: {relationships_file}")
return
with open(relationships_file) as f:
relationships = yaml.safe_load(f)
if not relationships:
self.logger.warning("No relationships found in file")
return
for rel_data in relationships:
name = rel_data["name"]
if not self.should_load_item(rel_data, f"relationship '{name}'"):
continue
label = rel_data.get("label", name)
# Look up by key — the stable unique identifier used by GraphQL.
# Looking up by label alone fails when another job mutates the
# label on an existing relationship, causing a duplicate with a
# "-N" key suffix that breaks GraphQL.
rel_key = rel_data.get("key") or slugify_dashes_to_underscores(label)
relationship, created = Relationship.objects.update_or_create(
key=rel_key,
defaults={
"label": label,
"description": rel_data.get("description", ""),
"type": rel_data.get("type", "one-to-many"),
"source_type": self._resolve_content_type(rel_data["source_type"]),
"source_label": rel_data.get("source_label", ""),
"destination_type": self._resolve_content_type(rel_data["destination_type"]),
"destination_label": rel_data.get("destination_label", ""),
"required_on": rel_data.get("required_on", ""),
},
)
if created:
self.logger.success(
f"Created relationship: {name}",
extra={"grouping": "relationships", "object": relationship},
)
else:
self.logger.info(
f"Relationship already exists: {name}",
extra={"grouping": "relationships", "object": relationship},
)
def load_config_context_schemas(self):
"""Load config context schemas from YAML template."""
self.logger.info("Loading Config Context Schemas", extra={"grouping": "config_context_schemas"})
schemas_file = self.data_path / "config_context_schemas.yaml"
if not schemas_file.exists():
self.logger.warning(f"Config context schemas file not found: {schemas_file}")
return
try:
with open(schemas_file) as f:
schemas = yaml.safe_load(f)
if not schemas:
self.logger.warning("No config context schemas found in file")
return
for schema_data in schemas:
try:
name = schema_data.get("name")
if not name:
self.logger.warning("Skipping config context schema with no name")
continue
# Check deployment type filtering
if not self.should_load_item(schema_data, f"config context schema '{name}'"):
continue
schema, created = ConfigContextSchema.objects.update_or_create(
name=name,
defaults={
"description": schema_data.get("description", ""),
"data_schema": schema_data.get("data_schema", {}),
},
)
schema.validated_save()
if created:
self.logger.success(
f"Created config context schema: {name}",
extra={"grouping": "config_context_schemas", "object": schema},
)
else:
self.logger.info(
f"Config context schema already exists: {name}",
extra={"grouping": "config_context_schemas", "object": schema},
)
except Exception as e:
self.logger.error(
f"Error processing config context schema: {schema_data.get('name', 'unknown')}",
extra={"grouping": "config_context_schemas"},
)
self.logger.debug(str(e))
except Exception as e:
self.logger.failure(
"Error reading config context schemas file",
extra={"grouping": "config_context_schemas"},
)
self.logger.debug(str(e))
def load_config_contexts(self):
"""Load config contexts from YAML template."""
self.logger.info("Loading Config Contexts", extra={"grouping": "config_contexts"})
config_contexts_file = self.data_path / "config_contexts.yaml"
if not config_contexts_file.exists():
self.logger.warning(f"Config contexts file not found: {config_contexts_file}")
return
try:
with open(config_contexts_file) as f:
config_contexts = yaml.safe_load(f)
if not config_contexts:
self.logger.warning("No config contexts found in file")
return
for cc_data in config_contexts:
try:
name = cc_data.get("name")
if not name:
self.logger.warning("Skipping config context with no name")
continue
# Check deployment type filtering
if not self.should_load_item(cc_data, f"config context '{name}'"):
continue
# Build defaults dict
defaults = {
"description": cc_data.get("description", ""),
"weight": cc_data.get("weight", 1000),
"is_active": cc_data.get("is_active", True),
"data": cc_data.get("data", {}),
}
# Get schema if specified
if "schema" in cc_data and cc_data["schema"]:
schema_name = cc_data["schema"]
try:
schema = ConfigContextSchema.objects.get(name=schema_name)
defaults["config_context_schema"] = schema
except ConfigContextSchema.DoesNotExist:
self.logger.warning(
f"Schema not found for config context '{name}': {schema_name}",
extra={"grouping": "config_contexts"},
)
cc, created = ConfigContext.objects.update_or_create(
name=name,
defaults=defaults,
)
# Set roles if specified
if "roles" in cc_data and cc_data["roles"]:
roles = []
for role_name in cc_data["roles"]:
try:
role = Role.objects.get(name=role_name)
roles.append(role)
except Role.DoesNotExist:
self.logger.warning(
f"Role not found for config context: {role_name}",
extra={"grouping": "config_contexts"},
)
if roles:
cc.roles.set(roles)
# Set platforms if specified
if "platforms" in cc_data and cc_data["platforms"]:
platforms = []
for platform_name in cc_data["platforms"]:
try:
platform = Platform.objects.get(name=platform_name)
platforms.append(platform)
except Platform.DoesNotExist:
self.logger.warning(
f"Platform not found for config context: {platform_name}",
extra={"grouping": "config_contexts"},
)
if platforms:
cc.platforms.set(platforms)
cc.validated_save()
if created:
self.logger.success(
f"Created config context: {name}",
extra={"grouping": "config_contexts", "object": cc},
)
else:
self.logger.info(
f"Config context already exists: {name}",
extra={"grouping": "config_contexts", "object": cc},
)
except Exception as e:
self.logger.error(
f"Error processing config context: {cc_data.get('name', 'unknown')}",
extra={"grouping": "config_contexts"},
)
self.logger.debug(str(e))
except Exception as e:
self.logger.failure(
"Error reading config contexts file",
extra={"grouping": "config_contexts"},
)
self.logger.debug(str(e))
register_jobs(LoadBootstrapData)