From d14cdfd9156669f81f6ca78889e8b9a31077315b Mon Sep 17 00:00:00 2001 From: Idan Hefetz Date: Mon, 22 Jan 2018 12:36:04 +0000 Subject: [PATCH] template add validate fix client Change-Id: I9e78471cd2b62298742246b658632790bd0b15b0 --- vitrageclient/v1/cli/template.py | 7 ++++++- vitrageclient/v1/template.py | 27 ++++++++++++++++----------- 2 files changed, 22 insertions(+), 12 deletions(-) diff --git a/vitrageclient/v1/cli/template.py b/vitrageclient/v1/cli/template.py index a3f0e6f..5be6b47 100644 --- a/vitrageclient/v1/cli/template.py +++ b/vitrageclient/v1/cli/template.py @@ -28,6 +28,10 @@ class TemplateValidate(show.ShowOne): required=True, help='full path for template file or templates dir' ) + parser.add_argument('--type', + choices=['standard', 'definition', 'equivalence'], + help='Template type. Valid types:' + '[standard, definition, equivalence]') return parser @property @@ -37,7 +41,8 @@ class TemplateValidate(show.ShowOne): def take_action(self, parsed_args): result = utils.get_client(self).template.validate( - path=parsed_args.path) + path=parsed_args.path, + template_type=parsed_args.type) return self.dict2columns(result) diff --git a/vitrageclient/v1/template.py b/vitrageclient/v1/template.py index 9914ebc..e34f958 100644 --- a/vitrageclient/v1/template.py +++ b/vitrageclient/v1/template.py @@ -35,10 +35,11 @@ class Template(object): url = self.url + uuid return self.api.get(url).json() - def add(self, path, template_type): + def add(self, path, template_type=None): """Add a new template""" - params = dict(path=path, template_type=template_type) + files_content = self._load_yaml_files(path) + params = dict(templates=files_content, template_type=template_type) return self.api.put(self.url, json=params).json() def delete(self, uuid): @@ -46,7 +47,7 @@ class Template(object): params = dict(uuid=uuid) return self.api.delete(self.url, json=params).json() - def validate(self, path=None): + def validate(self, path, template_type=None): """Template validation Make sure that the template file is correct in terms of syntax @@ -56,26 +57,30 @@ class Template(object): directory must contain only templates) :param path: the template file path or templates dir path + :param template_type: type of templates ('standard','definition',...) """ + files_content = self._load_yaml_files(path) + params = dict(templates=files_content, template_type=template_type) + return self.api.post(self.url, json=params).json() + + def _load_yaml_files(self, path): if os.path.isdir(path): - templates = [] + files_content = [] for file_name in os.listdir(path): file_path = '%s/%s' % (path, file_name) if os.path.isfile(file_path): - template = self.load_template_definition(file_path) - templates.append((file_path, template)) + template = self._load_yaml_file(file_path) + files_content.append((file_path, template)) else: - templates = [(path, self.load_template_definition(path))] + files_content = [(path, self._load_yaml_file(path))] - params = dict(templates=templates) - - return self.api.post(self.url, json=params).json() + return files_content @staticmethod - def load_template_definition(path): + def _load_yaml_file(path): with open(path, 'r') as stream: try: