Exemple #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"])

    try:
        result = query.all()
    except sa_exc.NoResultFound:
        raise exceptions.NotFound()
    except Exception as err:
        raise exceptions.UnknownException(message=err)
    return result
Exemple #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()
Exemple #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()
Exemple #4
0
def cells_get_by_name(context, region, cell):
    """Get cell details given for a given cell in a region."""
    try:
        query = model_query(context, models.Cell).\
            filter_by(region_id=region).\
            filter_by(id=cell)
        return query.one()
    except sqlalchemy.orm.exc.NoResultFound:
        raise exceptions.NotFound()
Exemple #5
0
def cells_get_by_id(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(id=cell_id)
        return query.one()
    except sa_exc.NoResultFound:
        raise exceptions.NotFound()
Exemple #6
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)
Exemple #7
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
Exemple #8
0
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
Exemple #9
0
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
Exemple #10
0
 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)
Exemple #11
0
 def test_get_host_by_non_existing_region_raises404(self, fake_hosts):
     fake_hosts.side_effect = exceptions.NotFound()
     resp = self.get('/v1/host?region=5')
     self.assertEqual(404, resp.status_code)
Exemple #12
0
 def test_delete_region_no_exist_fails(self, mock_region):
     mock_region.return_value = None
     mock_region.side_effect = exceptions.NotFound()
     resp = self.delete('v1/regionss/100')
     self.assertEqual(404, resp.status_code)
Exemple #13
0
 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)