Custom Script #13976
Replies: 2 comments 3 replies
-
Hey @RenatoPereira91! I notice when you define When you call One alternative might be to include the call to save Something like the following: for interface in structured_interfaces.keys():
filterhardinterface = re.compile("^[gGeEfFtT]\S+[0-9]/?[0-9]*")
filterPrimeraInterface = re.compile("^[gGeEfFtT]\S+"+device_inventory_ID+"/0/?[0-9]*")
if filterhardinterface.search(interface):
if device_inventory_ID=="1":
if "." not in interface:
new_intf = Interface(
name=interface,
type=Interface_Type[structured_interfaces[interface]['media_type']],
device_id=new_device_type
)
new_intf.full_clean() # best practice to call full_clean() first
new_intf.save()
new_device_type.assigned_object = new_intf
else:
if filterPrimeraInterface.search(interface) and "." not in interface:
interface1 = interface.split("/")
interface = interface1[0].rstrip(interface1[0][-1])+"1/"+interface1[1]+"/"+interface1[2]
new_intf = Interface(
name=interface,
devicetype=new_device_type,
type=Interface_Type[structured_interfaces[interface]['media_type']],
)
new_intf.full_clean() # best practice to call full_clean() first
new_intf.save()
new_device_type.assigned_object = new_intf Also, noticed I have added Hope this helps! If you you're still having trouble, it would be helpful to post the specific error message you're getting. |
Beta Was this translation helpful? Give feedback.
-
Ah okay, are you trying to create a new device or a new device type? The error is indicating that you cannot pass a This is creating a Device Type, not a Device: new_device_type = DeviceType(
manufacturer=data['manufacturer'],
model=structured_inventory["slot"][device_inventory_ID]["rp"][device_inventory]['pid'],
slug=structured_inventory["slot"][device_inventory_ID]["rp"][device_inventory]['pid'],
part_number=structured_inventory["slot"][device_inventory_ID]["rp"][device_inventory]['descr'],
is_full_depth=0,
comments="Added via API"
)
new_device_type.save()
self.log_info(f"Created the new device: {new_device_type}") So when you pass this object to the To create a Device, you'd do something like: new_device = Device(
name=name,
device_type=type,
device_role=role,
tenant=tenant,
platform=platform,
site=site,
custom_field_data=custom_fields,
config_template_id=config_template_id,
rack=rack,
position=position,
face=face,
)
new_device.full_clean()
new_device.save()
self.log_info(f"Created the new device: {new_device}") Then, to create an interface on that device: iface = Interface(
device=new_device,
name=name,
label=label,
type=type,
mgmt_only=is_mgmt_only,
description=description,
)
iface.full_clean()
iface.save()
self.log_success(f"Created interface") Here we are passing the device object If you were in fact trying to create a new DeviceType and then create interface templates on that DeviceType, you would need to find the specific models and parameters to use for that purpose. Netbox is architectured as a collection of data object types and relationships between them. You can explore these relationships by using the Netbox Shell. See @candlerb's comment here to learn more (also try searching for nbshell in the discussions on GitHub, there are many other such comments and discussions that mention the Netbox Shell that you may find helpful). It's all about finding which objects you need, how they relate to other objects, and what kinds of parameters those objects are expecting in order to interact with them. The Netbox API documentation can also be helpful as a place to start. You'll still probably need the Netbox Shell to get more familiar with the data models that exist, but the API is exposing the same underlying data models, so sometimes it can give you a rough idea of what you're looking for as you dive into the nbshell. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hello everyone,
Today I have a script with Python that I can add Cisco device in the NetBox, I'm trying to convert to Custom Script to make it easier to interact (today I run the python and setting the information to start).
But now I have one doubt, I'm trying to add one interface to device type but never work I getting the err in the moment "new_intf.save(). I'm post my code below ":
-------- CODE --------
Beta Was this translation helpful? Give feedback.
All reactions