def do_stuff(host=None, in_device=None, args=None): ''' Instantiates a network device object. Then it sets the network device object's attributes with data from the input and runs update method. It also pretty-prints the network object's properties to JSON before editing/commit. ''' if not host and in_device and args: raise TypeError('Missing required arguments.') conn_params = { 'host': host, 'port': args['port'], 'username': args['username'], 'key_filename': args['key'], 'hostkey_verify': args['verify'], 'timeout': args['timeout'], } try: device_params = detect(conn_params) except Exception as error: logging.error('{} - "{}"'.format( host, error )) return None if device_params.get('name') == 'junos': conn_params['device_params'] = device_params dev_obj = Junos(conn_params) for val_name, val_vars in in_device.iteritems(): if type(val_vars) is list: val_vars = sorted(val_vars) if getattr(dev_obj, val_name) != val_vars: setattr(dev_obj, val_name, val_vars) print json.dumps(dict(dev_obj), indent=4) return dev_obj.update(edit=True, commit=True) if device_params.get('name') == 'nexus': raise NotImplemented('Soon!')
class Tests(unittest.TestCase): def setUp(self): self.conn = Junos(TestData()) def test_get_serial(self): self.assertTrue(self.conn.serial) self.assertEqual(self.conn.serial, '98f086798881') def test_get_model(self): self.assertTrue(self.conn.model) self.assertEqual(self.conn.model, 'FIREFLY-PERIMETER') def test_get_ifaces(self): self.assertTrue(self.conn.ifaces) def test_get_hostname(self): self.assertTrue(self.conn.hostname) self.assertEqual(self.conn.hostname, 'vsrx') def test_get_ntp(self): ntp_list = [ '10.0.2.1', '10.0.2.2', ] self.assertTrue(self.conn.ntp_servers) self.assertEqual(self.conn.ntp_servers, sorted(ntp_list)) def test_get_snmp_comms(self): comm_list = [ 'public', 'private', ] self.assertTrue(self.conn.snmp_communities) self.assertEqual(self.conn.snmp_communities, sorted(comm_list)) def test_update(self): self.conn.ntp_servers = [ '10.0.2.1', '10.0.2.2', '10.0.2.3', ] self.assertTrue(self.conn.update(edit=False))
def setUp(self): self.conn = Junos(TestData())