From 8c0f63f72b5dcbaa30f8c4ab00092650121c55ae Mon Sep 17 00:00:00 2001 From: Lakshmi N Sampath Date: Thu, 24 Apr 2014 17:20:34 -0700 Subject: [PATCH] Refactored capability type and namespace dao's Change-Id: I384d71a9b9067ebb752156f7f8de9dee03b53e9a --- config.py | 20 +++---- .../api/controllers/v1/capability_type.py | 12 ++-- graffiti/api/controllers/v1/namespace.py | 6 +- .../controllers/v1/ns_controller_factory.py | 57 ------------------- graffiti/api/model/v1/dao/__init__.py | 0 .../v1/dao/captype_dao.py} | 12 ++-- .../v1/dao/captype_dao_factory.py} | 31 +++++----- .../v1/dao/captype_db_dao.py} | 11 ++-- .../v1/dao/captype_file_dao.py} | 11 ++-- .../v1/dao/captype_mem_dao.py} | 11 ++-- .../v1/dao/ns_dao.py} | 12 ++-- graffiti/api/model/v1/dao/ns_dao_factory.py | 57 +++++++++++++++++++ .../v1/dao/ns_db_dao.py} | 8 +-- .../v1/dao/ns_file_dao.py} | 14 +++-- .../v1/dao/ns_mem_dao.py} | 8 +-- .../resource_dao.py} | 24 +++++--- .../api/model/v1/dao/resource_dao_factory.py | 38 +++++++++++++ graffiti/api/model/v1/dao/resource_db_dao.py | 40 +++++++++++++ .../api/model/v1/dao/resource_file_dao.py | 42 ++++++++++++++ .../resource_mem_dao.py} | 39 +++++-------- graffiti/api/tests/test_controller_v1.py | 50 +++++++++------- graffiti/common/exception.py | 50 +++++++++++----- graffiti/drivers/modules/glance.py | 20 ++++--- graffiti/drivers/modules/local.py | 7 ++- 24 files changed, 370 insertions(+), 210 deletions(-) delete mode 100755 graffiti/api/controllers/v1/ns_controller_factory.py create mode 100644 graffiti/api/model/v1/dao/__init__.py rename graffiti/api/{controllers/v1/captype_controller.py => model/v1/dao/captype_dao.py} (88%) rename graffiti/api/{controllers/v1/captype_controller_factory.py => model/v1/dao/captype_dao_factory.py} (52%) rename graffiti/api/{controllers/v1/captype_db_controller.py => model/v1/dao/captype_db_dao.py} (93%) rename graffiti/api/{controllers/v1/captype_file_controller.py => model/v1/dao/captype_file_dao.py} (91%) rename graffiti/api/{controllers/v1/captype_mem_controller.py => model/v1/dao/captype_mem_dao.py} (85%) rename graffiti/api/{controllers/v1/ns_controller.py => model/v1/dao/ns_dao.py} (89%) create mode 100755 graffiti/api/model/v1/dao/ns_dao_factory.py rename graffiti/api/{controllers/v1/ns_db_controller.py => model/v1/dao/ns_db_dao.py} (91%) rename graffiti/api/{controllers/v1/ns_file_controller.py => model/v1/dao/ns_file_dao.py} (92%) rename graffiti/api/{controllers/v1/ns_mem_controller.py => model/v1/dao/ns_mem_dao.py} (88%) rename graffiti/api/model/v1/{resource_dao_factory.py => dao/resource_dao.py} (64%) create mode 100644 graffiti/api/model/v1/dao/resource_dao_factory.py create mode 100644 graffiti/api/model/v1/dao/resource_db_dao.py create mode 100644 graffiti/api/model/v1/dao/resource_file_dao.py rename graffiti/api/model/v1/{resource_dao.py => dao/resource_mem_dao.py} (65%) diff --git a/config.py b/config.py index 8b75bce..70d31be 100644 --- a/config.py +++ b/config.py @@ -92,8 +92,8 @@ cfg.CONF.register_group(keystone_group) cfg.CONF.register_opts(keystone_opts, group=keystone_group) # DEFAULT group -default_controller_group = cfg.OptGroup('DEFAULT') -default_controller_opts = [ +default_group = cfg.OptGroup('DEFAULT') +default_opts = [ cfg.StrOpt( 'persistence_type', default="memory", @@ -101,13 +101,13 @@ default_controller_opts = [ "values = 'memory' or 'file' or 'db")) ] -cfg.CONF.register_group(default_controller_group) -cfg.CONF.register_opts(default_controller_opts, - group=default_controller_group) +cfg.CONF.register_group(default_group) +cfg.CONF.register_opts(default_opts, + group=default_group) # FILE_PERSISTENCE group -file_controller_group = cfg.OptGroup('FILE_PERSISTENCE') -file_controller_opts = [ +file_group = cfg.OptGroup('FILE_PERSISTENCE') +file_opts = [ cfg.StrOpt( 'dictionary_folder', default="/tmp/graffiti-dictionary/", @@ -115,9 +115,9 @@ file_controller_opts = [ ) ] -cfg.CONF.register_group(file_controller_group) -cfg.CONF.register_opts(file_controller_opts, - group=file_controller_group) +cfg.CONF.register_group(file_group) +cfg.CONF.register_opts(file_opts, + group=file_group) # Used for remote debugging, like pychcharms or pydev diff --git a/graffiti/api/controllers/v1/capability_type.py b/graffiti/api/controllers/v1/capability_type.py index 8a3439b..60bce62 100644 --- a/graffiti/api/controllers/v1/capability_type.py +++ b/graffiti/api/controllers/v1/capability_type.py @@ -18,10 +18,10 @@ from pecan.rest import RestController from wsme.api import Response from wsmeext.pecan import wsexpose -from graffiti.api.controllers.v1.captype_controller_factory \ - import CapTypeControllerFactory from graffiti.api.model.v1.capability_type import CapabilityType -from ns_controller_factory import NSControllerFactory +from graffiti.api.model.v1.dao.captype_dao_factory \ + import CapabilityTypeDAOFactory +from graffiti.api.model.v1.dao.ns_dao_factory import NSDAOFactory from oslo.config import cfg @@ -37,9 +37,9 @@ class CapabilityTypeController(RestController): self._load_controller() def _load_controller(self): - controller_type = cfg.CONF.DEFAULT.persistence_type - self._cap_controller = CapTypeControllerFactory.create(controller_type) - self._ns_controller = NSControllerFactory.get() + dao_type = cfg.CONF.DEFAULT.persistence_type + self._cap_controller = CapabilityTypeDAOFactory.create(dao_type) + self._ns_controller = NSDAOFactory.get() @wsexpose def options(): diff --git a/graffiti/api/controllers/v1/namespace.py b/graffiti/api/controllers/v1/namespace.py index 6e538ef..b303edd 100644 --- a/graffiti/api/controllers/v1/namespace.py +++ b/graffiti/api/controllers/v1/namespace.py @@ -17,8 +17,8 @@ from pecan.rest import RestController from wsmeext.pecan import wsexpose -from graffiti.api.controllers.v1.ns_controller_factory\ - import NSControllerFactory +from graffiti.api.model.v1.dao.ns_dao_factory \ + import NSDAOFactory from graffiti.api.model.v1.namespace import Namespace from oslo.config import cfg import six @@ -32,7 +32,7 @@ class NamespaceController(RestController): def _load_controller(self): controller_type = cfg.CONF.DEFAULT.persistence_type - _controller = NSControllerFactory.create(controller_type) + _controller = NSDAOFactory.create(controller_type) return _controller @wsexpose diff --git a/graffiti/api/controllers/v1/ns_controller_factory.py b/graffiti/api/controllers/v1/ns_controller_factory.py deleted file mode 100755 index 0789184..0000000 --- a/graffiti/api/controllers/v1/ns_controller_factory.py +++ /dev/null @@ -1,57 +0,0 @@ -# Copyright (c) 2014 Hewlett-Packard Development Company, L.P. -# -# 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. - - -from graffiti.api.controllers.v1.ns_db_controller \ - import DBNSController -from graffiti.api.controllers.v1.ns_file_controller \ - import FileNSController -from graffiti.api.controllers.v1.ns_mem_controller \ - import MemNSController -from oslo.config import cfg - - -class NSControllerFactory(object): - - __controller = None - __controller_type = cfg.CONF.DEFAULT.persistence_type - - def __init__(self, **kwargs): - super(NSControllerFactory, self).__init__(**kwargs) - - @staticmethod - def create(controller_type, **kwargs): - if controller_type.lower() == 'memory': - print "Namespace persistence = memory" - NSControllerFactory.__controller = MemNSController(**kwargs) - return NSControllerFactory.__controller - elif controller_type.lower() == "db": - print "Namespace persistence = db" - NSControllerFactory.__controller = DBNSController(**kwargs) - return NSControllerFactory.__controller - elif controller_type.lower() == "file": - print "Namespace persistence = File" - NSControllerFactory.__controller = FileNSController(**kwargs) - return NSControllerFactory.__controller - - return None - - @staticmethod - def get(): - if NSControllerFactory.__controller: - return NSControllerFactory.__controller - else: - return NSControllerFactory.create( - NSControllerFactory.__controller_type) diff --git a/graffiti/api/model/v1/dao/__init__.py b/graffiti/api/model/v1/dao/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/graffiti/api/controllers/v1/captype_controller.py b/graffiti/api/model/v1/dao/captype_dao.py similarity index 88% rename from graffiti/api/controllers/v1/captype_controller.py rename to graffiti/api/model/v1/dao/captype_dao.py index bd93513..98bf37f 100644 --- a/graffiti/api/controllers/v1/captype_controller.py +++ b/graffiti/api/model/v1/dao/captype_dao.py @@ -14,18 +14,18 @@ # limitations under the License. -class CapabilityTypeControllerBase(object): +class CapabilityTypeDAOBase(object): def __init__(self, **kwargs): - super(CapabilityTypeControllerBase, self).__init__(**kwargs) - self._type = 'None' - - def get_capability_type(self, name, namespace): - return None + super(CapabilityTypeDAOBase, self).__init__(**kwargs) + self._type = "CapabilityTypeDAOBase" def get_type(self): return self._type + def get_capability_type(self, name, namespace): + return None + def find_capability_types(self, query_string): return [] diff --git a/graffiti/api/controllers/v1/captype_controller_factory.py b/graffiti/api/model/v1/dao/captype_dao_factory.py similarity index 52% rename from graffiti/api/controllers/v1/captype_controller_factory.py rename to graffiti/api/model/v1/dao/captype_dao_factory.py index 3c48b96..1680155 100644 --- a/graffiti/api/controllers/v1/captype_controller_factory.py +++ b/graffiti/api/model/v1/dao/captype_dao_factory.py @@ -13,30 +13,29 @@ # See the License for the specific language governing permissions and # limitations under the License. - -from graffiti.api.controllers.v1.captype_db_controller \ - import DBCapabilityTypeController -from graffiti.api.controllers.v1.captype_file_controller \ - import FileCapabilityTypeController -from graffiti.api.controllers.v1.captype_mem_controller \ - import MemCapabilityTypeController +from graffiti.api.model.v1.dao.captype_db_dao \ + import DBCapabilityTypeDAO +from graffiti.api.model.v1.dao.captype_file_dao \ + import FileCapabilityTypeDAO +from graffiti.api.model.v1.dao.captype_mem_dao \ + import MemCapabilityTypeDAO -class CapTypeControllerFactory(object): +class CapabilityTypeDAOFactory(object): def __init__(self, **kwargs): - super(CapTypeControllerFactory, self).__init__(**kwargs) + super(CapabilityTypeDAOFactory, self).__init__(**kwargs) @staticmethod - def create(controller_type, **kwargs): - if controller_type.lower() == 'memory': + def create(dao_type, **kwargs): + if dao_type.lower() == 'memory': print "Dictionary persistence = memory" - return MemCapabilityTypeController(**kwargs) - elif controller_type.lower() == "db": + return MemCapabilityTypeDAO(**kwargs) + elif dao_type.lower() == "db": print "Dictionary persistence = db" - return DBCapabilityTypeController(**kwargs) - elif controller_type.lower() == "file": + return DBCapabilityTypeDAO(**kwargs) + elif dao_type.lower() == "file": print "Dictionary persistence = File" - return FileCapabilityTypeController(**kwargs) + return FileCapabilityTypeDAO(**kwargs) return None diff --git a/graffiti/api/controllers/v1/captype_db_controller.py b/graffiti/api/model/v1/dao/captype_db_dao.py similarity index 93% rename from graffiti/api/controllers/v1/captype_db_controller.py rename to graffiti/api/model/v1/dao/captype_db_dao.py index 222d278..b3f62ae 100644 --- a/graffiti/api/controllers/v1/captype_db_controller.py +++ b/graffiti/api/model/v1/dao/captype_db_dao.py @@ -13,8 +13,8 @@ # See the License for the specific language governing permissions and # limitations under the License. -from captype_controller import CapabilityTypeControllerBase from graffiti.api.model.v1.capability_type import CapabilityType +from graffiti.api.model.v1.dao.captype_dao import CapabilityTypeDAOBase from graffiti.api.model.v1.derived_type import DerivedType from graffiti.api.model.v1.property_type import PropertyType from graffiti.db import api as dbapi @@ -23,11 +23,14 @@ from wsme.rest.json import fromjson from wsme.rest.json import tojson -class DBCapabilityTypeController(CapabilityTypeControllerBase): +class DBCapabilityTypeDAO(CapabilityTypeDAOBase): def __init__(self, **kwargs): - super(DBCapabilityTypeController, self).__init__(**kwargs) - self._type = 'DBCapabilityTypeController' + super(DBCapabilityTypeDAO, self).__init__(**kwargs) + self._type = "DBCapabilityTypeDAO" + + def get_type(self): + return self._type def _to_model(self, db_captype): model_captype = CapabilityType.to_model(db_captype) diff --git a/graffiti/api/controllers/v1/captype_file_controller.py b/graffiti/api/model/v1/dao/captype_file_dao.py similarity index 91% rename from graffiti/api/controllers/v1/captype_file_controller.py rename to graffiti/api/model/v1/dao/captype_file_dao.py index b3bc47e..66825eb 100644 --- a/graffiti/api/controllers/v1/captype_file_controller.py +++ b/graffiti/api/model/v1/dao/captype_file_dao.py @@ -14,23 +14,26 @@ # limitations under the License. -from captype_controller import CapabilityTypeControllerBase from graffiti.api.model.v1.capability_type import CapabilityType +from graffiti.api.model.v1.dao.captype_dao import CapabilityTypeDAOBase import json from oslo.config import cfg from wsme.rest.json import fromjson from wsme.rest.json import tojson -class FileCapabilityTypeController(CapabilityTypeControllerBase): +class FileCapabilityTypeDAO(CapabilityTypeDAOBase): def __init__(self, **kwargs): - super(FileCapabilityTypeController, self).__init__(**kwargs) - self._type = 'FileCapabilityTypeController' + super(FileCapabilityTypeDAO, self).__init__(**kwargs) self._graffiti_folder = cfg.CONF.FILE_PERSISTENCE.dictionary_folder self._filename = "dictionary.json" self._dictionaryfile = self._graffiti_folder + self._filename self._capability_types = self.__file_to_memory() + self._type = "FileCapabilityTypeDAO" + + def get_type(self): + return self._type def get_capability_type(self, name, namespace): id = namespace + ":" + name diff --git a/graffiti/api/controllers/v1/captype_mem_controller.py b/graffiti/api/model/v1/dao/captype_mem_dao.py similarity index 85% rename from graffiti/api/controllers/v1/captype_mem_controller.py rename to graffiti/api/model/v1/dao/captype_mem_dao.py index 180ddb7..fcd5fd1 100644 --- a/graffiti/api/controllers/v1/captype_mem_controller.py +++ b/graffiti/api/model/v1/dao/captype_mem_dao.py @@ -13,15 +13,18 @@ # See the License for the specific language governing permissions and # limitations under the License. -from captype_controller import CapabilityTypeControllerBase +from graffiti.api.model.v1.dao.captype_dao import CapabilityTypeDAOBase -class MemCapabilityTypeController(CapabilityTypeControllerBase): +class MemCapabilityTypeDAO(CapabilityTypeDAOBase): def __init__(self, **kwargs): - super(MemCapabilityTypeController, self).__init__(**kwargs) - self._type = 'MemCapabilityTypeController' + super(MemCapabilityTypeDAO, self).__init__(**kwargs) self._capability_types = {} + self._type = "MemCapabilityTypeDAO" + + def get_type(self): + return self._type def get_capability_type(self, name, namespace): id = namespace + ":" + name diff --git a/graffiti/api/controllers/v1/ns_controller.py b/graffiti/api/model/v1/dao/ns_dao.py similarity index 89% rename from graffiti/api/controllers/v1/ns_controller.py rename to graffiti/api/model/v1/dao/ns_dao.py index 1fab29b..6345b21 100755 --- a/graffiti/api/controllers/v1/ns_controller.py +++ b/graffiti/api/model/v1/dao/ns_dao.py @@ -14,18 +14,18 @@ # limitations under the License. -class NSTypeControllerBase(object): +class NSDAOBase(object): def __init__(self, **kwargs): - super(NSTypeControllerBase, self).__init__(**kwargs) - self._type = 'None' - - def get_namespace(self, namespace_name): - return None + super(NSDAOBase, self).__init__(**kwargs) + self._type = "NSDAOBase" def get_type(self): return self._type + def get_namespace(self, namespace_name): + return None + def find_namespaces(self, query_string): return [] diff --git a/graffiti/api/model/v1/dao/ns_dao_factory.py b/graffiti/api/model/v1/dao/ns_dao_factory.py new file mode 100755 index 0000000..abad8b3 --- /dev/null +++ b/graffiti/api/model/v1/dao/ns_dao_factory.py @@ -0,0 +1,57 @@ +# Copyright (c) 2014 Hewlett-Packard Development Company, L.P. +# +# 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. + + +from graffiti.api.model.v1.dao.ns_db_dao \ + import DBNSDAO +from graffiti.api.model.v1.dao.ns_file_dao \ + import FileNSDAO +from graffiti.api.model.v1.dao.ns_mem_dao \ + import MemNSDAO +from oslo.config import cfg + + +class NSDAOFactory(object): + + __dao = None + __dao_type = cfg.CONF.DEFAULT.persistence_type + + def __init__(self, **kwargs): + super(NSDAOFactory, self).__init__(**kwargs) + + @staticmethod + def create(dao_type, **kwargs): + if dao_type.lower() == 'memory': + print "Namespace persistence = memory" + NSDAOFactory.__dao = MemNSDAO(**kwargs) + return NSDAOFactory.__dao + elif dao_type.lower() == "db": + print "Namespace persistence = db" + NSDAOFactory.__dao = DBNSDAO(**kwargs) + return NSDAOFactory.__dao + elif dao_type.lower() == "file": + print "Namespace persistence = File" + NSDAOFactory.__dao = FileNSDAO(**kwargs) + return NSDAOFactory.__dao + + return None + + @staticmethod + def get(): + if NSDAOFactory.__dao: + return NSDAOFactory.__dao + else: + return NSDAOFactory.create( + NSDAOFactory.__dao_type) diff --git a/graffiti/api/controllers/v1/ns_db_controller.py b/graffiti/api/model/v1/dao/ns_db_dao.py similarity index 91% rename from graffiti/api/controllers/v1/ns_db_controller.py rename to graffiti/api/model/v1/dao/ns_db_dao.py index 98213e6..265a3ab 100755 --- a/graffiti/api/controllers/v1/ns_db_controller.py +++ b/graffiti/api/model/v1/dao/ns_db_dao.py @@ -13,16 +13,16 @@ # See the License for the specific language governing permissions and # limitations under the License. +from graffiti.api.model.v1.dao.ns_dao import NSDAOBase from graffiti.api.model.v1.namespace import Namespace from graffiti.db import api as dbapi -from ns_controller import NSTypeControllerBase -class DBNSController(NSTypeControllerBase): +class DBNSDAO(NSDAOBase): def __init__(self, **kwargs): - super(DBNSController, self).__init__(**kwargs) - self._type = 'DBNSController' + super(DBNSDAO, self).__init__(**kwargs) + self._type = "DBNSDAO" def get_type(self): return self._type diff --git a/graffiti/api/controllers/v1/ns_file_controller.py b/graffiti/api/model/v1/dao/ns_file_dao.py similarity index 92% rename from graffiti/api/controllers/v1/ns_file_controller.py rename to graffiti/api/model/v1/dao/ns_file_dao.py index 8ff5d6d..def060d 100755 --- a/graffiti/api/controllers/v1/ns_file_controller.py +++ b/graffiti/api/model/v1/dao/ns_file_dao.py @@ -14,23 +14,29 @@ # limitations under the License. +from graffiti.api.model.v1.dao.ns_dao import NSDAOBase from graffiti.api.model.v1.namespace import Namespace + import json -from ns_controller import NSTypeControllerBase + from oslo.config import cfg + from wsme.rest.json import fromjson from wsme.rest.json import tojson -class FileNSController(NSTypeControllerBase): +class FileNSDAO(NSDAOBase): def __init__(self, **kwargs): - super(FileNSController, self).__init__(**kwargs) - self._type = 'FileNSController' + super(FileNSDAO, self).__init__(**kwargs) self._graffiti_folder = cfg.CONF.FILE_PERSISTENCE.dictionary_folder self._filename = "namespaces.json" self._namespacefile = self._graffiti_folder + self._filename self._namespaces = self.__file_to_memory() + self._type = "FileNSDAO" + + def get_type(self): + return self._type def get_namespace(self, namespace_name): return self._namespaces[namespace_name] diff --git a/graffiti/api/controllers/v1/ns_mem_controller.py b/graffiti/api/model/v1/dao/ns_mem_dao.py similarity index 88% rename from graffiti/api/controllers/v1/ns_mem_controller.py rename to graffiti/api/model/v1/dao/ns_mem_dao.py index e6c641c..7ffe296 100755 --- a/graffiti/api/controllers/v1/ns_mem_controller.py +++ b/graffiti/api/model/v1/dao/ns_mem_dao.py @@ -13,15 +13,15 @@ # See the License for the specific language governing permissions and # limitations under the License. -from ns_controller import NSTypeControllerBase +from graffiti.api.model.v1.dao.ns_dao import NSDAOBase -class MemNSController(NSTypeControllerBase): +class MemNSDAO(NSDAOBase): def __init__(self, **kwargs): - super(MemNSController, self).__init__(**kwargs) - self._type = 'MemNSController' + super(MemNSDAO, self).__init__(**kwargs) self._namespaces = {} + self._type = "MemNSDAO" def get_type(self): return self._type diff --git a/graffiti/api/model/v1/resource_dao_factory.py b/graffiti/api/model/v1/dao/resource_dao.py similarity index 64% rename from graffiti/api/model/v1/resource_dao_factory.py rename to graffiti/api/model/v1/dao/resource_dao.py index f2aac0f..a3d8dfc 100644 --- a/graffiti/api/model/v1/resource_dao_factory.py +++ b/graffiti/api/model/v1/dao/resource_dao.py @@ -13,18 +13,24 @@ # See the License for the specific language governing permissions and # limitations under the License. -from graffiti.api.model.v1.resource_dao import LocalResourceDAO - -class ResourceDAOFactory(object): +class ResourceDAOBase(object): def __init__(self, **kwargs): - super(ResourceDAOFactory, self).__init__(**kwargs) + super(ResourceDAOBase, self).__init__(**kwargs) + self._type = "ResourceDAOBase" - @staticmethod - def create(dao_type, **kwargs): - if dao_type.lower() == 'memory': - print "Directory persistence = memory" - return LocalResourceDAO(**kwargs) + def get_type(self): + return self._type + def get_resource(self, id): return None + + def find_resources(self, query_string): + return [] + + def set_resource(self, id, resource): + pass + + def delete_resource(self, id): + pass diff --git a/graffiti/api/model/v1/dao/resource_dao_factory.py b/graffiti/api/model/v1/dao/resource_dao_factory.py new file mode 100644 index 0000000..1e6b934 --- /dev/null +++ b/graffiti/api/model/v1/dao/resource_dao_factory.py @@ -0,0 +1,38 @@ +# Copyright (c) 2014 Hewlett-Packard Development Company, L.P. +# +# 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. + +from graffiti.api.model.v1.dao.resource_db_dao import DBResourceDAO +from graffiti.api.model.v1.dao.resource_file_dao import FileResourceDAO +from graffiti.api.model.v1.dao.resource_mem_dao import MemResourceDAO + + +class ResourceDAOFactory(object): + + def __init__(self, **kwargs): + super(ResourceDAOFactory, self).__init__(**kwargs) + + @staticmethod + def create(dao_type, **kwargs): + if dao_type.lower() == 'memory': + print "Directory persistence = memory" + return MemResourceDAO(**kwargs) + elif dao_type.lower() == 'file': + print "Directory persistence = file" + return FileResourceDAO(**kwargs) + elif dao_type.lower() == 'db': + print "Directory persistence = db" + return DBResourceDAO(**kwargs) + + return None diff --git a/graffiti/api/model/v1/dao/resource_db_dao.py b/graffiti/api/model/v1/dao/resource_db_dao.py new file mode 100644 index 0000000..48d6666 --- /dev/null +++ b/graffiti/api/model/v1/dao/resource_db_dao.py @@ -0,0 +1,40 @@ +# Copyright (c) 2014 Hewlett-Packard Development Company, L.P. +# +# 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. + + +from graffiti.api.model.v1.dao.resource_dao import ResourceDAOBase + + +#TODO(Lakshmi): Implement db persistence for resource +class DBResourceDAO(ResourceDAOBase): + + def __init__(self, **kwargs): + super(DBResourceDAO, self).__init__(**kwargs) + self._type = "DBResourceDAO" + + def get_type(self): + return self._type + + def get_resource(self, id): + pass + + def find_resources(self, query_string): + pass + + def set_resource(self, id=None, resource_definition=None): + pass + + def delete_resource(self, id): + pass diff --git a/graffiti/api/model/v1/dao/resource_file_dao.py b/graffiti/api/model/v1/dao/resource_file_dao.py new file mode 100644 index 0000000..d5534fe --- /dev/null +++ b/graffiti/api/model/v1/dao/resource_file_dao.py @@ -0,0 +1,42 @@ +# Copyright (c) 2014 Hewlett-Packard Development Company, L.P. +# +# 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. + + +from graffiti.api.model.v1.dao.resource_dao import ResourceDAOBase + + +class FileResourceDAO(ResourceDAOBase): + + def __init__(self, **kwargs): + super(FileResourceDAO, self).__init__(**kwargs) + self._type = "FileResourceDAO" + + def get_type(self): + return self._type + + def get_resource(self, id): + pass + + def find_resources(self, query_string): + pass + + def set_resource(self, id=None, resource_definition=None): + pass + + def delete_resource(self, id): + pass + + def _generate_id(self): + pass diff --git a/graffiti/api/model/v1/resource_dao.py b/graffiti/api/model/v1/dao/resource_mem_dao.py similarity index 65% rename from graffiti/api/model/v1/resource_dao.py rename to graffiti/api/model/v1/dao/resource_mem_dao.py index 64625dc..6267847 100644 --- a/graffiti/api/model/v1/resource_dao.py +++ b/graffiti/api/model/v1/dao/resource_mem_dao.py @@ -14,49 +14,36 @@ # limitations under the License. -class ResourceDAOBase(object): +from graffiti.api.model.v1.dao.resource_dao import ResourceDAOBase + + +class MemResourceDAO(ResourceDAOBase): def __init__(self, **kwargs): - super(ResourceDAOBase, self).__init__(**kwargs) - - self._type = 'None' - - def get_resource(self, id): - return None + super(MemResourceDAO, self).__init__(**kwargs) + self._resources = dict() + self._last_id = 0 + self._type = "MemResourceDAO" def get_type(self): return self._type - def find_resources(self, query_string): - return [] - - def set_resource(self, id=None, resource_definition=None): - pass - - -class LocalResourceDAO(ResourceDAOBase): - - def __init__(self, **kwargs): - super(LocalResourceDAO, self).__init__(**kwargs) - - self._type = 'LocalResourceDAO' - - self._resources = dict() - self._last_id = 0 - def get_resource(self, id): return self._resources[id] def find_resources(self, query_string): return self._resources - def set_resource(self, id=None, resource_definition=None): + def set_resource(self, id=None, resource=None): if not id: id = self._generate_id() - self._resources[id] = resource_definition + self._resources[id] = resource return id + def delete_resource(self, id): + pass + def _generate_id(self): return_value = self._last_id self._last_id += 1 diff --git a/graffiti/api/tests/test_controller_v1.py b/graffiti/api/tests/test_controller_v1.py index 404d306..a980c0d 100644 --- a/graffiti/api/tests/test_controller_v1.py +++ b/graffiti/api/tests/test_controller_v1.py @@ -25,11 +25,11 @@ from graffiti.api.tests import base from graffiti.api.controllers.root import RootController from graffiti.api.controllers.versions import V1Controller -from graffiti.api.controllers.v1.captype_controller_factory \ - import CapTypeControllerFactory -from graffiti.api.controllers.v1.ns_controller_factory \ - import NSControllerFactory -from graffiti.api.model.v1.resource_dao_factory \ +from graffiti.api.model.v1.dao.captype_dao_factory \ + import CapabilityTypeDAOFactory +from graffiti.api.model.v1.dao.ns_dao_factory \ + import NSDAOFactory +from graffiti.api.model.v1.dao.resource_dao_factory \ import ResourceDAOFactory @@ -44,42 +44,50 @@ class TestControllerV1(base.TestCase): self.assertIn(hasattr(v1, 'namespace'), [True]) def test_v1_namespace_controller_factory__memory(self): - rc = NSControllerFactory.create('memory') - self.assertEquals(rc.get_type(), 'MemNSController') + rc = NSDAOFactory.create('memory') + self.assertEquals(rc.get_type(), 'MemNSDAO') # TODO(Lakshmi): Create folder before any tests run # def test_v1_namespace_controller_factory__file(self): # rc = NSControllerFactory.create('file') - # self.assertEquals(rc.get_type(), 'FileNSController') + # self.assertEquals(rc.get_type(), 'FileNSDAO') def test_v1_namespace_controller_factory__db(self): - rc = NSControllerFactory.create('db') - self.assertEquals(rc.get_type(), 'DBNSController') + rc = NSDAOFactory.create('db') + self.assertEquals(rc.get_type(), 'DBNSDAO') def test_v1_capability_type_exists(self): v1 = V1Controller() self.assertIn(hasattr(v1, 'capability_type'), [True]) - def test_v1_capability_type_controller_factory__memory(self): - rc = CapTypeControllerFactory.create('memory') - self.assertEquals(rc.get_type(), 'MemCapabilityTypeController') + def test_v1_capability_type_dao_factory__memory(self): + rc = CapabilityTypeDAOFactory.create('memory') + self.assertEquals(rc.get_type(), 'MemCapabilityTypeDAO') # TODO(Lakshmi): Create folder before any tests run - # def test_v1_capability_type_controller_factory__file(self): - # rc = CapTypeControllerFactory.create('file') - # self.assertEquals(rc.get_type(), 'FileCapabilityTypeController') + # def test_v1_capability_type_dao_factory__file(self): + # rc = CapabilityTypeDAOFactory.create('file') + # self.assertEquals(rc.get_type(), 'FileCapabilityTypeDAO') - def test_v1_capability_type_controller_factory__db(self): - rc = CapTypeControllerFactory.create('db') - self.assertEquals(rc.get_type(), 'DBCapabilityTypeController') + def test_v1_capability_type_dao_factory__db(self): + rc = CapabilityTypeDAOFactory.create('db') + self.assertEquals(rc.get_type(), 'DBCapabilityTypeDAO') def test_v1_resource_exists(self): v1 = V1Controller() self.assertIn(hasattr(v1, 'resource'), [True]) - def test_v1_resource_controller_factory__local(self): + def test_v1_resource_dao_factory__memory(self): rc = ResourceDAOFactory.create('memory') - self.assertEquals(rc.get_type(), 'LocalResourceDAO') + self.assertEquals(rc.get_type(), 'MemResourceDAO') + + def test_v1_resource_dao_factory__file(self): + rc = ResourceDAOFactory.create('file') + self.assertEquals(rc.get_type(), 'FileResourceDAO') + + def test_v1_resource_dao_factory__db(self): + rc = ResourceDAOFactory.create('db') + self.assertEquals(rc.get_type(), 'DBResourceDAO') def test_v1_resource_controller_factory__unknown(self): rc = ResourceDAOFactory.create('invalid_controller') diff --git a/graffiti/common/exception.py b/graffiti/common/exception.py index 46e55c0..9bad58e 100644 --- a/graffiti/common/exception.py +++ b/graffiti/common/exception.py @@ -13,19 +13,25 @@ # See the License for the specific language governing permissions and # limitations under the License. +import sys + from graffiti.openstack.common.gettextutils import _ +from graffiti.openstack.common.log import logging + +_FATAL_EXCEPTION_FORMAT_ERRORS = False + +logger = logging.getLogger(__name__) class GraffitiException(Exception): - """Base Exception for the project + """Base Graffiti Exception To correctly use this class, inherit from it and define - the 'message' property. - That message will get printf'd + a 'msg_fmt' property. That msg_fmt will get printf'd with the keyword arguments provided to the constructor. - """ - message = _("An unknown exception occurred") + """ + message = _("An unknown exception occurred.") def __str__(self): return self.message @@ -34,34 +40,48 @@ class GraffitiException(Exception): self.kwargs = kwargs try: - message = self.message % kwargs + self.message = self.msg_fmt % kwargs except KeyError: - #TODO(Any): print to log - pass + exc_info = sys.exc_info() + #kwargs doesn't match a variable in the message + #log the issue and the kwargs + logger.exception(_('Exception in string format operation')) + for name, value in kwargs.iteritems(): + logger.error("%s: %s" % (name, value)) - super(GraffitiException, self).__init__(message) + if _FATAL_EXCEPTION_FORMAT_ERRORS: + raise exc_info[0], exc_info[1], exc_info[2] + + #def __str__(self): + # return unicode(self.message).encode('UTF-8') + + def __unicode__(self): + return unicode(self.message) + + def __deepcopy__(self, memo): + return self.__class__(**self.kwargs) class NotFound(GraffitiException): - message = _("Object not found") + msg_fmt = _("Object not found") class DuplicateEntry(GraffitiException): - message = _("Database object already exists") + msg_fmt = _("Database object already exists") class DriverNotFound(NotFound): - message = _("Failed to load driver %(driver_name)s.") + msg_fmt = _("Failed to load driver %(driver_name)s.") class DriverLoadError(GraffitiException): - message = _("Driver %(driver)s could not be loaded. Reason: %(reason)s.") + msg_fmt = _("Driver %(driver)s could not be loaded. Reason: %(reason)s.") class MethodNotSupported(GraffitiException): - message = _("Method %(method)s is not supported by this driver") + msg_fmt = _("Method %(method)s is not supported by this driver") class DriverNotFoundForResourceType(NotFound): - message = _("Cannot find a registered driver for the resource " + msg_fmt = _("Cannot find a registered driver for the resource " "type %(resource_type)s") diff --git a/graffiti/drivers/modules/glance.py b/graffiti/drivers/modules/glance.py index bdf4756..1820e53 100644 --- a/graffiti/drivers/modules/glance.py +++ b/graffiti/drivers/modules/glance.py @@ -64,7 +64,6 @@ class GlanceResourceDriver(base.ResourceInterface): # replace if check with pattern matching if key.count(self.separator) == 2: (namespace, capability_type, prop_name) = key.split(".") - image_properties = [] image_property = Property() image_property.name = prop_name image_property.value = glance_image_properties[key] @@ -77,13 +76,12 @@ class GlanceResourceDriver(base.ResourceInterface): if not image_capability: image_capability = Capability() + image_capability.properties = [] image_resource.capabilities.append(image_capability) image_capability.capability_type_namespace = namespace image_capability.capability_type = capability_type - image_properties.append(image_property) - - image_capability.properties = image_properties + image_capability.properties.append(image_property) return image_resource @@ -155,10 +153,16 @@ class GlanceResourceDriver(base.ResourceInterface): password=cfg.CONF.keystone.password, tenant_name=cfg.CONF.keystone.tenant_name ) - self.__endpoint_list = keystone.endpoints.list() - for endpoint in self.__endpoint_list: - if endpoint.id == endpoint_id: - glance_public_url = endpoint.publicurl + + glance_public_url = None + for entry in keystone.service_catalog.catalog.get('serviceCatalog'): + for endpoint in entry['endpoints']: + if endpoint['id'] == endpoint_id: + glance_public_url = endpoint['publicURL'] + break + if glance_public_url: + break + glance_client = Client( '1', endpoint=glance_public_url, diff --git a/graffiti/drivers/modules/local.py b/graffiti/drivers/modules/local.py index 3f09474..f0bfd23 100644 --- a/graffiti/drivers/modules/local.py +++ b/graffiti/drivers/modules/local.py @@ -13,7 +13,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -from graffiti.api.model.v1.resource_dao_factory import \ +from graffiti.api.model.v1.dao.resource_dao_factory import \ ResourceDAOFactory from graffiti.drivers import base @@ -26,6 +26,7 @@ class LocalResourceDriver(base.ResourceInterface): def __init__(self): super(LocalResourceDriver, self).__init__() persistence_type = cfg.CONF.DEFAULT.persistence_type + self._resource_dao = ResourceDAOFactory.create(persistence_type) def get_resource(self, resource_id, auth_token, endpoint_id=None, @@ -66,7 +67,7 @@ class LocalResourceDriver(base.ResourceInterface): """ id = resource.id if hasattr(resource, 'id') else None - self._resource_dao.set_resource(id, resource_definition=resource) + self._resource_dao.set_resource(id, resource=resource) return resource @@ -81,7 +82,7 @@ class LocalResourceDriver(base.ResourceInterface): """ self._resource_dao.set_resource( resource_id, - resource_definition=resource + resource=resource ) return resource