You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@ariatosca.apache.org by da...@apache.org on 2017/01/05 14:51:30 UTC
[2/3] incubator-ariatosca git commit: ARIA-34 Adding get/download
resource and render API (as jinja template)
ARIA-34 Adding get/download resource and render API (as jinja template)
Project: http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/commit/fa221f7f
Tree: http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/tree/fa221f7f
Diff: http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/diff/fa221f7f
Branch: refs/heads/ARIA-56-node-ip
Commit: fa221f7fa654c8957a7a2bad1790f73ffe1de78c
Parents: 8a00b5f
Author: Dan Kilman <da...@gigaspaces.com>
Authored: Thu Jan 5 14:15:40 2017 +0200
Committer: Dan Kilman <da...@gigaspaces.com>
Committed: Thu Jan 5 16:49:40 2017 +0200
----------------------------------------------------------------------
aria/orchestrator/context/common.py | 48 ++++++++++---
.../context/test_resource_render.py | 73 ++++++++++++++++++++
2 files changed, 113 insertions(+), 8 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/fa221f7f/aria/orchestrator/context/common.py
----------------------------------------------------------------------
diff --git a/aria/orchestrator/context/common.py b/aria/orchestrator/context/common.py
index fdbe152..53844e8 100644
--- a/aria/orchestrator/context/common.py
+++ b/aria/orchestrator/context/common.py
@@ -17,6 +17,8 @@ A common context for both workflow and operation
"""
from uuid import uuid4
+import jinja2
+
from aria import logger
from aria.storage import exceptions
@@ -97,19 +99,49 @@ class BaseContext(logger.LoggerMixin):
Download a blueprint resource from the resource storage
"""
try:
- return self.resource.deployment.download(entry_id=self.deployment.id,
- destination=destination,
- path=path)
+ self.resource.deployment.download(entry_id=str(self.deployment.id),
+ destination=destination,
+ path=path)
except exceptions.StorageError:
- return self.resource.blueprint.download(entry_id=self.blueprint.id,
- destination=destination,
- path=path)
+ self.resource.blueprint.download(entry_id=str(self.blueprint.id),
+ destination=destination,
+ path=path)
+
+ def download_resource_and_render(self, destination, path=None, variables=None):
+ """
+ Download a blueprint resource from the resource storage render its content as a jinja
+ template using the provided variables. ctx is available to the template without providing it
+ explicitly.
+ """
+ self.download_resource(destination=destination, path=path)
+ with open(destination, 'rb') as f:
+ resource_content = f.read()
+ resource_content = self._render_resource(resource_content=resource_content,
+ variables=variables)
+ with open(destination, 'wb') as f:
+ f.write(resource_content)
def get_resource(self, path=None):
"""
Read a deployment resource as string from the resource storage
"""
try:
- return self.resource.deployment.read(entry_id=self.deployment.id, path=path)
+ return self.resource.deployment.read(entry_id=str(self.deployment.id), path=path)
except exceptions.StorageError:
- return self.resource.blueprint.read(entry_id=self.blueprint.id, path=path)
+ return self.resource.blueprint.read(entry_id=str(self.blueprint.id), path=path)
+
+ def get_resource_and_render(self, path=None, variables=None):
+ """
+ Read a deployment resource as string from the resource storage and render it as a jinja
+ template using the provided variables. ctx is available to the template without providing it
+ explicitly.
+ """
+ resource_content = self.get_resource(path=path)
+ return self._render_resource(resource_content=resource_content, variables=variables)
+
+ def _render_resource(self, resource_content, variables):
+ variables = variables or {}
+ if 'ctx' not in variables:
+ variables['ctx'] = self
+ resource_template = jinja2.Template(resource_content)
+ return resource_template.render(variables)
http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/fa221f7f/tests/orchestrator/context/test_resource_render.py
----------------------------------------------------------------------
diff --git a/tests/orchestrator/context/test_resource_render.py b/tests/orchestrator/context/test_resource_render.py
new file mode 100644
index 0000000..ca2ef42
--- /dev/null
+++ b/tests/orchestrator/context/test_resource_render.py
@@ -0,0 +1,73 @@
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+import pytest
+
+from tests import mock, storage
+
+_IMPLICIT_CTX_TEMPLATE = '{{ctx.deployment.name}}'
+_IMPLICIT_CTX_TEMPLATE_PATH = 'implicit-ctx.template'
+_VARIABLES_TEMPLATE = '{{variable}}'
+_VARIABLES_TEMPLATE_PATH = 'variables.template'
+
+
+def test_get_resource_and_render_implicit_ctx_no_variables(ctx):
+ content = ctx.get_resource_and_render(_IMPLICIT_CTX_TEMPLATE_PATH)
+ assert content == mock.models.DEPLOYMENT_NAME
+
+
+def test_get_resource_and_render_provided_variables(ctx):
+ variable = 'VARIABLE'
+ content = ctx.get_resource_and_render(_VARIABLES_TEMPLATE_PATH,
+ variables={'variable': variable})
+ assert content == variable
+
+
+def test_download_resource_and_render_implicit_ctx_no_variables(tmpdir, ctx):
+ destination = tmpdir.join('destination')
+ ctx.download_resource_and_render(destination=str(destination),
+ path=_IMPLICIT_CTX_TEMPLATE_PATH)
+ assert destination.read() == mock.models.DEPLOYMENT_NAME
+
+
+def test_download_resource_and_render_provided_variables(tmpdir, ctx):
+ destination = tmpdir.join('destination')
+ variable = 'VARIABLE'
+ ctx.download_resource_and_render(destination=str(destination),
+ path=_VARIABLES_TEMPLATE_PATH,
+ variables={'variable': variable})
+ assert destination.read() == variable
+
+
+@pytest.fixture
+def ctx(tmpdir):
+ context = mock.context.simple(storage.get_sqlite_api_kwargs(),
+ resources_dir=str(tmpdir.join('resources')))
+ yield context
+ storage.release_sqlite_storage(context.model)
+
+
+@pytest.fixture(autouse=True)
+def resources(tmpdir, ctx):
+ implicit_ctx_template_path = tmpdir.join(_IMPLICIT_CTX_TEMPLATE_PATH)
+ implicit_ctx_template_path.write(_IMPLICIT_CTX_TEMPLATE)
+ variables_template_path = tmpdir.join(_VARIABLES_TEMPLATE_PATH)
+ variables_template_path.write(_VARIABLES_TEMPLATE)
+ ctx.resource.deployment.upload(entry_id='1',
+ source=str(implicit_ctx_template_path),
+ path=_IMPLICIT_CTX_TEMPLATE_PATH)
+ ctx.resource.deployment.upload(entry_id='1',
+ source=str(variables_template_path),
+ path=_VARIABLES_TEMPLATE_PATH)