blazar/climate/db/utils.py
Cristian A Sanchez 69b0497ab9 Update openstack.common with latest oslo-incubator
openstack.common was updated with the latest oslo-incubator changes.
rpc and notif were manually removed. DB layer was modified to adapt
to the changes done in Oslo. '# noqa' was also removed from imports.

Also after new release of oslo.messaging appeared, we also needed
to update openstack.common to make Climate work with oslo.

Change-Id: I1b5ce64f8d4906d1fcfea9379d115b7bb2ec3f7b
Closes-Bug: #1237896
Closes-Bug: #1284749
Partial-bug: #1253497
Co-Authored-By: Pablo Fernando Cargnelutti <pablo.fernando.cargnelutti@intel.com>
Co-Authored-By: Dina Belova <dbelova@mirantis.com>
2014-03-06 16:30:42 -03:00

137 lines
3.9 KiB
Python

# -*- coding: utf-8 -*-
#
# Author: François Rossigneux <francois.rossigneux@inria.fr>
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
"""Defines interface for DB access.
Functions in this module are imported into the climate.db namespace. Call these
functions from climate.db namespace, not the climate.db.api namespace.
All functions in this module return objects that implement a dictionary-like
interface.
**Related Flags**
:db_backend: string to lookup in the list of LazyPluggable backends.
`sqlalchemy` is the only supported backend right now.
:sql_connection: string specifying the sqlalchemy connection to use, like:
`sqlite:///var/lib/climate/climate.sqlite`.
"""
from climate.openstack.common.db import api as db_api
from climate.openstack.common.db import options as db_options
from climate.openstack.common import log as logging
_BACKEND_MAPPING = {
'sqlalchemy': 'climate.db.sqlalchemy.utils',
}
IMPL = db_api.DBAPI(db_options.CONF.database.backend,
backend_mapping=_BACKEND_MAPPING)
LOG = logging.getLogger(__name__)
def setup_db():
"""Set up database, create tables, etc.
Return True on success, False otherwise
"""
return IMPL.setup_db()
def drop_db():
"""Drop database.
Return True on success, False otherwise
"""
return IMPL.drop_db()
## Helpers for building constraints / equality checks
def constraint(**conditions):
"""Return a constraint object suitable for use with some updates."""
return IMPL.constraint(**conditions)
def equal_any(*values):
"""Return an equality condition object suitable for use in a constraint.
Equal_any conditions require that a model object's attribute equal any
one of the given values.
"""
return IMPL.equal_any(*values)
def not_equal(*values):
"""Return an inequality condition object suitable for use in a constraint.
Not_equal conditions require that a model object's attribute differs from
all of the given values.
"""
return IMPL.not_equal(*values)
def to_dict(func):
def decorator(*args, **kwargs):
res = func(*args, **kwargs)
if isinstance(res, list):
return [item.to_dict() for item in res]
if res:
return res.to_dict()
else:
return None
return decorator
def get_free_periods(resource_id, start_date, end_date, duration):
"""Returns a list of free periods."""
return IMPL.get_free_periods(resource_id, start_date, end_date, duration)
def get_full_periods(resource_id, start_date, end_date, duration):
"""Returns a list of full periods."""
return IMPL.get_full_periods(resource_id, start_date, end_date, duration)
def reservation_ratio(resource_id, start_date, end_date):
return IMPL.reservation_ratio(resource_id, start_date, end_date)
def availability_time(resource_id, start_date, end_date):
return IMPL.availability_time(resource_id, start_date, end_date)
def reservation_time(resource_id, start_date, end_date):
return IMPL.reservation_time(resource_id, start_date, end_date)
def number_of_reservations(resource_id, start_date, end_date):
return IMPL.number_of_reservations(resource_id, start_date, end_date)
def longest_lease(resource_id, start_date, end_date):
return IMPL.longest_lease(resource_id, start_date, end_date)
def shortest_lease(resource_id, start_date, end_date):
return IMPL.shortest_lease(resource_id, start_date, end_date)