def test_get_nexusvm_binding_no_result_found_handling(self): with mock.patch('sqlalchemy.orm.Query.first') as mock_first: mock_first.return_value = None with self.assertRaises(c_exc.NexusPortBindingNotFound): nexus_db.get_nexusvm_binding(port_id=10, vlan_id=20, switch_ip='10.0.0.1')
def delete_port(self, device_id, vlan_id): """ Delete port bindings from the database and scan whether the network is still required on the interfaces trunked """ LOG.debug("NexusPlugin:delete_port() called\n") # Delete DB row for this port row = nxos_db.get_nexusvm_binding(vlan_id, device_id) if row: nxos_db.remove_nexusport_binding(row['port_id'], row['vlan_id'], row['switch_ip'], row['instance_id']) # Check for any other bindings with the same vlan_id and switch_ip bindings = nxos_db.get_nexusvlan_binding( row['vlan_id'], row['switch_ip']) if not bindings: # Delete this vlan from this switch _nexus_ip = row['switch_ip'] _nexus_ports = (row['port_id'],) _nexus_ssh_port = \ self._nexus_switches[_nexus_ip]['ssh_port']['ssh_port'] _nexus_creds = self.get_credential(_nexus_ip) _nexus_username = _nexus_creds['username'] _nexus_password = _nexus_creds['password'] self._client.delete_vlan( str(row['vlan_id']), _nexus_ip, _nexus_username, _nexus_password, _nexus_ports, _nexus_ssh_port) return row['instance_id']
def delete_port(self, device_id, vlan_id): """ Delete port bindings from the database and scan whether the network is still required on the interfaces trunked """ LOG.debug(_("NexusPlugin:delete_port() called")) # Delete DB row for this port row = nxos_db.get_nexusvm_binding(vlan_id, device_id) if row: nxos_db.remove_nexusport_binding(row['port_id'], row['vlan_id'], row['switch_ip'], row['instance_id']) # Check for any other bindings with the same vlan_id and switch_ip bindings = nxos_db.get_nexusvlan_binding( row['vlan_id'], row['switch_ip']) if not bindings: # Delete this vlan from this switch _nexus_ip = row['switch_ip'] _nexus_ports = (row['port_id'],) _nexus_ssh_port = \ self._nexus_switches[_nexus_ip]['ssh_port']['ssh_port'] _nexus_creds = self.get_credential(_nexus_ip) _nexus_username = _nexus_creds['username'] _nexus_password = _nexus_creds['password'] self._client.delete_vlan( str(row['vlan_id']), _nexus_ip, _nexus_username, _nexus_password, _nexus_ports, _nexus_ssh_port) return row['instance_id']
def delete_port(self, device_id, vlan_id): """Delete port. Delete port bindings from the database and scan whether the network is still required on the interfaces trunked. """ LOG.debug(_("NexusPlugin:delete_port() called")) # Delete DB row for this port try: row = nxos_db.get_nexusvm_binding(vlan_id, device_id) except cisco_exc.NexusPortBindingNotFound: return nxos_db.remove_nexusport_binding(row['port_id'], row['vlan_id'], row['switch_ip'], row['instance_id']) # Check for any other bindings with the same vlan_id and switch_ip try: nxos_db.get_nexusvlan_binding(row['vlan_id'], row['switch_ip']) except cisco_exc.NexusPortBindingNotFound: try: # Delete this vlan from this switch _nexus_ip = row['switch_ip'] _nexus_ports = () if row['port_id'] != 'router': _nexus_ports = (row['port_id'],) _nexus_ssh_port = (self._nexus_switches[_nexus_ip, 'ssh_port']) _nexus_creds = self.get_credential(_nexus_ip) _nexus_username = _nexus_creds['username'] _nexus_password = _nexus_creds['password'] self._client.delete_vlan( str(row['vlan_id']), _nexus_ip, _nexus_username, _nexus_password, _nexus_ports, _nexus_ssh_port) except Exception as e: # The delete vlan operation on the Nexus failed, # so this delete_port request has failed. For # consistency, roll back the Nexus database to what # it was before this request. try: nxos_db.add_nexusport_binding(row['port_id'], row['vlan_id'], row['switch_ip'], row['instance_id']) finally: # Raise the original exception raise e return row['instance_id']
def add_router_interface(self, vlan_name, vlan_id, subnet_id, gateway_ip, router_id): """Create VLAN SVI on the Nexus switch.""" # Find a switch to create the SVI on switch_ip = self._find_switch_for_svi() if not switch_ip: raise cisco_exc.NoNexusSwitch() _nexus_ip = switch_ip _nexus_ssh_port = self._nexus_switches[switch_ip, 'ssh_port'] _nexus_creds = self.get_credential(_nexus_ip) _nexus_username = _nexus_creds['username'] _nexus_password = _nexus_creds['password'] # Check if this vlan exists on the switch already try: nxos_db.get_nexusvlan_binding(vlan_id, switch_ip) except cisco_exc.NexusPortBindingNotFound: # Create vlan and trunk vlan on the port self._client.create_vlan( vlan_name, str(vlan_id), _nexus_ip, _nexus_username, _nexus_password, [], _nexus_ssh_port, vlan_id) # Check if a router interface has already been created try: nxos_db.get_nexusvm_binding(vlan_id, router_id) raise cisco_exc.SubnetInterfacePresent(subnet_id=subnet_id, router_id=router_id) except cisco_exc.NexusPortBindingNotFound: self._client.create_vlan_svi(vlan_id, _nexus_ip, _nexus_username, _nexus_password, _nexus_ssh_port, gateway_ip) nxos_db.add_nexusport_binding('router', str(vlan_id), switch_ip, router_id) return True
def add_router_interface(self, vlan_name, vlan_id, subnet_id, gateway_ip, router_id): """Create VLAN SVI on the Nexus switch.""" # Find a switch to create the SVI on switch_ip = self._find_switch_for_svi() if not switch_ip: raise cisco_exc.NoNexusSwitch() _nexus_ip = switch_ip _nexus_ssh_port = self._nexus_switches[switch_ip, 'ssh_port'] _nexus_creds = self.get_credential(_nexus_ip) _nexus_username = _nexus_creds['username'] _nexus_password = _nexus_creds['password'] # Check if this vlan exists on the switch already try: nxos_db.get_nexusvlan_binding(vlan_id, switch_ip) except cisco_exc.NexusPortBindingNotFound: # Create vlan and trunk vlan on the port self._client.create_vlan(vlan_name, str(vlan_id), _nexus_ip, _nexus_username, _nexus_password, [], _nexus_ssh_port, vlan_id) # Check if a router interface has already been created try: nxos_db.get_nexusvm_binding(vlan_id, router_id) raise cisco_exc.SubnetInterfacePresent(subnet_id=subnet_id, router_id=router_id) except cisco_exc.NexusPortBindingNotFound: self._client.create_vlan_svi(vlan_id, _nexus_ip, _nexus_username, _nexus_password, _nexus_ssh_port, gateway_ip) nxos_db.add_nexusport_binding('router', str(vlan_id), switch_ip, router_id) return True
def delete_port(self, device_id, vlan_id): """Delete port. Delete port bindings from the database and scan whether the network is still required on the interfaces trunked. """ LOG.debug(_("NexusPlugin:delete_port() called")) # Delete DB row for this port try: row = nxos_db.get_nexusvm_binding(vlan_id, device_id) except cisco_exc.NexusPortBindingNotFound: return nxos_db.remove_nexusport_binding(row['port_id'], row['vlan_id'], row['switch_ip'], row['instance_id']) # Check for any other bindings with the same vlan_id and switch_ip try: nxos_db.get_nexusvlan_binding(row['vlan_id'], row['switch_ip']) except cisco_exc.NexusPortBindingNotFound: try: # Delete this vlan from this switch _nexus_ip = row['switch_ip'] _nexus_ports = () if row['port_id'] != 'router': _nexus_ports = (row['port_id'], ) _nexus_ssh_port = (self._nexus_switches[_nexus_ip, 'ssh_port']) _nexus_creds = self.get_credential(_nexus_ip) _nexus_username = _nexus_creds['username'] _nexus_password = _nexus_creds['password'] self._client.delete_vlan(str(row['vlan_id']), _nexus_ip, _nexus_username, _nexus_password, _nexus_ports, _nexus_ssh_port) except Exception as e: # The delete vlan operation on the Nexus failed, # so this delete_port request has failed. For # consistency, roll back the Nexus database to what # it was before this request. try: nxos_db.add_nexusport_binding(row['port_id'], row['vlan_id'], row['switch_ip'], row['instance_id']) finally: # Raise the original exception raise e return row['instance_id']
def remove_router_interface(self, vlan_id, router_id): """Remove VLAN SVI from the Nexus Switch.""" # Grab switch_ip from database row = nxos_db.get_nexusvm_binding(vlan_id, router_id) # Delete the SVI interface from the switch _nexus_ip = row['switch_ip'] _nexus_ssh_port = self._nexus_switches[_nexus_ip, 'ssh_port'] _nexus_creds = self.get_credential(_nexus_ip) _nexus_username = _nexus_creds['username'] _nexus_password = _nexus_creds['password'] self._client.delete_vlan_svi(vlan_id, _nexus_ip, _nexus_username, _nexus_password, _nexus_ssh_port) # Invoke delete_port to delete this row # And delete vlan if required return self.delete_port(router_id, vlan_id)
def remove_router_interface(self, vlan_id, router_id): """Remove VLAN SVI from the Nexus Switch.""" # Grab switch_ip from database row = nxos_db.get_nexusvm_binding(vlan_id, router_id) # Delete the SVI interface from the switch _nexus_ip = row['switch_ip'] _nexus_ssh_port = self._nexus_switches[_nexus_ip, 'ssh_port'] _nexus_creds = self.get_credential(_nexus_ip) _nexus_username = _nexus_creds['username'] _nexus_password = _nexus_creds['password'] self._client.delete_vlan_svi(vlan_id, _nexus_ip, _nexus_username, _nexus_password, _nexus_ssh_port) # Invoke delete_port to delete this row # And delete vlan if required return self.delete_port(router_id, vlan_id)