From: Steve Baker Date: Wed, 21 Aug 2013 21:51:24 +0000 (+1200) Subject: Replace httplib with requests for ec2tokens auth X-Git-Tag: 2014.1~149^2 X-Git-Url: https://review.fuel-infra.org/gitweb?a=commitdiff_plain;h=ff24b65eaeca07c07ef70b62e18a286ee52fb54c;p=openstack-build%2Fheat-build.git Replace httplib with requests for ec2tokens auth This vastly simplifies the implementation and test mocking. ec2token middleware is the last piece of code in the heat repository to be making direct httplib calls. Change-Id: I3807b27a7699ff19eb46e2721aaa5afd9d6c8ff9 --- diff --git a/heat/api/aws/ec2token.py b/heat/api/aws/ec2token.py index 10089c41..d223bd07 100644 --- a/heat/api/aws/ec2token.py +++ b/heat/api/aws/ec2token.py @@ -13,9 +13,8 @@ # License for the specific language governing permissions and limitations # under the License. -import urlparse -import httplib import hashlib +import requests from heat.openstack.common import gettextutils @@ -144,26 +143,11 @@ class EC2Token(wsgi.Middleware): creds_json = json.dumps(creds) headers = {'Content-Type': 'application/json'} - # Disable 'has no x member' pylint error - # for httplib and urlparse - # pylint: disable-msg=E1101 - keystone_ec2_uri = self._conf_get_keystone_ec2_uri() logger.info('Authenticating with %s' % keystone_ec2_uri) - o = urlparse.urlparse(keystone_ec2_uri) - if o.scheme == 'http': - conn = httplib.HTTPConnection(o.netloc) - else: - conn = httplib.HTTPSConnection(o.netloc) - conn.request('POST', o.path, body=creds_json, headers=headers) - response = conn.getresponse().read() - conn.close() - - # NOTE(vish): We could save a call to keystone by - # having keystone return token, tenant, - # user, and roles from this call. - - result = json.loads(response) + response = requests.post(keystone_ec2_uri, data=creds_json, + headers=headers) + result = response.json() try: token_id = result['access']['token']['id'] tenant = result['access']['token']['tenant']['name'] diff --git a/heat/tests/test_api_ec2token.py b/heat/tests/test_api_ec2token.py index f0f28d49..d5a25565 100644 --- a/heat/tests/test_api_ec2token.py +++ b/heat/tests/test_api_ec2token.py @@ -14,9 +14,8 @@ from heat.tests.common import HeatTestCase -import mox -import httplib +import requests import json from oslo.config import cfg @@ -176,15 +175,12 @@ class Ec2TokenTest(HeatTestCase): def _stub_http_connection(self, headers={}, params={}, response=None): class DummyHTTPResponse(object): - resp = response + text = response - def read(self): - return self.resp + def json(self): + return json.loads(self.text) - self.m.StubOutWithMock(httplib.HTTPConnection, '__init__') - httplib.HTTPConnection.__init__(mox.IgnoreArg()).AndReturn(None) - - self.m.StubOutWithMock(httplib.HTTPConnection, 'request') + self.m.StubOutWithMock(requests, 'post') body_hash = ('e3b0c44298fc1c149afbf4c8996fb9' '2427ae41e4649b934ca495991b7852b855') req_creds = json.dumps({"ec2Credentials": @@ -197,16 +193,9 @@ class Ec2TokenTest(HeatTestCase): "path": "/v1", "body_hash": body_hash}}) req_headers = {'Content-Type': 'application/json'} - req_path = '/v2.0/ec2tokens' - httplib.HTTPConnection.request('POST', req_path, - body=req_creds, - headers=req_headers).AndReturn(None) - - self.m.StubOutWithMock(httplib.HTTPConnection, 'getresponse') - httplib.HTTPConnection.getresponse().AndReturn(DummyHTTPResponse()) - - self.m.StubOutWithMock(httplib.HTTPConnection, 'close') - httplib.HTTPConnection.close().AndReturn(None) + req_url = 'http://123:5000/v2.0/ec2tokens' + requests.post(req_url, data=req_creds, + headers=req_headers).AndReturn(DummyHTTPResponse()) def test_call_ok(self): dummy_conf = {'auth_uri': 'http://123:5000/v2.0'}