Exemplo n.º 1
0
 def test_list_none_table(self):
     with self.assertRaises(exception.DatabaseException):
         with database.session() as session:
             utils.list_db_objects(
                 session,
                 models.Dummy,
             )
Exemplo n.º 2
0
def start_check_cluster_health(user, cluster_id, send_report_url,
                               session=None, check_health={}):
    """Start to check cluster health."""
    cluster_state = utils.get_db_object(
        session, models.Cluster, True, id=cluster_id
    ).state_dict()

    if cluster_state['state'] != 'SUCCESSFUL':
        logging.debug("state is %s" % cluster_state['state'])
        err_msg = "Healthcheck starts only after cluster finished deployment!"
        raise exception.Forbidden(err_msg)

    reports = utils.list_db_objects(
        session, models.HealthCheckReport,
        cluster_id=cluster_id, state='verifying'
    )
    if reports:
        err_msg = 'Healthcheck in progress, please wait for it to complete!'
        raise exception.Forbidden(err_msg)

    # Clear all preivous report
    utils.del_db_objects(
        session, models.HealthCheckReport, cluster_id=cluster_id
    )

    from compass.tasks import client as celery_client
    celery_client.celery.send_task(
        'compass.tasks.cluster_health',
        (cluster_id, send_report_url, user.email)
    )
    return {
        "cluster_id": cluster_id,
        "status": "start to check cluster health."
    }
Exemplo n.º 3
0
def list_user_actions(user_id, user=None, session=None, **filters):
    """list user actions of a user."""
    list_user = user_api.get_user(user_id, user=user, session=session)
    return utils.list_db_objects(
        session, models.UserLog, order_by=['timestamp'],
        user_id=list_user['id'], **filters
    )
Exemplo n.º 4
0
def list_host_networks(host_id, user=None, session=None, **filters):
    """Get host networks for a host."""
    host = _get_host(host_id, session=session)
    return utils.list_db_objects(
        session, models.HostNetwork,
        host_id=host.id, **filters
    )
Exemplo n.º 5
0
def start_check_cluster_health(cluster_id, send_report_url,
                               user=None, session=None, check_health={}):
    """Start to check cluster health."""
    cluster = cluster_api.get_cluster_internal(cluster_id, session=session)

    if cluster.state.state != 'SUCCESSFUL':
        logging.debug("state is %s" % cluster.state.state)
        err_msg = "Healthcheck starts only after cluster finished deployment!"
        raise exception.Forbidden(err_msg)

    reports = utils.list_db_objects(
        session, models.HealthCheckReport,
        cluster_id=cluster.id, state='verifying'
    )
    if reports:
        err_msg = 'Healthcheck in progress, please wait for it to complete!'
        raise exception.Forbidden(err_msg)

    # Clear all preivous report
    # TODO(grace): the delete should be moved into celery task.
    # We should consider the case that celery task is down.
    utils.del_db_objects(
        session, models.HealthCheckReport, cluster_id=cluster.id
    )

    from compass.tasks import client as celery_client
    celery_client.celery.send_task(
        'compass.tasks.cluster_health',
        (cluster.id, send_report_url, user.email)
    )
    return {
        "cluster_id": cluster.id,
        "status": "start to check cluster health."
    }
Exemplo n.º 6
0
def list_switchmachines_hosts(user=None, session=None, **filters):
    """List switch machines hnd possible hosts."""
    switch_machines = utils.list_db_objects(
        session, models.SwitchMachine, **filters
    )
    return _filter_switch_machines_hosts(
        switch_machines
    )
Exemplo n.º 7
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)
Exemplo n.º 8
0
def list_machines(user=None, session=None, **filters):
    """List machines."""
    machines = utils.list_db_objects(
        session, models.Machine, **filters
    )
    if not user.is_admin and len(machines):
        machines = [m for m in machines if m.owner_id == user.id]
    return machines
Exemplo n.º 9
0
def list_switch_machines(
    switch_id, user=None, session=None, **filters
):
    """Get switch machines of a switch."""
    switch = _get_switch(switch_id, session=session)
    switch_machines = utils.list_db_objects(
        session, models.SwitchMachine, switch_id=switch.id, **filters
    )
    return _filter_switch_machines(switch_machines)
Exemplo n.º 10
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)
Exemplo n.º 11
0
def list_switchmachines_hosts(user=None, session=None, **filters):
    """List switch machines hnd possible hosts."""
    switch_machines = utils.list_db_objects(session, models.SwitchMachine,
                                            **filters)
    if not user.is_admin and len(switch_machines):
        switch_machines = [
            m for m in switch_machines if m.machine.owner_id == user.id
        ]
    return _filter_switch_machines_hosts(switch_machines)
Exemplo n.º 12
0
def get_metadatas_internal(session):
    metadata_mapping = {}
    with session.begin(subtransactions=True):
        adapters = utils.list_db_objects(
            session, models.Adapter
        )
        for adapter in adapters:
            metadata_mapping[adapter.id] = adapter.metadata_dict()
        return metadata_mapping
Exemplo n.º 13
0
def get_flavor_metadatas_internal(session):
    metadata_mapping = {}
    flavors = utils.list_db_objects(
        session, models.AdapterFlavor
    )
    for flavor in flavors:
        flavor_metadata_dict = flavor.metadata_dict()
        metadata_mapping[flavor.id] = _filter_metadata(
            flavor_metadata_dict, session=session
        )
        adapters = utils.list_db_objects(
            session, models.Adapter, id=flavor.adapter_id
        )
        for adapter in adapters:
            package_metadata_dict = adapter.metadata_dict()
            metadata_mapping[flavor.id].update(_filter_metadata(
                package_metadata_dict, session=session
            ))
    return metadata_mapping
Exemplo n.º 14
0
def _complement_distributed_system_adapters(session):
    with session.begin(subtransactions=True):
        root_dses = utils.list_db_objects(
            session, models.DistributedSystem,
            parent_id=None
        )
        for root_ds in root_dses:
            _copy_adapters_from_parent(
                session, models.PackageAdapter, root_ds, 'distributed_system'
            )
Exemplo n.º 15
0
def _complement_os_adapters(session):
    with session.begin(subtransactions=True):
        root_oses = utils.list_db_objects(
            session, models.OperatingSystem,
            parent_id=None
        )
        for root_os in root_oses:
            _copy_adapters_from_parent(
                session, models.OSAdapter, root_os, 'os'
            )
Exemplo n.º 16
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)
Exemplo n.º 17
0
def list_machines_or_hosts(user=None, session=None, **filters):
    """List machines or hosts if possible."""
    machines = utils.list_db_objects(session, models.Machine, **filters)
    machines_or_hosts = []
    for machine in machines:
        host = machine.host
        if host:
            machines_or_hosts.append(host)
        else:
            machines_or_hosts.append(machine)
    return machines_or_hosts
Exemplo n.º 18
0
def list_machines(lister, **filters):
    """List machines."""
    with database.session() as session:
        user_api.check_user_permission_internal(
            session, lister, permission.PERMISSION_LIST_MACHINES)
        return [
            machine.to_dict()
            for machine in utils.list_db_objects(
                 session, models.Machine, **filters
            )
        ]
Exemplo n.º 19
0
def list_subnets(lister, **filters):
    """List subnets."""
    with database.session() as session:
        user_api.check_user_permission_internal(
            session, lister, permission.PERMISSION_LIST_NETWORKS)
        return [
            network.to_dict()
            for network in utils.list_db_objects(
                session, models.Network, **filters
            )
        ]
Exemplo n.º 20
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
         )
Exemplo n.º 21
0
def list_switch_machines_hosts(
    switch_id, user=None, session=None, **filters
):
    """Get switch machines and possible hosts of a switch."""
    switch = _get_switch(switch_id, session=session)
    switch_machines = utils.list_db_objects(
        session, models.SwitchMachine, switch_id=switch.id, **filters
    )
    return _filter_switch_machines_hosts(
        switch_machines
    )
Exemplo n.º 22
0
def list_switch_machines(
    switch_id, user=None, session=None, **filters
):
    """Get switch machines of a switch."""
    switch = _get_switch(switch_id, session=session)
    switch_machines = utils.list_db_objects(
        session, models.SwitchMachine, switch_id=switch.id, **filters
    )
    if not user.is_admin and len(switch_machines):
        switch_machines = [m for m in switch_machines if m.machine.owner_id == user.id]
    return _filter_switch_machines(switch_machines)
Exemplo n.º 23
0
def list_switches(session, lister, **filters):
    """List switches."""
    switches = utils.list_db_objects(
        session, models.Switch, **filters
    )
    if 'ip_int' in filters:
        return switches
    else:
        return [
            switch for switch in switches
            if switch.ip != setting.DEFAULT_SWITCH_IP
        ]
Exemplo n.º 24
0
def get_permissions(
    user_id, user=None, exception_when_missing=True,
    session=None, **kwargs
):
    """List permissions of a user."""
    get_user = _get_user(
        user_id, session=session,
        exception_when_missing=exception_when_missing
    )
    return utils.list_db_objects(
        session, models.UserPermission, user_id=get_user.id, **kwargs
    )
Exemplo n.º 25
0
def list_machines_or_hosts(session, lister, **filters):
    """List hosts."""
    machines = utils.list_db_objects(
        session, models.Machine, **filters
    )
    machines_or_hosts = []
    for machine in machines:
        host = machine.host
        if host:
            machines_or_hosts.append(host)
        else:
            machines_or_hosts.append(machine)
    return machines_or_hosts
Exemplo n.º 26
0
def list_permissions(lister, **filters):
    """list permissions."""
    from compass.db.api import user as user_api
    with database.session() as session:
        user_api.check_user_permission_internal(
            session, lister, PERMISSION_LIST_PERMISSIONS
        )
        return [
            permission.to_dict()
            for permission in utils.list_db_objects(
                session, models.Permission, **filters
            )
        ]
Exemplo n.º 27
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)
Exemplo n.º 28
0
def list_machines_or_hosts(user=None, session=None, **filters):
    """List machines or hosts if possible."""
    machines = utils.list_db_objects(
        session, models.Machine, **filters
    )
    machines_or_hosts = []
    for machine in machines:
        host = machine.host
        if host:
            machines_or_hosts.append(host)
        else:
            machines_or_hosts.append(machine)
    return machines_or_hosts
Exemplo n.º 29
0
def get_permissions(user_id,
                    user=None,
                    exception_when_missing=True,
                    session=None,
                    **kwargs):
    """List permissions of a user."""
    get_user = _get_user(user_id,
                         session=session,
                         exception_when_missing=exception_when_missing)
    return utils.list_db_objects(session,
                                 models.UserPermission,
                                 user_id=get_user.id,
                                 **kwargs)
Exemplo n.º 30
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()
         )
Exemplo n.º 31
0
def get_adapters_internal(session):
    adapter_mapping = {}
    adapters = utils.list_db_objects(
        session, models.Adapter
    )
    for adapter in adapters:
        if adapter.deployable:
            adapter_dict = adapter.to_dict()
            adapter_mapping[adapter.id] = adapter_dict
        else:
            logging.info(
                'ignore adapter %s since it is not deployable',
                adapter.to_dict()
            )
    return adapter_mapping
Exemplo n.º 32
0
def add_adapters_internal(session):
    with session.begin(subtransactions=True):
        package_adapters = [
            package_adapter
            for package_adapter in utils.list_db_objects(
                session, models.PackageAdapter
            )
            if package_adapter.deployable
        ]
        os_adapters = [
            os_adapter
            for os_adapter in utils.list_db_objects(
                session, models.OSAdapter
            )
            if os_adapter.deployable
        ]
        adapters = []
        for os_adapter in os_adapters:
            adapters.append(utils.add_db_object(
                session, models.Adapter, True,
                os_adapter.id, None
            ))
        for package_adapter in package_adapters:
            adapters.append(utils.add_db_object(
                session, models.Adapter, True,
                None, package_adapter.id
            ))
            for os_adapter in os_adapters:
                for os_pattern in package_adapter.support_os_patterns:
                    if re.match(os_pattern, os_adapter.name):
                        adapters.append(utils.add_db_object(
                            session, models.Adapter, True,
                            os_adapter.id, package_adapter.id
                        ))
                        break
        return adapters
Exemplo n.º 33
0
def list_switches(user=None, session=None, **filters):
    """List switches."""
    # TODO(xicheng): should discuss with weidong.
    # If we can deprecate the use of DEFAULT_SWITCH_IP,
    # The code will be simpler.
    # The UI should use /machines-hosts instead of
    # /switches-machines-hosts and can show multi switch ip/port
    # under one row of machine info.
    switches = utils.list_db_objects(session, models.Switch, **filters)
    if 'ip_int' in filters:
        return switches
    else:
        return [
            switch for switch in switches
            if switch.ip != setting.DEFAULT_SWITCH_IP
        ]
Exemplo n.º 34
0
def get_package_metadatas_internal(session):
    metadata_mapping = {}
    adapters = utils.list_db_objects(
        session, models.Adapter
    )
    for adapter in adapters:
        if adapter.deployable:
            metadata_dict = adapter.metadata_dict()
            metadata_mapping[adapter.id] = _filter_metadata(
                metadata_dict, session=session
            )
        else:
            logging.info(
                'ignore metadata since its adapter %s is not deployable',
                adapter.id
            )
    return metadata_mapping
Exemplo n.º 35
0
def get_os_metadatas_internal(session):
    metadata_mapping = {}
    oses = utils.list_db_objects(
        session, models.OperatingSystem
    )
    for os in oses:
        if os.deployable:
            metadata_dict = os.metadata_dict()
            metadata_mapping[os.id] = _filter_metadata(
                metadata_dict, session=session
            )
        else:
            logging.info(
                'ignore metadata since its os %s is not deployable',
                os.id
            )
    return metadata_mapping
Exemplo n.º 36
0
def list_switches(user=None, session=None, **filters):
    """List switches."""
    # TODO(xicheng): should discuss with weidong.
    # If we can deprecate the use of DEFAULT_SWITCH_IP,
    # The code will be simpler.
    # The UI should use /machines-hosts instead of
    # /switches-machines-hosts and can show multi switch ip/port
    # under one row of machine info.
    switches = utils.list_db_objects(
        session, models.Switch, **filters
    )
    if 'ip_int' in filters:
        return switches
    else:
        return [
            switch for switch in switches
            if switch.ip != setting.DEFAULT_SWITCH_IP
        ]
Exemplo n.º 37
0
def list_user_actions(lister, user_id, **filters):
    """list user actions."""
    with database.session() as session:
        user = user_api.get_user_internal(session, id=user_id)
        if not lister.is_admin and lister_id != user_id:
            # The user is not allowed to list users actions.
            raise Forbidden(
                'User %s has no permission to list user %s actions.' % (
                    lister.email, user.email
                )
            )

        user_actions = []
        for action in utils.list_db_objects(
                session, models.UserLog, user_id=user_id, **filters
        ):
            action_dict = action.to_dict()
            del action_dict['user_id']
            user_actions.append(action_dict)

        return {'user_id': user_id, 'logs': user_actions}
Exemplo n.º 38
0
def start_check_cluster_health(cluster_id, send_report_url,
                               user=None, session=None, check_health={}):
    """Start to check cluster health."""
    cluster = cluster_api.get_cluster_internal(cluster_id, session=session)

    if cluster.state.state != 'SUCCESSFUL':
        logging.debug("state is %s" % cluster.state.state)
        err_msg = "Healthcheck starts only after cluster finished deployment!"
        raise exception.Forbidden(err_msg)

    reports = utils.list_db_objects(
        session, models.HealthCheckReport,
        cluster_id=cluster.id, state='verifying'
    )
    if reports:
        err_msg = 'Healthcheck in progress, please wait for it to complete!'
        raise exception.Forbidden(err_msg)

    # Clear all preivous report
    # TODO(grace): the delete should be moved into celery task.
    # We should consider the case that celery task is down.
    utils.del_db_objects(
        session, models.HealthCheckReport, cluster_id=cluster.id
    )

    from compass.tasks import client as celery_client
    celery_client.celery.send_task(
        'compass.tasks.cluster_health',
        (cluster.id, send_report_url, user.email),
        queue=user.email,
        exchange=user.email,
        routing_key=user.email
    )
    return {
        "cluster_id": cluster.id,
        "status": "start to check cluster health."
    }
Exemplo n.º 39
0
def list_actions(lister, **filters):
    """list actions."""
    with database.session() as session:
        if not lister.is_admin:
             # The user is not allowed to list users actions.
            raise Forbidden(
                'User %s has no permission to list all users actions.' % (
                    lister.email, user.email
                )
            )

        actions = {}
        for action in utils.list_db_objects(
            session, models.UserLog, **filters
        ):
            action_dict = action.to_dict()
            user_id = action_dict['user_id']
            del action_dict['user_id']
            actions.setdefault(user_id, []).append(action_dict)

        return [
            {'user_id': user_id, 'logs': user_actions}
            for user_id, user_actions in actions.items()
        ]
Exemplo n.º 40
0
def list_permissions(user=None, session=None, **filters):
    """list permissions."""
    return utils.list_db_objects(session, models.Permission, **filters)
Exemplo n.º 41
0
def list_hosts(user=None, session=None, **filters):
    """List hosts."""
    return utils.list_db_objects(session, models.Host, **filters)
Exemplo n.º 42
0
def list_hostnetworks(user=None, session=None, **filters):
    """Get host networks."""
    return utils.list_db_objects(session, models.HostNetwork, **filters)
Exemplo n.º 43
0
def list_actions(user=None, session=None, **filters):
    """list actions of all users."""
    return utils.list_db_objects(
        session, models.UserLog, order_by=['timestamp'], **filters
    )
Exemplo n.º 44
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)
Exemplo n.º 45
0
def list_users(user=None, session=None, **filters):
    """List all users."""
    return utils.list_db_objects(session, models.User, **filters)
Exemplo n.º 46
0
def list_switchmachines(user=None, session=None, **filters):
    """List switch machines."""
    switch_machines = utils.list_db_objects(session, models.SwitchMachine,
                                            **filters)
    return _filter_switch_machines(switch_machines)
Exemplo n.º 47
0
def get_switch_machines_internal(session, **filters):
    return utils.list_db_objects(session, models.SwitchMachine, **filters)
Exemplo n.º 48
0
def list_switch_filters(user=None, session=None, **filters):
    """List all switches' filters."""
    return utils.list_db_objects(session, models.Switch, **filters)
Exemplo n.º 49
0
def list_subnets(user=None, session=None, **filters):
    """List subnets."""
    return utils.list_db_objects(session, models.Subnet, **filters)
Exemplo n.º 50
0
def list_permissions_internal(session, **filters):
    """internal functions used only by other db.api modules."""
    return utils.list_db_objects(session, models.Permission, **filters)
Exemplo n.º 51
0
def list_health_reports(cluster_id, user=None, session=None):
    """List all reports in the specified cluster."""
    cluster = cluster_api.get_cluster_internal(cluster_id, session=session)
    return utils.list_db_objects(
        session, models.HealthCheckReport, cluster_id=cluster.id
    )
Exemplo n.º 52
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)
Exemplo n.º 53
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)
Exemplo n.º 54
0
def list_machines(user=None, session=None, **filters):
    """List machines."""
    return utils.list_db_objects(
        session, models.Machine, **filters
    )
Exemplo n.º 55
0
def get_host_log_histories(host_id, user=None, session=None, **kwargs):
    """Get host log history."""
    host = _get_host(host_id, session=session)
    return utils.list_db_objects(
        session, models.HostLogHistory, id=host.id, **kwargs
    )