Ejemplo n.º 1
0
def _update_neutron(task):
    """Send or update the DHCP BOOT options to Neutron for this node."""
    options = _dhcp_options_for_instance()
    vifs = _get_node_vif_ids(task)
    if not vifs:
        LOG.warning(
            _("No VIFs found for node %(node)s when attempting to "
              "update Neutron DHCP BOOT options."), {'node': task.node.uuid})
        return

    # TODO(deva): decouple instantiation of NeutronAPI from task.context.
    #             Try to use the user's task.context.auth_token, but if it
    #             is not present, fall back to a server-generated context.
    #             We don't need to recreate this in every method call.
    api = neutron.NeutronAPI(task.context)
    failures = []
    for port_id, port_vif in vifs.iteritems():
        try:
            api.update_port_dhcp_opts(port_vif, options)
        except exception.FailedToUpdateDHCPOptOnPort:
            failures.append(port_id)

    if failures:
        if len(failures) == len(vifs):
            raise exception.FailedToUpdateDHCPOptOnPort(
                _("Failed to set DHCP BOOT options for any port on node %s.") %
                task.node.uuid)
        else:
            LOG.warning(
                _("Some errors were encountered when updating the "
                  "DHCP BOOT options for node %(node)s on the "
                  "following ports: %(ports)s."), {
                      'node': task.node.uuid,
                      'ports': failures
                  })
Ejemplo n.º 2
0
    def update_port(self, context, port_obj):
        """Update a port.

        :param context: request context.
        :param port_obj: a changed (but not saved) port object.
        :raises: FailedToUpdateMacOnPort if MAC address changed and update
                 Neutron failed.
        """
        port_uuid = port_obj.uuid
        LOG.debug("RPC update_port called for port %s.", port_uuid)

        with task_manager.acquire(context, port_obj.node_id) as task:
            node = task.node
            if 'address' in port_obj.obj_what_changed():
                vif = port_obj.extra.get('vif_port_id')
                if vif:
                    api = neutron.NeutronAPI(context)
                    api.update_port_address(vif, port_obj.address)
                # Log warning if there is no vif_port_id and an instance
                # is associated with the node.
                elif node.instance_uuid:
                    LOG.warning(_("No VIF found for instance %(instance)s "
                        "port %(port)s when attempting to update Neutron "
                        "port MAC address."),
                        {'port': port_uuid, 'instance': node.instance_uuid})

            port_obj.save(context)

            return port_obj
Ejemplo n.º 3
0
def get_vif_port_info(task, port_id):
    """ Get  detail port info from neutron with a given port id """
    api = neutron.NeutronAPI(task.context)
    try:
        port_info = api.client.show_port(port_id)
    except neutron_client_exc.NeutronClientException:
        LOG.exception(_("Failed to get port info %s."), port_id)
        raise exception.FailedToGetInfoOnPort(port_id=port_id)
    return port_info
Ejemplo n.º 4
0
 def test_neutron_address_update(self, mock_client_init, mock_update_port):
     address = 'fe:54:00:77:07:d9'
     port_id = 'fake-port-id'
     expected = {'port': {'mac_address': address}}
     my_context = context.RequestContext(user='******',
                                         tenant='test-tenant')
     mock_client_init.return_value = None
     api = neutron.NeutronAPI(my_context)
     mock_update_port.return_value = None
     api.update_port_address(port_id, address)
     mock_update_port.assert_called_once_with(port_id, expected)
Ejemplo n.º 5
0
 def test_neutron_address_update_with_exception(self, mock_client_init,
                                                mock_update_port):
     address = 'fe:54:00:77:07:d9'
     port_id = 'fake-port-id'
     my_context = context.RequestContext(user='******',
                                         tenant='test-tenant')
     mock_client_init.return_value = None
     api = neutron.NeutronAPI(my_context)
     mock_update_port.side_effect = (
                             neutron_client_exc.NeutronClientException())
     self.assertRaises(exception.FailedToUpdateMacOnPort,
                       api.update_port_address, port_id, address)
Ejemplo n.º 6
0
 def test_neutron_port_update_with_execption(self):
     opts = [{}]
     port_id = 'fake-port-id'
     my_context = context.RequestContext(user='******',
                                         tenant='test-tenant')
     with mock.patch.object(client.Client, "__init__") as mock_client_init:
         mock_client_init.return_value = None
         api = neutron.NeutronAPI(my_context)
         with mock.patch.object(client.Client,
                                "update_port") as mock_update_port:
             mock_update_port.side_effect = (
                 neutron_client_exc.NeutronClientException())
             self.assertRaises(exception.FailedToUpdateDHCPOptOnPort,
                               api.update_port_dhcp_opts, port_id, opts)
Ejemplo n.º 7
0
    def test_create_noauth(self):
        self.config(auth_strategy='noauth', group='neutron')
        my_context = context.RequestContext()
        expected = {
            'ca_cert': 'test-file',
            'insecure': False,
            'endpoint_url': 'test-url',
            'timeout': 30,
            'auth_strategy': 'noauth'
        }

        with mock.patch.object(client.Client, "__init__") as mock_client_init:
            mock_client_init.return_value = None
            neutron.NeutronAPI(my_context)
            mock_client_init.assert_called_once_with(**expected)
Ejemplo n.º 8
0
    def test_create_without_token(self):
        my_context = context.RequestContext(user='******',
                                            tenant='test-tenant')
        expected = {'timeout': 30,
                    'insecure': False,
                    'ca_cert': 'test-file',
                    'endpoint_url': 'test-url',
                    'username': '******',
                    'tenant_name': 'test-admin-tenant',
                    'password': '******',
                    'auth_url': 'test-auth-uri'}

        with mock.patch.object(client.Client, "__init__") as mock_client_init:
            mock_client_init.return_value = None
            neutron.NeutronAPI(my_context)
            mock_client_init.assert_called_once_with(**expected)
Ejemplo n.º 9
0
    def test_create_with_token(self):
        token = 'test-token-123'
        my_context = context.RequestContext(user='******',
                                            tenant='test-tenant',
                                            auth_token=token)
        expected = {'timeout': 30,
                    'insecure': False,
                    'ca_cert': 'test-file',
                    'token': token,
                    'endpoint_url': 'test-url',
                    'auth_strategy': None}

        with mock.patch.object(client.Client, "__init__") as mock_client_init:
            mock_client_init.return_value = None
            neutron.NeutronAPI(my_context)
            mock_client_init.assert_called_once_with(**expected)
Ejemplo n.º 10
0
    def test_neutron_port_update(self):
        opts = [{'opt_name': 'bootfile-name',
                    'opt_value': 'pxelinux.0'},
                {'opt_name': 'tftp-server',
                    'opt_value': '1.1.1.1'},
                {'opt_name': 'server-ip-address',
                    'opt_value': '1.1.1.1'}]
        port_id = 'fake-port-id'
        expected = {'port': {'extra_dhcp_opts': opts}}
        my_context = context.RequestContext(user='******',
                                            tenant='test-tenant')

        with mock.patch.object(client.Client, "__init__") as mock_client_init:
            mock_client_init.return_value = None
            api = neutron.NeutronAPI(my_context)
            with mock.patch.object(client.Client,
                                   "update_port") as mock_update_port:
                mock_update_port.return_value = None
                api.update_port_dhcp_opts(port_id, opts)
                mock_update_port.assert_called_once_with(port_id, expected)