def test_return_new_connection(self): model = DeviceModel.objects.create(type=DeviceType.rack_server, name="DevModel F1") master_device = Device.objects.create(model=model, sn="sn_1", name="dev1.dc1") connected_device_1 = Device.objects.create(model=model, sn="sn_2", name="dev2.dc1") connected_device_2 = Device.objects.create(model=model, sn="sn_3", name="dev3.dc1") Ethernet.objects.create(mac="112233AABBCC", device=connected_device_2) IPAddress.objects.create(address="10.20.30.2", device=connected_device_2) IPAddress.objects.create(address="10.20.30.3", device=connected_device_2) connection = connection_from_data( master_device, { "connected_device_serial_number": "sn_2", "connected_device_mac_addresses": "", "connection_type": "network connection", }, ) self.assertEqual(connection.outbound.id, master_device.id) self.assertEqual(connection.inbound.id, connected_device_1.id) connection = connection_from_data( master_device, { "connected_device_serial_number": "", "connected_device_mac_addresses": "112233AABBCC", "connected_device_ip_addresses": "10.20.30.2,10.20.30.3", "connection_type": "network connection", }, ) self.assertEqual(connection.outbound.id, master_device.id) self.assertEqual(connection.inbound.id, connected_device_2.id)
def test_return_new_connection(self): model = DeviceModel.objects.create(type=DeviceType.rack_server, name="DevModel F1") master_device = Device.objects.create(model=model, sn='sn_1', name='dev1.dc1') connected_device_1 = Device.objects.create(model=model, sn='sn_2', name='dev2.dc1') connected_device_2 = Device.objects.create(model=model, sn='sn_3', name='dev3.dc1') Ethernet.objects.create(mac='112233AABBCC', device=connected_device_2) IPAddress.objects.create(address='10.20.30.2', device=connected_device_2) IPAddress.objects.create(address='10.20.30.3', device=connected_device_2) connection = connection_from_data( master_device, { 'connected_device_serial_number': 'sn_2', 'connected_device_mac_addresses': '', 'connection_type': 'network connection' }) self.assertEqual(connection.outbound.id, master_device.id) self.assertEqual(connection.inbound.id, connected_device_1.id) connection = connection_from_data( master_device, { 'connected_device_serial_number': '', 'connected_device_mac_addresses': '112233AABBCC', 'connected_device_ip_addresses': '10.20.30.2,10.20.30.3', 'connection_type': 'network connection' }) self.assertEqual(connection.outbound.id, master_device.id) self.assertEqual(connection.inbound.id, connected_device_2.id)
def test_return_new_connection(self): model = DeviceModel.objects.create( type=DeviceType.rack_server, name="DevModel F1" ) master_device = Device.objects.create( model=model, sn='sn_1', name='dev1.dc1' ) connected_device_1 = Device.objects.create( model=model, sn='sn_2', name='dev2.dc1' ) connected_device_2 = Device.objects.create( model=model, sn='sn_3', name='dev3.dc1' ) Ethernet.objects.create(mac='112233AABBCC', device=connected_device_2) IPAddress.objects.create( address='10.20.30.2', device=connected_device_2 ) IPAddress.objects.create( address='10.20.30.3', device=connected_device_2 ) connection = connection_from_data( master_device, { 'connected_device_serial_number': 'sn_2', 'connected_device_mac_addresses': '', 'connection_type': 'network connection' } ) self.assertEqual(connection.outbound.id, master_device.id) self.assertEqual(connection.inbound.id, connected_device_1.id) connection = connection_from_data( master_device, { 'connected_device_serial_number': '', 'connected_device_mac_addresses': '112233AABBCC', 'connected_device_ip_addresses': '10.20.30.2,10.20.30.3', 'connection_type': 'network connection' } ) self.assertEqual(connection.outbound.id, master_device.id) self.assertEqual(connection.inbound.id, connected_device_2.id)
def test_connection_details(self): model = DeviceModel.objects.create(type=DeviceType.rack_server, name="DevModel F1") master_device = Device.objects.create(model=model, sn='sn_1', name='dev1.dc1') connected_device = Device.objects.create(model=model, sn='sn_2', name='dev2.dc1') Ethernet.objects.create(mac='112233AABBDD', device=connected_device) connection = Connection.objects.create( connection_type=ConnectionType.network, outbound=master_device, inbound=connected_device) NetworkConnection.objects.create(connection=connection, outbound_port="eth0", inbound_port="eth1") connection = connection_from_data( master_device, { 'connected_device_serial_number': 'sn_2', 'connected_device_mac_addresses': '112233AABBDD', 'connection_type': 'network connection' }) self.assertEqual(connection.networkconnection.outbound_port, "eth0") self.assertEqual(connection.networkconnection.inbound_port, "eth1")
def _create_or_update_connection(device, connection_data): if connection_data['connection_type'] != 'network': return try: connection = connection_from_data(device, connection_data) except ValueError as e: sn = '' if 'connected_device_serial_number' in connection_data: sn = connection_data['connected_device_serial_number'] mac_addresses = '' if 'connected_device_mac_addresses' in connection_data: mac_addresses = connection_data['connected_device_mac_addresses'] ip_addresses = '' if 'connected_device_ip_addresses' in connection_data: mac_addresses = connection_data['connected_device_ip_addresses'] msg = """ Connection creating failed. Connection params: sn={}; mac_addresses={}; ip_addresses={}; Exception message: {}. """.format(sn, mac_addresses, ip_addresses, unicode(e)) logger.exception(msg) connetion_details = connection_data.get('details', {}) if connetion_details: outbound_port = connetion_details.get('outbound_port') inbound_port = connetion_details.get('inbound_port') try: details = NetworkConnection.objects.get(connection=connection) except NetworkConnection.DoesNotExist: details = NetworkConnection(connection=connection) if outbound_port: details.outbound_port = outbound_port if inbound_port: details.inbound_port = inbound_port details.save() return connection
def test_connection_details(self): model = DeviceModel.objects.create(type=DeviceType.rack_server, name="DevModel F1") master_device = Device.objects.create(model=model, sn="sn_1", name="dev1.dc1") connected_device = Device.objects.create(model=model, sn="sn_2", name="dev2.dc1") Ethernet.objects.create(mac="112233AABBDD", device=connected_device) connection = Connection.objects.create( connection_type=ConnectionType.network, outbound=master_device, inbound=connected_device ) NetworkConnection.objects.create(connection=connection, outbound_port="eth0", inbound_port="eth1") connection = connection_from_data( master_device, { "connected_device_serial_number": "sn_2", "connected_device_mac_addresses": "112233AABBDD", "connection_type": "network connection", }, ) self.assertEqual(connection.networkconnection.outbound_port, "eth0") self.assertEqual(connection.networkconnection.inbound_port, "eth1")
def test_connection_details(self): model = DeviceModel.objects.create( type=DeviceType.rack_server, name="DevModel F1" ) master_device = Device.objects.create( model=model, sn='sn_1', name='dev1.dc1' ) connected_device = Device.objects.create( model=model, sn='sn_2', name='dev2.dc1' ) Ethernet.objects.create(mac='112233AABBDD', device=connected_device) connection = Connection.objects.create( connection_type=ConnectionType.network, outbound=master_device, inbound=connected_device ) NetworkConnection.objects.create( connection=connection, outbound_port="eth0", inbound_port="eth1" ) connection = connection_from_data( master_device, { 'connected_device_serial_number': 'sn_2', 'connected_device_mac_addresses': '112233AABBDD', 'connection_type': 'network connection' } ) self.assertEqual(connection.networkconnection.outbound_port, "eth0") self.assertEqual(connection.networkconnection.inbound_port, "eth1")