]> review.fuel-infra Code Review - openstack-build/heat-build.git/commitdiff
Replace httplib with requests for ec2tokens auth
authorSteve Baker <sbaker@redhat.com>
Wed, 21 Aug 2013 21:51:24 +0000 (09:51 +1200)
committerSteve Baker <sbaker@redhat.com>
Wed, 21 Aug 2013 23:10:22 +0000 (11:10 +1200)
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

heat/api/aws/ec2token.py
heat/tests/test_api_ec2token.py

index 10089c419902b21c827b687fff47f2bae0c9f9dd..d223bd07e92df0678c82fee017f78632772a45dd 100644 (file)
@@ -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']
index f0f28d49ad5ab4e4f457855b3ef5839845d2d504..d5a255659b0b4a3d564309cc6c3a7bda2a0234b8 100644 (file)
@@ -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'}