def handle_network(self, network): '''Take a single network and create/update in NSoT''' c = self.client cidr = '%s/%d' % (network['network_address'], network['prefix_length']) network.update({'site_id': self.site_id}) self.logger.debug('Network: %s', network) already_exists = False try: # Test if existing network to determine whether to PATCH or POST # Remove attributes since these may not match exactly and the point # is to be updating them lookup = network.copy() lookup.pop('attributes', None) self.logger.debug('Lookup kwargs: %s', lookup) existing = c.networks.get(**lookup) self.logger.debug('Existing networks: %s', existing) if existing: already_exists = True except ConnectionError: self.click_ctx.fail('Cannot connect to NSoT server') except HttpClientError as e: self.handle_pynsot_err(e) except Exception as e: self.logger.exception('handle_network, checking for existing net') if already_exists: # Set the proper ID to PATCH network['id'] = existing[0]['id'] try: self.logger.info('Patching: %s', network) c.networks.patch([network]) success('%s updated!' % cidr) except ConnectionError: self.click_ctx.fail('Cannot connect to NSoT server') except HttpClientError as e: self.handle_pynsot_err(e, cidr) except Exception as e: self.logger.exception('handle_network, patching network') else: try: self.logger.info('Posting: %s', network) c.networks.post(network) success('%s created!' % cidr) except ConnectionError: self.click_ctx.fail('Cannot connect to NSoT server') except HttpClientError as e: self.handle_pynsot_err(e, cidr) except Exception as e: self.logger.exception('handle_network, posting network')
def handle_device(self, device): '''Take a single device and create/update in NSoT''' c = self.client device.update({'site_id': self.site_id}) self.logger.debug('Device', device) already_exists = False try: lookup = device.copy() lookup.pop('attributes', None) self.logger.debug('Lookup kwargs: %s', lookup) existing = c.devices.get(**lookup) self.logger.debug('Existing devices: %s', existing) if existing: already_exists = True except ConnectionError: self.click_ctx.fail('Cannot connect to NSoT server') except HttpClientError as e: self.handle_pynsot_err(e) except Exception as e: self.logger.exception('handle_device, checking for existing dev') if already_exists: device['id'] = existing[0]['id'] try: self.logger.info('Patching: %s', device) c.devices.patch([device]) success('%s updated!' % device['hostname']) except ConnectionError: self.click_ctx.fail('Cannot connect to NSoT server') except HttpClientError as e: self.handle_pynsot_err(e, device['hostname']) except Exception as e: self.logger.exception('handle_device, patching device') else: try: self.logger.info('Posting: %s', device) c.devices.post(device) success('%s created!' % device['hostname']) except ConnectionError: self.click_ctx.fail('Cannot connect to NSoT server') except HttpClientError as e: self.handle_pynsot_err(e, device['hostname']) except Exception as e: self.logger.exception('handle_device, posting device')
def handle_interface(self, interface): '''Take a single interface and create/update in NSoT Note: As part of the driver contract, an interface passed here might have 'device' set to a hostname instead of the ID. Until NSoT supports natural key references within a resource, we should detect and fetch this ''' c = self.client name = interface['name'] device = interface['device'] interface.update({'site_id': self.site_id}) self.logger.debug('Interface: %s', interface) try: result = c.devices.get(hostname=interface['device']) if not result or 'count' in result and result['count'] == 0: raise ValueError else: interface['device'] = result[0]['id'] except ValueError: # interface['device'] = int(interface['device']) interface['device'] = interface['device'] except Exception as e: self.logger.exception('handle_interface, setting device ID') already_exists = False try: # Test if existing interface to determine whether to PATCH or POST # Remove attributes since these may not match exactly and the point # is to be updating them lookup = interface.copy() lookup.pop('attributes', None) self.logger.debug('Lookup kwargs: %s', lookup) existing = c.interfaces.get(**lookup) self.logger.debug('Existing interface: %s', existing) if existing: already_exists = True except ConnectionError: self.click_ctx.fail('Cannot connect to NSoT server') except HttpClientError as e: self.handle_pynsot_err(e) except Exception as e: self.logger.exception('handle_interface, checking for existing') if already_exists: # Set the proper ID to PATCH interface['id'] = existing[0]['id'] try: self.logger.info('Patching: %s', interface) c.interfaces.patch([interface]) success('%s:%s updated!' % (device, name)) except ConnectionError: self.click_ctx.fail('Cannot connect to NSoT server') except HttpClientError as e: self.handle_pynsot_err(e, name) except Exception as e: self.logger.exception('handle_interface, patching interface') else: try: self.logger.info('Posting: %s', interface) c.interfaces.post(interface) success('%s:%s created!' % (device, name)) except ConnectionError: self.click_ctx.fail('Cannot connect to NSoT server') except HttpClientError as e: self.handle_pynsot_err(e, name) except Exception as e: self.logger.exception('handle_interface, posting interface')
def handle_interface(self, interface): '''Take a single interface and create/update in NSoT Note: As part of the driver contract, an interface passed here might have 'device' set to a hostname instead of the ID. Until NSoT supports natural key references within a resource, we should detect and fetch this ''' c = self.client name = interface['name'] device = interface['device'] interface.update({'site_id': self.site_id}) self.logger.debug('Interface: %s', interface) try: result = c.devices.get(hostname=interface['device']) if not result or 'count' in result and result['count'] == 0: raise ValueError else: interface['device'] = result[0]['id'] except ValueError: interface['device'] = int(interface['device']) except Exception as e: self.logger.exception('handle_interface, setting device ID') already_exists = False try: # Test if existing interface to determine whether to PATCH or POST # Remove attributes since these may not match exactly and the point # is to be updating them lookup = interface.copy() lookup.pop('attributes', None) self.logger.debug('Lookup kwargs: %s', lookup) existing = c.interfaces.get(**lookup) self.logger.debug('Existing interface: %s', existing) if existing: already_exists = True except ConnectionError: self.click_ctx.fail('Cannot connect to NSoT server') except HttpClientError as e: self.handle_pynsot_err(e) except Exception as e: self.logger.exception('handle_interface, checking for existing') if already_exists: # Set the proper ID to PATCH interface['id'] = existing[0]['id'] try: self.logger.info('Patching: %s', interface) c.interfaces.patch([interface]) success('%s:%s updated!' % (device, name)) except ConnectionError: self.click_ctx.fail('Cannot connect to NSoT server') except HttpClientError as e: self.handle_pynsot_err(e, name) except Exception as e: self.logger.exception('handle_interface, patching interface') else: try: self.logger.info('Posting: %s', interface) c.interfaces.post(interface) success('%s:%s created!' % (device, name)) except ConnectionError: self.click_ctx.fail('Cannot connect to NSoT server') except HttpClientError as e: self.handle_pynsot_err(e, name) except Exception as e: self.logger.exception('handle_interface, posting interface')