|
| 1 | +import netbox.exceptions as exceptions |
| 2 | +from netbox.dcim import Dcim |
| 3 | + |
| 4 | + |
| 5 | +class Circuits(object): |
| 6 | + |
| 7 | + def __init__(self, netbox_con): |
| 8 | + self.netbox_con = netbox_con |
| 9 | + self.dcim = Dcim(self.netbox_con) |
| 10 | + |
| 11 | + def get_circuits(self, **kwargs): |
| 12 | + """Returns the circuits""" |
| 13 | + return self.netbox_con.get('/circuits/circuits/', **kwargs) |
| 14 | + |
| 15 | + def create_circuit(self, circuit_provider, cid, circuit_type, status_id, **kwargs): |
| 16 | + """Create a new circuits |
| 17 | +
|
| 18 | + :param circuit_provider: provider name |
| 19 | + :param cid: Unique circuit id |
| 20 | + :param circuit_type: circuit type |
| 21 | + :param status_id: see below for the status codes |
| 22 | + :param kwargs: optional fields |
| 23 | + :return: netbox object if successful otherwise exception raised |
| 24 | +
|
| 25 | + circuit status codes: |
| 26 | + 0: Deprovisioning |
| 27 | + 1: Active |
| 28 | + 2: Planned |
| 29 | + 3: Provisioning |
| 30 | + 4: Offline |
| 31 | + 5: Decommissioned |
| 32 | +
|
| 33 | + """ |
| 34 | + try: |
| 35 | + provider_id = self.get_providers(name=circuit_provider)[0]['id'] |
| 36 | + except IndexError: |
| 37 | + raise exceptions.NotFoundException('cirtcuit provider: {}'.format(circuit_provider)) from None |
| 38 | + |
| 39 | + try: |
| 40 | + type_id = self.get_types(name=circuit_type)[0]['id'] |
| 41 | + except IndexError: |
| 42 | + raise exceptions.NotFoundException('circuit type: {}'.format(circuit_type)) from None |
| 43 | + |
| 44 | + required_fields = {"provider": provider_id, "circuit": cid, "type": type_id, |
| 45 | + "status": status_id} |
| 46 | + return self.netbox_con.post('/circuits/circuits/', required_fields, **kwargs) |
| 47 | + |
| 48 | + def delete_circuit(self, cid, provider): |
| 49 | + """Delete circuits |
| 50 | +
|
| 51 | + :param cid: circuit |
| 52 | + :param provider: Name of the provider |
| 53 | + :return: bool True if successful otherwise delete exception |
| 54 | + """ |
| 55 | + try: |
| 56 | + circuits_id = self.get_circuits(cid=cid, provider=provider)[0]['id'] |
| 57 | + except IndexError: |
| 58 | + raise exceptions.NotFoundException('Circuit with circuit: {} and provider: {}'.format(cid, provider)) from None |
| 59 | + return self.netbox_con.delete('/circuits/circuits/', circuits_id) |
| 60 | + |
| 61 | + def update_circuit(self, cid, provider, **kwargs): |
| 62 | + """Update circuit |
| 63 | +
|
| 64 | + :param cid: circuit |
| 65 | + :param provider: provider name |
| 66 | + :param kwargs: requests body dict |
| 67 | + :return: bool True if successful otherwise raise UpdateException |
| 68 | + """ |
| 69 | + try: |
| 70 | + circuits_id = self.get_circuits(cid=cid, provider=provider)[0]['id'] |
| 71 | + except IndexError: |
| 72 | + raise exceptions.NotFoundException('Circuit with circuit: {} and provider {}'.format(cid, provider)) from None |
| 73 | + return self.netbox_con.patch('/circuits/circuits/', circuits_id, **kwargs) |
| 74 | + |
| 75 | + def get_providers(self, **kwargs): |
| 76 | + """Returns circuit providers""" |
| 77 | + return self.netbox_con.get('/circuits/providers/', **kwargs) |
| 78 | + |
| 79 | + def create_provider(self, name, slug): |
| 80 | + """Create a new circuit provider |
| 81 | +
|
| 82 | + :param name: provider name |
| 83 | + :param slug: slug name |
| 84 | + :return: netbox object if successful otherwise exception raised |
| 85 | + """ |
| 86 | + required_fields = {"name": name, "slug": slug} |
| 87 | + return self.netbox_con.post('/circuits/providers/', required_fields) |
| 88 | + |
| 89 | + def delete_provider(self, provider_name): |
| 90 | + """Delete circuit provider |
| 91 | +
|
| 92 | + :param provider_name: circuit provider to delete |
| 93 | + :return: bool True if successful otherwise delete exception |
| 94 | + """ |
| 95 | + try: |
| 96 | + circuits_provider_id = self.get_providers(name=provider_name)[0]['id'] |
| 97 | + except IndexError: |
| 98 | + raise exceptions.NotFoundException('circuit provider: {}'.format(provider_name)) from None |
| 99 | + return self.netbox_con.delete('/circuits/providers/', circuits_provider_id) |
| 100 | + |
| 101 | + def update_provider(self, provider_name, **kwargs): |
| 102 | + """Update circuit provider |
| 103 | +
|
| 104 | + :param provider_name: circuits role to update |
| 105 | + :param kwargs: requests body dict |
| 106 | + :return: bool True if successful otherwise raise UpdateException |
| 107 | + """ |
| 108 | + try: |
| 109 | + circuits_provider_id = self.get_providers(name=provider_name)[0]['id'] |
| 110 | + except IndexError: |
| 111 | + raise exceptions.NotFoundException('circuit provider: {}'.format(provider_name)) from None |
| 112 | + return self.netbox_con.patch('/circuits/providers/', circuits_provider_id, **kwargs) |
| 113 | + |
| 114 | + def get_types(self, **kwargs): |
| 115 | + """Returns the circuit types""" |
| 116 | + return self.netbox_con.get('/circuits/circuit-types/', **kwargs) |
| 117 | + |
| 118 | + def create_type(self, name, slug): |
| 119 | + """Create a new circuit type |
| 120 | +
|
| 121 | + :param name: type name |
| 122 | + :param slug: slug name |
| 123 | + :return: netbox object if successful otherwise exception raised |
| 124 | + """ |
| 125 | + required_fields = {"name": name, "slug": slug} |
| 126 | + return self.netbox_con.post('/circuits/circuit-types/', required_fields) |
| 127 | + |
| 128 | + def delete_type(self, type_name): |
| 129 | + """Delete circuit type |
| 130 | +
|
| 131 | + :param type_name: circuit type to delete |
| 132 | + :return: bool True if succesful otherwase delete exception |
| 133 | + """ |
| 134 | + try: |
| 135 | + circuits_type_id = self.get_types(name=type_name)[0]['id'] |
| 136 | + except IndexError: |
| 137 | + raise exceptions.NotFoundException('circuit type: {}'.format(type_name)) from None |
| 138 | + return self.netbox_con.delete('/circuits/circuit-types/', circuits_type_id) |
| 139 | + |
| 140 | + def update_type(self, circuit_type_name, **kwargs): |
| 141 | + """Update circuit role |
| 142 | +
|
| 143 | + :param circuit_type_name: circuits type to update |
| 144 | + :param kwargs: requests body dict |
| 145 | + :return: bool True if successful otherwise raise UpdateException |
| 146 | + """ |
| 147 | + try: |
| 148 | + type_id = self.get_types(name=circuit_type_name)[0]['id'] |
| 149 | + except IndexError: |
| 150 | + raise exceptions.NotFoundException('circuit type: {}'.format(circuit_type_name)) from None |
| 151 | + return self.netbox_con.patch('/circuits/circuit-types/', type_id, **kwargs) |
| 152 | + |
| 153 | + def get_terminations(self, **kwargs): |
| 154 | + """Returns the circuits""" |
| 155 | + return self.netbox_con.get('/circuits/circuit-terminations/', **kwargs) |
| 156 | + |
| 157 | + def create_termination(self, circuit, term_side, site, port_speed, **kwargs): |
| 158 | + """Create a new circuit termination |
| 159 | +
|
| 160 | + :param circuit: circuit id |
| 161 | + :param term_side: term side A or Z |
| 162 | + :param site: Site name |
| 163 | + :param port_speed: port speed value |
| 164 | + :return: netbox object if successful otherwise exception raised |
| 165 | + """ |
| 166 | + try: |
| 167 | + site_id = self.dcim.get_sites(name=site)[0]['id'] |
| 168 | + except IndexError: |
| 169 | + raise exceptions.NotFoundException('site: {}'.format(site)) from None |
| 170 | + |
| 171 | + required_fields = {"circuit": circuit, "term_side": term_side, "site": site_id, "port_speed": port_speed} |
| 172 | + return self.netbox_con.post('/circuits/circuit-terminations/', required_fields, **kwargs) |
| 173 | + |
| 174 | + def delete_termination(self, circuit, term_side, site, port_speed): |
| 175 | + """Delete circuit termination |
| 176 | +
|
| 177 | + :param circuit: circuit id |
| 178 | + :param term_side: term side A or Z |
| 179 | + :param site: Site name |
| 180 | + :param port_speed: port speed value |
| 181 | + :return: bool True if successful otherwise delete exception |
| 182 | + """ |
| 183 | + try: |
| 184 | + site_id = self.dcim.get_sites(name=site)[0]['id'] |
| 185 | + except IndexError: |
| 186 | + raise exceptions.NotFoundException('site: {}'.format(site)) from None |
| 187 | + |
| 188 | + try: |
| 189 | + circuit_termination = self.get_terminations(circuit_id=circuit, term_side=term_side, site_id=site_id, |
| 190 | + port_speed=port_speed)[0]['id'] |
| 191 | + except IndexError: |
| 192 | + raise exceptions.NotFoundException('circuit termination with given arguments') from None |
| 193 | + |
| 194 | + return self.netbox_con.delete('/circuits/circuit-terminations/', circuit_termination) |
| 195 | + |
| 196 | + def update_termination(self, circuit, term_side, site, port_speed, **kwargs): |
| 197 | + """Update circuit termination |
| 198 | +
|
| 199 | + :param circuit: circuit id |
| 200 | + :param term_side: Termination side |
| 201 | + :param site: Dcim Site |
| 202 | + :param port_speed: Port speed (Kbps). maximum: 2147483647 |
| 203 | + :param kwargs: requests body dict |
| 204 | + :return: bool True if successful otherwise raise UpdateException |
| 205 | + """ |
| 206 | + try: |
| 207 | + site_id = self.dcim.get_sites(name=site)[0]['id'] |
| 208 | + except IndexError: |
| 209 | + raise exceptions.NotFoundException('site: {}'.format(site)) from None |
| 210 | + |
| 211 | + try: |
| 212 | + circuit_termination_id = self.get_terminations(circuit_id=circuit, term_side=term_side, site_id=site_id, |
| 213 | + port_speed=port_speed)[0]['id'] |
| 214 | + except IndexError: |
| 215 | + raise exceptions.NotFoundException('circuit termination with given arguments') from None |
| 216 | + |
| 217 | + return self.netbox_con.patch('/circuits/circuit-terminations/', circuit_termination_id, **kwargs) |
0 commit comments