Actual implementation of CloudFormation
This commit is contained in:
@@ -13,47 +13,70 @@
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
import json
|
||||
from oslo.config import cfg
|
||||
import requests
|
||||
|
||||
from openstack.common import log
|
||||
from os_collect_config import common
|
||||
from os_collect_config import exc
|
||||
|
||||
EC2_METADATA_URL = 'http://169.254.169.254/latest/meta-data'
|
||||
CONF = cfg.CONF
|
||||
logger = log.getLogger(__name__)
|
||||
|
||||
opts = [
|
||||
cfg.StrOpt('metadata-url',
|
||||
help='URL to query for CloudFormation Metadata'),
|
||||
cfg.StrOpt('stack-name',
|
||||
help='Stack name to describe'),
|
||||
cfg.MultiStrOpt('path',
|
||||
help='Path to Metadata'),
|
||||
]
|
||||
|
||||
|
||||
def _fetch_metadata(fetch_url, session):
|
||||
try:
|
||||
r = session.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"):
|
||||
if '=' in subkey:
|
||||
subkey = subkey[:subkey.index('=')] + '/'
|
||||
sub_fetch_url = fetch_url + subkey
|
||||
if subkey[-1] == '/':
|
||||
subkey = subkey[:-1]
|
||||
new_content[subkey] = _fetch_metadata(sub_fetch_url, session)
|
||||
content = new_content
|
||||
return content
|
||||
class CollectCfn(object):
|
||||
def __init__(self, requests_impl=common.requests):
|
||||
self._requests_impl = requests_impl
|
||||
self._session = requests_impl.Session()
|
||||
|
||||
def collect(self):
|
||||
if CONF.cfn.metadata_url is None:
|
||||
logger.warn('No metadata_url configured.')
|
||||
raise exc.CfnMetadataNotConfigured
|
||||
url = CONF.cfn.metadata_url
|
||||
stack_name = CONF.cfn.stack_name
|
||||
headers = {'Content-Type': 'application/json'}
|
||||
final_content = {}
|
||||
if CONF.cfn.path is None:
|
||||
logger.warn('No path configured')
|
||||
raise exc.CfnMetadataNotConfigured
|
||||
|
||||
def collect():
|
||||
root_url = '%s/' % (CONF.ec2.metadata_url)
|
||||
session = requests.Session()
|
||||
return _fetch_metadata(root_url, session)
|
||||
for path in CONF.cfn.path:
|
||||
if '.' not in path:
|
||||
logger.error('Path not in format resource.field[.x.y] (%s)' %
|
||||
path)
|
||||
raise exc.CfnMetadataNotConfigured
|
||||
resource, field = path.split('.', 1)
|
||||
if '.' in field:
|
||||
field, sub_path = field.split('.', 1)
|
||||
else:
|
||||
sub_path = ''
|
||||
params = {'Action': 'DescribeStackResource',
|
||||
'Stackname': stack_name,
|
||||
'LogicalResourceId': resource}
|
||||
try:
|
||||
content = self._session.get(
|
||||
url, params=params, headers=headers)
|
||||
except self._requests_impl.exceptions.RequestException as e:
|
||||
logger.warn(e)
|
||||
raise exc.CfnMetadataNotAvailable
|
||||
map_content = json.loads(content.text)
|
||||
if sub_path:
|
||||
if sub_path not in map_content:
|
||||
logger.warn('Sub-path could not be found for Resource (%s)'
|
||||
% path)
|
||||
raise exc.CfnMetadataNotConfigured
|
||||
map_content = map_content[sub_path]
|
||||
|
||||
final_content.update(map_content)
|
||||
return final_content
|
||||
|
||||
@@ -19,6 +19,7 @@ import subprocess
|
||||
|
||||
from openstack.common import log
|
||||
from os_collect_config import cache
|
||||
from os_collect_config import cfn
|
||||
from os_collect_config import common
|
||||
from os_collect_config import ec2
|
||||
from oslo.config import cfg
|
||||
@@ -40,8 +41,13 @@ def setup_conf():
|
||||
ec2_group = cfg.OptGroup(name='ec2',
|
||||
title='EC2 Metadata options')
|
||||
|
||||
cfn_group = cfg.OptGroup(name='cfn',
|
||||
title='CloudFormation API Metadata options')
|
||||
|
||||
CONF.register_group(ec2_group)
|
||||
CONF.register_group(cfn_group)
|
||||
CONF.register_cli_opts(ec2.opts, group='ec2')
|
||||
CONF.register_cli_opts(cfn.opts, group='cfn')
|
||||
|
||||
CONF.register_cli_opts(opts)
|
||||
|
||||
|
||||
@@ -8,3 +8,7 @@ class Ec2MetadataNotAvailable(SourceNotAvailable):
|
||||
|
||||
class CfnMetadataNotAvailable(SourceNotAvailable):
|
||||
"""The cfn metadata service is not available."""
|
||||
|
||||
|
||||
class CfnMetadataNotConfigured(SourceNotAvailable):
|
||||
"""The cfn metadata service is not fully configured."""
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
|
||||
import fixtures
|
||||
import json
|
||||
from oslo.config import cfg
|
||||
import requests
|
||||
import testtools
|
||||
from testtools import matchers
|
||||
@@ -41,47 +42,65 @@ class FakeResponse(dict):
|
||||
pass
|
||||
|
||||
|
||||
class FakeSession(object):
|
||||
def get(self, url):
|
||||
url = urlparse.urlparse(url)
|
||||
params = urlparse.parse_qsl(url.query)
|
||||
# TODO(clint-fewbar) Refactor usage of requests to a factory
|
||||
if 'Action' not in params:
|
||||
raise Exception('No Action')
|
||||
if params['Action'] != 'DescribeStackResources':
|
||||
raise Exception('Wrong Action (%s)' % params['Action'])
|
||||
return FakeResponse(json.dumps(META_DATA))
|
||||
class FakeRequests(object):
|
||||
exceptions = requests.exceptions
|
||||
|
||||
def __init__(self, testcase):
|
||||
self._test = testcase
|
||||
|
||||
def Session(self):
|
||||
class FakeReqSession(object):
|
||||
def __init__(self, testcase):
|
||||
self._test = testcase
|
||||
|
||||
def get(self, url, params, headers):
|
||||
url = urlparse.urlparse(url)
|
||||
self._test.assertEquals('/', url.path)
|
||||
self._test.assertEquals('application/json',
|
||||
headers['Content-Type'])
|
||||
# TODO(clint-fewbar) Refactor usage of requests to a factory
|
||||
self._test.assertIn('Action', params)
|
||||
self._test.assertEquals('DescribeStackResource',
|
||||
params['Action'])
|
||||
self._test.assertIn('LogicalResourceId', params)
|
||||
self._test.assertEquals('foo', params['LogicalResourceId'])
|
||||
return FakeResponse(json.dumps(META_DATA))
|
||||
return FakeReqSession(self._test)
|
||||
|
||||
|
||||
class FakeFailSession(object):
|
||||
def get(self, url):
|
||||
raise requests.exceptions.HTTPError(403, 'Forbidden')
|
||||
class FakeFailRequests(object):
|
||||
exceptions = requests.exceptions
|
||||
|
||||
class Session(object):
|
||||
def get(self, url, params, headers):
|
||||
raise requests.exceptions.HTTPError(403, 'Forbidden')
|
||||
|
||||
|
||||
class TestCfn(testtools.TestCase):
|
||||
def setUp(self):
|
||||
super(TestCfn, self).setUp()
|
||||
self.log = self.useFixture(fixtures.FakeLogger())
|
||||
collect.setup_conf()
|
||||
cfg.CONF.cfn.metadata_url = 'http://127.0.0.1:8000/'
|
||||
cfg.CONF.cfn.path = ['foo.Metadata']
|
||||
|
||||
def test_collect_cfn(self):
|
||||
self.useFixture(
|
||||
fixtures.MonkeyPatch('requests.Session', FakeSession))
|
||||
collect.setup_conf()
|
||||
cfn_md = cfn.collect()
|
||||
cfn_md = cfn.CollectCfn(requests_impl=FakeRequests(self)).collect()
|
||||
self.assertThat(cfn_md, matchers.IsInstance(dict))
|
||||
|
||||
for k in ('int1', 'strfoo', 'mapab'):
|
||||
for k in ('int1', 'strfoo', 'map_ab'):
|
||||
self.assertIn(k, cfn_md)
|
||||
self.assertEquals(cfn_md[k], META_DATA[k])
|
||||
|
||||
self.assertEquals(cfn_md['block-device-mapping']['ami'], 'vda')
|
||||
|
||||
self.assertEquals('', self.log.output)
|
||||
|
||||
def test_collect_cfn_fail(self):
|
||||
self.useFixture(
|
||||
fixtures.MonkeyPatch(
|
||||
'requests.Session', FakeFailSession))
|
||||
collect.setup_conf()
|
||||
self.assertRaises(exc.CfnMetadataNotAvailable, cfn.collect)
|
||||
cfn_collect = cfn.CollectCfn(requests_impl=FakeFailRequests)
|
||||
self.assertRaises(exc.CfnMetadataNotAvailable, cfn_collect.collect)
|
||||
self.assertIn('Forbidden', self.log.output)
|
||||
|
||||
def test_collect_cfn_no_path(self):
|
||||
cfg.CONF.cfn.path = None
|
||||
cfn_collect = cfn.CollectCfn(requests_impl=FakeRequests(self))
|
||||
self.assertRaises(exc.CfnMetadataNotConfigured, cfn_collect.collect)
|
||||
self.assertIn('No path configured', self.log.output)
|
||||
|
||||
@@ -13,6 +13,7 @@
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
import extras
|
||||
import fixtures
|
||||
import json
|
||||
import os
|
||||
@@ -86,3 +87,5 @@ class TestConf(testtools.TestCase):
|
||||
def test_setup_conf(self):
|
||||
collect.setup_conf()
|
||||
self.assertEquals('/var/run/os-collect-config', cfg.CONF.cachedir)
|
||||
self.assertTrue(extras.safe_hasattr(cfg.CONF, 'ec2'))
|
||||
self.assertTrue(extras.safe_hasattr(cfg.CONF, 'cfn'))
|
||||
|
||||
Reference in New Issue
Block a user