Migrate from httplib2 to requests.

This commit is contained in:
Clint Byrum 2013-06-28 12:47:30 -07:00
parent 33d869975f
commit 5d961341c0
4 changed files with 29 additions and 26 deletions

View File

@ -13,8 +13,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import httplib2
from oslo.config import cfg
import requests
from openstack.common import log
from os_collect_config import exc
@ -22,8 +22,6 @@ from os_collect_config import exc
EC2_METADATA_URL = 'http://169.254.169.254/latest/meta-data'
CONF = cfg.CONF
h = httplib2.Http()
opts = [
cfg.StrOpt('metadata-url',
default=EC2_METADATA_URL,
@ -34,10 +32,14 @@ opts = [
def _fetch_metadata(fetch_url):
global h
try:
(resp, content) = h.request(fetch_url)
except httplib2.socks.HTTPError as e:
r = requests.get(fetch_url)
r.raise_for_status()
except (requests.HTTPError,
requests.ConnectionError,
requests.Timeout) as e:
log.getLogger(__name__).warn(e)
raise exc.Ec2MetadataNotAvailable
content = r.text
if fetch_url[-1] == '/':
new_content = {}
for subkey in content.split("\n"):

View File

@ -29,7 +29,7 @@ class TestCollect(testtools.TestCase):
super(TestCollect, self).setUp()
self.useFixture(
fixtures.MonkeyPatch(
'os_collect_config.ec2.h', test_ec2.FakeHttp()))
'requests.get', test_ec2.fake_get))
def tearDown(self):
super(TestCollect, self).tearDown()

View File

@ -14,7 +14,7 @@
# limitations under the License.
import fixtures
import httplib2
import requests
import testtools
from testtools import matchers
import urlparse
@ -51,28 +51,29 @@ META_DATA = {'local-ipv4': '192.0.2.1',
class FakeResponse(dict):
status = 200
def __init__(self, text):
self.text = text
def raise_for_status(self):
pass
class FakeHttp(object):
def fake_get(url):
url = urlparse.urlparse(url)
def request(self, url):
url = urlparse.urlparse(url)
if url.path == '/latest/meta-data/':
# Remove keys which have anything after /
ks = [x for x in META_DATA.keys() if ('/' not in x
or not len(x.split('/')[1]))]
return FakeResponse("\n".join(ks))
if url.path == '/latest/meta-data/':
# Remove keys which have anything after /
ks = [x for x in META_DATA.keys() if ('/' not in x
or not len(x.split('/')[1]))]
return (FakeResponse(), "\n".join(ks))
path = url.path
path = path.replace('/latest/meta-data/', '')
return (FakeResponse(), META_DATA[path])
path = url.path
path = path.replace('/latest/meta-data/', '')
return FakeResponse(META_DATA[path])
class FakeFailHttp(object):
def request(self, url):
raise httplib2.socks.HTTPError(403, 'Forbidden')
def fake_fail_get(url):
raise requests.exceptions.HTTPError(403, 'Forbidden')
class TestCollect(testtools.TestCase):
@ -82,7 +83,7 @@ class TestCollect(testtools.TestCase):
def test_collect_ec2(self):
self.useFixture(
fixtures.MonkeyPatch('os_collect_config.ec2.h', FakeHttp()))
fixtures.MonkeyPatch('requests.get', fake_get))
collect.setup_conf()
ec2_md = ec2.collect()
self.assertThat(ec2_md, matchers.IsInstance(dict))
@ -102,7 +103,7 @@ class TestCollect(testtools.TestCase):
def test_collect_ec2_fail(self):
self.useFixture(
fixtures.MonkeyPatch(
'os_collect_config.ec2.h', FakeFailHttp()))
'requests.get', fake_fail_get))
collect.setup_conf()
self.assertRaises(exc.Ec2MetadataNotAvailable, ec2.collect)
self.assertIn('Forbidden', self.log.output)

View File

@ -2,7 +2,7 @@ anyjson
argparse
d2to1
eventlet>=0.12.0
httplib2
requests
iso8601>=0.1.4
oslo.config>=1.1.0
pbr