|  | 
| 1 | 1 | import pynetbox | 
| 2 | 2 | from pprint import pprint | 
|  | 3 | +from dataclasses import dataclass | 
|  | 4 | +import sys | 
| 3 | 5 | 
 | 
| 4 | 6 | nb = pynetbox.api( | 
| 5 | 7 |     'http://localhost:8080', | 
| 6 | 8 |     token='0123456789abcdef0123456789abcdef01234567' | 
| 7 | 9 | ) | 
| 8 | 10 | 
 | 
| 9 |  | -# create custom fields and associate custom fields with IP/IPRange/Prefix | 
| 10 |  | -devices = list(nb.ipam.prefixes.all()) | 
| 11 |  | -for device in devices: | 
| 12 |  | -    pprint(device) | 
| 13 |  | - | 
| 14 |  | -""" | 
| 15 |  | --- create Custom Fields | 
| 16 |  | -INSERT INTO public.extras_customfield (id, type, name, label, description, required, filter_logic, "default", weight, validation_minimum, validation_maximum, validation_regex, created, last_updated, related_object_type_id, group_name, search_weight, is_cloneable, choice_set_id, ui_editable, ui_visible, comments, "unique", related_object_filter) | 
| 17 |  | -VALUES (2, 'text', 'netboxOperatorRestorationHash', 'Netbox Restoration Hash', 'Used to rediscover previously claimed IP Addresses', false, 'exact', NULL, 100, NULL, NULL, '', '2024-06-13 15:17:08.65334+00', '2024-06-13 15:17:08.653359+00', NULL, 'netbox-operator', 100, false, NULL, 'hidden', 'always', '', false, NULL); | 
| 18 |  | -
 | 
| 19 |  | -INSERT INTO public.extras_customfield (id, type, name, label, description, required, filter_logic, "default", weight, validation_minimum, validation_maximum, validation_regex, created, last_updated, related_object_type_id, group_name, search_weight, is_cloneable, choice_set_id, ui_editable, ui_visible, comments, "unique", related_object_filter) | 
| 20 |  | -VALUES (3, 'text', 'example_field', 'Example Field', 'example description', false, 'exact', NULL, 100, NULL, NULL, '', '2024-06-13 15:17:08.65334+00', '2024-06-13 15:17:08.653359+00', NULL, 'netbox-operator', 100, false, NULL, 'hidden', 'always', '', false, NULL); | 
| 21 |  | -
 | 
| 22 |  | -INSERT INTO public.extras_customfield (id, type, name, label, description, required, filter_logic, "default", weight, validation_minimum, validation_maximum, validation_regex, created, last_updated, related_object_type_id, group_name, search_weight, is_cloneable, choice_set_id, ui_editable, ui_visible, comments, "unique", related_object_filter) | 
| 23 |  | -VALUES (4, 'text', 'environment', 'Environment', 'Custom field 1 for ParentPrefixSelector', false, 'exact', NULL, 100, NULL, NULL, '', '2024-06-13 15:17:08.65334+00', '2024-06-13 15:17:08.653359+00', NULL, 'netbox-operator', 100, false, NULL, 'hidden', 'always', '', false, NULL); | 
| 24 |  | -
 | 
| 25 |  | -INSERT INTO public.extras_customfield (id, type, name, label, description, required, filter_logic, "default", weight, validation_minimum, validation_maximum, validation_regex, created, last_updated, related_object_type_id, group_name, search_weight, is_cloneable, choice_set_id, ui_editable, ui_visible, comments, "unique", related_object_filter) | 
| 26 |  | -VALUES (5, 'text', 'poolName', 'Pool Name', 'Custom field 2 for ParentPrefixSelector', false, 'exact', NULL, 100, NULL, NULL, '', '2024-06-13 15:17:08.65334+00', '2024-06-13 15:17:08.653359+00', NULL, 'netbox-operator', 100, false, NULL, 'hidden', 'always', '', false, NULL); | 
| 27 |  | -
 | 
| 28 |  | -INSERT INTO public.extras_customfield (id, type, name, label, description, required, filter_logic, "default", weight, validation_minimum, validation_maximum, validation_regex, created, last_updated, related_object_type_id, group_name, search_weight, is_cloneable, choice_set_id, ui_editable, ui_visible, comments, "unique", related_object_filter) | 
| 29 |  | -VALUES (6, 'boolean', 'cfDataTypeBool', 'cf Data Type Bool', 'Custom field 3 for ParentPrefixSelector', false, 'exact', NULL, 100, NULL, NULL, '', '2024-06-13 15:17:08.65334+00', '2024-06-13 15:17:08.653359+00', NULL, 'netbox-operator', 100, false, NULL, 'hidden', 'always', '', false, NULL); | 
| 30 |  | -
 | 
| 31 |  | -INSERT INTO public.extras_customfield (id, type, name, label, description, required, filter_logic, "default", weight, validation_minimum, validation_maximum, validation_regex, created, last_updated, related_object_type_id, group_name, search_weight, is_cloneable, choice_set_id, ui_editable, ui_visible, comments, "unique", related_object_filter) | 
| 32 |  | -VALUES (7, 'integer', 'cfDataTypeInteger', 'cf Data Type Integer', 'Custom field 4 for ParentPrefixSelector', false, 'exact', NULL, 100, NULL, NULL, '', '2024-06-13 15:17:08.65334+00', '2024-06-13 15:17:08.653359+00', NULL, 'netbox-operator', 100, false, NULL, 'hidden', 'always', '', false, NULL); | 
| 33 |  | -
 | 
| 34 |  | --- associate custom fields with IP | 
| 35 |  | -INSERT INTO public.extras_customfield_object_types (id, customfield_id, objecttype_id) | 
| 36 |  | -VALUES (2, 2, 69); | 
| 37 |  | -
 | 
| 38 |  | -INSERT INTO public.extras_customfield_object_types (id, customfield_id, objecttype_id) | 
| 39 |  | -VALUES (3, 3, 69); | 
| 40 |  | -
 | 
| 41 |  | --- associate custom fields with Prefix | 
| 42 |  | -INSERT INTO public.extras_customfield_object_types (id, customfield_id, objecttype_id) | 
| 43 |  | -VALUES (4, 2, 70); | 
| 44 |  | -
 | 
| 45 |  | -INSERT INTO public.extras_customfield_object_types (id, customfield_id, objecttype_id) | 
| 46 |  | -VALUES (5, 3, 70); | 
| 47 |  | -
 | 
| 48 |  | -INSERT INTO public.extras_customfield_object_types (id, customfield_id, objecttype_id) | 
| 49 |  | -VALUES (6, 4, 70); | 
| 50 |  | -
 | 
| 51 |  | -INSERT INTO public.extras_customfield_object_types (id, customfield_id, objecttype_id) | 
| 52 |  | -VALUES (7, 5, 70); | 
| 53 |  | -
 | 
| 54 |  | -INSERT INTO public.extras_customfield_object_types (id, customfield_id, objecttype_id) | 
| 55 |  | -VALUES (8, 6, 70); | 
| 56 |  | -
 | 
| 57 |  | -INSERT INTO public.extras_customfield_object_types (id, customfield_id, objecttype_id) | 
| 58 |  | -VALUES (9, 7, 70); | 
| 59 |  | -
 | 
| 60 |  | --- associate custom fields with IP Range | 
| 61 |  | -INSERT INTO public.extras_customfield_object_types (id, customfield_id, objecttype_id) | 
| 62 |  | -VALUES (10, 2, 78); | 
| 63 |  | -
 | 
| 64 |  | -INSERT INTO public.extras_customfield_object_types (id, customfield_id, objecttype_id) | 
| 65 |  | -VALUES (11, 3, 78); | 
| 66 |  | -""" | 
| 67 |  | - | 
| 68 | 11 | # insert Tenants | 
| 69 | 12 | 
 | 
| 70 |  | -""" | 
| 71 |  | --- insert Tenant | 
| 72 |  | -INSERT INTO public.tenancy_tenant (created, last_updated, custom_field_data, id, name, slug, description, comments, group_id) | 
| 73 |  | -VALUES ('2024-06-14 09:57:11.709344+00', '2024-06-14 09:57:11.709359+00', '{"cust_id": null}', 100, 'MY_TENANT', 'my_tenant', '', '', NULL); | 
| 74 |  | -""" | 
|  | 13 | +@dataclass | 
|  | 14 | +class Tenant: | 
|  | 15 | +    name: str | 
|  | 16 | +    slug: str | 
|  | 17 | +    custom_fields: dict | 
|  | 18 | + | 
|  | 19 | +tenants = [ | 
|  | 20 | +    Tenant( | 
|  | 21 | +        name="MY_TENANT", | 
|  | 22 | +        slug="my_tenant", | 
|  | 23 | +        custom_fields={ | 
|  | 24 | +            "cust_id": None, | 
|  | 25 | +        }, | 
|  | 26 | +    ), | 
|  | 27 | +] | 
|  | 28 | + | 
|  | 29 | +for tenant in tenants: | 
|  | 30 | +    try: | 
|  | 31 | +        nb.tenancy.tenants.create( | 
|  | 32 | +            name=tenant.name, | 
|  | 33 | +            slug=tenant.slug, | 
|  | 34 | +            custom_fields=tenant.custom_fields, | 
|  | 35 | +        ) | 
|  | 36 | +    except pynetbox.RequestError as e: | 
|  | 37 | +        pprint(e.error) | 
|  | 38 | +        sys.exit(1) | 
|  | 39 | + | 
|  | 40 | +# devices = list(nb.tenancy.tenants.all()) | 
|  | 41 | +# for device in devices: | 
|  | 42 | +#     pprint(device) | 
| 75 | 43 | 
 | 
| 76 | 44 | # insert IPs | 
| 77 | 45 | 
 | 
|  | 
| 136 | 104 | INSERT INTO public.ipam_prefix (created, last_updated, custom_field_data, prefix, status, is_pool, description, role_id, site_id, tenant_id, vlan_id, vrf_id, _children, _depth, mark_utilized, comments) | 
| 137 | 105 | VALUES ('2024-06-14 10:01:10.094083+00', '2024-06-14 10:01:10.094095+00', '{"environment": "development", "poolName": "pool 4", "cfDataTypeBool": false, "cfDataTypeInteger": 8}', '2:0:0:2::/64', 'active', false, '', NULL, NULL, 5, NULL, NULL, 0, 0, false, ''); | 
| 138 | 106 | """ | 
|  | 107 | + | 
|  | 108 | +devices = list(nb.ipam.prefixes.all()) | 
|  | 109 | +for device in devices: | 
|  | 110 | +    pprint(device) | 
|  | 111 | + | 
|  | 112 | +# create custom fields and associate custom fields with IP/IPRange/Prefix | 
|  | 113 | + | 
|  | 114 | +@dataclass | 
|  | 115 | +class CustomField: | 
|  | 116 | +    object_types: list[str] | 
|  | 117 | +    type: str | 
|  | 118 | +    name: str | 
|  | 119 | +    label: str | 
|  | 120 | +    description: str | 
|  | 121 | +    required: bool | 
|  | 122 | +    filter_logic: str | 
|  | 123 | + | 
|  | 124 | +custom_fields = [ | 
|  | 125 | +    CustomField( | 
|  | 126 | +        object_types=["ipam.ipaddress", "ipam.iprange", "ipam.prefix"], | 
|  | 127 | +        type="text", | 
|  | 128 | +        name="netboxOperatorRestorationHash", | 
|  | 129 | +        label="Netbox Restoration Hash", | 
|  | 130 | +        description="Used to rediscover previously claimed IP Addresses", | 
|  | 131 | +        required=False, | 
|  | 132 | +        filter_logic="exact" | 
|  | 133 | +    ), | 
|  | 134 | +    CustomField( | 
|  | 135 | +        object_types=["ipam.ipaddress", "ipam.iprange", "ipam.prefix"], | 
|  | 136 | +        type="text", | 
|  | 137 | +        name="example_field", | 
|  | 138 | +        label="Example Field", | 
|  | 139 | +        description="example description", | 
|  | 140 | +        required=False, | 
|  | 141 | +        filter_logic="exact" | 
|  | 142 | +    ), | 
|  | 143 | +    CustomField( | 
|  | 144 | +        object_types=["ipam.prefix"], | 
|  | 145 | +        type="text", | 
|  | 146 | +        name="environment", | 
|  | 147 | +        label="Environment", | 
|  | 148 | +        description="Custom field 1 for ParentPrefixSelector", | 
|  | 149 | +        required=False, | 
|  | 150 | +        filter_logic="exact" | 
|  | 151 | +    ), | 
|  | 152 | +    CustomField( | 
|  | 153 | +        object_types=["ipam.prefix"], | 
|  | 154 | +        type="text", | 
|  | 155 | +        name="poolName", | 
|  | 156 | +        label="Pool Name", | 
|  | 157 | +        description="Custom field 2 for ParentPrefixSelector", | 
|  | 158 | +        required=False, | 
|  | 159 | +        filter_logic="exact" | 
|  | 160 | +    ), | 
|  | 161 | +    CustomField( | 
|  | 162 | +        object_types=["ipam.prefix"], | 
|  | 163 | +        type="boolean", | 
|  | 164 | +        name="cfDataTypeBool", | 
|  | 165 | +        label="cf Data Type Bool", | 
|  | 166 | +        description="Custom field 3 for ParentPrefixSelector", | 
|  | 167 | +        required=False, | 
|  | 168 | +        filter_logic="exact" | 
|  | 169 | +    ), | 
|  | 170 | +    CustomField( | 
|  | 171 | +        object_types=["ipam.prefix"], | 
|  | 172 | +        type="integer", | 
|  | 173 | +        name="cfDataTypeInteger", | 
|  | 174 | +        label="cf Data Type Integer", | 
|  | 175 | +        description="Custom field 4 for ParentPrefixSelector", | 
|  | 176 | +        required=False, | 
|  | 177 | +        filter_logic="exact" | 
|  | 178 | +    ), | 
|  | 179 | +] | 
|  | 180 | + | 
|  | 181 | +for custom_field in custom_fields: | 
|  | 182 | +    try: | 
|  | 183 | +        nb.extras.custom_fields.create( | 
|  | 184 | +            object_types=custom_field.object_types, | 
|  | 185 | +            type=custom_field.type, | 
|  | 186 | +            name=custom_field.name, | 
|  | 187 | +            label=custom_field.label, | 
|  | 188 | +            description=custom_field.description, | 
|  | 189 | +            required=custom_field.required, | 
|  | 190 | +            filter_logic=custom_field.filter_logic, | 
|  | 191 | +            default=None | 
|  | 192 | +        ) | 
|  | 193 | +    except pynetbox.RequestError as e: | 
|  | 194 | +        pprint(e.error) | 
|  | 195 | +        sys.exit(1) | 
|  | 196 | + | 
|  | 197 | +# custom_fields = list(nb.extras.custom_fields.all()) | 
|  | 198 | +# for custom_field in custom_fields: | 
|  | 199 | +#     pprint(custom_field) | 
0 commit comments