diff --git a/netbox/dcim.py b/netbox/dcim.py index 7911cca..1da29e2 100644 --- a/netbox/dcim.py +++ b/netbox/dcim.py @@ -250,6 +250,42 @@ def update_rack_group_by_id(self, rack_group_id, **kwargs): """ return self.netbox_con.patch('/dcim/rack-groups/', rack_group_id, **kwargs) + def create_virtual_chassis(self, name, **kwargs): + """Create a new virtual chassis + + :param name: Name of the virtual chassis + :param kwargs: Optional arguments + :return: netbox object if successful otherwise raise CreateException + """ + required_fields = {"name": name} + return self.netbox_con.post('/dcim/virtual-chassis/', required_fields, **kwargs) + + def get_virtual_chassis(self, **kwargs): + """Get all virtual chassis""" + return self.netbox_con.get('/dcim/virtual-chassis/', **kwargs) + + def update_virtual_chassis(self, virtual_chassis_name, **kwargs): + """Update virtual chassis by virtual chassis name + + :param virtual_chassis_name: virtual chassis name to update + :param kwargs: requests body dict + :return: bool True if successful otherwise raise UpdateException + """ + virtual_chassis_id = self.get_virtual_chassis(name=virtual_chassis_name)[0]['id'] + return self.netbox_con.patch('/dcim/virtual-chassis/', virtual_chassis_id, **kwargs) + + def delete_virtual_chassis(self, virtual_chassis_name): + """Delete virtual chassis by virtual chassis name name + + :param virtual_chassis_name: Virtual chassis to delete + :return: bool True if successful otherwise raise DeleteException + """ + try: + device_id = self.get_virtual_chassis(name=virtual_chassis_name)[0]['id'] + except IndexError: + raise exceptions.NotFoundException({"detail": "device: {}".format(virtual_chassis_name)}) from None + return self.netbox_con.delete('/dcim/virtual-chassis/', device_id) + def get_devices(self, **kwargs): """Get all devices""" return self.netbox_con.get('/dcim/devices/', **kwargs) @@ -769,3 +805,71 @@ def get_power_ports(self, **kwargs): def get_power_connections(self, **kwargs): """Return power connections """ return self.netbox_con.get('/dcim/power-connections/', **kwargs) + + def create_rear_port(self, name, device_id, type, **kwargs): + """Create a new rear port for a device + + :param name: rear port name + :param device_id: device id for the device + :param type: the type for the rear port (for instance: 8p8c) + :param kwargs: optional fields + :return: netbox object if successful otherwise exception raised + """ + required_fields = { + "name": name, + "device": device_id, + "type": type + } + return self.netbox_con.post('/dcim/rear-ports/', required_fields, **kwargs) + + def get_rear_ports(self, **kwargs): + """Return rear ports""" + return self.netbox_con.get('/dcim/rear-ports/', **kwargs) + + def create_front_port(self, name, device_id, type, rear_port_id, **kwargs): + """Create a new front port for a device + + :param name: rear port name + :param device_id: device id for the device + :param type: the type for the rear port (for instance: 8p8c) + :param rear_port_id: the id for the connected rear port + :param kwargs: optional fields + :return: netbox object if successful otherwise exception raised + """ + required_fields = { + "name": name, + "device": device_id, + "rear_port": rear_port_id, + "type": type + } + return self.netbox_con.post('/dcim/front-ports/', required_fields, **kwargs) + + def get_front_ports(self, **kwargs): + """Return front ports""" + return self.netbox_con.get('/dcim/front-ports/', **kwargs) + + def create_cable(self, termination_a_type, termination_a_id, termination_b_type, termination_b_id, **kwargs): + """Create a new front port for a device + + :param termination_a_type: the type for the first termination (dcim.interface, dcim.frontport, etc.) + :param termination_a_id: the id for the first termination + :param termination_b_type: the type for the second termination (dcim.interface, dcim.frontport, etc.) + :param termination_b_id: the id for the second termination + :param kwargs: optional fields + :return: netbox object if successful otherwise exception raised + """ + required_fields = { + "a_terminations": [ + { + "object_type": termination_a_type, + "object_id": termination_a_id + } + ], + "b_terminations": [ + { + "object_type": termination_b_type, + "object_id": termination_b_id + } + ], + } + return self.netbox_con.post('/dcim/cables/', required_fields, **kwargs) \ No newline at end of file