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
Beispiel #2
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()
Beispiel #3
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()
Beispiel #4
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()
Beispiel #5
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()
Beispiel #6
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()
Beispiel #7
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()
Beispiel #8
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]
Beispiel #9
0
def del_cluster(deleter, cluster_id, **kwargs):
    """Delete a cluster."""
    with database.session() as session:
        user_api.check_user_permission_internal(
            session, deleter, permission.PERMISSION_DEL_CLUSTER)
        cluster = utils.get_db_object(
            session, models.Cluster, id=cluster_id
        )
        utils.del_db_object(session, cluster)
        return cluster.to_dict()
Beispiel #10
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()
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])
Beispiel #12
0
def add_cluster(creator, name, adapter_id, **kwargs):
    """Create a cluster."""
    with database.session() as session:
        user_api.check_user_permission_internal(
            session, creator, permission.PERMISSION_ADD_CLUSTER)
        cluster = utils.add_db_object(
            session, models.Cluster, True,
            name, adapter_id, creator.id, **kwargs
        )
        return cluster.to_dict()
Beispiel #13
0
def list_clusters(lister, **filters):
    """List clusters."""
    with database.session() as session:
        user_api.check_user_permission_internal(
            session, lister, permission.PERMISSION_LIST_CLUSTERS)
        return [
            cluster.to_dict()
            for cluster in utils.list_db_object(
                session, models.Cluster, **filters
            )
        ]
Beispiel #14
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
            )
        ]
Beispiel #15
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
            )
        ]
Beispiel #16
0
def get_permission(getter, permission_id, **kwargs):
    """get permissions."""
    from compass.db.api import user as user_api
    with database.session() as session:
        user_api.check_user_permission_internal(
            session, getter, PERMISSION_LIST_PERMISSIONS
        )
        permission = utils.get_db_object(
            session, models.Permission, id=permission_id
        )
        return permission.to_dict()
Beispiel #17
0
def delete_cluster_config(updater, cluster_id):
    """Update a cluster config."""
    with database.session() as session:
        user_api.check_user_permission_internal(
            session, updater, permission.PERMISSION_DEL_CLUSTER)
        cluster = utils.get_db_object(
            session, models.Cluster, id=cluster_id
        )
        utils.update_db_object(
            session, cluster, os_config={}, package_config={}
        )
        return cluster.to_dict()
Beispiel #18
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
            )
        ]
Beispiel #19
0
def patch_machine(updater, machine_id, **kwargs):
    """Update a switch."""
    with database.session() as session:
         user_api.check_user_permission_internal(
            session, updater, permission.PERMISSION_UDPATE_MACHINE)
         machine = utils.get_db_object(session, models.Machine, id=machine_id)
         utils.update_db_object(session, machine, **kwargs)
         machine_dict = machine.to_dict()
         utils.validates(
             [], {'ipmi_credentials': _check_ipmi_credentials},
             **machine_dict
         )
         return machine_dict
def get_adapter_roles(getter, adapter_id, **kwargs):
    """get adapter roles."""
    with database.session() as session:
        user_api.check_user_permission_internal(
            session, lister, permission.PERMISSION_LIST_ADAPTER)
        if adapter_id not in ADAPTER_MAPPING:
            raise excedption.RecordNotExists(
                'adpater %s does not exist' % adapter_id
            )
        adapter_dict = ADAPTER_MAPPING[adapter_id]
        if 'package_adapter' not in adapter_dict:
            raise excedption.RecordNotExists(
                'adapter %s does not contain package_adapter' % adapter_id
            )
        return ADAPTER_MAPPING[adapter_id]['package_adapter']['roles']
Beispiel #21
0
def update_cluster_hosts(
    updater, cluster_id, add_hosts={}, set_hosts=None,
    remove_hosts=[]
):
    """Get subnet info."""
    with database.session() as session:
        user_api.check_user_permission_internal(
            session, getter, permission.PERMISSION_UPDATE_CLUSTER_HOSTS)
        cluster = utils.get_db_object(
            session, models.Cluster, id=cluster_id
        )
        if remove_hosts:
            _remove_clusterhosts(session, cluster, remove_hosts)
        if add_hosts:
            _add_clusterhosts(session, cluster, add_hosts)
        if set_hosts is not None:
            _set_clusterhosts(session, cluster, set_hosts)
        return [host.to_dict() for host in cluster.clusterhosts]
Beispiel #22
0
def review_cluster(reviewer, cluster_id):
    """review cluster."""
    with database.session() as session:
        user_api.check_user_permission_internal(
            session, getter, permission.PERMISSION_REVIEW_CLUSTER)
        cluster = utils.get_db_object(
            session, models.Cluster, id=cluster_id
        )
        os_config = cluster.os_config
        if os_config:
            metadata_api.validate_os_config(
                os_config, cluster.apdater_id, True
            )
            for clusterhost in cluster.clusterhosts:
                host = clusterhost.host
                host_os_config = host.os_config
                host.deployed_os_config = util.merge_dict(
                    os_config, host_os_config
                )
                metadata_api.validate_os_config(
                    host.deployed_os_config, cluster.apdater_id, True
                )
        package_config = cluster.package_config
        if package_config:
            metadata_api.validate_package_config(
                package_config, cluster.adapter_id, True
            )
            for clusterhost in cluster.clusterhosts:
                clusterhost_package_config = clusterhost.package_config
                clusterhost.deployed_package_config = util.mrege_dict(
                    package_config, clusterhost_package_config
                )
                metadata_api.validate_os_config(
                    clusterhost.deployed_package_config,
                    cluster.apdater_id, True
                )
        return {
            'cluster': cluster.to_dict(),
            'clusterhosts': [
                clusterhost.to_dict()
                for clusterhost in cluster.clusterhosts
            ]
        }
Beispiel #23
0
def update_cluster_config(updater, cluster_id, **kwargs):
    """Update a cluster config."""
    with database.session() as session:
        user_api.check_user_permission_internal(
            session, updater, permission.PERMISSION_ADD_CLUSTER)
        cluster = utils.get_db_object(
            session, models.Cluster, id=cluster_id
        )
        utils.update_db_object(session, cluster, **kwargs)
        os_config = cluster.os_config
        if os_config:
            metadata_api.validate_os_config(
                os_config, cluster.adapter_id
            )
        package_config = cluster.package_config
        if package_config:
            metadata_api.validate_package_config(
                package_config, cluster.adapter_id
            )
        return cluster.to_dict()
Beispiel #24
0
    with database.session() as session:
        user_api.check_user_permission_internal(
            session, creator, permission.PERMISSION_ADD_CLUSTER)
        cluster = utils.add_db_object(
            session, models.Cluster, True,
            name, adapter_id, creator.id, **kwargs
        )
        return cluster.to_dict()


@utils.wrap_to_dict(RESP_FIELDS)
@utils.supported_filters(UPDATED_FIELDS)
def update_cluster(updater, cluster_id, **kwargs):
     """Update a cluster."""
    with database.session() as session:
        user_api.check_user_permission_internal(
            session, updater, permission.PERMISSION_ADD_CLUSTER)
        cluster = utils.get_db_object(
            session, models.Cluster, id=cluster_id
        )
        utils.update_db_object(session, cluster, **kwargs)
        return cluster.to_dict()


@utils.wrap_to_dict(RESP_FIELDS)
@utils.supported_filters([])
def del_cluster(deleter, cluster_id, **kwargs):
    """Delete a cluster."""
    with database.session() as session:
        user_api.check_user_permission_internal(
            session, deleter, permission.PERMISSION_DEL_CLUSTER)
        cluster = utils.get_db_object(
Beispiel #25
0
    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()


@utils.wrap_to_dict(RESP_FIELDS)
@utils.input_validates(subnet=_check_network)
@utils.supported_filters(UPDATED_FIELDS)
def update_subnet(updater, subnet_id, **kwargs):
     """Update a subnet."""
    with database.session() as session:
        user_api.check_user_permission_internal(
            session, updater, permission.PERMISSION_ADD_NETWORK)
        network = utils.get_db_object(
            session, models.Network, id=subnet_id
        )
        utils.update_db_object(session, network, **kwargs)
        return network.to_dict()


@utils.wrap_to_dict(RESP_FIELDS)
@utils.supported_filters([])
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(