Ejemplo n.º 1
0
def add_user(creator_id, email, password, firstname=None, lastname=None):
    """Create a user"""
    REQUIRED_PERM = 'create_user'

    with database.session() as session:

        try:
            creator = _get_user(session, admin_id)
        except RecordNotExists as ex:
            raise RecordNotExists(ex.message)

        if not creator.is_admin or REQUIRED_PERM not in creator.permissions:
            # The user is not allowed to create a user.
            err_msg = ERROR_MSG['forbidden']
            raise Forbidden(err_msg)

        if session.query(User).filter_by(email=email).first():
            # The user already exists!
            err_msg = ERROR_MSG['duplicatedUser']
            raise DuplicatedRecord(err_msg)

        new_user = _add_user(email, password, firstname, lastname)
        new_user_info = new_user.to_dict()

    return new_user_info
Ejemplo n.º 2
0
def list_users(filters=None):
    """List all users, optionally filtered by some fields"""
    with database.session() as session:
        users = _list_users(session, filters)
        users_list = [user.to_dict() for user in users]

    return users_list
Ejemplo n.º 3
0
 def test_add_switch_session(self):
     with database.session() as session:
         add_switch = switch.add_switch(ip='2887583784',
                                        user=self.user_object,
                                        session=session)
     expected = '172.29.8.40'
     self.assertEqual(expected, add_switch['ip'])
Ejemplo n.º 4
0
 def test_add_user_session(self):
     with database.session() as session:
         user_objs = user_api.add_user(email='*****@*****.**',
                                       password='******',
                                       user=self.user_object,
                                       session=session)
     self.assertEqual('*****@*****.**', user_objs['email'])
Ejemplo n.º 5
0
 def test_get_none_with_flag_off(self):
     with database.session() as session:
         dummy = utils.get_db_object(session,
                                     models.Permission,
                                     False,
                                     name='dummy')
         self.assertEqual(None, dummy)
Ejemplo n.º 6
0
    def get_history(self):
        """Get log file read history from database.

        :returns: (line_matcher_name progress)

        .. note::
           The function should be called out of database session.
           It reads the log_progressing_history table to get the
           position in the log file it has read in last run,
           the partial line of the log, the line matcher name
           in the last run, the progress, the message and the
           severity it has got in the last run.
        """
        with database.session() as session:
            history = session.query(
                models.LogProgressingHistory
            ).filter_by(
                pathname=self.pathname_
            ).first()
            if history:
                self.position_ = history.position
                self.partial_line_ = history.partial_line
                line_matcher_name = history.line_matcher_name
                progress = Progress(history.progress,
                                    history.message,
                                    history.severity)
            else:
                line_matcher_name = 'start'
                progress = Progress(0.0, '', None)

            return line_matcher_name, progress
Ejemplo n.º 7
0
def get_subnet(getter, subnet_id, **kwargs):
    """Get subnet info."""
    with database.session() as session:
        user_api.check_user_permission_internal(
            session, getter, permission.PERMISSION_LIST_NETWORKS)
        return utils.get_db_object(session, models.Network, id=subnet_id
        ).to_dict()
Ejemplo n.º 8
0
    def setUp(self):
        super(AdapterTestCase, self).setUp()
        reload(setting)
        setting.CONFIG_DIR = os.path.join(
            os.path.dirname(os.path.abspath(__file__)),
            'data'
        )
        database.init('sqlite://')
        database.create_db()
        self.user_object = (
            user_api.get_user_object(
                setting.COMPASS_ADMIN_EMAIL
            )
        )

        mock_config = mock.Mock()
        self.backup_adapter_configs = util.load_configs
        util.load_configs = mock_config
        configs = [{
            'NAME': 'openstack_test',
            'DISLAY_NAME': 'Test OpenStack Icehouse',
            'PACKAGE_INSTALLER': 'chef_installer',
            'OS_INSTALLER': 'cobbler',
            'SUPPORTED_OS_PATTERNS': ['(?i)centos.*', '(?i)ubuntu.*'],
            'DEPLOYABLE': True
        }]
        util.load_configs.return_value = configs
        with database.session() as session:
            adapter_api.add_adapters_internal(session)
        adapter.load_adapters()
        self.adapter_object = adapter.list_adapters(user=self.user_object)
        for adapter_obj in self.adapter_object:
            if adapter_obj['name'] == 'openstack_icehouse':
                self.adapter_id = adapter_obj['id']
                break
Ejemplo n.º 9
0
 def test_update_db_object(self):
     with database.session() as session:
         db_obj = utils.get_db_object(session, models.Permission, id=1)
         updated_obj = utils.update_db_object(session,
                                              db_obj,
                                              alias='updated')
         self.assertEqual('updated', updated_obj.alias)
Ejemplo n.º 10
0
 def test_list_none_table(self):
     with self.assertRaises(exception.DatabaseException):
         with database.session() as session:
             utils.list_db_objects(
                 session,
                 models.Dummy,
             )
Ejemplo n.º 11
0
 def test_update_db_obj_none_exist(self):
     with self.assertRaises(exception.DatabaseException):
         with database.session() as session:
             db_obj = utils.get_db_object(session,
                                          models.Permission,
                                          id=1000)
             utils.update_db_object(session, db_obj, name='dummy')
Ejemplo n.º 12
0
def list_adpaters(lister, **filters):
    """list adapters."""
    translated_filters = {}
    for filter_name, filter_value in filters:
        if filter_name in OS_FIELD_MAPPING:
            translated_filters.setdefault('os_adapter', {})[
                OS_FIELD_MAPPING[filter_name]
            ] = filter_value
        elif filter_name in PACKAGE_FIELD_MAPPING:
            translated_filters.setdefault('package-adapter', {})[
                PACKAGE_FIELD_MAPPING[filter_name]
            ] = filter_value
        else:
            translated_filters[filter_name] = filter_value
    with database.session() as session:
        user_api.check_user_permission_internal(
            session, lister, permission.PERMISSION_LIST_ADAPTER)
        filtered_adapter_dicts = []
        adapter_dicts = ADAPTER_MAPPING.values()
        for adapter_dict in adapter_dicts:
            if all([
                _filter_adapters(adapter_dict, filter_name, filter_value)
                for filter_name, filter_value in translated_filters.items()
            ]):
                filtered_adapter_dicts.append(adapter_dicts)
        return filtered_adapter_dicts
Ejemplo n.º 13
0
 def test_filter_with_list(self):
     with database.session() as session:
         query = utils.model_query(session, models.Permission)
         filters = {
             'name': [
                 'list_permissions',
                 'list_switches'
             ]
         }
         resources = utils.model_filter(query, models.Permission, **filters)
         ret = [resource.to_dict() for resource in resources.all()]
         expected = [
             {
                 'description': 'list all permissions',
                 'alias': 'list permissions',
                 'id': 1,
                 'name': 'list_permissions'
             },
             {
                 'description': 'list all switches',
                 'alias': 'list switches',
                 'id': 2,
                 'name': u'list_switches'
             }
         ]
         for i, v in enumerate(ret):
             self.assertTrue(
                 all(item in ret[i].items() for item in expected[i].items())
             )
Ejemplo n.º 14
0
 def test_add_with_multiple_args(self):
     with database.session() as session:
         db_permission = utils.add_db_object(session, models.Permission, False, "test", alias="test")
         db_user = utils.add_db_object(session, models.User, False, "*****@*****.**", password="******")
         db_objs = utils.add_db_object(session, models.UserPermission, True, db_user.id, db_permission.id)
         self.assertEqual(db_user.id, db_objs.user_id)
         self.assertEqual(db_permission.id, db_objs.permission_id)
Ejemplo n.º 15
0
 def test_list_none_table(self):
     with self.assertRaises(exception.DatabaseException):
         with database.session() as session:
             utils.list_db_objects(
                 session,
                 models.Dummy,
             )
Ejemplo n.º 16
0
    def update_progress(self, clusterid, hostids):
        """Update cluster progress and hosts progresses.

        :param clusterid: the id of the cluster to update.
        :type clusterid: int.
        :param hostids: the ids of the hosts to update.
        :type hostids: list of int.
        """
        host_progresses = {}
        with database.session():
            for hostid in hostids:
                fullname, host_state, host_progress = (
                    self._get_host_progress(hostid))
                if not fullname or not host_progress:
                    logging.error(
                        'nothing to update host %s => '
                        'state %s progress %s',
                        fullname, host_state, host_progress)
                    continue

                logging.debug('got host %s state %s progress %s',
                              fullname, host_state, host_progress)
                host_progresses[hostid] = (
                    fullname, host_state, host_progress)

        for hostid, host_value in host_progresses.items():
            fullname, host_state, host_progress = host_value
            if host_state == 'INSTALLING' and host_progress.progress < 1.0:
                self.os_matcher_.update_progress(
                    fullname, host_progress)
                self.package_matcher_.update_progress(
                    fullname, host_progress)
            else:
                logging.error(
                    'there is no need to update host %s '
                    'progress: state %s progress %s',
                    fullname, host_state, host_progress)

        with database.session():
            for hostid in hostids:
                if hostid not in host_progresses:
                    continue

                _, _, host_progress = host_progresses[hostid]
                self._update_host_progress(hostid, host_progress)

            self._update_cluster_progress(clusterid)
Ejemplo n.º 17
0
 def test_list_none_objs(self):
     with database.session() as session:
         db_objs = utils.list_db_objects(
             session,
             models.Permission,
             id=99
         )
         self.assertListEqual([], db_objs)
Ejemplo n.º 18
0
 def test_filter_with_other_type(self):
     with database.session() as session:
         query = utils.model_query(session, models.Permission)
         filters = {"id": 1}
         resources = utils.model_filter(query, models.Permission, **filters)
         ret = [resource.to_dict() for resource in resources.all()]
         expected = {"id": 1}
         self.assertTrue(all(item in ret[0].items() for item in expected.items()))
Ejemplo n.º 19
0
 def test_get_alias(self):
     with database.session() as session:
         machines = utils.get_db_object(session,
                                        models.Permission,
                                        name='list_machines')
         expected = 'list machines'
         self.assertEqual(expected, machines.alias)
         self.assertEqual(expected, machines.description)
Ejemplo n.º 20
0
def get_cluster(getter, cluster_id, **kwargs):
    """Get subnet info."""
    with database.session() as session:
        user_api.check_user_permission_internal(
            session, getter, permission.PERMISSION_LIST_CLUSTERS)
        return utils.get_db_object(
            session, models.Cluster, id=cluster_id
        ).to_dict()
Ejemplo n.º 21
0
def del_machine(deleter, machine_id, **kwargs):
    """Delete a machine."""
    with database.session() as session:
        user_api.check_user_permission_internal(
            session, deleter, permission.PERMISSION_DEL_MACHINE)
        machine = utils.get_db_object(session, models.Switch, id=machine_id)
        utils.del_db_object(session, machine)
        return machine.to_dict()
Ejemplo n.º 22
0
 def test_list_specfic_objs(self):
     with database.session() as session:
         db_objs = utils.list_db_objects(
             session,
             models.Permission,
             name=['list_permissions', 'list_machines'])
         self.assertEqual(['list_permissions', 'list_machines'].sort(),
                          [obj.name for obj in db_objs].sort())
Ejemplo n.º 23
0
def get_machine(getter, machine_id, **kwargs):
    """get field dict of a machine."""
    with database.session() as session:
        user_api.check_user_permission_internal(
            session, getter, permission.PERMISSION_LIST_MACHINES)
        return utils.get_db_object(
            session, models.Machine, True, id=machine_id
        ).to_dict()
Ejemplo n.º 24
0
def update_machine(updater, machine_id, **kwargs):
    """Update a machine."""
    with database.session() as session:
         user_api.check_user_permission_internal(
            session, updater, permission.PERMISSION_UPDATE_MACHINE)
         machine = utils.get_db_object(session, models.Machine, id=machine_id)
         utils.update_db_object(session, machine, **kwargs)
         return machine.to_dict()
Ejemplo n.º 25
0
 def test_get_none_with_flag_on(self):
     with self.assertRaises(exception.RecordNotExists):
         with database.session() as session:
             utils.get_db_object(
                 session,
                 models.Permission,
                 name='dummy'
             )
Ejemplo n.º 26
0
 def test_model_query_non_exist(self):
     with database.session() as session:
         self.assertRaises(
             exception.DatabaseException,
             utils.model_query,
             session,
             models.JSONEncoded
         )
Ejemplo n.º 27
0
 def test_get_none_with_flag_off(self):
     with database.session() as session:
         dummy = utils.get_db_object(
             session,
             models.Permission,
             False,
             name='dummy'
         )
         self.assertEqual(None, dummy)
Ejemplo n.º 28
0
def get_cluster_hosts(getter, cluster_id, **kwargs):
    """Get subnet info."""
    with database.session() as session:
        user_api.check_user_permission_internal(
            session, getter, permission.PERMISSION_LIST_CLUSTER_HOSTS)
        cluster = utils.get_db_object(
            session, models.Cluster, id=cluster_id
        )
        return [host.to_dict() for host in cluster.clusterhosts]
Ejemplo n.º 29
0
 def test_filter_with_other_type(self):
     with database.session() as session:
         query = utils.model_query(session, models.Permission)
         filters = {'id': 1}
         resources = utils.model_filter(query, models.Permission, **filters)
         ret = [resource.to_dict() for resource in resources.all()]
         expected = {'id': 1}
         self.assertTrue(
             all(item in ret[0].items() for item in expected.items()))
Ejemplo n.º 30
0
 def test_add_alias(self):
     with database.session() as session:
         db_objs = utils.add_db_object(session,
                                       models.Permission,
                                       True,
                                       'test',
                                       alias='test')
         expected = 'test'
         self.assertEqual(expected, db_objs.alias)
Ejemplo n.º 31
0
def list_clusters(filters=None):
    """List all users, optionally filtered by some fields"""

    filters = filters or {}
    with database.session() as session:
        clusters = _list_clusters(session, filters)
        clusters_info = [cluster.to_dict() for cluster in clusters]

    return clusters_info
Ejemplo n.º 32
0
 def test_add_user_session(self):
     with database.session() as session:
         user_objs = user_api.add_user(
             email='*****@*****.**',
             password='******',
             user=self.user_object,
             session=session
         )
     self.assertEqual('*****@*****.**', user_objs['email'])
Ejemplo n.º 33
0
def add_subnet(creator, subnet, **kwargs):
    """Create a subnet."""
    with database.session() as session:
        user_api.check_user_permission_internal(
            session, creator, permission.PERMISSION_ADD_NETWORK)
        network = utils.add_db_object(
            session, models.Network, True, subnet
        )
        return network.to_dict()
Ejemplo n.º 34
0
 def test_add_with_invalid_args(self):
     with self.assertRaises(exception.InvalidParameter):
         with database.session() as session:
             utils.add_db_object(session,
                                 models.Permission,
                                 True,
                                 'test1',
                                 'test2',
                                 name='test1')
Ejemplo n.º 35
0
 def test_del_single_object(self):
     with database.session() as session:
         utils.del_db_objects(session,
                              models.Permission,
                              name='list_permissions')
         query_deleted = utils.list_db_objects(session,
                                               models.Permission,
                                               name='list_permissions')
         self.assertListEqual([], query_deleted)
Ejemplo n.º 36
0
 def test_add_switch_session(self):
     with database.session() as session:
         add_switch = switch.add_switch(
             ip='2887583784',
             user=self.user_object,
             session=session
         )
     expected = '172.29.8.40'
     self.assertEqual(expected, add_switch['ip'])
Ejemplo n.º 37
0
 def test_add_nothing(self):
     with database.session() as session:
         db_objs = utils.add_db_object(
             session,
             models.Permission,
             True,
             'test'
         )
         self.assertEqual('test', db_objs.name)
         self.assertIsNone(db_objs.alias)
Ejemplo n.º 38
0
 def test_get_alias(self):
     with database.session() as session:
         machines = utils.get_db_object(
             session,
             models.Permission,
             name='list_machines'
         )
         expected = 'list machines'
         self.assertEqual(expected, machines.alias)
         self.assertEqual(expected, machines.description)
Ejemplo n.º 39
0
 def _filter_test_dict_util(self, op, exp_name, exp_id, **kwargs):
     with database.session() as session:
         query = utils.model_query(session, models.Permission)
         filters = {}
         for key, value in kwargs.iteritems():
             filters[key] = {op: value}
         resources = utils.model_filter(query, models.Permission, **filters)
         ret = [resource.to_dict() for resource in resources.all()]
         expected = {'name': exp_name, 'id': exp_id}
         return (expected, ret)
Ejemplo n.º 40
0
def get_cluster_config(cluster_id):
    """Get configuration info for a specified cluster"""

    with database.session() as session:
        try:
            config = _get_cluster_config(session, cluster_id)
        except RecordNotExists as ex:
            raise RecordNotExists(ex.message)

    return config
Ejemplo n.º 41
0
 def test_add_switch_machine_session(self):
     with database.session() as session:
         add_switch_machine = switch.add_switch_machine(
             1,
             mac='28:6e:d4:46:c4:25',
             user=self.user_object,
             session=session,
             port='1')
     expected = '28:6e:d4:46:c4:25'
     self.assertEqual(expected, add_switch_machine['mac'])
Ejemplo n.º 42
0
def get_metadata(getter, adapter_id, **kwargs):
    """get adapter."""
    with database.session() as session:
        user_api.check_user_permission_internal(
            session, lister, permission.PERMISSION_LIST_METADATAS)
        if adapter_id not in METADATA_MAPPING:
            raise excedption.RecordNotExists(
                'adpater %s does not exist' % adapter_id
            )
        return _filter_metadata(METADATA_MAPPING[adapter_id])
Ejemplo n.º 43
0
 def test_add_subnet_session(self):
     with database.session() as session:
         network.add_subnet(self.user_object,
                            subnet='10.145.89.0/22',
                            user=self.user_object,
                            session=session)
     add_subnets = network.list_subnets(user=self.user_object)
     expected = '10.145.89.0/22'
     for add_subnet in add_subnets:
         self.assertEqual(expected, add_subnet['subnet'])
Ejemplo n.º 44
0
 def _get_os_installer(self):
     with database.session() as session:
         installer = utils.get_db_object(
             session, models.OSInstaller
         )
         os_installer = {}
         os_installer['name'] = health_check_utils.strip_name(
             installer.name)
         os_installer.update(installer.settings)
     return os_installer
Ejemplo n.º 45
0
def del_network(deleter, subnet_id, **kwargs):
    """Delete a subnet."""
    with database.session() as session:
        user_api.check_user_permission_internal(
            session, deleter, permission.PERMISSION_DEL_NETWORK)
        network = utils.get_db_object(
            session, models.Network, id=subnet_id
        )
        utils.del_db_object(session, network)
        return network.to_dict()
Ejemplo n.º 46
0
 def test_add_duplicate_with_no_flag(self):
     with database.session() as session:
         db_objs = utils.add_db_object(session,
                                       models.Permission,
                                       False,
                                       'test',
                                       alias='test')
         duplicate = utils.add_db_object(session,
                                         models.Permission,
                                         False,
                                         'test',
                                         alias='test')
         self.assertEqual(duplicate, db_objs)
Ejemplo n.º 47
0
 def test_add_duplicate_with_flag(self):
     with self.assertRaises(exception.DuplicatedRecord):
         with database.session() as session:
             utils.add_db_object(session,
                                 models.Permission,
                                 True,
                                 'test',
                                 alias='test')
             utils.add_db_object(session,
                                 models.Permission,
                                 True,
                                 'test',
                                 alias='test')
Ejemplo n.º 48
0
 def test_add_permission_session(self):
     with database.session() as session:
         user_api.add_permission(self.user_object.id,
                                 user=self.user_object,
                                 permission_id=2,
                                 session=session)
     permissions = user_api.get_permissions(
         self.user_object.id,
         user=self.user_object,
     )
     result = None
     for permission in permissions:
         if permission['id'] == 2:
             result = permission['name']
     self.assertEqual(result, 'list_switches')
Ejemplo n.º 49
0
 def test_add_host_log_history_session(self):
     with database.session() as session:
         host.add_host_log_history(
             self.host_ids[0],
             user=self.user_object,
             filename='add_log_session',
             session=session
         )
     logs = host.get_host_log_histories(
         self.host_ids[0],
         user=self.user_object,
     )
     result = []
     for log in logs:
         result.append(log['filename'])
     self.assertIn('add_log_session', result)
Ejemplo n.º 50
0
 def test_add_with_multiple_args(self):
     with database.session() as session:
         db_permission = utils.add_db_object(session,
                                             models.Permission,
                                             False,
                                             'test',
                                             alias='test')
         db_user = utils.add_db_object(session,
                                       models.User,
                                       False,
                                       '*****@*****.**',
                                       password='******')
         db_objs = utils.add_db_object(session, models.UserPermission, True,
                                       db_user.id, db_permission.id)
         self.assertEqual(db_user.id, db_objs.user_id)
         self.assertEqual(db_permission.id, db_objs.permission_id)
Ejemplo n.º 51
0
 def test_add_host_network_session(self):
     with database.session() as session:
         host.add_host_network(
             self.host_ids[0],
             user=self.user_object,
             interface='eth1',
             ip='10.145.88.40',
             subnet_id=self.subnet_ids[0],
             is_mgmt=True,
             session=session
         )
     host_network = host.list_host_networks(
         self.host_ids[0],
         user=self.user_object,
     )
     result = []
     for item in host_network:
         result.append(item['ip'])
     self.assertIn('10.145.88.40', result)
Ejemplo n.º 52
0
def search(cluster_hosts, cluster_propreties_match, cluster_properties_name,
           host_properties_match, host_properties_name):
    """search clusters.

    :param cluster_hosts: clusters and hosts in each cluster to search.
    :type cluster_hosts: dict of int or str to list of int or str

    .. note::
        The function should be called out of database session.
    """
    logging.debug('search cluster_hosts: %s', cluster_hosts)
    with database.session():
        cluster_hosts, os_versions, target_systems = (
            util.update_cluster_hosts(cluster_hosts))
        manager = ConfigManager()
        return manager.filter_cluster_and_hosts(cluster_hosts, os_versions,
                                                target_systems,
                                                cluster_propreties_match,
                                                cluster_properties_name,
                                                host_properties_match,
                                                host_properties_name)
Ejemplo n.º 53
0
 def test_filter_with_list(self):
     with database.session() as session:
         query = utils.model_query(session, models.Permission)
         filters = {'name': ['list_permissions', 'list_switches']}
         resources = utils.model_filter(query, models.Permission, **filters)
         ret = [resource.to_dict() for resource in resources.all()]
         expected = [{
             'description': 'list all permissions',
             'alias': 'list permissions',
             'id': 1,
             'name': 'list_permissions'
         }, {
             'description': 'list all switches',
             'alias': 'list switches',
             'id': 2,
             'name': u'list_switches'
         }]
         for i, v in enumerate(ret):
             self.assertTrue(
                 all(item in ret[i].items()
                     for item in expected[i].items()))
Ejemplo n.º 54
0
 def test_list_none_objs(self):
     with database.session() as session:
         db_objs = utils.list_db_objects(session, models.Permission, id=99)
         self.assertListEqual([], db_objs)
Ejemplo n.º 55
0
 def test_del_db_object(self):
     with self.assertRaises(exception.RecordNotExists):
         with database.session() as session:
             db_obj = utils.get_db_object(session, models.Permission, id=1)
             utils.del_db_object(session, db_obj)
             utils.get_db_object(session, models.Permission, id=1)
Ejemplo n.º 56
0
 def test_del_all_objects(self):
     with database.session() as session:
         utils.del_db_objects(session, models.Permission)
         remained = utils.list_db_objects(session, models.Permission)
         self.assertListEqual([], remained)
Ejemplo n.º 57
0
 def test_model_query(self):
     with database.session() as session:
         model = models.Machine
         res = utils.model_query(session, model)
         self.assertIsNotNone(res)
Ejemplo n.º 58
0
 def test_add_nothing(self):
     with database.session() as session:
         db_objs = utils.add_db_object(session, models.Permission, True,
                                       'test')
         self.assertEqual('test', db_objs.name)
         self.assertIsNone(db_objs.alias)
Ejemplo n.º 59
0
 def test_model_query_non_exist(self):
     with database.session() as session:
         self.assertRaises(exception.DatabaseException, utils.model_query,
                           session, models.JSONEncoded)
Ejemplo n.º 60
0
 def test_list_specific_obj(self):
     with database.session() as session:
         db_objs = utils.list_db_objects(session,
                                         models.Permission,
                                         name='list_permissions')
         self.assertEqual('list permissions', db_objs[0].alias)