Merge "Use the abstract factory pattern for creating resource controllers"

This commit is contained in:
Jenkins 2014-03-19 19:41:02 +00:00 committed by Gerrit Code Review
commit cd9adfe81d
4 changed files with 46 additions and 3 deletions

View File

@ -20,7 +20,8 @@ from wsmeext.pecan import wsexpose
from graffiti.api.model.v1.resource import Resource
from graffiti.api.model.v1.resource_controller import LocalResourceController
from graffiti.api.model.v1.resource_controller_factory import \
ResourceControllerFactory
from graffiti.common.utils import _
@ -52,8 +53,7 @@ class ResourceController(RestController):
controller_type = cfg.CONF.resource_controller.type
controller_type = controller_type if controller_type else 'Local'
# TODO(lakshmi): Load the controller here
_controller = LocalResourceController()
_controller = ResourceControllerFactory.create(controller_type)
return _controller

View File

@ -24,6 +24,9 @@ class ResourceControllerBase(object):
def get_resource(self, id):
return None
def get_type(self):
return self._type
def find_resources(self, query_string):
return []

View File

@ -0,0 +1,29 @@
# 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.resource_controller import LocalResourceController
class ResourceControllerFactory(object):
def __init__(self, **kwargs):
super(ResourceControllerFactory, self).__init__(**kwargs)
@staticmethod
def create(controller_type, **kwargs):
if controller_type.lower() == 'local':
return LocalResourceController(**kwargs)
return None

View File

@ -25,6 +25,9 @@ from graffiti.api.tests import base
from graffiti.api.controllers.root import RootController
from graffiti.api.controllers.versions import V1Controller
from graffiti.api.model.v1.resource_controller_factory \
import ResourceControllerFactory
class TestControllerV1(base.TestCase):
@ -35,3 +38,11 @@ class TestControllerV1(base.TestCase):
def test_v1_resource_exists(self):
v1 = V1Controller()
self.assertIn(hasattr(v1, 'resource'), [True])
def test_v1_resource_controller_factory__local(self):
rc = ResourceControllerFactory.create('local')
self.assertEquals(rc.get_type(), 'LocalResourceController')
def test_v1_resource_controller_factory__unknown(self):
rc = ResourceControllerFactory.create('invalid_controller')
self.assertTrue(rc is None)