コード例 #1
0
def hosts_get_by_region(context, region_id, filters):
    """Get all hosts for this region.

    :param region_id: ID for the region
    :param filters: filters wich contains differnt keys/values to match.
    Supported filters are by name, ip_address, id and cell_id.
    """
    host_devices = with_polymorphic(models.Device, [models.Host])
    query = model_query(context, host_devices, project_only=True)
    query = query.filter_by(region_id=region_id)

    if "name" in filters:
        query = query.filter_by(name=filters["name"])
    if "ip_address" in filters:
        query = query.filter_by(ip_address=filters["ip_address"])
    if "id" in filters:
        query = query.filter_by(id=filters["id"])
    if "cell" in filters:
        query = query.filter_by(cell_id=filters["cell"])
    if "device_type" in filters:
        query = query.filter_by(device_type=filters["device_type"])

    try:
        result = query.all()
    except sa_exc.NoResultFound:
        raise exceptions.NotFound()
    except Exception as err:
        raise exceptions.UnknownException(message=err)
    return result
コード例 #2
0
def regions_get_all(context):
    """Get all available regions."""
    query = model_query(context, models.Region, project_only=True)
    try:
        return query.all()
    except sa_exc.NoResultFound:
        raise exceptions.NotFound()
コード例 #3
0
def regions_get_by_id(context, region_id):
    """Get cell detail for the region with given id."""
    query = model_query(context, models.Region, project_only=True)
    query = query.filter_by(id=region_id)
    try:
        return query.one()
    except sa_exc.NoResultFound:
        raise exceptions.NotFound()
コード例 #4
0
def cells_get_by_id(context, cell_id):
    """Get cell details given for a given cell id."""
    try:
        query = model_query(context, models.Cell).\
            filter_by(id=cell_id)
        return query.one()
    except sa_exc.NoResultFound:
        raise exceptions.NotFound()
コード例 #5
0
ファイル: api.py プロジェクト: thomasem/craton
def clouds_get_by_id(context, cloud_id):
    """Get cell detail for the cloud with given id."""
    query = model_query(context, models.Cloud, project_only=True)
    query = query.filter_by(id=cloud_id)
    try:
        return query.one()
    except sa_exc.NoResultFound:
        raise exceptions.NotFound()
コード例 #6
0
def cells_get_by_name(context, region_id, cell_id):
    """Get cell details given for a given cell in a region."""
    try:
        query = model_query(context, models.Cell).\
            filter_by(region_id=region_id).\
            filter_by(name=cell_id)
        return query.one()
    except sa_exc.NoResultFound:
        raise exceptions.NotFound()
コード例 #7
0
def projects_get_all(context):
    """Get all the projects."""
    query = model_query(context, models.Project)
    try:
        return query.all()
    except sa_exc.NoResultFound:
        raise exceptions.NotFound()
    except Exception as err:
        raise exceptions.UnknownException(message=err)
コード例 #8
0
def projects_get_by_name(context, project_name):
    """Get all projects that match the given name."""
    query = model_query(context, models.Project)
    query = query.filter(models.Project.name.like(project_name))
    try:
        return query.all()
    except sa_exc.NoResultFound:
        raise exceptions.NotFound()
    except Exception as err:
        raise exceptions.UnknownException(message=err)
コード例 #9
0
def get_user_info(context, username):
    """Get user info."""
    query = model_query(context, models.User, project_only=True)
    query = query.filter_by(username=username)
    try:
        return query.one()
    except sa_exc.NoResultFound:
        raise exceptions.NotFound()
    except Exception as err:
        raise exceptions.UnknownException(message=err)
コード例 #10
0
def projects_get_by_id(context, project_id):
    """Get project by its id."""
    query = model_query(context, models.Project)
    query = query.filter_by(id=project_id)
    try:
        return query.one()
    except sa_exc.NoResultFound:
        raise exceptions.NotFound()
    except Exception as err:
        raise exceptions.UnknownException(message=err)
コード例 #11
0
def net_interfaces_get_by_id(context, interface_id):
    """Get a given network interface by its id."""
    query = model_query(context, models.NetInterface, project_only=True)
    query = query.filter_by(id=interface_id)
    try:
        result = query.one()
    except sa_exc.NoResultFound:
        raise exceptions.NotFound()

    return result
コード例 #12
0
def cells_get_all(context, region):
    """Get all cells."""
    query = model_query(context, models.Cell, project_only=True)
    if region is not None:
        query = query.filter_by(region_id=region)

    try:
        return query.all()
    except sa_exc.NoResultFound:
        raise exceptions.NotFound()
    except Exception as err:
        raise exceptions.UnknownException(message=err)
コード例 #13
0
def users_get_by_id(context, user_id):
    """Get user by its id."""
    if is_admin_context(context):
        query = model_query(context, models.User)
    else:
        query = model_query(context, models.User, project_only=True)

    query = query.filter_by(id=user_id)
    try:
        return query.one()
    except sa_exc.NoResultFound:
        raise exceptions.NotFound()
コード例 #14
0
def netdevices_get_by_id(context, netdevice_id):
    """Get a given network device by its id."""
    devices = with_polymorphic(models.Device, [models.NetDevice])
    query = model_query(context, devices, project_only=True)
    query = query.filter_by(type='net_devices')
    query = query.filter_by(id=netdevice_id)
    try:
        result = query.one()
    except sa_exc.NoResultFound:
        raise exceptions.NotFound()

    return result
コード例 #15
0
ファイル: api.py プロジェクト: thomasem/craton
def projects_get_by_name(context, project_name, filters, pagination_params):
    """Get all projects that match the given name."""
    query = model_query(context, models.Project)
    query = query.filter(models.Project.name.like(project_name))
    if "vars" in filters:
        query = add_var_filters_to_query(query, filters)
    try:
        return _paginate(context, query, models.Project, session, filters,
                         pagination_params)
    except sa_exc.NoResultFound:
        raise exceptions.NotFound()
    except Exception as err:
        raise exceptions.UnknownException(message=err)
コード例 #16
0
ファイル: api.py プロジェクト: mark4h/craton
def _paginate(context,
              query,
              model,
              session,
              filters,
              pagination_params,
              project_only=False):
    # NOTE(sigmavirus24) Retrieve the instance of the model represented by the
    # marker.
    try:
        marker = _marker_from(context, session, model,
                              pagination_params['marker'], project_only)
    except sa_exc.NoResultFound:
        raise exceptions.BadRequest(message='Marker "{}" does not exist'.
                                    format(pagination_params['marker']))
    except Exception as err:
        raise exceptions.UnknownException(message=err)

    filters.setdefault('sort_keys', ['created_at', 'id'])
    filters.setdefault('sort_dir', 'asc')
    # Retrieve the results based on the marker and the limit
    try:
        results = db_utils.paginate_query(
            query,
            model,
            limit=pagination_params['limit'],
            sort_keys=filters['sort_keys'],
            sort_dir=filters['sort_dir'],
            marker=marker,
        ).all()
    except sa_exc.NoResultFound:
        raise exceptions.NotFound()
    except db_exc.InvalidSortKey as invalid_key:
        raise exceptions.BadRequest(
            message='"{}" is an invalid sort key'.format(invalid_key.key))
    except Exception as err:
        raise exceptions.UnknownException(message=err)

    try:
        links = _link_params_for(
            query,
            model,
            filters,
            pagination_params,
            marker,
            results,
        )
    except Exception as err:
        raise exceptions.UnknownException(message=err)

    return results, links
コード例 #17
0
def hosts_get_by_id(context, host_id):
    """Get details for the host with given id."""
    host_devices = with_polymorphic(models.Device, '*')
    query = model_query(context, host_devices, project_only=True).\
        filter_by(id=host_id)
    try:
        result = query.one()
        LOG.info("Result by host id %s" % result)
    except sa_exc.NoResultFound:
        LOG.error("No result found for host with id %s" % host_id)
        raise exceptions.NotFound()
    except Exception as err:
        raise exceptions.UnknownException(message=err)
    return result
コード例 #18
0
ファイル: api.py プロジェクト: sulochan/craton
def _device_labels_update(context, device_type, device_id, labels):
    """Update labels for the given device. Add the label if it is not present
    in host labels list, otherwise do nothing."""
    session = get_session()
    with session.begin():
        devices = with_polymorphic(models.Device, '*')
        query = model_query(context, devices, session=session,
                            project_only=True)
        query = query.filter_by(type=device_type)
        query = query.filter_by(id=device_id)
        try:
            device = query.one()
        except sa_exc.NoResultFound:
            raise exceptions.NotFound()

        device.labels.update(labels["labels"])
        device.save(session)
        return device
コード例 #19
0
ファイル: api.py プロジェクト: sulochan/craton
def _device_labels_delete(context, device_type, device_id, labels):
    """Delete labels from the device labels list if it matches
    the given label in the query, otherwise do nothing."""
    session = get_session()
    with session.begin():
        devices = with_polymorphic(models.Device, '*')
        query = model_query(context, devices, session=session,
                            project_only=True)
        query = query.filter_by(type=device_type)
        query = query.filter_by(id=device_id)
        try:
            device = query.one()
        except sa_exc.NoResultFound:
            raise exceptions.NotFound()

        for label in labels["labels"]:
            device.labels.discard(label)
        device.save(session)
        return device
コード例 #20
0
ファイル: api.py プロジェクト: sulochan/craton
def hosts_data_update(context, host_id, data):
    """
    Update existing host variables or create when its not present.
    """
    session = get_session()
    with session.begin():
        host_devices = with_polymorphic(models.Device, '*')
        query = model_query(context, host_devices, session=session,
                            project_only=True)
        query = query.filter_by(id=host_id)

        try:
            host_ref = query.with_lockmode('update').one()
        except sa_exc.NoResultFound:
            raise exceptions.NotFound()

        for key in data:
            host_ref.variables[key] = data[key]

    return host_ref
コード例 #21
0
ファイル: api.py プロジェクト: sulochan/craton
def hosts_data_delete(context, host_id, data):
    """Delete the existing key (variable) from region data."""
    session = get_session()
    with session.begin():
        host_devices = with_polymorphic(models.Device, '*')
        query = model_query(context, host_devices, session=session,
                            project_only=True)
        query = query.filter_by(id=host_id)

        try:
            host_ref = query.with_lockmode('update').one()
        except sa_exc.NoResultFound:
            raise exceptions.NotFound()

        for key in data:
            try:
                del host_ref.variables[data[key]]
            except KeyError:
                pass

    return host_ref
コード例 #22
0
ファイル: api.py プロジェクト: thomasem/craton
def resource_get_by_id(
        context, resources, resource_id, session=None, for_update=False
        ):
    """Get resource for the unique resource id."""
    model = _get_resource_model(resources)

    query = model_query(context, model, project_only=True, session=session)

    if resources in ("hosts", "network-devices"):
        query = query.filter_by(type=resources.replace("-", "_"))

    query = query.filter_by(id=resource_id)

    if for_update:
        query = query.with_for_update()

    try:
        resource = query.one()
    except sa_exc.NoResultFound:
        raise exceptions.NotFound()
    else:
        return resource
コード例 #23
0
ファイル: test_api.py プロジェクト: sigmavirus24/craton-1
 def test_get_cell_no_exist_by_name_fails(self, mock_cell):
     err = exceptions.NotFound()
     mock_cell.side_effect = err
     resp = self.get('v1/cells?region=1&name=dontexist')
     self.assertEqual(404, resp.status_code)
コード例 #24
0
ファイル: test_api.py プロジェクト: sigmavirus24/craton-1
 def test_get_cells_by_bad_id_is_404(self, mock_cells):
     mock_cells.side_effect = exceptions.NotFound()
     resp = self.get('v1/cells/3')
     self.assertEqual(404, resp.status_code)
コード例 #25
0
ファイル: test_api.py プロジェクト: sigmavirus24/craton-1
 def test_get_networks_by_non_existing_region_raises404(self, fake_network):
     fake_network.side_effect = exceptions.NotFound()
     resp = self.get('/v1/networks?region_id=5')
     self.assertEqual(404, resp.status_code)
コード例 #26
0
ファイル: test_api.py プロジェクト: sigmavirus24/craton-1
 def test_get_host_by_non_existing_region_raises404(self, fake_hosts):
     fake_hosts.side_effect = exceptions.NotFound()
     resp = self.get('/v1/hosts?region=5')
     self.assertEqual(404, resp.status_code)
コード例 #27
0
ファイル: test_api.py プロジェクト: sigmavirus24/craton-1
 def test_get_hosts_by_bad_id_is_404(self, mock_hosts):
     mock_hosts.side_effect = exceptions.NotFound()
     resp = self.get('v1/hosts/1')
     self.assertEqual(404, resp.status_code)
コード例 #28
0
ファイル: test_api.py プロジェクト: sigmavirus24/craton-1
 def test_get_region_no_exist_by_name_fails(self, mock_regions):
     mock_regions.side_effect = exceptions.NotFound()
     resp = self.get('v1/regions?name=bla')
     self.assertEqual(404, resp.status_code)