Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id BF9CB200BF9 for ; Sun, 8 Jan 2017 10:45:49 +0100 (CET) Received: by cust-asf.ponee.io (Postfix) id BE238160B36; Sun, 8 Jan 2017 09:45:49 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id DE906160B2C for ; Sun, 8 Jan 2017 10:45:48 +0100 (CET) Received: (qmail 1945 invoked by uid 500); 8 Jan 2017 09:45:48 -0000 Mailing-List: contact dev-help@ariatosca.incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@ariatosca.incubator.apache.org Delivered-To: mailing list dev@ariatosca.incubator.apache.org Received: (qmail 1933 invoked by uid 99); 8 Jan 2017 09:45:48 -0000 Received: from pnap-us-west-generic-nat.apache.org (HELO spamd1-us-west.apache.org) (209.188.14.142) by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 08 Jan 2017 09:45:48 +0000 Received: from localhost (localhost [127.0.0.1]) by spamd1-us-west.apache.org (ASF Mail Server at spamd1-us-west.apache.org) with ESMTP id A7758C122A for ; Sun, 8 Jan 2017 09:45:47 +0000 (UTC) X-Virus-Scanned: Debian amavisd-new at spamd1-us-west.apache.org X-Spam-Flag: NO X-Spam-Score: -5.22 X-Spam-Level: X-Spam-Status: No, score=-5.22 tagged_above=-999 required=6.31 tests=[HK_RANDOM_FROM=0.999, KAM_ASCII_DIVIDERS=0.8, KAM_LAZY_DOMAIN_SECURITY=1, RCVD_IN_DNSWL_HI=-5, RCVD_IN_MSPIKE_H3=-0.01, RCVD_IN_MSPIKE_WL=-0.01, RP_MATCHES_RCVD=-2.999] autolearn=disabled Received: from mx1-lw-eu.apache.org ([10.40.0.8]) by localhost (spamd1-us-west.apache.org [10.40.0.7]) (amavisd-new, port 10024) with ESMTP id JgKwhHQneN9r for ; Sun, 8 Jan 2017 09:45:45 +0000 (UTC) Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by mx1-lw-eu.apache.org (ASF Mail Server at mx1-lw-eu.apache.org) with SMTP id 2D7AC5F4AC for ; Sun, 8 Jan 2017 09:45:44 +0000 (UTC) Received: (qmail 1905 invoked by uid 99); 8 Jan 2017 09:45:43 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 08 Jan 2017 09:45:43 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 45105DFA00; Sun, 8 Jan 2017 09:45:43 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: mxmrlv@apache.org To: dev@ariatosca.incubator.apache.org Date: Sun, 08 Jan 2017 09:45:44 -0000 Message-Id: In-Reply-To: References: X-Mailer: ASF-Git Admin Mailer Subject: [2/4] incubator-ariatosca git commit: ARIA-34 Adding get/download resource and render API (as jinja template) archived-at: Sun, 08 Jan 2017 09:45:49 -0000 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-52-Support-order-management-in-relationships Commit: fa221f7fa654c8957a7a2bad1790f73ffe1de78c Parents: 8a00b5f Author: Dan Kilman Authored: Thu Jan 5 14:15:40 2017 +0200 Committer: Dan Kilman 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)