Migrate from httplib2 to requests.
This commit is contained in:
parent
33d869975f
commit
5d961341c0
@ -13,8 +13,8 @@
|
|||||||
# 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 httplib2
|
|
||||||
from oslo.config import cfg
|
from oslo.config import cfg
|
||||||
|
import requests
|
||||||
|
|
||||||
from openstack.common import log
|
from openstack.common import log
|
||||||
from os_collect_config import exc
|
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'
|
EC2_METADATA_URL = 'http://169.254.169.254/latest/meta-data'
|
||||||
CONF = cfg.CONF
|
CONF = cfg.CONF
|
||||||
|
|
||||||
h = httplib2.Http()
|
|
||||||
|
|
||||||
opts = [
|
opts = [
|
||||||
cfg.StrOpt('metadata-url',
|
cfg.StrOpt('metadata-url',
|
||||||
default=EC2_METADATA_URL,
|
default=EC2_METADATA_URL,
|
||||||
@ -34,10 +32,14 @@ opts = [
|
|||||||
def _fetch_metadata(fetch_url):
|
def _fetch_metadata(fetch_url):
|
||||||
global h
|
global h
|
||||||
try:
|
try:
|
||||||
(resp, content) = h.request(fetch_url)
|
r = requests.get(fetch_url)
|
||||||
except httplib2.socks.HTTPError as e:
|
r.raise_for_status()
|
||||||
|
except (requests.HTTPError,
|
||||||
|
requests.ConnectionError,
|
||||||
|
requests.Timeout) as e:
|
||||||
log.getLogger(__name__).warn(e)
|
log.getLogger(__name__).warn(e)
|
||||||
raise exc.Ec2MetadataNotAvailable
|
raise exc.Ec2MetadataNotAvailable
|
||||||
|
content = r.text
|
||||||
if fetch_url[-1] == '/':
|
if fetch_url[-1] == '/':
|
||||||
new_content = {}
|
new_content = {}
|
||||||
for subkey in content.split("\n"):
|
for subkey in content.split("\n"):
|
||||||
|
@ -29,7 +29,7 @@ class TestCollect(testtools.TestCase):
|
|||||||
super(TestCollect, self).setUp()
|
super(TestCollect, self).setUp()
|
||||||
self.useFixture(
|
self.useFixture(
|
||||||
fixtures.MonkeyPatch(
|
fixtures.MonkeyPatch(
|
||||||
'os_collect_config.ec2.h', test_ec2.FakeHttp()))
|
'requests.get', test_ec2.fake_get))
|
||||||
|
|
||||||
def tearDown(self):
|
def tearDown(self):
|
||||||
super(TestCollect, self).tearDown()
|
super(TestCollect, self).tearDown()
|
||||||
|
@ -14,7 +14,7 @@
|
|||||||
# limitations under the License.
|
# limitations under the License.
|
||||||
|
|
||||||
import fixtures
|
import fixtures
|
||||||
import httplib2
|
import requests
|
||||||
import testtools
|
import testtools
|
||||||
from testtools import matchers
|
from testtools import matchers
|
||||||
import urlparse
|
import urlparse
|
||||||
@ -51,28 +51,29 @@ META_DATA = {'local-ipv4': '192.0.2.1',
|
|||||||
|
|
||||||
|
|
||||||
class FakeResponse(dict):
|
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):
|
if url.path == '/latest/meta-data/':
|
||||||
url = urlparse.urlparse(url)
|
# 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/':
|
path = url.path
|
||||||
# Remove keys which have anything after /
|
path = path.replace('/latest/meta-data/', '')
|
||||||
ks = [x for x in META_DATA.keys() if ('/' not in x
|
return FakeResponse(META_DATA[path])
|
||||||
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])
|
|
||||||
|
|
||||||
|
|
||||||
class FakeFailHttp(object):
|
def fake_fail_get(url):
|
||||||
def request(self, url):
|
raise requests.exceptions.HTTPError(403, 'Forbidden')
|
||||||
raise httplib2.socks.HTTPError(403, 'Forbidden')
|
|
||||||
|
|
||||||
|
|
||||||
class TestCollect(testtools.TestCase):
|
class TestCollect(testtools.TestCase):
|
||||||
@ -82,7 +83,7 @@ class TestCollect(testtools.TestCase):
|
|||||||
|
|
||||||
def test_collect_ec2(self):
|
def test_collect_ec2(self):
|
||||||
self.useFixture(
|
self.useFixture(
|
||||||
fixtures.MonkeyPatch('os_collect_config.ec2.h', FakeHttp()))
|
fixtures.MonkeyPatch('requests.get', fake_get))
|
||||||
collect.setup_conf()
|
collect.setup_conf()
|
||||||
ec2_md = ec2.collect()
|
ec2_md = ec2.collect()
|
||||||
self.assertThat(ec2_md, matchers.IsInstance(dict))
|
self.assertThat(ec2_md, matchers.IsInstance(dict))
|
||||||
@ -102,7 +103,7 @@ class TestCollect(testtools.TestCase):
|
|||||||
def test_collect_ec2_fail(self):
|
def test_collect_ec2_fail(self):
|
||||||
self.useFixture(
|
self.useFixture(
|
||||||
fixtures.MonkeyPatch(
|
fixtures.MonkeyPatch(
|
||||||
'os_collect_config.ec2.h', FakeFailHttp()))
|
'requests.get', fake_fail_get))
|
||||||
collect.setup_conf()
|
collect.setup_conf()
|
||||||
self.assertRaises(exc.Ec2MetadataNotAvailable, ec2.collect)
|
self.assertRaises(exc.Ec2MetadataNotAvailable, ec2.collect)
|
||||||
self.assertIn('Forbidden', self.log.output)
|
self.assertIn('Forbidden', self.log.output)
|
||||||
|
@ -2,7 +2,7 @@ anyjson
|
|||||||
argparse
|
argparse
|
||||||
d2to1
|
d2to1
|
||||||
eventlet>=0.12.0
|
eventlet>=0.12.0
|
||||||
httplib2
|
requests
|
||||||
iso8601>=0.1.4
|
iso8601>=0.1.4
|
||||||
oslo.config>=1.1.0
|
oslo.config>=1.1.0
|
||||||
pbr
|
pbr
|
||||||
|
Loading…
x
Reference in New Issue
Block a user