Support extracting the main template from a CSAR archive

Improve the CSAR support by adding an interface for returning the content
of the main template. Also, fix an issue in the CSAR package where reference
to the main template is missing.

Change-Id: I417fef12716445387e376b77582ac92f6dc06176
Partially-Implements: blueprint tosca-csar-translation
This commit is contained in:
Vahid Hashemian 2015-09-22 14:43:08 -07:00
parent c452df5953
commit e53f198bcb
4 changed files with 20 additions and 10 deletions

View File

@ -100,13 +100,8 @@ class CSAR(object):
def get_main_template(self):
return self._get_metadata('Entry-Definitions')
def get_description(self):
desc = self._get_metadata('Description')
if desc is not None:
return desc
def get_main_template_yaml(self):
main_template = self.get_main_template()
# extract the description from the main template
data = self.zfile.read(main_template)
invalid_tosca_yaml_err_msg = (
_('The file %(template)s in %(csar)s does not contain valid TOSCA '
@ -116,7 +111,15 @@ class CSAR(object):
tosca_yaml = yaml.load(data)
if type(tosca_yaml) is not dict:
raise ValidationError(message=invalid_tosca_yaml_err_msg)
self.metadata['Description'] = tosca_yaml['description']
return tosca_yaml
except Exception:
raise ValidationError(message=invalid_tosca_yaml_err_msg)
def get_description(self):
desc = self._get_metadata('Description')
if desc is not None:
return desc
self.metadata['Description'] = \
self.get_main_template_yaml()['description']
return self.metadata['Description']

View File

@ -1,7 +1,5 @@
TOSCA-Meta-File-Version: 1.0
CSAR-Version: 1.1
Created-By: OASIS TOSCA TC
Name: Definitions/tosca_single_instance_wordpress.yaml
Entry-Definitions: Definitions/tosca_single_instance_wordpress.yaml
Content-Type: application/vnd.oasis.tosca.definitions.yaml

View File

@ -15,6 +15,7 @@ import os
from toscaparser.common.exception import ValidationError
from toscaparser.prereq.csar import CSAR
from toscaparser.tests.base import TestCase
import toscaparser.utils
from toscaparser.utils.gettextutils import _
@ -109,3 +110,11 @@ class CSARPrereqTest(TestCase):
self.assertEqual('tosca_helloworld.yaml', csar.get_main_template())
self.assertEqual('Template for deploying a single server with '
'predefined properties.', csar.get_description())
def test_csar_main_template(self):
path = os.path.join(self.base_path, "data/CSAR/csar_hello_world.zip")
csar = CSAR(path)
yaml_file = os.path.join(os.path.dirname(os.path.abspath(__file__)),
"data/tosca_helloworld.yaml")
expected_yaml = toscaparser.utils.yamlparser.load_yaml(yaml_file)
self.assertEqual(expected_yaml, csar.get_main_template_yaml())