incubator-ariatosca git commit: ARIA-34 Adding get/download resource and render API (as jinja template) [Forced Update!]
Repository: incubator-ariatosca Updated Branches: refs/heads/ARIA-34-render-resource ee62a02a4 -> 97e078954 (forced update) 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/97e07895 Tree: http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/tree/97e07895 Diff: http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/diff/97e07895 Branch: refs/heads/ARIA-34-render-resource Commit: 97e078954e6326a0727909b92423de63fe2114f9 Parents: 12e175b Author: Dan KilmanAuthored: Thu Jan 5 14:15:40 2017 +0200 Committer: Dan Kilman Committed: Thu Jan 5 16:26:16 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/97e07895/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/97e07895/tests/orchestrator/context/test_resource_render.py
incubator-ariatosca git commit: ARIA-34 Adding get/download resource and render API (as jinja template) [Forced Update!]
Repository: incubator-ariatosca Updated Branches: refs/heads/ARIA-34-render-resource 06b53650e -> ee62a02a4 (forced update) 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/ee62a02a Tree: http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/tree/ee62a02a Diff: http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/diff/ee62a02a Branch: refs/heads/ARIA-34-render-resource Commit: ee62a02a4bca1cc63d6c6053c3a1b57e7c5391dc Parents: 12e175b Author: Dan KilmanAuthored: Thu Jan 5 14:15:40 2017 +0200 Committer: Dan Kilman Committed: Thu Jan 5 15:13:53 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/ee62a02a/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/ee62a02a/tests/orchestrator/context/test_resource_render.py