Commit e7621bdd authored by openstack's avatar openstack

Add return-request-id-to-caller function(v3/contrib)

Added return-request-id-to-caller function tovresources and resource
managers in the following files.

* keystoneclient/v3/contrib/simple_cert.py
* keystoneclient/v3/contrib/endpoint_policy.py
* keystoneclient/v3/contrib/oauth1/access_tokens.py
* keystoneclient/v3/contrib/oauth1/request_tokens.py

Adding request-id to below V3 contrib API's is covered in base patch
[1] but this patch is specifically for V3 API's so covered their test
cases in this patch.

* keystoneclient/v3/contrib/endpoint_filter.py
* keystoneclient/v3/contrib/federation/identity_providers.py
* keystoneclient/v3/contrib/federation/mappings.py
* keystoneclient/v3/contrib/federation/protocols.py
* keystoneclient/v3/contrib/federation/service_providers.py

The methods in the resource class and resource manager return
a 'base.Response' class that has 'request_ids' property.
The caller can get request ids of the callee via that property.

[1] https://review.openstack.org/#/c/329913

Change-Id: I5f90c31020e0dd672a160c7b587f41ba8f2b596c
Co-authored-by: default avatarDinesh Bhor <dinesh.bhor@nttdata.com>
Co-authored-by: default avatarAnkit Agrawal <ankit11.agrawal@nttdata.com>
Co-authored-by: default avatarNeha Alhat <neha.alhat@nttdata.com>
Implements: blueprint return-request-id-to-caller
parent 8982841b
...@@ -11,6 +11,7 @@ ...@@ -11,6 +11,7 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
import fixtures
import uuid import uuid
import mock import mock
...@@ -277,6 +278,53 @@ class AuthenticateWithOAuthTests(utils.TestCase, TokenTests): ...@@ -277,6 +278,53 @@ class AuthenticateWithOAuthTests(utils.TestCase, TokenTests):
oauth_client) oauth_client)
class OauthRequestIdTests(utils.TestRequestId, TokenTests):
def setUp(self):
super(OauthRequestIdTests, self).setUp()
self.mgr = consumers.ConsumerManager(self.client)
def _mock_request_method(self, method=None, body=None):
return self.useFixture(fixtures.MockPatchObject(
self.client, method, autospec=True,
return_value=(self.resp, body))
).mock
def test_get_consumers(self):
body = {"consumer": {"name": "admin"}}
get_mock = self._mock_request_method(method='get', body=body)
response = self.mgr.get("admin")
self.assertEqual(response.request_ids[0], self.TEST_REQUEST_ID)
get_mock.assert_called_once_with('/OS-OAUTH1/consumers/admin')
def test_create_consumers(self):
body = {"consumer": {"name": "admin"}}
post_mock = self._mock_request_method(method='post', body=body)
response = self.mgr.create(name="admin", description="fake")
self.assertEqual(response.request_ids[0], self.TEST_REQUEST_ID)
post_mock.assert_called_once_with('/OS-OAUTH1/consumers', body={
'consumer': {'name': 'admin', 'description': 'fake'}})
def test_update_consumers(self):
body = {"consumer": {"name": "admin"}}
patch_mock = self._mock_request_method(method='patch', body=body)
self._mock_request_method(method='post', body=body)
response = self.mgr.update("admin", "demo")
self.assertEqual(response.request_ids[0], self.TEST_REQUEST_ID)
patch_mock.assert_called_once_with('/OS-OAUTH1/consumers/admin', body={
'consumer': {'description': 'demo'}})
def test_delete_consumers(self):
get_mock = self._mock_request_method(method='delete')
_, resp = self.mgr.delete("admin")
self.assertEqual(resp.request_ids[0], self.TEST_REQUEST_ID)
get_mock.assert_called_once_with('/OS-OAUTH1/consumers/admin')
class TestOAuthLibModule(utils.TestCase): class TestOAuthLibModule(utils.TestCase):
def test_no_oauthlib_installed(self): def test_no_oauthlib_installed(self):
......
...@@ -11,16 +11,12 @@ ...@@ -11,16 +11,12 @@
# under the License. # under the License.
import fixtures import fixtures
import requests
import uuid import uuid
from keystoneauth1 import exceptions as ksa_exceptions from keystoneauth1 import exceptions as ksa_exceptions
from keystoneauth1.identity import v3
from keystoneauth1 import session
from keystoneclient import exceptions as ksc_exceptions from keystoneclient import exceptions as ksc_exceptions
from keystoneclient.tests.unit.v3 import utils from keystoneclient.tests.unit.v3 import utils
from keystoneclient.v3 import client
from keystoneclient.v3 import projects from keystoneclient.v3 import projects
...@@ -402,20 +398,12 @@ class ProjectTests(utils.ClientTestCase, utils.CrudTests): ...@@ -402,20 +398,12 @@ class ProjectTests(utils.ClientTestCase, utils.CrudTests):
return ret return ret
class ProjectsRequestIdTests(utils.TestCase): class ProjectsRequestIdTests(utils.TestRequestId):
url = "/projects" url = "/projects"
resp = requests.Response()
TEST_REQUEST_ID = uuid.uuid4().hex
resp.headers['x-openstack-request-id'] = TEST_REQUEST_ID
def setUp(self): def setUp(self):
super(ProjectsRequestIdTests, self).setUp() super(ProjectsRequestIdTests, self).setUp()
auth = v3.Token(auth_url='http://127.0.0.1:5000',
token=self.TEST_TOKEN)
session_ = session.Session(auth=auth)
self.client = client.Client(session=session_,
include_metadata='True')._adapter
self.mgr = projects.ProjectManager(self.client) self.mgr = projects.ProjectManager(self.client)
self.mgr.resource_class = projects.Project self.mgr.resource_class = projects.Project
......
...@@ -11,10 +11,12 @@ ...@@ -11,10 +11,12 @@
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
import fixtures
import testresources import testresources
from keystoneclient.tests.unit import client_fixtures from keystoneclient.tests.unit import client_fixtures
from keystoneclient.tests.unit.v3 import utils from keystoneclient.tests.unit.v3 import utils
from keystoneclient.v3.contrib import simple_cert
class SimpleCertTests(utils.ClientTestCase, testresources.ResourcedTestCase): class SimpleCertTests(utils.ClientTestCase, testresources.ResourcedTestCase):
...@@ -36,5 +38,36 @@ class SimpleCertTests(utils.ClientTestCase, testresources.ResourcedTestCase): ...@@ -36,5 +38,36 @@ class SimpleCertTests(utils.ClientTestCase, testresources.ResourcedTestCase):
self.assertEqual(self.examples.SIGNING_CERT, res) self.assertEqual(self.examples.SIGNING_CERT, res)
class SimpleCertRequestIdTests(utils.TestRequestId):
def setUp(self):
super(SimpleCertRequestIdTests, self).setUp()
self.mgr = simple_cert.SimpleCertManager(self.client)
def _mock_request_method(self, method=None, body=None):
return self.useFixture(fixtures.MockPatchObject(
self.client, method, autospec=True,
return_value=(self.resp, body))
).mock
def test_list_ca_certificates(self):
body = {"certificates": [{"name": "admin"}, {"name": "admin2"}]}
get_mock = self._mock_request_method(method='get', body=body)
response = self.mgr.get_ca_certificates()
self.assertEqual(response.request_ids[0], self.TEST_REQUEST_ID)
get_mock.assert_called_once_with(
'/OS-SIMPLE-CERT/ca', authenticated=False)
def test_list_certificates(self):
body = {"certificates": [{"name": "admin"}, {"name": "admin2"}]}
get_mock = self._mock_request_method(method='get', body=body)
response = self.mgr.get_certificates()
self.assertEqual(response.request_ids[0], self.TEST_REQUEST_ID)
get_mock.assert_called_once_with(
'/OS-SIMPLE-CERT/certificates', authenticated=False)
def load_tests(loader, tests, pattern): def load_tests(loader, tests, pattern):
return testresources.OptimisingTestSuite(tests) return testresources.OptimisingTestSuite(tests)
...@@ -10,12 +10,16 @@ ...@@ -10,12 +10,16 @@
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
import requests
import uuid import uuid
from six.moves.urllib import parse as urlparse from six.moves.urllib import parse as urlparse
from keystoneauth1.identity import v3
from keystoneauth1 import session
from keystoneclient.tests.unit import client_fixtures from keystoneclient.tests.unit import client_fixtures
from keystoneclient.tests.unit import utils from keystoneclient.tests.unit import utils
from keystoneclient.v3 import client
def parameterize(ref): def parameterize(ref):
...@@ -375,3 +379,17 @@ class CrudTests(object): ...@@ -375,3 +379,17 @@ class CrudTests(object):
self.stub_entity('DELETE', id=ref['id'], status_code=204) self.stub_entity('DELETE', id=ref['id'], status_code=204)
self.manager.delete(ref['id']) self.manager.delete(ref['id'])
class TestRequestId(TestCase):
resp = requests.Response()
TEST_REQUEST_ID = uuid.uuid4().hex
resp.headers['x-openstack-request-id'] = TEST_REQUEST_ID
def setUp(self):
super(TestRequestId, self).setUp()
auth = v3.Token(auth_url='http://127.0.0.1:5000',
token=self.TEST_TOKEN)
session_ = session.Session(auth=auth)
self.client = client.Client(session=session_,
include_metadata='True')._adapter
...@@ -39,17 +39,17 @@ class EndpointPolicyManager(base.Manager): ...@@ -39,17 +39,17 @@ class EndpointPolicyManager(base.Manager):
def create_policy_association_for_endpoint(self, policy, endpoint): def create_policy_association_for_endpoint(self, policy, endpoint):
"""Create an association between a policy and an endpoint.""" """Create an association between a policy and an endpoint."""
self._act_on_policy_association_for_endpoint( return self._act_on_policy_association_for_endpoint(
policy, endpoint, self._put) policy, endpoint, self._put)
def check_policy_association_for_endpoint(self, policy, endpoint): def check_policy_association_for_endpoint(self, policy, endpoint):
"""Check an association between a policy and an endpoint.""" """Check an association between a policy and an endpoint."""
self._act_on_policy_association_for_endpoint( return self._act_on_policy_association_for_endpoint(
policy, endpoint, self._head) policy, endpoint, self._head)
def delete_policy_association_for_endpoint(self, policy, endpoint): def delete_policy_association_for_endpoint(self, policy, endpoint):
"""Delete an association between a policy and an endpoint.""" """Delete an association between a policy and an endpoint."""
self._act_on_policy_association_for_endpoint( return self._act_on_policy_association_for_endpoint(
policy, endpoint, self._delete) policy, endpoint, self._delete)
def _act_on_policy_association_for_service(self, policy, service, action): def _act_on_policy_association_for_service(self, policy, service, action):
...@@ -67,17 +67,17 @@ class EndpointPolicyManager(base.Manager): ...@@ -67,17 +67,17 @@ class EndpointPolicyManager(base.Manager):
def create_policy_association_for_service(self, policy, service): def create_policy_association_for_service(self, policy, service):
"""Create an association between a policy and a service.""" """Create an association between a policy and a service."""
self._act_on_policy_association_for_service( return self._act_on_policy_association_for_service(
policy, service, self._put) policy, service, self._put)
def check_policy_association_for_service(self, policy, service): def check_policy_association_for_service(self, policy, service):
"""Check an association between a policy and a service.""" """Check an association between a policy and a service."""
self._act_on_policy_association_for_service( return self._act_on_policy_association_for_service(
policy, service, self._head) policy, service, self._head)
def delete_policy_association_for_service(self, policy, service): def delete_policy_association_for_service(self, policy, service):
"""Delete an association between a policy and a service.""" """Delete an association between a policy and a service."""
self._act_on_policy_association_for_service( return self._act_on_policy_association_for_service(
policy, service, self._delete) policy, service, self._delete)
def _act_on_policy_association_for_region_and_service( def _act_on_policy_association_for_region_and_service(
...@@ -99,19 +99,19 @@ class EndpointPolicyManager(base.Manager): ...@@ -99,19 +99,19 @@ class EndpointPolicyManager(base.Manager):
def create_policy_association_for_region_and_service( def create_policy_association_for_region_and_service(
self, policy, region, service): self, policy, region, service):
"""Create an association between a policy and a service in a region.""" """Create an association between a policy and a service in a region."""
self._act_on_policy_association_for_region_and_service( return self._act_on_policy_association_for_region_and_service(
policy, region, service, self._put) policy, region, service, self._put)
def check_policy_association_for_region_and_service( def check_policy_association_for_region_and_service(
self, policy, region, service): self, policy, region, service):
"""Check an association between a policy and a service in a region.""" """Check an association between a policy and a service in a region."""
self._act_on_policy_association_for_region_and_service( return self._act_on_policy_association_for_region_and_service(
policy, region, service, self._head) policy, region, service, self._head)
def delete_policy_association_for_region_and_service( def delete_policy_association_for_region_and_service(
self, policy, region, service): self, policy, region, service):
"""Delete an association between a policy and a service in a region.""" """Delete an association between a policy and a service in a region."""
self._act_on_policy_association_for_region_and_service( return self._act_on_policy_association_for_region_and_service(
policy, region, service, self._delete) policy, region, service, self._delete)
def get_policy_for_endpoint(self, endpoint): def get_policy_for_endpoint(self, endpoint):
...@@ -130,9 +130,10 @@ class EndpointPolicyManager(base.Manager): ...@@ -130,9 +130,10 @@ class EndpointPolicyManager(base.Manager):
'endpoint_id': endpoint_id, 'endpoint_id': endpoint_id,
'ext_name': self.OS_EP_POLICY_EXT} 'ext_name': self.OS_EP_POLICY_EXT}
_resp, body = self.client.get(url) resp, body = self.client.get(url)
return policies.Policy( return self._prepare_return_value(
self, body[policies.PolicyManager.key], loaded=True) resp, policies.Policy(self, body[policies.PolicyManager.key],
loaded=True))
def list_endpoints_for_policy(self, policy): def list_endpoints_for_policy(self, policy):
"""List endpoints with the effective association to a policy. """List endpoints with the effective association to a policy.
......
...@@ -48,4 +48,5 @@ class AccessTokenManager(base.CrudManager): ...@@ -48,4 +48,5 @@ class AccessTokenManager(base.CrudManager):
http_method='POST') http_method='POST')
resp, body = self.client.post(endpoint, headers=headers) resp, body = self.client.post(endpoint, headers=headers)
token = utils.get_oauth_token_from_body(resp.content) token = utils.get_oauth_token_from_body(resp.content)
return self.resource_class(self, token) return self._prepare_return_value(resp,
self.resource_class(self, token))
...@@ -70,4 +70,5 @@ class RequestTokenManager(base.CrudManager): ...@@ -70,4 +70,5 @@ class RequestTokenManager(base.CrudManager):
headers=headers) headers=headers)
resp, body = self.client.post(endpoint, headers=headers) resp, body = self.client.post(endpoint, headers=headers)
token = utils.get_oauth_token_from_body(resp.content) token = utils.get_oauth_token_from_body(resp.content)
return self.resource_class(self, token) return self._prepare_return_value(resp,
self.resource_class(self, token))
...@@ -11,12 +11,15 @@ ...@@ -11,12 +11,15 @@
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
from keystoneclient import base
class SimpleCertManager(object): class SimpleCertManager(object):
"""Manager for the OS-SIMPLE-CERT extension.""" """Manager for the OS-SIMPLE-CERT extension."""
def __init__(self, client): def __init__(self, client):
self._client = client self._client = client
self.mgr = base.Manager(self._client)
def get_ca_certificates(self): def get_ca_certificates(self):
"""Get CA certificates. """Get CA certificates.
...@@ -27,7 +30,7 @@ class SimpleCertManager(object): ...@@ -27,7 +30,7 @@ class SimpleCertManager(object):
""" """
resp, body = self._client.get('/OS-SIMPLE-CERT/ca', resp, body = self._client.get('/OS-SIMPLE-CERT/ca',
authenticated=False) authenticated=False)
return resp.text return self.mgr._prepare_return_value(resp, resp.text)
def get_certificates(self): def get_certificates(self):
"""Get signing certificates. """Get signing certificates.
...@@ -38,4 +41,4 @@ class SimpleCertManager(object): ...@@ -38,4 +41,4 @@ class SimpleCertManager(object):
""" """
resp, body = self._client.get('/OS-SIMPLE-CERT/certificates', resp, body = self._client.get('/OS-SIMPLE-CERT/certificates',
authenticated=False) authenticated=False)
return resp.text return self.mgr._prepare_return_value(resp, resp.text)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment