Add Service datamodel and Service handler
Moved the service datamodel to a separate file and added service handler class. Also modified the service controller and unit test. This code is based on the new API worker architecture. Change-Id: Icf7997f91439b6a26e4792438fc01552e4348420
This commit is contained in:
parent
b6b8de46fb
commit
2a5b49f24b
|
@ -0,0 +1,61 @@
|
|||
# Copyright 2014 - Rackspace
|
||||
#
|
||||
# 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 wsme import types as wtypes
|
||||
|
||||
from solum.api.controllers.v1.datamodel import types as api_types
|
||||
|
||||
|
||||
class Service(api_types.Base):
|
||||
"""The Service resource represents a networked service.
|
||||
|
||||
You may create Component resources that refer to
|
||||
Service resources. A Component represents an instance of a Service.
|
||||
Your application connects to such a Component using a network protocol.
|
||||
For example, the Platform may offer a default Service named "mysql".
|
||||
You may create multiple Component resources that reference different
|
||||
instances of the "mysql" service. Each Component may be a multi-tenant
|
||||
instance of a MySQL database (perhaps a logical database) service offered
|
||||
by the Platform for a given Assembly.
|
||||
"""
|
||||
|
||||
read_only = bool
|
||||
"""The service is read only when this value is true."""
|
||||
|
||||
service_type = wtypes.text
|
||||
"""Type of service. Example: language_pack or db::mysql"""
|
||||
|
||||
@classmethod
|
||||
def sample(cls):
|
||||
return cls(uri='http://example.com/v1/language_packs/java1.4',
|
||||
name='language-pack',
|
||||
type='service',
|
||||
service_type='language_pack',
|
||||
description='A language pack service',
|
||||
project_id='1dae5a09ef2b4d8cbf3594b0eb4f6b94',
|
||||
user_id='55f41cf46df74320b9486a35f5d28a11',
|
||||
tags=['group_xyz'],
|
||||
read_only=False)
|
||||
|
||||
@classmethod
|
||||
def sample1(cls):
|
||||
return cls(uri='http://example.com/v1/database/mysql',
|
||||
name='database',
|
||||
type='service',
|
||||
service_type='db::mysql',
|
||||
description='A mysql service',
|
||||
project_id='1dae5a09ef2b4d8cbf3594b0eb4f6b94',
|
||||
user_id='55f41cf46df74320b9486a35f5d28a11',
|
||||
tags=['group_xyz'],
|
||||
read_only=False)
|
|
@ -17,38 +17,12 @@ import wsme
|
|||
from wsme import types as wtypes
|
||||
import wsmeext.pecan as wsme_pecan
|
||||
|
||||
from solum.api.controllers.v1.datamodel import types as api_types
|
||||
from solum.api.controllers.v1.datamodel import service
|
||||
from solum.api.handlers import service_handler
|
||||
from solum.common import exception
|
||||
from solum.openstack.common.gettextutils import _
|
||||
|
||||
|
||||
class Service(api_types.Base):
|
||||
"""The Service resource represents a networked service.
|
||||
|
||||
You may create Component resources that refer to
|
||||
Service resources. A Component represents an instance of a Service.
|
||||
Your application connects to such a Component using a network protocol.
|
||||
For example, the Platform may offer a default Service named "mysql".
|
||||
You may create multiple Component resources that reference different
|
||||
instances of the "mysql" service. Each Component may be a multi-tenant
|
||||
instance of a MySQL database (perhaps a logical database) service offered
|
||||
by the Platform for a given Assembly.
|
||||
"""
|
||||
|
||||
read_only = bool
|
||||
"The service is read only."
|
||||
|
||||
@classmethod
|
||||
def sample(cls):
|
||||
return cls(uri='http://example.com/v1/services/mysql',
|
||||
name='mysql',
|
||||
type='service',
|
||||
description='A mysql service',
|
||||
project_id='1dae5a09ef2b4d8cbf3594b0eb4f6b94',
|
||||
user_id='55f41cf46df74320b9486a35f5d28a11',
|
||||
tags=['group_xyz'],
|
||||
read_only=False)
|
||||
|
||||
|
||||
class ServiceController(rest.RestController):
|
||||
"""Manages operations on a single service."""
|
||||
|
||||
|
@ -56,14 +30,17 @@ class ServiceController(rest.RestController):
|
|||
pecan.request.context['service_id'] = service_id
|
||||
self._id = service_id
|
||||
|
||||
@wsme_pecan.wsexpose(Service, wtypes.text)
|
||||
@wsme_pecan.wsexpose(service.Service, wtypes.text)
|
||||
def get(self):
|
||||
"""Return this service."""
|
||||
error = _("Not implemented")
|
||||
pecan.response.translatable_error = error
|
||||
raise wsme.exc.ClientSideError(six.text_type(error))
|
||||
try:
|
||||
handler = service_handler.ServiceHandler()
|
||||
return handler.get(self._id)
|
||||
except exception.SolumException as excp:
|
||||
pecan.response.translatable_error = excp
|
||||
raise wsme.exc.ClientSideError(six.text_type(excp), excp.code)
|
||||
|
||||
@wsme_pecan.wsexpose(Service, wtypes.text, body=Service)
|
||||
@wsme_pecan.wsexpose(service.Service, wtypes.text, body=service.Service)
|
||||
def put(self, data):
|
||||
"""Modify this service."""
|
||||
error = _("Not implemented")
|
||||
|
@ -87,14 +64,15 @@ class ServicesController(rest.RestController):
|
|||
remainder = remainder[:-1]
|
||||
return ServiceController(service_id), remainder
|
||||
|
||||
@wsme_pecan.wsexpose(Service, body=Service, status_code=201)
|
||||
@wsme_pecan.wsexpose(service.Service, body=service.Service,
|
||||
status_code=201)
|
||||
def post(self, data):
|
||||
"""Create a new service."""
|
||||
error = _("Not implemented")
|
||||
pecan.response.translatable_error = error
|
||||
raise wsme.exc.ClientSideError(six.text_type(error))
|
||||
|
||||
@wsme_pecan.wsexpose([Service])
|
||||
@wsme_pecan.wsexpose([service.Service])
|
||||
def get_all(self):
|
||||
"""Return the collection of services."""
|
||||
return []
|
||||
|
|
|
@ -0,0 +1,27 @@
|
|||
# Copyright 2014 - Rackspace
|
||||
#
|
||||
# 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 solum.api.controllers.v1.datamodel import service
|
||||
from solum.api.handlers import handler
|
||||
|
||||
|
||||
class ServiceHandler(handler.Handler):
|
||||
"""Fulfills a request on the service resource."""
|
||||
|
||||
def __init__(self):
|
||||
pass
|
||||
|
||||
def get(self, id):
|
||||
response = service.Service.sample()
|
||||
return response
|
|
@ -25,7 +25,7 @@ class TestServiceController(base.BaseTestCase):
|
|||
def test_service_get(self, resp_mock, request_mock):
|
||||
obj = service.ServiceController('test_id')
|
||||
obj.get()
|
||||
self.assertEqual(400, resp_mock.status)
|
||||
self.assertEqual(200, resp_mock.status)
|
||||
|
||||
def test_service_put(self, resp_mock, request_mock):
|
||||
obj = service.ServiceController('test_id')
|
||||
|
|
Loading…
Reference in New Issue