Exemple #1
0
class BaseDriverTests(TestCase):
    def setUp(self):
        super(BaseDriverTests, self).setUp()

        self.mock_client = Mock()
        self.mock_stdout = Mock()
        self.driver = _DummyDriver(self.mock_client, out=self.mock_stdout)
        self.store = TemplateStore(self.driver)

    def test_create(self):
        self.driver.create(self.store, "swift.yaml", "YAML")
        self.mock_client.create.assert_called_once_with(
            self.store, "swift.yaml", "YAML")
        self.mock_client.create.assert_called_once_with(
            self.store, "swift.yaml", "YAML")

    def test_retrieve(self):

        self.driver.retrieve(self.store, "uuid")
        self.mock_client.retrieve.assert_called_once_with(self.store, "uuid")

    def test_update(self):
        self.driver.update(self.store, "uuid", "swift2.yaml", "YAML2")
        self.mock_client.update.assert_called_once_with(
            self.store, "uuid", "swift2.yaml", "YAML2")

    def test_delete(self):
        self.driver.delete(self.store, "uuid")
        self.mock_client.delete.assert_called_once_with(self.store, "uuid")

    def test_list(self):
        self.driver.list(self.store)
        self.mock_client.list.assert_called_once_with(self.store,
                                                      only_latest=False)

        self.mock_client.list.reset_mock()

        self.store.list(only_latest=True)
        self.mock_client.list.assert_called_once_with(self.store,
                                                      only_latest=True)

    def test_retrieve_by_name(self):

        self.driver.retrieve_by_name(self.store, "name")
        self.mock_client.retrieve_by_name.assert_called_once_with(self.store,
                                                                  "name",
                                                                  version=None)

        self.mock_client.retrieve_by_name.reset_mock()

        self.driver.retrieve_by_name(self.store, "name", version=2)
        self.mock_client.retrieve_by_name.assert_called_once_with(self.store,
                                                                  "name",
                                                                  version=2)
Exemple #2
0
class BaseDriverTests(TestCase):

    def setUp(self):
        super(BaseDriverTests, self).setUp()

        self.mock_client = Mock()
        self.mock_stdout = Mock()
        self.driver = _DummyDriver(self.mock_client, out=self.mock_stdout)
        self.store = TemplateStore(self.driver)

    def test_create(self):
        self.driver.create(self.store, "swift.yaml", "YAML")
        self.mock_client.create.assert_called_once_with(
            self.store, "swift.yaml", "YAML")
        self.mock_client.create.assert_called_once_with(
            self.store, "swift.yaml", "YAML")

    def test_retrieve(self):

        self.driver.retrieve(self.store, "uuid")
        self.mock_client.retrieve.assert_called_once_with(self.store, "uuid")

    def test_update(self):
        self.driver.update(self.store, "uuid", "swift2.yaml", "YAML2")
        self.mock_client.update.assert_called_once_with(
            self.store, "uuid", "swift2.yaml", "YAML2")

    def test_delete(self):
        self.driver.delete(self.store, "uuid")
        self.mock_client.delete.assert_called_once_with(self.store, "uuid")

    def test_list(self):
        self.driver.list(self.store)
        self.mock_client.list.assert_called_once_with(
            self.store, only_latest=False)

        self.mock_client.list.reset_mock()

        self.store.list(only_latest=True)
        self.mock_client.list.assert_called_once_with(
            self.store, only_latest=True)

    def test_retrieve_by_name(self):

        self.driver.retrieve_by_name(self.store, "name")
        self.mock_client.retrieve_by_name.assert_called_once_with(
            self.store, "name", version=None)

        self.mock_client.retrieve_by_name.reset_mock()

        self.driver.retrieve_by_name(self.store, "name", version=2)
        self.mock_client.retrieve_by_name.assert_called_once_with(
            self.store, "name", version=2)
Exemple #3
0
class RoleManager(object):

    def __init__(self):
        super(RoleManager, self).__init__()
        self.template_store = TemplateStore()

    def list_roles(self, only_latest=False):
        """Returns a list of all roles known to Tuskar.

        :param only_latest: if true, only the highest version of each role
               will be returned
        :type  only_latest: bool
        :return: list of tuskar model instances for each role
        :rtype:  [tuskar.manager.models.Role]
        """
        db_roles = self.template_store.list(only_latest=only_latest)
        roles = [self._role_to_tuskar_object(r) for r in db_roles]
        return roles

    def retrieve_role_by_uuid(self, role_uuid):
        """Returns the role with the given UUID.

        :type role_uuid: str
        :rtype: tuskar.manager.models.Role
        :raises tuskar.storage.exceptions.UnknownUUID: if there is no role with
                the given ID
        """
        db_role = self.template_store.retrieve(role_uuid)
        role = self._role_to_tuskar_object(db_role)
        return role

    @staticmethod
    def _role_to_tuskar_object(db_role):
        parsed = parser.parse_template(db_role.contents)
        role = models.Role(db_role.uuid, db_role.name, db_role.version,
                           parsed.description, parsed)
        return role
Exemple #4
0
class RoleManager(object):
    def __init__(self):
        super(RoleManager, self).__init__()
        self.template_store = TemplateStore()

    def list_roles(self, only_latest=False):
        """Returns a list of all roles known to Tuskar.

        :param only_latest: if true, only the highest version of each role
               will be returned
        :type  only_latest: bool
        :return: list of tuskar model instances for each role
        :rtype:  [tuskar.manager.models.Role]
        """
        db_roles = self.template_store.list(only_latest=only_latest)
        roles = [self._role_to_tuskar_object(r) for r in db_roles]
        return roles

    def retrieve_role_by_uuid(self, role_uuid):
        """Returns the role with the given UUID.

        :type role_uuid: str
        :rtype: tuskar.manager.models.Role
        :raises tuskar.storage.exceptions.UnknownUUID: if there is no role with
                the given ID
        """
        db_role = self.template_store.retrieve(role_uuid)
        role = self._role_to_tuskar_object(db_role)
        return role

    @staticmethod
    def _role_to_tuskar_object(db_role):
        parsed = parser.parse_template(db_role.contents)
        role = models.Role(db_role.uuid, db_role.name, db_role.version,
                           parsed.description, parsed)
        return role
Exemple #5
0
class RoleManager(object):
    def __init__(self):
        super(RoleManager, self).__init__()
        self.template_store = TemplateStore()
        self.template_extra_store = TemplateExtraStore()

    def list_roles(self, only_latest=False):
        """Returns a list of all roles known to Tuskar.

        :param only_latest: if true, only the highest version of each role
               will be returned
        :type  only_latest: bool
        :return: list of tuskar model instances for each role
        :rtype:  [tuskar.manager.models.Role]
        """
        db_roles = self.template_store.list(only_latest=only_latest)
        roles = [self._role_to_tuskar_object(r) for r in db_roles]
        return roles

    def retrieve_role_by_uuid(self, role_uuid):
        """Returns the role with the given UUID.

        :type role_uuid: str
        :rtype: tuskar.manager.models.Role
        :raises tuskar.storage.exceptions.UnknownUUID: if there is no role with
                the given ID
        """
        db_role = self.template_store.retrieve(role_uuid)
        role = self._role_to_tuskar_object(db_role)
        return role

    def retrieve_db_role_by_uuid(self, role_uuid):
        return self.template_store.retrieve(role_uuid)

    def retrieve_db_role_extra(self):
        return self.template_extra_store.list(only_latest=False)

    def template_extra_data_for_output(self, template_extra_paths, prefix=''):
        """Compile and return role-extra data for output as a string

            :param template_extra_paths: a list of {k,v} (name=>path)
            :type template_extra_paths: list of dict

            :param prefix: a prefix path
            :type prefix: string

            :return: a dict of path=>contents
            :rtype: dict

            The keys in template_extra_paths correspond to the names of stored
            role-extra data and the values are the paths at which the
            corresponding files ares expected to be. This list is returned by
            common.utils.resolve_template_extra_data for example:

                [{'extra_common_yaml': 'hieradata/common.yaml'},
                 {'extra_object_yaml': 'hieradata/object.yaml'}]

            Using this create a new dict that maps the path (values above) as
            key to the contents of the corresponding stored role-extra object
            (using the name above to retrieve it). For the example input
            above, the output would be like:

            {
                "hieradata/common.yaml": "CONTENTS",
                "hieradata/object.yaml": "CONTENTS"
            }

            In those cases that the template_extra_paths were generated for a
            non Role template (i.e. those templates read from the resource
            registry), include their path prefix - so that the extra data files
            are created relative to the template. For example the template
            'path/to/some_template.yaml' has a reference to the extra-data file
            'hieradata/common.yaml'. The resulting extra-data file returned by
            tuskar must then be:

            {
                "path/to/hieradata/common.yaml": "CONTENTS",
            }

        """
        res = {}
        for path in template_extra_paths:
            role_extra_name = path.keys()[0]
            role_extra_path = path[role_extra_name]
            db_role_extra = self.template_extra_store.retrieve_by_name(
                role_extra_name)
            role_extra_path = os_path.join(prefix, role_extra_path)
            res[role_extra_path] = db_role_extra.contents
        return res

    @staticmethod
    def _role_to_tuskar_object(db_role):
        parsed = parser.parse_template(db_role.contents)
        role = models.Role(db_role.uuid, db_role.name, db_role.version,
                           parsed.description, parsed)
        return role
Exemple #6
0
class RoleManager(object):

    def __init__(self):
        super(RoleManager, self).__init__()
        self.template_store = TemplateStore()
        self.template_extra_store = TemplateExtraStore()

    def list_roles(self, only_latest=False):
        """Returns a list of all roles known to Tuskar.

        :param only_latest: if true, only the highest version of each role
               will be returned
        :type  only_latest: bool
        :return: list of tuskar model instances for each role
        :rtype:  [tuskar.manager.models.Role]
        """
        db_roles = self.template_store.list(only_latest=only_latest)
        roles = [self._role_to_tuskar_object(r) for r in db_roles]
        return roles

    def retrieve_role_by_uuid(self, role_uuid):
        """Returns the role with the given UUID.

        :type role_uuid: str
        :rtype: tuskar.manager.models.Role
        :raises tuskar.storage.exceptions.UnknownUUID: if there is no role with
                the given ID
        """
        db_role = self.template_store.retrieve(role_uuid)
        role = self._role_to_tuskar_object(db_role)
        return role

    def retrieve_db_role_by_uuid(self, role_uuid):
        return self.template_store.retrieve(role_uuid)

    def retrieve_db_role_extra(self):
        return self.template_extra_store.list(only_latest=False)

    def template_extra_data_for_output(self, template_extra_paths, prefix=''):
        """Compile and return role-extra data for output as a string

            :param template_extra_paths: a list of {k,v} (name=>path)
            :type template_extra_paths: list of dict

            :param prefix: a prefix path
            :type prefix: string

            :return: a dict of path=>contents
            :rtype: dict

            The keys in template_extra_paths correspond to the names of stored
            role-extra data and the values are the paths at which the
            corresponding files ares expected to be. This list is returned by
            common.utils.resolve_template_extra_data for example:

                [{'extra_common_yaml': 'hieradata/common.yaml'},
                 {'extra_object_yaml': 'hieradata/object.yaml'}]

            Using this create a new dict that maps the path (values above) as
            key to the contents of the corresponding stored role-extra object
            (using the name above to retrieve it). For the example input
            above, the output would be like:

            {
                "hieradata/common.yaml": "CONTENTS",
                "hieradata/object.yaml": "CONTENTS"
            }

            In those cases that the template_extra_paths were generated for a
            non Role template (i.e. those templates read from the resource
            registry), include their path prefix - so that the extra data files
            are created relative to the template. For example the template
            'path/to/some_template.yaml' has a reference to the extra-data file
            'hieradata/common.yaml'. The resulting extra-data file returned by
            tuskar must then be:

            {
                "path/to/hieradata/common.yaml": "CONTENTS",
            }

        """
        res = {}
        for path in template_extra_paths:
            role_extra_name = path.keys()[0]
            role_extra_path = path[role_extra_name]
            db_role_extra = self.template_extra_store.retrieve_by_name(
                role_extra_name)
            role_extra_path = os_path.join(prefix, role_extra_path)
            res[role_extra_path] = db_role_extra.contents
        return res

    @staticmethod
    def _role_to_tuskar_object(db_role):
        parsed = parser.parse_template(db_role.contents)
        role = models.Role(db_role.uuid, db_role.name, db_role.version,
                           parsed.description, parsed)
        return role