|
| 1 | +## API |
| 2 | + |
| 3 | +The NetBox Custom Objects plugin provides CRUD operations through the standard NetBox API, with endpoints located at: `/api/plugins/custom-objects/` |
| 4 | + |
| 5 | +```json |
| 6 | +{ |
| 7 | + "custom-object-types": "http://127.0.0.1:8000/api/plugins/custom-objects/custom-object-types/", |
| 8 | + "custom-object-type-fields": "http://127.0.0.1:8000/api/plugins/custom-objects/custom-object-type-fields/", |
| 9 | + "my-custom-type": "http://127.0.0.1:8000/api/plugins/custom-objects/my-custom-type/" |
| 10 | +} |
| 11 | +``` |
| 12 | + |
| 13 | +The plugin dynamically creates endpoints for each Custom Object Type you define. The endpoint names are based on the `name` of the Custom Object Type. |
| 14 | + |
| 15 | +### Custom Object Types |
| 16 | + |
| 17 | +Create a Custom Object Type with a POST call to `/api/plugins/custom-objects/custom-object-types/` using a payload |
| 18 | +similar to the following: |
| 19 | + |
| 20 | +```json |
| 21 | +{ |
| 22 | + "name": "Server", |
| 23 | + "description": "Server inventory objects", |
| 24 | + "verbose_name_plural": "Servers" |
| 25 | +} |
| 26 | +``` |
| 27 | + |
| 28 | +### Custom Object Type Fields |
| 29 | + |
| 30 | +Define the schema of the Custom Object Type by creating fields of various types, with POST requests to |
| 31 | +`/api/plugins/custom-objects/custom-object-type-fields/`: |
| 32 | + |
| 33 | +```json |
| 34 | +{ |
| 35 | + "custom_object_type": 9, |
| 36 | + "name": "internal_id", |
| 37 | + "label": "Internal ID", |
| 38 | + "type": "integer", |
| 39 | + "required": true, |
| 40 | + "validation_minimum": 0, |
| 41 | + "validation_maximum": 9999 |
| 42 | +} |
| 43 | +``` |
| 44 | + |
| 45 | +Available `type` values are: |
| 46 | +- `text` - Short text field |
| 47 | +- `longtext` - Long text field with textarea widget |
| 48 | +- `integer` - Integer field |
| 49 | +- `decimal` - Decimal field |
| 50 | +- `boolean` - Boolean field |
| 51 | +- `date` - Date field |
| 52 | +- `datetime` - DateTime field |
| 53 | +- `url` - URL field |
| 54 | +- `json` - JSON field |
| 55 | +- `select` - Single select from choice set |
| 56 | +- `multiselect` - Multiple select from choice set |
| 57 | +- `object` - Reference to a single object |
| 58 | +- `multiobject` - Reference to multiple objects |
| 59 | + |
| 60 | +Field-specific attributes can be specified using the validation and configuration fields: |
| 61 | + |
| 62 | +#### Text Fields (`text`, `longtext`) |
| 63 | +```json |
| 64 | +{ |
| 65 | + "custom_object_type": 9, |
| 66 | + "name": "hostname", |
| 67 | + "label": "Hostname", |
| 68 | + "type": "text", |
| 69 | + "required": true, |
| 70 | + "validation_regex": "^[a-zA-Z0-9-]+$" |
| 71 | +} |
| 72 | +``` |
| 73 | + |
| 74 | +#### Numeric Fields (`integer`, `decimal`) |
| 75 | +```json |
| 76 | +{ |
| 77 | + "custom_object_type": 9, |
| 78 | + "name": "cpu_cores", |
| 79 | + "label": "CPU Cores", |
| 80 | + "type": "integer", |
| 81 | + "validation_minimum": 1, |
| 82 | + "validation_maximum": 128 |
| 83 | +} |
| 84 | +``` |
| 85 | + |
| 86 | +#### Choice Fields (`select`, `multiselect`) |
| 87 | +```json |
| 88 | +{ |
| 89 | + "custom_object_type": 9, |
| 90 | + "name": "environment", |
| 91 | + "label": "Environment", |
| 92 | + "type": "select", |
| 93 | + "choice_set": 5 |
| 94 | +} |
| 95 | +``` |
| 96 | + |
| 97 | +#### Object Reference Fields (`object`, `multiobject`) |
| 98 | + |
| 99 | +If the type is `object` or `multiobject`, the content type of the field is designated using the `app_label` and `model` attributes: |
| 100 | + |
| 101 | +```json |
| 102 | +{ |
| 103 | + "custom_object_type": 9, |
| 104 | + "name": "primary_device", |
| 105 | + "label": "Primary Device", |
| 106 | + "type": "object", |
| 107 | + "app_label": "dcim", |
| 108 | + "model": "device" |
| 109 | +} |
| 110 | +``` |
| 111 | + |
| 112 | +```json |
| 113 | +{ |
| 114 | + "custom_object_type": 9, |
| 115 | + "name": "device_list", |
| 116 | + "label": "Device List", |
| 117 | + "type": "multiobject", |
| 118 | + "app_label": "dcim", |
| 119 | + "model": "device" |
| 120 | +} |
| 121 | +``` |
| 122 | + |
| 123 | +> [!NOTE] |
| 124 | +> An `object` or `multiobject` field can point to any Custom Object, as well as any other existing object internal to NetBox. |
| 125 | +> Use an `app_label` of `custom-objects` and a `model` of the Custom Object name to reference other custom objects. |
| 126 | +
|
| 127 | + |
| 128 | +### Custom Objects |
| 129 | + |
| 130 | +Once the schema of a Custom Object Type is defined through its list of fields, you can create Custom Objects, |
| 131 | +which are instances of Custom Object Types with specific values populated into the fields defined in the schema. |
| 132 | + |
| 133 | +Create a Custom Object with a POST to `/api/plugins/custom-objects/<custom-object-type>/` where `<custom-object-type>` is the name of your Custom Object Type: |
| 134 | + |
| 135 | +```json |
| 136 | +{ |
| 137 | + "internal_id": 102, |
| 138 | + "hostname": "server-001", |
| 139 | + "cpu_cores": 8, |
| 140 | + "environment": "production", |
| 141 | + "device_list": [34, 1], |
| 142 | + "primary_device": 16 |
| 143 | +} |
| 144 | +``` |
| 145 | + |
| 146 | +The response will include the created object with its assigned ID and additional metadata: |
| 147 | + |
| 148 | +```json |
| 149 | +{ |
| 150 | + "id": 15, |
| 151 | + "url": "http://127.0.0.1:8000/api/plugins/custom-objects/server/15/", |
| 152 | + "custom_object_type": { |
| 153 | + "id": 9, |
| 154 | + "name": "Server", |
| 155 | + "description": "Server inventory objects" |
| 156 | + }, |
| 157 | + "internal_id": 102, |
| 158 | + "hostname": "server-001", |
| 159 | + "cpu_cores": 8, |
| 160 | + "environment": "production", |
| 161 | + "device_list": [34, 1], |
| 162 | + "primary_device": 16, |
| 163 | + "tags": [], |
| 164 | + "created": "2024-01-15T10:30:00Z", |
| 165 | + "last_updated": "2024-01-15T10:30:00Z" |
| 166 | +} |
| 167 | +``` |
| 168 | + |
| 169 | +PATCH requests can be used to update all the above objects, as well as DELETE and GET operations, using the detail |
| 170 | +URL for each model: |
| 171 | +- Custom Object Types: `/api/plugins/custom-objects/custom-object-types/15/` |
| 172 | +- Custom Object Type Fields: `/api/plugins/custom-objects/custom-object-type-fields/23/` |
| 173 | +- Custom Objects: `/api/plugins/custom-objects/<custom-object-type>/15/` |
0 commit comments