-
Hi, |
Beta Was this translation helpful? Give feedback.
Replies: 6 comments 2 replies
-
Doesn't look like there is anyway to do that through the API. The |
Beta Was this translation helpful? Give feedback.
-
I neet the same thing.. If no with the API, is there a automatable mechanism to batch import images to device types? |
Beta Was this translation helpful? Give feedback.
-
@nyevess , I suspect I have been walking your path from June 2021 :) I spent the day failing with trying to post device_type images during DeviceType creation via pynetbox api, curl, requests, similarly with ImageAttachements. I won't further detail my adventures in failure unless anyone wants the comedy... You probably already solved this, but here is what I was able to figure out after bumping into this article and seeing @candlerb 's hint on using the shell to write the model directly, here is my hack... My CI/CD creates new netbox containers often, and during container startup I Here is the ugly code I run to script adding the images (based on the expectation that the containers have the device-type images pre-populated in the convention import os from django.core.files import File print('starting...') for device_type in DeviceType.objects.all(): device_type_slug = device_type.slug front_registered_image_location = f'/opt/netbox/netbox/media/devicetype-images/{device_type_slug}_front.jpeg' if os.path.exists(front_registered_image_location): # Check if the image has been registered if not device_type.front_image: print(f'Registering image {front_registered_image_location} for device_type {device_type_slug}') front_image_basename = os.path.basename(front_registered_image_location) front_image_tmp_location = f'/tmp/{front_image_basename}' os.system(f'mv {front_registered_image_location} {front_image_tmp_location}') device_type.front_image.save(name=front_image_basename, content=File(open(front_image_tmp_location, 'rb'))) rear_registered_image_location = f'/opt/netbox/netbox/media/devicetype-images/{device_type_slug}_rear.jpeg' if os.path.exists(rear_registered_image_location): # Check if the image has been registered if not device_type.rear_image: print(f'Registering image {rear_registered_image_location} for device_type {device_type_slug}') rear_image_basename = os.path.basename(rear_registered_image_location) rear_image_tmp_location = f'/tmp/{rear_image_basename}' os.system(f'mv {rear_registered_image_location} {rear_image_tmp_location}') device_type.rear_image.save(name=rear_image_basename, content=File(open(rear_image_tmp_location, 'rb'))) print('done') |
Beta Was this translation helpful? Give feedback.
-
Found another way to do this using Python requests and PATCH through the api: netbox-community/Device-Type-Library-Import#85
|
Beta Was this translation helpful? Give feedback.
-
For those wanting full code to do this, I have used @jbemmel idea and came up with this code:
Run it the first time and it will create several directories named as device+type under images sub dir. Hope it helps |
Beta Was this translation helpful? Give feedback.
-
I think it's more correct to use '/api/extras/image-attachments' like this: def _save_image(self, netbox_id: int, image_list: List[Image], content_type: str):
for image in image_list:
netbox_image = self._api.extras.image_attachments.get(object_id=netbox_id, name=image.name)
if not netbox_image:
header = { "Authorization": f"Token {self._api.token}" }
files = { 'image': (image.path, open(image.path, 'rb')) }
payload = {'name': image.name,
'object_id': netbox_id,
'content_type': f'{content_type}',
'image_height': image.height,
'image_width': image.width
}
url = f'{self._api.base_url}/extras/image-attachments/'
response = requests.post(url, headers=header,
files=files,
data=payload,
verify=False)
if response.status_code != 201:
raise ValueError(f'error on uploading image {response.text}') Where |
Beta Was this translation helpful? Give feedback.
Doesn't look like there is anyway to do that through the API. The
front_image
andrear_image
properties of a device_type are read only with the API.