Actual implementation of CloudFormation
This commit is contained in:
@@ -13,47 +13,70 @@
|
|||||||
# 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 json
|
||||||
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 common
|
||||||
from os_collect_config import exc
|
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
|
||||||
|
logger = log.getLogger(__name__)
|
||||||
|
|
||||||
opts = [
|
opts = [
|
||||||
cfg.StrOpt('metadata-url',
|
cfg.StrOpt('metadata-url',
|
||||||
help='URL to query for CloudFormation Metadata'),
|
help='URL to query for CloudFormation Metadata'),
|
||||||
|
cfg.StrOpt('stack-name',
|
||||||
|
help='Stack name to describe'),
|
||||||
cfg.MultiStrOpt('path',
|
cfg.MultiStrOpt('path',
|
||||||
help='Path to Metadata'),
|
help='Path to Metadata'),
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
def _fetch_metadata(fetch_url, session):
|
class CollectCfn(object):
|
||||||
try:
|
def __init__(self, requests_impl=common.requests):
|
||||||
r = session.get(fetch_url)
|
self._requests_impl = requests_impl
|
||||||
r.raise_for_status()
|
self._session = requests_impl.Session()
|
||||||
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
|
|
||||||
|
|
||||||
|
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():
|
for path in CONF.cfn.path:
|
||||||
root_url = '%s/' % (CONF.ec2.metadata_url)
|
if '.' not in path:
|
||||||
session = requests.Session()
|
logger.error('Path not in format resource.field[.x.y] (%s)' %
|
||||||
return _fetch_metadata(root_url, session)
|
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 openstack.common import log
|
||||||
from os_collect_config import cache
|
from os_collect_config import cache
|
||||||
|
from os_collect_config import cfn
|
||||||
from os_collect_config import common
|
from os_collect_config import common
|
||||||
from os_collect_config import ec2
|
from os_collect_config import ec2
|
||||||
from oslo.config import cfg
|
from oslo.config import cfg
|
||||||
@@ -40,8 +41,13 @@ def setup_conf():
|
|||||||
ec2_group = cfg.OptGroup(name='ec2',
|
ec2_group = cfg.OptGroup(name='ec2',
|
||||||
title='EC2 Metadata options')
|
title='EC2 Metadata options')
|
||||||
|
|
||||||
|
cfn_group = cfg.OptGroup(name='cfn',
|
||||||
|
title='CloudFormation API Metadata options')
|
||||||
|
|
||||||
CONF.register_group(ec2_group)
|
CONF.register_group(ec2_group)
|
||||||
|
CONF.register_group(cfn_group)
|
||||||
CONF.register_cli_opts(ec2.opts, group='ec2')
|
CONF.register_cli_opts(ec2.opts, group='ec2')
|
||||||
|
CONF.register_cli_opts(cfn.opts, group='cfn')
|
||||||
|
|
||||||
CONF.register_cli_opts(opts)
|
CONF.register_cli_opts(opts)
|
||||||
|
|
||||||
|
|||||||
@@ -8,3 +8,7 @@ class Ec2MetadataNotAvailable(SourceNotAvailable):
|
|||||||
|
|
||||||
class CfnMetadataNotAvailable(SourceNotAvailable):
|
class CfnMetadataNotAvailable(SourceNotAvailable):
|
||||||
"""The cfn metadata service is not available."""
|
"""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 fixtures
|
||||||
import json
|
import json
|
||||||
|
from oslo.config import cfg
|
||||||
import requests
|
import requests
|
||||||
import testtools
|
import testtools
|
||||||
from testtools import matchers
|
from testtools import matchers
|
||||||
@@ -41,47 +42,65 @@ class FakeResponse(dict):
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
class FakeSession(object):
|
class FakeRequests(object):
|
||||||
def get(self, url):
|
exceptions = requests.exceptions
|
||||||
url = urlparse.urlparse(url)
|
|
||||||
params = urlparse.parse_qsl(url.query)
|
def __init__(self, testcase):
|
||||||
# TODO(clint-fewbar) Refactor usage of requests to a factory
|
self._test = testcase
|
||||||
if 'Action' not in params:
|
|
||||||
raise Exception('No Action')
|
def Session(self):
|
||||||
if params['Action'] != 'DescribeStackResources':
|
class FakeReqSession(object):
|
||||||
raise Exception('Wrong Action (%s)' % params['Action'])
|
def __init__(self, testcase):
|
||||||
return FakeResponse(json.dumps(META_DATA))
|
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):
|
class FakeFailRequests(object):
|
||||||
def get(self, url):
|
exceptions = requests.exceptions
|
||||||
raise requests.exceptions.HTTPError(403, 'Forbidden')
|
|
||||||
|
class Session(object):
|
||||||
|
def get(self, url, params, headers):
|
||||||
|
raise requests.exceptions.HTTPError(403, 'Forbidden')
|
||||||
|
|
||||||
|
|
||||||
class TestCfn(testtools.TestCase):
|
class TestCfn(testtools.TestCase):
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
super(TestCfn, self).setUp()
|
super(TestCfn, self).setUp()
|
||||||
self.log = self.useFixture(fixtures.FakeLogger())
|
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):
|
def test_collect_cfn(self):
|
||||||
self.useFixture(
|
cfn_md = cfn.CollectCfn(requests_impl=FakeRequests(self)).collect()
|
||||||
fixtures.MonkeyPatch('requests.Session', FakeSession))
|
|
||||||
collect.setup_conf()
|
|
||||||
cfn_md = cfn.collect()
|
|
||||||
self.assertThat(cfn_md, matchers.IsInstance(dict))
|
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.assertIn(k, cfn_md)
|
||||||
self.assertEquals(cfn_md[k], META_DATA[k])
|
self.assertEquals(cfn_md[k], META_DATA[k])
|
||||||
|
|
||||||
self.assertEquals(cfn_md['block-device-mapping']['ami'], 'vda')
|
|
||||||
|
|
||||||
self.assertEquals('', self.log.output)
|
self.assertEquals('', self.log.output)
|
||||||
|
|
||||||
def test_collect_cfn_fail(self):
|
def test_collect_cfn_fail(self):
|
||||||
self.useFixture(
|
cfn_collect = cfn.CollectCfn(requests_impl=FakeFailRequests)
|
||||||
fixtures.MonkeyPatch(
|
self.assertRaises(exc.CfnMetadataNotAvailable, cfn_collect.collect)
|
||||||
'requests.Session', FakeFailSession))
|
|
||||||
collect.setup_conf()
|
|
||||||
self.assertRaises(exc.CfnMetadataNotAvailable, cfn.collect)
|
|
||||||
self.assertIn('Forbidden', self.log.output)
|
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
|
# See the License for the specific language governing permissions and
|
||||||
# limitations under the License.
|
# limitations under the License.
|
||||||
|
|
||||||
|
import extras
|
||||||
import fixtures
|
import fixtures
|
||||||
import json
|
import json
|
||||||
import os
|
import os
|
||||||
@@ -86,3 +87,5 @@ class TestConf(testtools.TestCase):
|
|||||||
def test_setup_conf(self):
|
def test_setup_conf(self):
|
||||||
collect.setup_conf()
|
collect.setup_conf()
|
||||||
self.assertEquals('/var/run/os-collect-config', cfg.CONF.cachedir)
|
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