Skip to content
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

Merged
merged 6 commits into from
Apr 17, 2020
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions dolphin/db/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,4 @@
"""
DB abstraction for Dolphin
"""

# from dolphin.db.api import * # noqa
from dolphin.db.api import * # noqa
98 changes: 98 additions & 0 deletions dolphin/db/api.py
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)
Empty file.
101 changes: 101 additions & 0 deletions dolphin/db/sqlalchemy/api.py
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
110 changes: 110 additions & 0 deletions dolphin/db/sqlalchemy/models.py
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):
Copy link
Collaborator

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

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, modified

"""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)")

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add all the models here

Copy link
Member Author

Choose a reason for hiding this comment

The 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)
Copy link
Collaborator

Choose a reason for hiding this comment

The 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)")
2 changes: 2 additions & 0 deletions etc/dolphin/dolphin.conf
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