The RESTAPI of resource Restore

Change-Id: I5441cabb2b947c4cd59c45adb31ffb9af2292f7d
Closes-Bug: #1549324
This commit is contained in:
chenying 2016-02-25 01:28:36 +08:00 committed by Eran Gampel
parent 800ee3a744
commit 62d11cc0cc
9 changed files with 488 additions and 7 deletions

View File

@ -9,5 +9,10 @@
"plan:update": "rule:admin_or_owner",
"plan:delete": "rule:admin_or_owner",
"plan:get": "rule:admin_or_owner",
"plan:get_all": "rule:admin_or_owner"
"plan:get_all": "rule:admin_or_owner",
"restore:create": "rule:admin_or_owner",
"restore:update": "rule:admin_or_owner",
"restore:get": "rule:admin_or_owner",
"restore:get_all": "rule:admin_or_owner"
}

317
smaug/api/v1/restores.py Normal file
View File

@ -0,0 +1,317 @@
# 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.
"""The restores api."""
from oslo_config import cfg
from oslo_log import log as logging
from oslo_serialization import jsonutils
from oslo_utils import uuidutils
from webob import exc
import smaug
from smaug.api import common
from smaug.api.openstack import wsgi
from smaug import exception
from smaug.i18n import _, _LI
from smaug import objects
from smaug.objects import base as objects_base
import smaug.policy
from smaug.services.protection import api as protection_api
from smaug import utils
import six
query_restore_filters_opt = cfg.ListOpt(
'query_restore_filters',
default=['status'],
help="Restore filter options which "
"non-admin user could use to "
"query restores. Default values "
"are: ['status']")
CONF = cfg.CONF
CONF.register_opt(query_restore_filters_opt)
LOG = logging.getLogger(__name__)
def check_policy(context, action, target_obj=None):
target = {
'project_id': context.project_id,
'user_id': context.user_id,
}
if isinstance(target_obj, objects_base.SmaugObject):
# Turn object into dict so target.update can work
target.update(
target_obj.obj_to_primitive() or {})
else:
target.update(target_obj or {})
_action = 'restore:%s' % action
smaug.policy.enforce(context, _action, target)
class RestoreViewBuilder(common.ViewBuilder):
"""Model a server API response as a python dictionary."""
_collection_name = "restores"
def __init__(self):
"""Initialize view builder."""
super(RestoreViewBuilder, self).__init__()
def detail(self, request, restore):
"""Detailed view of a single restore."""
restore_ref = {
'restore': {
'id': restore.get('id'),
'project_id': restore.get('project_id'),
'provider_id': restore.get('provider_id'),
'checkpoint_id': restore.get('checkpoint_id'),
'restore_target': restore.get('restore_target'),
'parameters': restore.get('parameters'),
'status': restore.get('status'),
}
}
return restore_ref
def detail_list(self, request, restores, restore_count=None):
"""Detailed view of a list of restores."""
return self._list_view(self.detail, request, restores,
restore_count,
self._collection_name)
def _list_view(self, func, request, restores, restore_count,
coll_name=_collection_name):
"""Provide a view for a list of restores.
:param func: Function used to format the restore data
:param request: API request
:param restores: List of restores in dictionary format
:param restore_count: Length of the original list of restores
:param coll_name: Name of collection, used to generate the next link
for a pagination query
:returns: restore data in dictionary format
"""
restores_list = [func(request, restore)['restore']
for restore in restores]
restores_links = self._get_collection_links(request,
restores,
coll_name,
restore_count)
restores_dict = {
'restores': restores_list
}
if restores_links:
restores_dict['restores_links'] = restores_links
return restores_dict
class RestoresController(wsgi.Controller):
"""The Restores API controller for the OpenStack API."""
_view_builder_class = RestoreViewBuilder
def __init__(self):
self.protection_api = protection_api.API()
super(RestoresController, self).__init__()
def show(self, req, id):
"""Return data about the given restore."""
context = req.environ['smaug.context']
LOG.info(_LI("Show restore with id: %s"), id, context=context)
if not uuidutils.is_uuid_like(id):
msg = _("Invalid restore id provided.")
raise exc.HTTPBadRequest(explanation=msg)
try:
restore = self._restore_get(context, id)
except exception.RestoreNotFound as error:
raise exc.HTTPNotFound(explanation=error.msg)
LOG.info(_LI("Show restore request issued successfully."),
resource={'id': restore.id})
return self._view_builder.detail(req, restore)
def index(self, req):
"""Returns a list of restores, transformed through view builder."""
context = req.environ['smaug.context']
LOG.info(_LI("Show restore list"), context=context)
params = req.params.copy()
marker, limit, offset = common.get_pagination_params(params)
sort_keys, sort_dirs = common.get_sort_params(params)
filters = params
utils.remove_invalid_filter_options(
context,
filters,
self._get_restore_filter_options())
utils.check_filters(filters)
restores = self._get_all(context, marker, limit,
sort_keys=sort_keys,
sort_dirs=sort_dirs,
filters=filters,
offset=offset)
retval_restores = self._view_builder.detail_list(req, restores)
LOG.info(_LI("Show restore list request issued successfully."))
return retval_restores
def _get_all(self, context, marker=None, limit=None, sort_keys=None,
sort_dirs=None, filters=None, offset=None):
check_policy(context, 'get_all')
if filters is None:
filters = {}
all_tenants = utils.get_bool_param('all_tenants', filters)
try:
if limit is not None:
limit = int(limit)
if limit <= 0:
msg = _('limit param must be positive')
raise exception.InvalidInput(reason=msg)
except ValueError:
msg = _('limit param must be an integer')
raise exception.InvalidInput(reason=msg)
if filters:
LOG.debug("Searching by: %s.", six.text_type(filters))
if context.is_admin and all_tenants:
# Need to remove all_tenants to pass the filtering below.
del filters['all_tenants']
restores = objects.RestoreList.get_all(
context, marker, limit,
sort_keys=sort_keys,
sort_dirs=sort_dirs,
filters=filters,
offset=offset)
else:
restores = objects.RestoreList.get_all_by_project(
context, context.project_id, marker, limit,
sort_keys=sort_keys, sort_dirs=sort_dirs, filters=filters,
offset=offset)
LOG.info(_LI("Get all restores completed successfully."))
return restores
def _get_restore_filter_options(self):
"""Return restores search options allowed by non-admin."""
return CONF.query_restore_filters
def create(self, req, body):
"""Creates a new restore."""
if not self.is_valid_body(body, 'restore'):
raise exc.HTTPUnprocessableEntity()
LOG.debug('Create restore request body: %s', body)
context = req.environ['smaug.context']
check_policy(context, 'create')
restore = body['restore']
LOG.debug('Create restore request : %s', restore)
if not restore.get("provider_id"):
msg = _("provider_id must be provided when creating "
"a restore.")
raise exception.InvalidInput(reason=msg)
if not restore.get("checkpoint_id"):
msg = _("checkpoint_id must be provided when creating "
"a restore.")
raise exception.InvalidInput(reason=msg)
parameters = restore.get("parameters")
if not isinstance(parameters, dict):
msg = _("parameters must be a dict when creating"
" a restore.")
raise exception.InvalidInput(reason=msg)
restore_properties = {
'project_id': context.project_id,
'provider_id': restore.get('provider_id'),
'checkpoint_id': restore.get('checkpoint_id'),
'restore_target': restore.get('restore_target'),
'parameters': jsonutils.dumps(parameters),
'status': 'started',
}
restoreobj = objects.Restore(context=context,
**restore_properties)
restoreobj.create()
LOG.debug('call restore RPC : restoreobj:%s', restoreobj)
# call restore rpc API of protection service
result = self.protection_api.restore(context, restoreobj)
if result is True:
status_update = "success"
else:
status_update = "failed"
# update the status of restore
update_dict = {
"status": status_update
}
check_policy(context, 'update', restoreobj)
self._restore_update(context,
restoreobj.get("id"), update_dict)
restoreobj.update(update_dict)
retval = self._view_builder.detail(req, restoreobj)
return retval
def _restore_get(self, context, restore_id):
if not uuidutils.is_uuid_like(restore_id):
msg = _("Invalid restore id provided.")
raise exc.HTTPBadRequest(explanation=msg)
restore = objects.Restore.get_by_id(context, restore_id)
try:
check_policy(context, 'get', restore)
except exception.PolicyNotAuthorized:
# raise RestoreNotFound instead to make sure smaug behaves
# as it used to
raise exception.RestoreNotFound(restore_id=restore_id)
LOG.info(_LI("Restore info retrieved successfully."))
return restore
def _restore_update(self, context, restore_id, fields):
try:
restore = self._restore_get(context, restore_id)
except exception.RestoreNotFound as error:
raise exc.HTTPNotFound(explanation=error.msg)
if isinstance(restore, objects_base.SmaugObject):
restore.update(fields)
restore.save()
LOG.info(_LI("restore updated successfully."))
else:
msg = _("The parameter restore must be a object of "
"SmaugObject class.")
raise exception.InvalidInput(reason=msg)
def create_resource():
return wsgi.Resource(RestoresController())

View File

@ -12,6 +12,7 @@
from smaug.api.openstack import ProjectMapper
from smaug.api.v1 import plans
from smaug.api.v1 import restores
from smaug.api.v1 import scheduled_operations
from smaug.wsgi import common as wsgi_common
@ -23,11 +24,16 @@ class APIRouter(wsgi_common.Router):
def __init__(self, mapper):
plans_resources = plans.create_resource()
restores_resources = restores.create_resource()
scheduled_operation_resources = scheduled_operations.create_resource()
mapper.resource("plan", "plans",
controller=plans_resources,
collection={},
member={'action': 'POST'})
mapper.resource("restore", "restores",
controller=restores_resources,
collection={},
member={'action': 'POST'})
mapper.resource("scheduled_operation", "scheduled_operations",
controller=scheduled_operation_resources,
collection={'detail': 'GET'},

View File

@ -30,6 +30,7 @@ from smaug import context
from smaug import db
from smaug import exception
from smaug.i18n import _, _LE, _LI, _LW
from smaug.objects import base as objects_base
from smaug import rpc
from smaug import version
from smaug.wsgi import common as wsgi_common
@ -116,7 +117,8 @@ class Service(service.Service):
target = messaging.Target(topic=self.topic, server=self.host)
endpoints = [self.manager]
endpoints.extend(self.manager.additional_endpoints)
self.rpcserver = rpc.get_server(target, endpoints)
serializer = objects_base.SmaugObjectSerializer()
self.rpcserver = rpc.get_server(target, endpoints, serializer)
self.rpcserver.start()
self.manager.init_host_with_rpc()

View File

@ -31,3 +31,6 @@ class API(base.Base):
def __init__(self, db_driver=None):
self.protection_rpcapi = protection_rpcapi.ProtectionAPI()
super(API, self).__init__(db_driver)
def restore(self, context, restore):
return self.protection_rpcapi.restore(context, restore)

View File

@ -64,9 +64,13 @@ class ProtectionManager(manager.Manager):
# TODO(wangliuan)
pass
def restore(self, checkpoint, **kwargs):
def restore(self, context, restore=None):
LOG.info(_LI("Starting restore service:restore action"))
LOG.debug('restore :%s tpye:%s', restore,
type(restore))
# TODO(wangliuan)
pass
return True
def delete(self, plan):
# TODO(wangliuan)

View File

@ -19,6 +19,7 @@ Client side of the protection manager RPC API.
from oslo_config import cfg
import oslo_messaging as messaging
from smaug.objects import base as objects_base
from smaug import rpc
@ -39,4 +40,13 @@ class ProtectionAPI(object):
super(ProtectionAPI, self).__init__()
target = messaging.Target(topic=CONF.protection_topic,
version=self.RPC_API_VERSION)
self.client = rpc.get_client(target, version_cap=None)
serializer = objects_base.SmaugObjectSerializer()
self.client = rpc.get_client(target, version_cap=None,
serializer=serializer)
def restore(self, ctxt, restore=None):
cctxt = self.client.prepare(version='1.0')
return cctxt.call(
ctxt,
'restore',
restore=restore)

View File

@ -142,13 +142,13 @@ class PlanApiTest(base.TestCase):
def test_plan_delete(self, moak_plan_get):
req = fakes.HTTPRequest.blank('/v1/plans')
self.controller.\
show(req, '2a9ce1f3-cc1a-4516-9435-0ebb13caa398')
delete(req, '2a9ce1f3-cc1a-4516-9435-0ebb13caa398')
self.assertTrue(moak_plan_get.called)
def test_plan_delete_Invalid(self):
req = fakes.HTTPRequest.blank('/v1/plans/1')
self.assertRaises(
exc.HTTPBadRequest, self.controller.show,
exc.HTTPBadRequest, self.controller.delete,
req, "1")
@mock.patch(

View File

@ -0,0 +1,134 @@
# 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.
import mock
from oslo_config import cfg
from webob import exc
from smaug.api.v1 import restores
from smaug import context
from smaug import exception
from smaug.tests import base
from smaug.tests.unit.api import fakes
CONF = cfg.CONF
DEFAULT_PROJECT_ID = '39bb894794b741e982bd26144d2949f6'
DEFAULT_PROVIDER_ID = 'efc6a88b-9096-4bb6-8634-cda182a6e12a'
DEFAULT_CHECKPOINT_ID = '09edcbdc-d1c2-49c1-a212-122627b20968'
DEFAULT_RESTORE_TARGET = '192.168.1.2:35357/v2.0'
DEFAULT_PARAMETERS = {
"username": "admin"
}
DEFAULT_STATUS = 'started'
class RestoreApiTest(base.TestCase):
def setUp(self):
super(RestoreApiTest, self).setUp()
self.controller = restores.RestoresController()
self.ctxt = context.RequestContext('admin', 'fakeproject', True)
@mock.patch(
'smaug.services.protection.api.API.restore')
@mock.patch(
'smaug.api.v1.restores.'
'RestoresController._restore_update')
@mock.patch(
'smaug.objects.restore.Restore.create')
def test_restore_create(self, mock_restore_create,
mock_restore_update,
mock_rpc_restore):
restore = self._restore_in_request_body()
body = {"restore": restore}
req = fakes.HTTPRequest.blank('/v1/restores')
self.controller.create(req, body)
self.assertTrue(mock_restore_create.called)
self.assertTrue(mock_restore_update.called)
self.assertTrue(mock_rpc_restore.called)
def test_restore_create_InvalidBody(self):
restore = self._restore_in_request_body()
body = {"restorexx": restore}
req = fakes.HTTPRequest.blank('/v1/restores')
self.assertRaises(exc.HTTPUnprocessableEntity, self.controller.create,
req, body)
def test_restore_create_InvalidProviderId(self):
restore = self._restore_in_request_body(provider_id="")
body = {"restore": restore}
req = fakes.HTTPRequest.blank('/v1/restores')
self.assertRaises(exception.InvalidInput, self.controller.create,
req, body)
def test_restore_create_Invalidcheckpoint_id(self):
restore = self._restore_in_request_body(checkpoint_id="")
body = {"restore": restore}
req = fakes.HTTPRequest.blank('/v1/restores')
self.assertRaises(exception.InvalidInput, self.controller.create,
req, body)
@mock.patch(
'smaug.api.v1.restores.RestoresController._get_all')
def test_restore_list_detail(self, moak_get_all):
req = fakes.HTTPRequest.blank('/v1/restores')
self.controller.index(req)
self.assertTrue(moak_get_all.called)
@mock.patch(
'smaug.api.v1.restores.RestoresController.'
'_restore_get')
def test_restore_show(self, moak_restore_get):
req = fakes.HTTPRequest.blank('/v1/restores')
self.controller.\
show(req, '2a9ce1f3-cc1a-4516-9435-0ebb13caa398')
self.assertTrue(moak_restore_get.called)
def test_restore_show_Invalid(self):
req = fakes.HTTPRequest.blank('/v1/restores/1')
self.assertRaises(
exc.HTTPBadRequest, self.controller.show,
req, "1")
@mock.patch(
'smaug.api.v1.restores.RestoresController.'
'_restore_get')
def test_restore_delete(self, moak_restore_get):
req = fakes.HTTPRequest.blank('/v1/restores')
self.controller.\
show(req, '2a9ce1f3-cc1a-4516-9435-0ebb13caa398')
self.assertTrue(moak_restore_get.called)
def test_restore_delete_Invalid(self):
req = fakes.HTTPRequest.blank('/v1/restores/1')
self.assertRaises(
exc.HTTPBadRequest, self.controller.show,
req, "1")
def _restore_in_request_body(
self, project_id=DEFAULT_PROJECT_ID,
provider_id=DEFAULT_PROVIDER_ID,
checkpoint_id=DEFAULT_CHECKPOINT_ID,
restore_target=DEFAULT_RESTORE_TARGET,
parameters=DEFAULT_PARAMETERS,
status=DEFAULT_STATUS):
restore_req = {
'project_id': project_id,
'provider_id': provider_id,
'checkpoint_id': checkpoint_id,
'restore_target': restore_target,
'parameters': parameters,
'status': status,
}
return restore_req