def test_get_instance_and_project_id(self): self.nova.servers.list.return_value = [ fakes.OSInstance(fakes.OS_INSTANCE_1), fakes.OSInstance(fakes.OS_INSTANCE_2) ] self.nova.fixed_ips.get.return_value = mock.Mock(hostname='fake_name') self.assertEqual( (fakes.ID_OS_INSTANCE_1, fakes.ID_OS_PROJECT), api.get_os_instance_and_project_id(self.fake_context, fakes.IP_NETWORK_INTERFACE_2)) self.nova.fixed_ips.get.assert_called_with( fakes.IP_NETWORK_INTERFACE_2) self.nova.servers.list.assert_called_with(search_opts={ 'hostname': 'fake_name', 'all_tenants': True }) def check_raise(): self.assertRaises(exception.EC2MetadataNotFound, api.get_os_instance_and_project_id, self.fake_context, fakes.IP_NETWORK_INTERFACE_2) self.nova.servers.list.return_value = [ fakes.OSInstance(fakes.OS_INSTANCE_2) ] check_raise() self.nova.fixed_ips.get.side_effect = nova_exception.NotFound('fake') self.nova.servers.list.return_value = [ fakes.OSInstance(fakes.OS_INSTANCE_1), fakes.OSInstance(fakes.OS_INSTANCE_2) ] check_raise()
def test_get_instance_and_project_id(self): self.nova.servers.list.return_value = [ fakes.OSInstance(fakes.OS_INSTANCE_1), fakes.OSInstance(fakes.OS_INSTANCE_2)] self.nova.fixed_ips.get.return_value = mock.Mock(hostname='fake_name') self.assertEqual( (fakes.ID_OS_INSTANCE_1, fakes.ID_OS_PROJECT), api.get_os_instance_and_project_id(self.fake_context, fakes.IP_NETWORK_INTERFACE_2)) self.nova.fixed_ips.get.assert_called_with( fakes.IP_NETWORK_INTERFACE_2) self.nova.servers.list.assert_called_with( search_opts={'hostname': 'fake_name', 'all_tenants': True}) def check_raise(): self.assertRaises(exception.EC2MetadataNotFound, api.get_os_instance_and_project_id, self.fake_context, fakes.IP_NETWORK_INTERFACE_2) self.nova.servers.list.return_value = [ fakes.OSInstance(fakes.OS_INSTANCE_2)] check_raise() self.nova.fixed_ips.get.side_effect = nova_exception.NotFound('fake') self.nova.servers.list.return_value = [ fakes.OSInstance(fakes.OS_INSTANCE_1), fakes.OSInstance(fakes.OS_INSTANCE_2)] check_raise()
def _build_proxy_request_headers(self, req): if req.headers.get('X-Instance-ID'): return req.headers remote_ip = self._get_remote_ip(req) context = ec2_context.get_os_admin_context() instance_id, project_id = (api.get_os_instance_and_project_id( context, remote_ip)) return { 'X-Forwarded-For': remote_ip, 'X-Instance-ID': instance_id, 'X-Tenant-ID': project_id, 'X-Instance-ID-Signature': self._sign_instance_id(instance_id), }
def _build_proxy_request_headers(self, req): if req.headers.get('X-Instance-ID'): return req.headers remote_ip = self._get_remote_ip(req) context = ec2_context.get_os_admin_context() instance_id, project_id = ( api.get_os_instance_and_project_id(context, remote_ip)) return { 'X-Forwarded-For': remote_ip, 'X-Instance-ID': instance_id, 'X-Tenant-ID': project_id, 'X-Instance-ID-Signature': self._sign_instance_id(instance_id), }
def _get_metadata(self, req, path_tokens): context = ec2_context.get_os_admin_context() if req.headers.get('X-Instance-ID'): os_instance_id, project_id, remote_ip = ( self._unpack_request_attributes(req)) else: remote_ip = self._get_remote_ip(req) os_instance_id, project_id = (api.get_os_instance_and_project_id( context, remote_ip)) # NOTE(ft): substitute project_id for context to instance's one. # It's needed for correct describe and auto update DB operations. # It doesn't affect operations via OpenStack's clients because # these clients use auth_token field only context.project_id = project_id return api.get_metadata_item(context, path_tokens, os_instance_id, remote_ip)
def _get_metadata(self, req, path_tokens): context = ec2_context.get_os_admin_context() if req.headers.get('X-Instance-ID'): os_instance_id, project_id, remote_ip = ( self._unpack_request_attributes(req)) else: remote_ip = self._get_remote_ip(req) os_instance_id, project_id = ( api.get_os_instance_and_project_id(context, remote_ip)) # NOTE(ft): substitute project_id for context to instance's one. # It's needed for correct describe and auto update DB operations. # It doesn't affect operations via OpenStack's clients because # these clients use auth_token field only context.project_id = project_id return api.get_metadata_item(context, path_tokens, os_instance_id, remote_ip)
def _get_requester(self, req): if req.headers.get('X-Metadata-Provider'): provider_id, remote_ip = self._unpack_nsx_request(req) context = ec2_context.get_os_admin_context() os_instance_id, project_id = ( api.get_os_instance_and_project_id_by_provider_id( context, provider_id, remote_ip)) elif req.headers.get('X-Instance-ID'): os_instance_id, project_id, remote_ip = ( self._unpack_neutron_request(req)) else: remote_ip = self._unpack_nova_network_request(req) context = ec2_context.get_os_admin_context() os_instance_id, project_id = ( api.get_os_instance_and_project_id(context, remote_ip)) return {'os_instance_id': os_instance_id, 'project_id': project_id, 'private_ip': remote_ip}