def update_port_address(self, port_id, address, token=None): """Update a port's mac address. :param port_id: Neutron port id. :param address: new MAC address. :param token: optional auth token. :raises: FailedToUpdateMacOnPort """ client = neutron.get_client(token) port_req_body = {'port': {'mac_address': address}} current_binding = self._get_binding(client, port_id) if current_binding: binding_clean_body = {'port': {'binding:host_id': ''}} try: client.update_port(port_id, binding_clean_body) except neutron_client_exc.NeutronClientException: LOG.exception( _LE("Failed to remove the current binding from " "Neutron port %s."), port_id) raise exception.FailedToUpdateMacOnPort(port_id=port_id) port_req_body['port']['binding:host_id'] = current_binding try: neutron.get_client(token).update_port(port_id, port_req_body) except neutron_client_exc.NeutronClientException: LOG.exception( _LE("Failed to update MAC address on Neutron " "port %s."), port_id) raise exception.FailedToUpdateMacOnPort(port_id=port_id)
def update_port_address(port_id, address): """Update a port's mac address. :param port_id: Neutron port id. :param address: new MAC address. :raises: FailedToUpdateMacOnPort """ client = get_client() port_req_body = {'port': {'mac_address': address}} try: msg = (_("Failed to get the current binding on Neutron " "port %s.") % port_id) port = client.show_port(port_id).get('port', {}) binding_host_id = port.get('binding:host_id') binding_profile = port.get('binding:profile') if binding_host_id: # Unbind port before we update it's mac address, because you can't # change a bound port's mac address. msg = (_("Failed to remove the current binding from " "Neutron port %s, while updating its MAC " "address.") % port_id) unbind_neutron_port(port_id, client=client) port_req_body['port']['binding:host_id'] = binding_host_id port_req_body['port']['binding:profile'] = binding_profile msg = (_("Failed to update MAC address on Neutron port %s.") % port_id) client.update_port(port_id, port_req_body) except (neutron_exceptions.NeutronClientException, exception.NetworkError): LOG.exception(msg) raise exception.FailedToUpdateMacOnPort(port_id=port_id)
def _get_binding(self, client, port_id): """Get binding:host_id property from Neutron.""" try: return client.show_port(port_id).get('port', {}).get('binding:host_id') except neutron_client_exc.NeutronClientException: LOG.exception( _LE('Failed to get the current binding on Neutron ' 'port %s.'), port_id) raise exception.FailedToUpdateMacOnPort(port_id=port_id)
def test_port_changed_address_VIF_MAC_update_fail(self, mac_update_mock): new_address = '11:22:33:44:55:bb' self.port.address = new_address mac_update_mock.side_effect = (exception.FailedToUpdateMacOnPort( port_id=self.port.uuid)) with task_manager.acquire(self.context, self.node.id) as task: self.assertRaises(exception.FailedToUpdateMacOnPort, self.interface.port_changed, task, self.port) mac_update_mock.assert_called_once_with( self.port.extra['vif_port_id'], new_address)
def test_vif_attach_update_port_exception(self, mock_upa, mock_client): self.port.extra = {} self.port.save() vif = {'id': "fake_vif_id"} mock_upa.side_effect = (exception.FailedToUpdateMacOnPort( port_id='fake')) with task_manager.acquire(self.context, self.node.id) as task: self.assertRaisesRegexp(exception.NetworkError, "can not update Neutron port", self.interface.vif_attach, task, vif)
def test_update_portgroup_address_fail(self, mac_update_mock): pg = obj_utils.create_test_portgroup(self.context, node_id=self.node.id, extra={'vif_port_id': 'fake-id'}) new_address = '11:22:33:44:55:bb' pg.address = new_address mac_update_mock.side_effect = ( exception.FailedToUpdateMacOnPort('boom')) with task_manager.acquire(self.context, self.node.id) as task: self.assertRaises(exception.FailedToUpdateMacOnPort, self.interface.portgroup_changed, task, pg) mac_update_mock.assert_called_once_with('fake-id', new_address)
def update_port_address(self, port_id, address): """Update a port's mac address. :param port_id: Neutron port id. :param address: new MAC address. :raises: FailedToUpdateMacOnPort """ port_req_body = {'port': {'mac_address': address}} try: self.client.update_port(port_id, port_req_body) except neutron_client_exc.NeutronClientException: LOG.exception(_("Failed to update MAC address on Neutron port %s." ), port_id) raise exception.FailedToUpdateMacOnPort(port_id=port_id)
def update_port_address(self, port_id, address, token=None): """Update a port's mac address. :param port_id: Neutron port id. :param address: new MAC address. :param token: optional auth token. :raises: FailedToUpdateMacOnPort """ port_req_body = {'port': {'mac_address': address}} try: _build_client(token).update_port(port_id, port_req_body) except neutron_client_exc.NeutronClientException: LOG.exception(_LE("Failed to update MAC address on Neutron " "port %s."), port_id) raise exception.FailedToUpdateMacOnPort(port_id=port_id)
def update_port_address(port_id, address, context=None): """Update a port's mac address. :param port_id: Neutron port id. :param address: new MAC address. :param context: request context :type context: ironic.common.context.RequestContext :raises: FailedToUpdateMacOnPort """ client = get_client(context=context) port_req_body = {'port': {'mac_address': address}} try: msg = (_("Failed to get the current binding on Neutron " "port %s.") % port_id) port = client.show_port(port_id).get('port', {}) binding_host_id = port.get('binding:host_id') binding_profile = port.get('binding:profile') if binding_host_id: # Unbind port before we update it's mac address, because you can't # change a bound port's mac address. msg = (_("Failed to remove the current binding from " "Neutron port %s, while updating its MAC " "address.") % port_id) unbind_neutron_port(port_id, client=client, context=context) msg = (_("Failed to update MAC address on Neutron port %s.") % port_id) client.update_port(port_id, port_req_body) # Restore original binding:profile and host_id if binding_host_id: msg = (_("Failed to update binding:host_id and profile on Neutron " "port %s.") % port_id) port_req_body = { 'port': { 'binding:host_id': binding_host_id, 'binding:profile': binding_profile } } client.update_port(port_id, port_req_body) except (neutron_exceptions.NeutronClientException, exception.NetworkError): LOG.exception(msg) raise exception.FailedToUpdateMacOnPort(port_id=port_id)