# Path: vitrageclient/exceptions.py
# class CommandError(Exception):
# class ClientException(Exception):
#     def __init__(self, code, message=None, request_id=None,
#                  url=None, method=None):
#     def __str__(self):
# def from_response(resp, url, method):
# 
# Path: vitrageclient/tests/utils.py
# def get_resources_dir():
#     return path.join(path.dirname(__file__), 'resources')
# 
# Path: vitrageclient/v1/template.py
# class Template(object):
# 
#     url = 'v1/template/'
# 
#     def __init__(self, api):
#         self.api = api
# 
#     def list(self):
#         """Get templates list"""
#         return self.api.get(self.url).json()
# 
#     def versions(self):
#         """Get templates versions"""
#         return self.api.get(self.url + 'versions').json()
# 
#     def show(self, _id):
#         """Show template content"""
# 
#         url = self.url + _id
#         return self.api.get(url).json()
# 
#     def add(self, path=None, template_type=None,
#             params=None, template_str=None, overwrite=False):
#         """Add a new template
# 
#         :param path: (optional) The template file path or templates dir path
#         :param template_type: (optional) The template type, in case it is not
#         written inside the template metadata section
#         :param params: (optional) Actual values for the template parameters
#         :param template_str: (optional) A string representation of the template
#         :param overwrite: (optional) overwrite the template if exists
#         yaml
#         Either path or template_str must exist (but not both)
# 
#         :return:
#         """
#         files_content = \
#             self._load_template(path=path, template_str=template_str)
#         api_params = dict(templates=files_content,
#                           template_type=template_type,
#                           params=params, overwrite=overwrite)
#         return self.api.put(self.url, json=api_params).json()
# 
#     def delete(self, ids):
#         """Delete existing"""
#         params = dict(id=ids)
#         return self.api.delete(self.url, json=params).json()
# 
#     def validate(self, path=None, template_type=None,
#                  params=None, template_str=None):
#         """Template validation
# 
#         Make sure that the template file is correct in terms of syntax
#         and content.
#         It is possible to pass a specific file path in order to validate one
#         template, or directory path for validation of several templates (the
#         directory must contain only templates)
# 
#         :param path: (optional) The template file path or templates dir path
#         :param template_type: (optional) The template type, in case it is not
#         written inside the template metadata section
#         :param params: (optional) Actual values for the template parameters
#         :param template_str: (optional) A string representation of the template
#         yaml
#         Either path or template_str must exist (but not both)
# 
#         :return:
#         """
#         files_content = \
#             self._load_template(path=path, template_str=template_str)
#         api_params = dict(templates=files_content,
#                           template_type=template_type,
#                           params=params)
#         return self.api.post(self.url, json=api_params).json()
# 
#     @classmethod
#     def _load_yaml_files(cls, path):
#         if os.path.isdir(path):
# 
#             files_content = []
#             for file_name in os.listdir(path):
# 
#                 file_path = '%s/%s' % (path, file_name)
#                 if os.path.isfile(file_path):
#                     template = cls._load_yaml_file(file_path)
#                     files_content.append((file_path, template))
#         else:
#             files_content = [(path, cls._load_yaml_file(path))]
# 
#         return files_content
# 
#     @classmethod
#     def _load_yaml_file(cls, path):
#         with open(path, 'r') as stream:
#             return cls._load_yaml(stream)
# 
#     @classmethod
#     def _load_yaml(cls, yaml_content):
#         try:
#             return yaml_utils.load(yaml_content)
#         except ValueError as e:
#             message = 'Could not load template: %s. Reason: %s' \
#                       % (yaml_content, e)
#             raise exc.CommandError(message)
# 
#     @classmethod
#     def _load_template(cls, path, template_str):
#         if path:
#             files_content = cls._load_yaml_files(path)
#         elif template_str:
#             files_content = [(None, cls._load_yaml(template_str))]
#         else:
#             raise exc.CommandError(
#                 'Add template API must be called with either \'path\' or '
#                 '\'template_str\'')
# 
#         return files_content


from unittest import mock
from oslotest import base
from vitrageclient import exceptions as exc
from vitrageclient.tests.utils import get_resources_dir
from vitrageclient.v1.template import Template

    def test_validate_by_nonexisting_path(self):
        template = Template(mock.Mock())
        self.assertRaises(IOError, template.validate,
                          path='non_existing_template_path.yaml')

    def test_validate_by_template(self):
        template = Template(mock.Mock())
        template.validate(template_str=TEMPLATE_STRING)

    def test_validate_by_nothing(self):
        template = Template(mock.Mock())
        self.assertRaises(exc.CommandError, template.validate)

    def test_add_by_path(self):
        template_path = get_resources_dir() + '/template1.yaml'
        template = Template(mock.Mock())
        template.add(path=template_path)

    def test_add_by_nonexisting_path(self):
        template = Template(mock.Mock())
        self.assertRaises(IOError, template.add,
                          path='non_existing_template_path.yaml')

    def test_add_by_template(self):
        template = Template(mock.Mock())
        template.add(template_str=TEMPLATE_STRING)

    def test_add_by_nothing(self):
        template = Template(mock.Mock())

# Path: vitrageclient/exceptions.py
# class CommandError(Exception):
# class ClientException(Exception):
#     def __init__(self, code, message=None, request_id=None,
#                  url=None, method=None):
#     def __str__(self):
# def from_response(resp, url, method):
# 
# Path: vitrageclient/tests/utils.py
# def get_resources_dir():
#     return path.join(path.dirname(__file__), 'resources')
# 
# Path: vitrageclient/v1/template.py
# class Template(object):
# 
#     url = 'v1/template/'
# 
#     def __init__(self, api):
#         self.api = api
# 
#     def list(self):
#         """Get templates list"""
#         return self.api.get(self.url).json()
# 
#     def versions(self):
#         """Get templates versions"""
#         return self.api.get(self.url + 'versions').json()
# 
#     def show(self, _id):
#         """Show template content"""
# 
#         url = self.url + _id
#         return self.api.get(url).json()
# 
#     def add(self, path=None, template_type=None,
#             params=None, template_str=None, overwrite=False):
#         """Add a new template
# 
#         :param path: (optional) The template file path or templates dir path
#         :param template_type: (optional) The template type, in case it is not
#         written inside the template metadata section
#         :param params: (optional) Actual values for the template parameters
#         :param template_str: (optional) A string representation of the template
#         :param overwrite: (optional) overwrite the template if exists
#         yaml
#         Either path or template_str must exist (but not both)
# 
#         :return:
#         """
#         files_content = \
#             self._load_template(path=path, template_str=template_str)
#         api_params = dict(templates=files_content,
#                           template_type=template_type,
#                           params=params, overwrite=overwrite)
#         return self.api.put(self.url, json=api_params).json()
# 
#     def delete(self, ids):
#         """Delete existing"""
#         params = dict(id=ids)
#         return self.api.delete(self.url, json=params).json()
# 
#     def validate(self, path=None, template_type=None,
#                  params=None, template_str=None):
#         """Template validation
# 
#         Make sure that the template file is correct in terms of syntax
#         and content.
#         It is possible to pass a specific file path in order to validate one
#         template, or directory path for validation of several templates (the
#         directory must contain only templates)
# 
#         :param path: (optional) The template file path or templates dir path
#         :param template_type: (optional) The template type, in case it is not
#         written inside the template metadata section
#         :param params: (optional) Actual values for the template parameters
#         :param template_str: (optional) A string representation of the template
#         yaml
#         Either path or template_str must exist (but not both)
# 
#         :return:
#         """
#         files_content = \
#             self._load_template(path=path, template_str=template_str)
#         api_params = dict(templates=files_content,
#                           template_type=template_type,
#                           params=params)
#         return self.api.post(self.url, json=api_params).json()
# 
#     @classmethod
#     def _load_yaml_files(cls, path):
#         if os.path.isdir(path):
# 
#             files_content = []
#             for file_name in os.listdir(path):
# 
#                 file_path = '%s/%s' % (path, file_name)
#                 if os.path.isfile(file_path):
#                     template = cls._load_yaml_file(file_path)
#                     files_content.append((file_path, template))
#         else:
#             files_content = [(path, cls._load_yaml_file(path))]
# 
#         return files_content
# 
#     @classmethod
#     def _load_yaml_file(cls, path):
#         with open(path, 'r') as stream:
#             return cls._load_yaml(stream)
# 
#     @classmethod
#     def _load_yaml(cls, yaml_content):
#         try:
#             return yaml_utils.load(yaml_content)
#         except ValueError as e:
#             message = 'Could not load template: %s. Reason: %s' \
#                       % (yaml_content, e)
#             raise exc.CommandError(message)
# 
#     @classmethod
#     def _load_template(cls, path, template_str):
#         if path:
#             files_content = cls._load_yaml_files(path)
#         elif template_str:
#             files_content = [(None, cls._load_yaml(template_str))]
#         else:
#             raise exc.CommandError(
#                 'Add template API must be called with either \'path\' or '
#                 '\'template_str\'')
# 
#         return files_content


# Path: vitrageclient/tests/test_template.py
from unittest import mock
from oslotest import base
from vitrageclient import exceptions as exc
from vitrageclient.tests.utils import get_resources_dir
from vitrageclient.v1.template import Template


    def test_validate_by_nonexisting_path(self):
        template = Template(mock.Mock())
        self.assertRaises(IOError, template.validate,
                          path='non_existing_template_path.yaml')

    def test_validate_by_template(self):
        template = Template(mock.Mock())
        template.validate(template_str=TEMPLATE_STRING)

    def test_validate_by_nothing(self):
        template = Template(mock.Mock())
        self.assertRaises(exc.CommandError, template.validate)

    def test_add_by_path(self):
        template_path = get_resources_dir() + '/template1.yaml'
        template = Template(mock.Mock())
        template.add(path=template_path)

    def test_add_by_nonexisting_path(self):
        template = Template(mock.Mock())
        self.assertRaises(IOError, template.add,
                          path='non_existing_template_path.yaml')

    def test_add_by_template(self):
        template = Template(mock.Mock())
        template.add(template_str=TEMPLATE_STRING)

    def test_add_by_nothing(self):
        template = Template(mock.Mock())

        self.assertRaises(exc.CommandError, template.add)