-
Notifications
You must be signed in to change notification settings - Fork 354
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
DB Framework for Infra Management #1
Changes from 5 commits
15e5afd
b5473b7
d208d81
093a828
7735738
455f3a9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,5 +16,4 @@ | |
""" | ||
DB abstraction for Dolphin | ||
""" | ||
|
||
# from dolphin.db.api import * # noqa | ||
from dolphin.db.api import * # noqa |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
# Copyright (c) 2011 X.commerce, a business unit of eBay Inc. | ||
# Copyright 2010 United States Government as represented by the | ||
# Administrator of the National Aeronautics and Space Administration. | ||
# All Rights Reserved. | ||
# | ||
# 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. | ||
|
||
The underlying driver is loaded as a :class:`LazyPluggable`. | ||
|
||
Functions in this module are imported into the dolphin.db namespace. Call these | ||
functions from dolphin.db namespace, not the dolphin.db.api namespace. | ||
|
||
All functions in this module return objects that implement a dictionary-like | ||
interface. Currently, many of these objects are sqlalchemy objects that | ||
implement a dictionary interface. However, a future goal is to have all of | ||
these objects be simple dictionaries. | ||
|
||
|
||
**Related Flags** | ||
|
||
:backend: string to lookup in the list of LazyPluggable backends. | ||
`sqlalchemy` is the only supported backend right now. | ||
|
||
:connection: string specifying the sqlalchemy connection to use, like: | ||
`sqlite:///var/lib/dolphin/dolphin.sqlite`. | ||
|
||
:enable_new_services: when adding a new service to the database, is it in the | ||
pool of available hardware (Default: True) | ||
|
||
""" | ||
from oslo_config import cfg | ||
from oslo_db import api as db_api | ||
|
||
db_opts = [ | ||
cfg.StrOpt('db_backend', | ||
default='sqlalchemy', | ||
help='The backend to use for database.'), | ||
|
||
] | ||
|
||
CONF = cfg.CONF | ||
CONF.register_opts(db_opts) | ||
|
||
_BACKEND_MAPPING = {'sqlalchemy': 'dolphin.db.sqlalchemy.api'} | ||
IMPL = db_api.DBAPI.from_config(cfg.CONF, backend_mapping=_BACKEND_MAPPING, | ||
lazy=True) | ||
|
||
|
||
def register_db(): | ||
IMPL.register_db() | ||
|
||
|
||
def storage_get(storage_id): | ||
return IMPL.storage_get(storage_id) | ||
|
||
|
||
def storage_create(storage, register_info): | ||
IMPL.storage_create(storage, register_info) | ||
|
||
|
||
def volume_create(volume, storage_id): | ||
IMPL.volume_create(volume) | ||
|
||
|
||
def volume_get(volume_id, storage_id): | ||
IMPL.volume_get(volume_id, storage_id) | ||
|
||
|
||
def volume_get_all(storage_id): | ||
IMPL.volume_get_all(storage_id) | ||
|
||
|
||
def pool_create(pool, storage_id): | ||
IMPL.pool_create(pool) | ||
|
||
|
||
def pool_get(pool_id, storage_id): | ||
IMPL.pool_get(pool_id, storage_id) | ||
|
||
|
||
def pool_get_all(storage_id): | ||
IMPL.pool_get_all(storage_id) | ||
|
||
|
||
def connection_info_get(storage_id): | ||
IMPL.connection_info_get(storage_id) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
# Copyright (c) 2011 X.commerce, a business unit of eBay Inc. | ||
# Copyright 2010 United States Government as represented by the | ||
# Administrator of the National Aeronautics and Space Administration. | ||
# Copyright (c) 2014 Mirantis, Inc. | ||
# All Rights Reserved. | ||
# | ||
# 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. | ||
|
||
"""Implementation of SQLAlchemy backend.""" | ||
|
||
from functools import wraps | ||
import sys | ||
from oslo_config import cfg | ||
from oslo_db import options as db_options | ||
from oslo_db.sqlalchemy import session | ||
from oslo_log import log | ||
from sqlalchemy import MetaData, create_engine | ||
from dolphin.db.sqlalchemy import models | ||
from dolphin.db.sqlalchemy.models import Storage, ConnectionParams | ||
|
||
CONF = cfg.CONF | ||
LOG = log.getLogger(__name__) | ||
_FACADE = None | ||
|
||
_DEFAULT_SQL_CONNECTION = 'sqlite:///' | ||
db_options.set_defaults(cfg.CONF, | ||
connection=_DEFAULT_SQL_CONNECTION) | ||
|
||
|
||
def get_engine(): | ||
facade = _create_facade_lazily() | ||
return facade.get_engine() | ||
|
||
|
||
def get_session(**kwargs): | ||
facade = _create_facade_lazily() | ||
return facade.get_session(**kwargs) | ||
|
||
|
||
def _create_facade_lazily(): | ||
global _FACADE | ||
if _FACADE is None: | ||
_FACADE = session.EngineFacade.from_config(cfg.CONF) | ||
return _FACADE | ||
|
||
|
||
def get_backend(): | ||
"""The backend is this module itself.""" | ||
return sys.modules[__name__] | ||
|
||
|
||
def register_db(): | ||
engine = create_engine(_DEFAULT_SQL_CONNECTION, echo=False) | ||
models = (Storage, | ||
ConnectionParams | ||
) | ||
engine = create_engine(CONF.database.connection, echo=False) | ||
for model in models: | ||
model.metadata.create_all(engine) | ||
|
||
|
||
def storage_get(storage_id): | ||
this_session = get_session() | ||
this_session.begin() | ||
storage_by_id = this_session.query(Storage) \ | ||
.filter(Storage.id == storage_id) \ | ||
.first() | ||
|
||
|
||
def register_info_create(register_info): | ||
register_ref = models.ConnectionParams() | ||
register_ref.storage_id = register_info.storage_id | ||
register_ref.hostname = register_info.hostname | ||
register_ref.password = register_info.password | ||
return register_ref | ||
|
||
|
||
def storage_create(storage, register_info): | ||
storage_ref = models.Storage() | ||
storage_ref.id = storage.id | ||
storage_ref.name = storage.name | ||
storage_ref.model = storage.model | ||
storage_ref.vendor = storage.vendor | ||
storage_ref.description = storage.description | ||
storage_ref.location = storage.location | ||
storage_ref.connection_param = register_info_create(register_info) | ||
this_session = get_session() | ||
this_session.begin() | ||
this_session.add(storage_ref) | ||
this_session.commit() | ||
return storage_ref |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
# Copyright (c) 2011 X.commerce, a business unit of eBay Inc. | ||
# Copyright 2010 United States Government as represented by the | ||
# Administrator of the National Aeronautics and Space Administration. | ||
# Copyright 2011 Piston Cloud Computing, Inc. | ||
# All Rights Reserved. | ||
# | ||
# 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. | ||
""" | ||
SQLAlchemy models for Dolphin data. | ||
""" | ||
|
||
from oslo_config import cfg | ||
from oslo_db.sqlalchemy import models | ||
from sqlalchemy import Column, Integer, String, schema, Numeric | ||
from sqlalchemy.ext.declarative import declarative_base | ||
from sqlalchemy import orm | ||
from sqlalchemy import ForeignKey, DateTime, Boolean, Enum | ||
from sqlalchemy.orm import relationship, backref | ||
|
||
from dolphin.common import constants | ||
|
||
CONF = cfg.CONF | ||
BASE = declarative_base() | ||
|
||
|
||
class DolphinBase(models.ModelBase, | ||
models.TimestampMixin): | ||
"""Base class for Dolphin Models.""" | ||
__table_args__ = {'mysql_engine': 'InnoDB'} | ||
metadata = None | ||
|
||
|
||
class ConnectionParams(BASE, DolphinBase): | ||
"""Represent registration parameters required for storage object.""" | ||
__tablename__ = "connection_params" | ||
storage_id = Column(String(36), primary_key=True) | ||
NajmudheenCT marked this conversation as resolved.
Show resolved
Hide resolved
|
||
hostname = Column(String(36), default='False') | ||
username = Column(String(255)) | ||
password = Column(String(255)) | ||
|
||
|
||
class Storage(BASE, DolphinBase): | ||
"""Represents a storage object.""" | ||
|
||
__tablename__ = 'storages' | ||
id = Column(Integer, primary_key=True) | ||
name = Column(String(255)) | ||
vendor = Column(String(255)) | ||
description = Column(String(255)) | ||
model = Column(String(255)) | ||
status = Column(String(255)) | ||
serial_number = Column(String(255)) | ||
location = Column(String(255)) | ||
connection_param = orm.relationship( | ||
ConnectionParams, | ||
backref="storages", | ||
foreign_keys=id, | ||
primaryjoin="and_(" | ||
"Storage.id==" | ||
"ConnectionParams.storage_id)") | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please add all the models here There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added Volumes and pool for now.. relation with storage is defined, more relations and models need to be added.. once the initial framework is up.. |
||
|
||
class Volume(BASE, DolphinBase): | ||
"""Represents a volume object.""" | ||
__tablename__ = 'volumes' | ||
id = Column(Integer, primary_key=True) | ||
name = Column(String(255)) | ||
storage_id = Column(String(255)) | ||
description = Column(String(255)) | ||
status = Column(String(255)) | ||
total_capacity = Column(Numeric) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is Numeric a double type? |
||
used_capacity = Column(Numeric) | ||
free_capacity = Column(Numeric) | ||
storage = orm.relationship( | ||
Storage, | ||
backref="volumes", | ||
foreign_keys=storage_id, | ||
primaryjoin="and_(" | ||
"Volume.storage_id==" | ||
"Storage.id)") | ||
|
||
|
||
class Pool(BASE, DolphinBase): | ||
"""Represents a pool object.""" | ||
__tablename__ = 'pools' | ||
id = Column(Integer, primary_key=True) | ||
name = Column(String(255)) | ||
storage_id = Column(String(255)) | ||
description = Column(String(255)) | ||
status = Column(String(255)) | ||
total_capacity = Column(Numeric) | ||
used_capacity = Column(Numeric) | ||
free_capacity = Column(Numeric) | ||
storage = orm.relationship( | ||
Storage, | ||
backref="pools", | ||
foreign_keys=storage_id, | ||
primaryjoin="and_(" | ||
"Pool.storage_id==" | ||
"Storage.id)") |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
[DEFAULT] | ||
api_paste_config = C:\pythonpath\dolphin\etc\dolphin\api-paste.ini | ||
|
||
[database] | ||
connection = sqlite:////var/lib/dolphin/dolphin.sqlite |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggest RegistryContext to replace ConnectionParams
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, modified