11import pynetbox
22from pprint import pprint
33from dataclasses import dataclass
4- import sys
54
65print ("Starting to load data onto NetBox through API" )
76try :
1110 )
1211except pynetbox .RequestError as e :
1312 pprint (e .error )
14- sys . exit ( 1 )
13+
1514print ("Connected to NetBoxAPI" )
1615
1716# insert Tenants
@@ -47,7 +46,7 @@ class Tenant:
4746 )
4847 except pynetbox .RequestError as e :
4948 pprint (e .error )
50- sys . exit ( 1 )
49+
5150print ("Tenants loaded" )
5251
5352# insert Sites
@@ -88,13 +87,14 @@ class Site:
8887 )
8988 except pynetbox .RequestError as e :
9089 pprint (e .error )
91- sys . exit ( 1 )
90+
9291print ("Sites loaded" )
9392
9493# create custom fields and associate custom fields with IP/IPRange/Prefix
9594@dataclass
9695class CustomField :
97- object_types : list [str ]
96+ content_types : list [str ] # for v3
97+ object_types : list [str ] # for v4
9898 type : str
9999 name : str
100100 label : str
@@ -104,6 +104,7 @@ class CustomField:
104104
105105custom_fields = [
106106 CustomField (
107+ content_types = ["ipam.ipaddress" , "ipam.iprange" , "ipam.prefix" ],
107108 object_types = ["ipam.ipaddress" , "ipam.iprange" , "ipam.prefix" ],
108109 type = "text" ,
109110 name = "netboxOperatorRestorationHash" ,
@@ -113,6 +114,7 @@ class CustomField:
113114 filter_logic = "exact"
114115 ),
115116 CustomField (
117+ content_types = ["ipam.ipaddress" , "ipam.iprange" , "ipam.prefix" ],
116118 object_types = ["ipam.ipaddress" , "ipam.iprange" , "ipam.prefix" ],
117119 type = "text" ,
118120 name = "example_field" ,
@@ -122,6 +124,7 @@ class CustomField:
122124 filter_logic = "exact"
123125 ),
124126 CustomField (
127+ content_types = ["ipam.prefix" ],
125128 object_types = ["ipam.prefix" ],
126129 type = "text" ,
127130 name = "environment" ,
@@ -131,6 +134,7 @@ class CustomField:
131134 filter_logic = "exact"
132135 ),
133136 CustomField (
137+ content_types = ["ipam.prefix" ],
134138 object_types = ["ipam.prefix" ],
135139 type = "text" ,
136140 name = "poolName" ,
@@ -140,6 +144,7 @@ class CustomField:
140144 filter_logic = "exact"
141145 ),
142146 CustomField (
147+ content_types = ["ipam.prefix" ],
143148 object_types = ["ipam.prefix" ],
144149 type = "boolean" ,
145150 name = "cfDataTypeBool" ,
@@ -149,6 +154,7 @@ class CustomField:
149154 filter_logic = "exact"
150155 ),
151156 CustomField (
157+ content_types = ["ipam.prefix" ],
152158 object_types = ["ipam.prefix" ],
153159 type = "integer" ,
154160 name = "cfDataTypeInteger" ,
@@ -162,6 +168,7 @@ class CustomField:
162168for custom_field in custom_fields :
163169 try :
164170 nb .extras .custom_fields .create (
171+ content_types = custom_field .content_types ,
165172 object_types = custom_field .object_types ,
166173 type = custom_field .type ,
167174 name = custom_field .name ,
@@ -173,7 +180,7 @@ class CustomField:
173180 )
174181 except pynetbox .RequestError as e :
175182 pprint (e .error )
176- sys . exit ( 1 )
183+
177184print ("Custom fields loaded" )
178185
179186# for debugging
@@ -461,5 +468,5 @@ class Prefix:
461468 )
462469 except pynetbox .RequestError as e :
463470 pprint (e .error )
464- sys . exit ( 1 )
471+
465472print ("Prefixes loaded" )
0 commit comments