Exemple #1
0
    def remove_role_from_plan(self, plan_uuid, role_uuid):
        """Removes a role from the given plan.

        :type plan_uuid: str
        :type role_uuid: str
        :raise tuskar.storage.exceptions.UnknownUUID: if the plan or role
               doesn't exist
        """

        # Load the objects from storage.
        db_plan = self.plan_store.retrieve(plan_uuid)
        db_role = self.template_store.retrieve(role_uuid)

        # Parse the plan into template objects.
        deployment_plan = self._plan_to_template_object(db_plan)

        # Delete the role from the plan by it's namespace.
        role_namespace = name_utils.generate_role_namespace(db_role.name,
                                                            db_role.version)
        deployment_plan.remove_template(role_namespace)

        # Save the updated plan.
        updated = self._save_updated_plan(plan_uuid, deployment_plan)

        return updated
Exemple #2
0
    def add_role_to_plan(self, plan_uuid, role_uuid):
        """Adds a role to the given plan, storing the changes in Tuskar's
        storage.

        :type plan_uuid: str
        :type role_uuid: str
        :return: updated plan model instance
        :rtype: tuskar.manager.models.DeploymentPlan
        :raises tuskar.storage.exceptions.UnknownUUID: if either the plan
                or the role cannot be found
        """

        # Load the plan and role from storage
        db_plan = self.plan_store.retrieve(plan_uuid)
        db_role = self.template_store.retrieve(role_uuid)

        # Parse the plan and role template into template objects.
        deployment_plan = self._plan_to_template_object(db_plan)
        role_template = self._role_to_template_object(db_role)

        # Use the combination logic to perform the addition.
        role_namespace = name_utils.generate_role_namespace(db_role.name,
                                                            db_role.version)
        template_filename = name_utils.role_template_filename(db_role.name,
                                                              db_role.version)
        deployment_plan.add_template(role_namespace, role_template,
                                     template_filename)

        # Save the updated plan.
        updated = self._save_updated_plan(plan_uuid, deployment_plan)

        return updated
Exemple #3
0
    def test_set_parameter_values(self):
        # Setup
        test_role = self._add_test_role()
        test_plan = self.plans_manager.create_plan('p1', 'd1')
        self.plans_manager.add_role_to_plan(test_plan.uuid, test_role.uuid)

        # Test
        ns = name_utils.generate_role_namespace(test_role.name,
                                                test_role.version)
        update_us = [
            ParameterValue(ns_utils.apply_template_namespace(ns, 'key_name'),
                           'test-key'),
            ParameterValue(ns_utils.apply_template_namespace(ns, 'image_id'),
                           'test-image'),
        ]
        updated_plan = self.plans_manager.set_parameter_values(test_plan.uuid,
                                                               update_us)

        # Verify
        self.assertTrue(updated_plan is not None)
        self.assertTrue(isinstance(updated_plan, DeploymentPlan))

        # Pull it from the database again to make sure it was saved
        found = self.plans_manager.retrieve_plan(test_plan.uuid)
        found_params = sorted(found.parameters, key=lambda x: x.name)
        self.assertEqual(4, len(found_params))  # 3 + 1 for scaling
        self.assertEqual(found_params[0].value, '1')
        self.assertEqual(found_params[1].value, 'test-image')
        self.assertEqual(found_params[2].value, 'm1.small')
        self.assertEqual(found_params[3].value, 'test-key')
Exemple #4
0
    def test_set_parameter_values(self):
        # Setup
        test_role = self._add_test_role()
        test_plan = self.plans_manager.create_plan('p1', 'd1')
        self.plans_manager.add_role_to_plan(test_plan.uuid, test_role.uuid)

        # Test
        ns = name_utils.generate_role_namespace(test_role.name,
                                                test_role.version)
        update_us = [
            ParameterValue(ns_utils.apply_template_namespace(ns, 'key_name'),
                           'test-key'),
            ParameterValue(ns_utils.apply_template_namespace(ns, 'image_id'),
                           'test-image'),
        ]
        updated_plan = self.plans_manager.set_parameter_values(
            test_plan.uuid, update_us)

        # Verify
        self.assertTrue(updated_plan is not None)
        self.assertTrue(isinstance(updated_plan, DeploymentPlan))

        # Pull it from the database again to make sure it was saved
        found = self.plans_manager.retrieve_plan(test_plan.uuid)
        found_params = sorted(found.parameters, key=lambda x: x.name)
        self.assertEqual(4, len(found_params))  # 3 + 1 for scaling
        self.assertEqual(found_params[0].value, '1')
        self.assertEqual(found_params[1].value, 'test-image')
        self.assertEqual(found_params[2].value, 'm1.small')
        self.assertEqual(found_params[3].value, 'test-key')
Exemple #5
0
    def remove_role_from_plan(self, plan_uuid, role_uuid):
        """Removes a role from the given plan.

        :type plan_uuid: str
        :type role_uuid: str
        :raise tuskar.storage.exceptions.UnknownUUID: if the plan or role
               doesn't exist
        """

        # Load the objects from storage.
        db_plan = self.plan_store.retrieve(plan_uuid)
        db_role = self.template_store.retrieve(role_uuid)

        # Parse the plan into template objects.
        deployment_plan = self._plan_to_template_object(db_plan)

        # Delete the role from the plan by it's namespace.
        role_namespace = name_utils.generate_role_namespace(db_role.name,
                                                            db_role.version)
        deployment_plan.remove_template(role_namespace)

        # Save the updated plan.
        updated = self._save_updated_plan(plan_uuid, deployment_plan)

        return updated
Exemple #6
0
    def test_set_non_existent_parameters(self):
        # Setup
        test_role = self._add_test_role()
        test_plan = self.plans_manager.create_plan('p1', 'd1')
        self.plans_manager.add_role_to_plan(test_plan.uuid, test_role.uuid)

        # Test
        ns = name_utils.generate_role_namespace(test_role.name,
                                                test_role.version)
        not_present_in_role_1_name = ns_utils.apply_template_namespace(
            ns, 'not_present_in_role_1')
        not_present_in_role_2_name = ns_utils.apply_template_namespace(
            ns, 'not_present_in_role_2')
        update_us = [
            ParameterValue(ns_utils.apply_template_namespace(ns, 'key_name'),
                           'test-key'),
            ParameterValue(ns_utils.apply_template_namespace(ns, 'image_id'),
                           'test-image'),
            ParameterValue(not_present_in_role_1_name,
                           'not-present-in-role-1-value'),
            ParameterValue(not_present_in_role_2_name,
                           'not-present-in-role-2-value'),
        ]

        # Verify
        exc = self.assertRaises(exception.PlanParametersNotExist,
                                self.plans_manager.set_parameter_values,
                                test_plan.uuid,
                                update_us)
        self.assertIn(not_present_in_role_1_name, str(exc))
        self.assertIn(not_present_in_role_2_name, str(exc))

        # Pull it from the database to make sure it was modified
        found = self.plans_manager.retrieve_plan(test_plan.uuid)
        found_params = sorted(found.parameters, key=lambda x: x.name)
        self.assertEqual(4, len(found_params))  # 3 + 1 for scaling
        self.assertEqual(found_params[0].value, '1')
        self.assertEqual(found_params[1].value,
                         '3e6270da-fbf7-4aef-bc78-6d0cfc3ad11b')
        self.assertEqual(found_params[2].value, 'm1.small')
        self.assertEqual(found_params[3].value, '')
Exemple #7
0
    def test_set_non_existent_parameters(self):
        # Setup
        test_role = self._add_test_role()
        test_plan = self.plans_manager.create_plan('p1', 'd1')
        self.plans_manager.add_role_to_plan(test_plan.uuid, test_role.uuid)

        # Test
        ns = name_utils.generate_role_namespace(test_role.name,
                                                test_role.version)
        not_present_in_role_1_name = ns_utils.apply_template_namespace(
            ns, 'not_present_in_role_1')
        not_present_in_role_2_name = ns_utils.apply_template_namespace(
            ns, 'not_present_in_role_2')
        update_us = [
            ParameterValue(ns_utils.apply_template_namespace(ns, 'key_name'),
                           'test-key'),
            ParameterValue(ns_utils.apply_template_namespace(ns, 'image_id'),
                           'test-image'),
            ParameterValue(not_present_in_role_1_name,
                           'not-present-in-role-1-value'),
            ParameterValue(not_present_in_role_2_name,
                           'not-present-in-role-2-value'),
        ]

        # Verify
        exc = self.assertRaises(exception.PlanParametersNotExist,
                                self.plans_manager.set_parameter_values,
                                test_plan.uuid, update_us)
        self.assertIn(not_present_in_role_1_name, str(exc))
        self.assertIn(not_present_in_role_2_name, str(exc))

        # Pull it from the database to make sure it was modified
        found = self.plans_manager.retrieve_plan(test_plan.uuid)
        found_params = sorted(found.parameters, key=lambda x: x.name)
        self.assertEqual(4, len(found_params))  # 3 + 1 for scaling
        self.assertEqual(found_params[0].value, '1')
        self.assertEqual(found_params[1].value,
                         '3e6270da-fbf7-4aef-bc78-6d0cfc3ad11b')
        self.assertEqual(found_params[2].value, 'm1.small')
        self.assertEqual(found_params[3].value, '')
Exemple #8
0
    def add_role_to_plan(self, plan_uuid, role_uuid):
        """Adds a role to the given plan, storing the changes in Tuskar's
        storage.

        :type plan_uuid: str
        :type role_uuid: str
        :return: updated plan model instance
        :rtype: tuskar.manager.models.DeploymentPlan
        :raises tuskar.storage.exceptions.UnknownUUID: if either the plan
                or the role cannot be found
        """
        # Load the plan and role from storage
        db_plan = self.plan_store.retrieve(plan_uuid)
        db_role = self.template_store.retrieve(role_uuid)

        # Parse the plan and role template into template objects.
        deployment_plan = self._plan_to_template_object(db_plan)
        role_template = self._role_to_template_object(db_role)

        # See if a master seed template has been set.
        try:
            db_master_seed = self.seed_store.retrieve_by_name(MASTER_SEED_NAME)
            master_seed = parser.parse_template(db_master_seed.contents)
        except UnknownName:
            master_seed = None
            special_properties = None

        def _find_role_type(registry):
            for path in registry.keys():
                if path in db_role.registry_path:
                    return registry[path]

        if master_seed is not None:
            try:
                db_registry_env = self.registry_store.retrieve_by_name(
                    RESOURCE_REGISTRY_NAME).contents
            except UnknownName:
                LOG.error("Could not load resource_registry. Make sure you "
                          "pass --resource-registry to tuskar-load-roles.")
                raise

            parsed_registry_env = parser.parse_environment(db_registry_env)
            registry = dict((e.filename, e.alias)
                            for e in parsed_registry_env.registry_entries)
            role_type = _find_role_type(registry)
            special_properties = template_seed.get_property_map_for_role(
                master_seed, role_type)

        # Use the combination logic to perform the addition.
        role_namespace = name_utils.generate_role_namespace(db_role.name,
                                                            db_role.version)
        template_filename = (
            name_utils.role_template_filename(db_role.name, db_role.version,
                                              db_role.relative_path))
        deployment_plan.add_template(role_namespace, role_template,
                                     template_filename,
                                     override_properties=special_properties)

        # If there is a master seed, add its top-level elements to the plan.
        # These calls are idempotent, so it's safe to call each time a role
        # is added.
        if master_seed is not None:
            template_seed.add_top_level_parameters(
                master_seed,
                deployment_plan.master_template,
                deployment_plan.environment)
            template_seed.add_top_level_resources(
                master_seed, deployment_plan.master_template)
            template_seed.add_top_level_outputs(
                master_seed, deployment_plan.master_template)

            if role_type is None:
                LOG.error(
                    "Role '%s' not found in seed template." % db_role.name)
                raise ValueError(db_role.name)
            seed_role = template_seed.find_role_from_type(
                master_seed.resources, role_type)
            if seed_role is None:
                LOG.error(
                    "Role '%s' of type '%s' not found in seed template." %
                    (db_role.name, role_type))
                raise ValueError(db_role.name)

            # These calls are idempotent, but must be called on each role as
            # new references may have been added.
            template_seed.update_role_resource_references(
                deployment_plan.master_template,
                seed_role,
                db_role.name)

            template_seed.update_role_property_references(
                deployment_plan.master_template,
                seed_role,
                role_namespace)

            # Update environment file to add top level mappings, which is made
            # up of all non-role files present in the resource registry, plus
            # required aliases
            reg_mapping = self.registry_mapping_store.list()

            environment = deployment_plan.environment
            for entry in parsed_registry_env.registry_entries:
                # check if registry_mapping is in database, if so add to
                # environment (later will become environment.yaml)
                if any(x.name == entry.filename for x in reg_mapping):
                    additem = RegistryEntry(entry.alias, entry.filename)
                    environment.add_registry_entry(additem, unique=True)

        # Save the updated plan.
        updated = self._save_updated_plan(plan_uuid, deployment_plan)

        return updated
 def test_generate_role_namespace(self):
     ns = name_utils.generate_role_namespace('r1', 'v1')
     self.assertEqual('r1-v1', ns)
 def test_generate_role_namespace(self):
     ns = name_utils.generate_role_namespace('r1', 'v1')
     self.assertEqual('r1-v1', ns)
Exemple #11
0
    def add_role_to_plan(self, plan_uuid, role_uuid):
        """Adds a role to the given plan, storing the changes in Tuskar's
        storage.

        :type plan_uuid: str
        :type role_uuid: str
        :return: updated plan model instance
        :rtype: tuskar.manager.models.DeploymentPlan
        :raises tuskar.storage.exceptions.UnknownUUID: if either the plan
                or the role cannot be found
        """

        # Load the plan and role from storage
        db_plan = self.plan_store.retrieve(plan_uuid)
        db_role = self.template_store.retrieve(role_uuid)

        # Parse the plan and role template into template objects.
        deployment_plan = self._plan_to_template_object(db_plan)
        role_template = self._role_to_template_object(db_role)

        # See if a master seed template has been set.
        try:
            db_master_seed = self.seed_store.retrieve_by_name(MASTER_SEED_NAME)
            master_seed = parser.parse_template(db_master_seed.contents)
        except UnknownName:
            master_seed = None
            special_properties = None

        if master_seed is not None:
            try:
                db_registry_env = self.registry_store.retrieve_by_name(
                    RESOURCE_REGISTRY_NAME).contents
            except UnknownName:
                LOG.error("Could not load resource_registry. Make sure you "
                          "pass --resource-registry to tuskar-load-roles.")
                raise
            parsed_registry_env = parser.parse_environment(db_registry_env)
            registry = dict((role_name_from_path(e.filename), e.alias)
                            for e in parsed_registry_env.registry_entries)
            special_properties = template_seed.get_property_map_for_role(
                master_seed, registry[db_role.name])

        # Use the combination logic to perform the addition.
        role_namespace = name_utils.generate_role_namespace(db_role.name,
                                                            db_role.version)
        template_filename = name_utils.role_template_filename(db_role.name,
                                                              db_role.version)
        deployment_plan.add_template(role_namespace, role_template,
                                     template_filename,
                                     override_properties=special_properties)

        # If there is a master seed, add its top-level elements to the plan.
        # These calls are idempotent, so it's safe to call each time a role
        # is added.
        if master_seed is not None:
            template_seed.add_top_level_parameters(
                master_seed,
                deployment_plan.master_template,
                deployment_plan.environment)
            template_seed.add_top_level_resources(
                master_seed, deployment_plan.master_template)
            template_seed.add_top_level_outputs(
                master_seed, deployment_plan.master_template)

            try:
                role_type = registry[db_role.name]
            except KeyError:
                LOG.error(
                    "Role '%s' not found in seed template." % db_role.name)
                raise
            seed_role = template_seed.find_role_from_type(
                master_seed.resources, role_type)
            if seed_role is None:
                LOG.error(
                    "Role '%s' of type '%s' not found in seed template." %
                    (db_role.name, role_type))
                raise ValueError(db_role.name)

            # These calls are idempotent, but must be called on each role as
            # new references may have been added.
            template_seed.update_role_resource_references(
                deployment_plan.master_template,
                seed_role,
                plan.generate_group_id(role_namespace))

            template_seed.update_role_property_references(
                deployment_plan.master_template,
                seed_role,
                role_namespace)

        # Save the updated plan.
        updated = self._save_updated_plan(plan_uuid, deployment_plan)

        return updated
Exemple #12
0
    def add_role_to_plan(self, plan_uuid, role_uuid):
        """Adds a role to the given plan, storing the changes in Tuskar's
        storage.

        :type plan_uuid: str
        :type role_uuid: str
        :return: updated plan model instance
        :rtype: tuskar.manager.models.DeploymentPlan
        :raises tuskar.storage.exceptions.UnknownUUID: if either the plan
                or the role cannot be found
        """
        # Load the plan and role from storage
        db_plan = self.plan_store.retrieve(plan_uuid)
        db_role = self.template_store.retrieve(role_uuid)

        # Parse the plan and role template into template objects.
        deployment_plan = self._plan_to_template_object(db_plan)
        role_template = self._role_to_template_object(db_role)

        # See if a master seed template has been set.
        try:
            db_master_seed = self.seed_store.retrieve_by_name(MASTER_SEED_NAME)
            master_seed = parser.parse_template(db_master_seed.contents)
        except UnknownName:
            master_seed = None
            special_properties = None

        def _find_role_type(registry):
            for path in registry.keys():
                if path in db_role.registry_path:
                    return registry[path]

        if master_seed is not None:
            try:
                db_registry_env = self.registry_store.retrieve_by_name(
                    RESOURCE_REGISTRY_NAME).contents
            except UnknownName:
                LOG.error("Could not load resource_registry. Make sure you "
                          "pass --resource-registry to tuskar-load-roles.")
                raise

            parsed_registry_env = parser.parse_environment(db_registry_env)
            registry = dict((e.filename, e.alias)
                            for e in parsed_registry_env.registry_entries)
            role_type = _find_role_type(registry)
            special_properties = template_seed.get_property_map_for_role(
                master_seed, role_type)

        # Use the combination logic to perform the addition.
        role_namespace = name_utils.generate_role_namespace(
            db_role.name, db_role.version)
        template_filename = (name_utils.role_template_filename(
            db_role.name, db_role.version, db_role.relative_path))
        deployment_plan.add_template(role_namespace,
                                     role_template,
                                     template_filename,
                                     override_properties=special_properties)

        # If there is a master seed, add its top-level elements to the plan.
        # These calls are idempotent, so it's safe to call each time a role
        # is added.
        if master_seed is not None:
            template_seed.add_top_level_parameters(
                master_seed, deployment_plan.master_template,
                deployment_plan.environment)
            template_seed.add_top_level_resources(
                master_seed, deployment_plan.master_template)
            template_seed.add_top_level_outputs(
                master_seed, deployment_plan.master_template)

            if role_type is None:
                LOG.error("Role '%s' not found in seed template." %
                          db_role.name)
                raise ValueError(db_role.name)
            seed_role = template_seed.find_role_from_type(
                master_seed.resources, role_type)
            if seed_role is None:
                LOG.error(
                    "Role '%s' of type '%s' not found in seed template." %
                    (db_role.name, role_type))
                raise ValueError(db_role.name)

            # These calls are idempotent, but must be called on each role as
            # new references may have been added.
            template_seed.update_role_resource_references(
                deployment_plan.master_template, seed_role, db_role.name)

            template_seed.update_role_property_references(
                deployment_plan.master_template, seed_role, role_namespace)

            # Update environment file to add top level mappings, which is made
            # up of all non-role files present in the resource registry, plus
            # required aliases
            reg_mapping = self.registry_mapping_store.list()

            environment = deployment_plan.environment
            for entry in parsed_registry_env.registry_entries:
                # check if registry_mapping is in database, if so add to
                # environment (later will become environment.yaml)
                if any(x.name == entry.filename for x in reg_mapping):
                    additem = RegistryEntry(entry.alias, entry.filename)
                    environment.add_registry_entry(additem, unique=True)

        # Save the updated plan.
        updated = self._save_updated_plan(plan_uuid, deployment_plan)

        return updated