The RESTAPI of resource checkpoints

Change-Id: I819896be8f3463ef01ecf1c1d74ef92dd7e9216f
Closes-Bug: #1550682
This commit is contained in:
chenying 2016-03-01 14:50:45 +08:00
parent 22f613d845
commit 61026e3d07
8 changed files with 480 additions and 10 deletions

View File

@ -20,5 +20,9 @@
"protectable:get_all": "rule:admin_or_owner",
"provider:get": "rule:admin_or_owner",
"provider:get_all": "rule:admin_or_owner"
"provider:get_all": "rule:admin_or_owner",
"provider:checkpoint_get": "rule:admin_or_owner",
"provider:checkpoint_get_all": "rule:admin_or_owner",
"provider:checkpoint_create": "rule:admin_or_owner",
"provider:checkpoint_delete": "rule:admin_or_owner"
}

View File

@ -23,6 +23,7 @@ from smaug.api.openstack import wsgi
from smaug import exception
from smaug.i18n import _, _LI
from smaug import objects
import smaug.policy
from smaug.services.protection import api as protection_api
from smaug import utils
@ -115,6 +116,61 @@ class ProviderViewBuilder(common.ViewBuilder):
return providers_dict
class CheckpointViewBuilder(common.ViewBuilder):
"""Model a server API response as a python dictionary."""
_collection_name = "checkpoints"
def __init__(self):
"""Initialize view builder."""
super(CheckpointViewBuilder, self).__init__()
def detail(self, request, checkpoint):
"""Detailed view of a single checkpoint."""
checkpoint_ref = {
'checkpoint': {
'id': checkpoint.get('id'),
'project_id': checkpoint.get('project_id'),
'status': checkpoint.get('status'),
'provider_id': checkpoint.get('provider_id'),
'protection_plan': checkpoint.get('protection_plan'),
}
}
return checkpoint_ref
def detail_list(self, request, checkpoints, checkpoint_count=None):
"""Detailed view of a list of checkpoints."""
return self._list_view(self.detail, request, checkpoints,
checkpoint_count,
self._collection_name)
def _list_view(self, func, request, checkpoints, checkpoint_count,
coll_name=_collection_name):
"""Provide a view for a list of checkpoint.
:param func: Function used to format the checkpoint data
:param request: API request
:param checkpoints: List of checkpoints in dictionary format
:param checkpoint_count: Length of the original list of checkpoints
:param coll_name: Name of collection, used to generate the next link
for a pagination query
:returns: Checkpoint data in dictionary format
"""
checkpoints_list = [func(request, checkpoint)['checkpoint']
for checkpoint in checkpoints]
checkpoints_links = self._get_collection_links(request,
checkpoints,
coll_name,
checkpoint_count)
checkpoints_dict = {
"checkpoints": checkpoints_list
}
if checkpoints_links:
checkpoints_dict['checkpoints_links'] = checkpoints_links
return checkpoints_dict
class ProvidersController(wsgi.Controller):
"""The Providers API controller for the OpenStack API."""
@ -122,6 +178,7 @@ class ProvidersController(wsgi.Controller):
def __init__(self):
self.protection_api = protection_api.API()
self._checkpoint_view_builder = CheckpointViewBuilder()
super(ProvidersController, self).__init__()
def show(self, req, id):
@ -178,7 +235,7 @@ class ProvidersController(wsgi.Controller):
try:
if limit is not None:
limit = int(limit)
if limit < 0:
if limit <= 0:
msg = _('limit param must be positive')
raise exception.InvalidInput(reason=msg)
except ValueError:
@ -227,6 +284,187 @@ class ProvidersController(wsgi.Controller):
LOG.info(_LI("Provider info retrieved successfully."))
return provider
def checkpoints_index(self, req, provider_id):
"""Returns a list of checkpoints, transformed through view builder."""
context = req.environ['smaug.context']
LOG.info(_LI("Show checkpoints list. "
"provider_id:%s"), provider_id)
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_checkpoint_filter_options())
utils.check_filters(filters)
checkpoints = self._checkpoints_get_all(
context, provider_id, marker, limit,
sort_keys=sort_keys, sort_dirs=sort_dirs,
filters=filters, offset=offset)
retval_checkpoints = self._checkpoint_view_builder.detail_list(
req, checkpoints)
LOG.info(_LI("Show checkpoints list request issued successfully."))
return retval_checkpoints
def _checkpoints_get_all(self, context, provider_id, marker=None,
limit=None, sort_keys=None, sort_dirs=None,
filters=None, offset=None):
check_policy(context, 'checkpoint_get_all')
if filters is None:
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))
checkpoints = self.protection_api.list_checkpoints(
context, provider_id, marker, limit,
sort_keys=sort_keys,
sort_dirs=sort_dirs,
filters=filters,
offset=offset)
LOG.info(_LI("Get all checkpoints completed successfully."))
return checkpoints
def checkpoints_create(self, req, provider_id, body):
"""Creates a new checkpoint."""
if not self.is_valid_body(body, 'checkpoint'):
raise exc.HTTPUnprocessableEntity()
context = req.environ['smaug.context']
LOG.debug('Create checkpoint request '
'body: %s provider_id:%s', body, provider_id)
check_policy(context, 'checkpoint_create')
checkpoint = body['checkpoint']
LOG.debug('Create checkpoint request checkpoint: %s',
checkpoint)
if not provider_id:
msg = _("provider_id must be provided when creating "
"a checkpoint.")
raise exception.InvalidInput(reason=msg)
plan_id = checkpoint.get("plan_id")
if not plan_id:
msg = _("plan_id must be provided when creating "
"a checkpoint.")
raise exception.InvalidInput(reason=msg)
if not uuidutils.is_uuid_like(plan_id):
msg = _("Invalid plan id provided.")
raise exc.HTTPBadRequest(explanation=msg)
plan = objects.Plan.get_by_id(context, plan_id)
if not plan:
raise exception.PlanNotFound(plan_id=plan_id)
checkpoint_properties = {
'project_id': context.project_id,
'status': 'protecting',
'provider_id': provider_id,
"protection_plan": {
"id": plan.get("id"),
"name": plan.get("name"),
"resources": plan.get("resources"),
}
}
self.protection_api.protect(context, plan)
returnval = self._checkpoint_view_builder.detail(
req, checkpoint_properties)
return returnval
def checkpoints_show(self, req, provider_id, checkpoint_id):
"""Return data about the given checkpoint id."""
context = req.environ['smaug.context']
LOG.info(_LI("Show checkpoint with id: %s."),
checkpoint_id)
LOG.info(_LI("provider_id: %s."), provider_id)
try:
checkpoint = self._checkpoint_get(context, provider_id,
checkpoint_id)
except exception.CheckpointNotFound as error:
raise exc.HTTPNotFound(explanation=error.msg)
LOG.info(_LI("Show checkpoint request issued successfully."))
LOG.info(_LI("checkpoint: %s"), checkpoint)
retval = self._checkpoint_view_builder.detail(req, checkpoint)
LOG.info(_LI("retval: %s"), retval)
return retval
def _checkpoint_get(self, context, provider_id, checkpoint_id):
if not uuidutils.is_uuid_like(provider_id):
msg = _("Invalid provider id provided.")
raise exc.HTTPBadRequest(explanation=msg)
if not uuidutils.is_uuid_like(checkpoint_id):
msg = _("Invalid checkpoint id provided.")
raise exc.HTTPBadRequest(explanation=msg)
try:
check_policy(context, 'checkpoint_get')
except exception.PolicyNotAuthorized:
# raise CheckpointNotFound instead to make sure smaug behaves
# as it used to
raise exception.CheckpointNotFound(checkpoint_id=checkpoint_id)
checkpoint = self.protection_api.show_checkpoint(
context, provider_id, checkpoint_id)
if checkpoint is None:
raise exception.CheckpointNotFound(checkpoint_id=checkpoint_id)
LOG.info(_LI("Checkpoint info retrieved successfully."))
return checkpoint
def checkpoints_delete(self, req, provider_id, checkpoint_id, body):
"""Delete a checkpoint."""
context = req.environ['smaug.context']
LOG.info(_LI("Delete checkpoint with id: %s."),
checkpoint_id)
LOG.info(_LI("provider_id: %s."), provider_id)
if not uuidutils.is_uuid_like(provider_id):
msg = _("Invalid provider id provided.")
raise exc.HTTPBadRequest(explanation=msg)
if not uuidutils.is_uuid_like(checkpoint_id):
msg = _("Invalid checkpoint id provided.")
raise exc.HTTPBadRequest(explanation=msg)
check_policy(context, 'checkpoint_delete')
self.protection_api.delete(context,
provider_id,
checkpoint_id)
LOG.info(_LI("Delete checkpoint request issued successfully."))
return {}
def create_resource():
return wsgi.Resource(ProvidersController())

View File

@ -52,6 +52,28 @@ class APIRouter(wsgi_common.Router):
controller=providers_resources,
collection={},
member={})
mapper.connect("provider",
"/{project_id}/providers/{provider_id}/checkpoints",
controller=providers_resources,
action='checkpoints_index',
conditions={"method": ['GET']})
mapper.connect("provider",
"/{project_id}/providers/{provider_id}/checkpoints",
controller=providers_resources,
action='checkpoints_create',
conditions={"method": ['POST']})
mapper.connect("provider",
"/{project_id}/providers/{provider_id}/checkpoints/"
"{checkpoint_id}",
controller=providers_resources,
action='checkpoints_show',
conditions={"method": ['GET']})
mapper.connect("provider",
"/{project_id}/providers/{provider_id}/checkpoints/"
"{checkpoint_id}",
controller=providers_resources,
action='checkpoints_delete',
conditions={"method": ['DELETE']})
mapper.resource("scheduled_operation", "scheduled_operations",
controller=scheduled_operation_resources,
collection={'detail': 'GET'},

View File

@ -228,3 +228,8 @@ class ProtectableTypeNotFound(NotFound):
class ProviderNotFound(NotFound):
message = _("Provider %(provider_id)s could"
" not be found.")
class CheckpointNotFound(NotFound):
message = _("Checkpoint %(checkpoint_id)s could"
" not be found.")

View File

@ -35,6 +35,23 @@ class API(base.Base):
def restore(self, context, restore):
return self.protection_rpcapi.restore(context, restore)
def protect(self, context, plan):
return self.protection_rpcapi.protect(context, plan)
def delete(self, context, provider_id, checkpoint_id):
return self.protection_rpcapi.\
delete(context, provider_id, checkpoint_id)
def show_checkpoint(self, context, provider_id, checkpoint_id):
return self.protection_rpcapi.\
show_checkpoint(context, provider_id, checkpoint_id)
def list_checkpoints(self, context, provider_id, marker, limit,
sort_keys, sort_dirs, filters, offset):
return self.protection_rpcapi.\
list_checkpoints(context, provider_id, marker, limit,
sort_keys, sort_dirs, filters)
def list_protectable_types(self, context):
return self.protection_rpcapi.list_protectable_types(context)

View File

@ -56,13 +56,17 @@ class ProtectionManager(manager.Manager):
LOG.info(_LI("Starting protection service"))
# TODO(wangliuan) use flow_engine to implement protect function
def protect(self, plan):
def protect(self, context, plan):
"""create protection for the given plan
:param plan: Define that protection plan should be done
"""
LOG.info(_LI("Starting protection service:protect action"))
LOG.debug('restoration :%s tpye:%s', plan,
type(plan))
# TODO(wangliuan)
pass
return True
def restore(self, context, restore=None):
LOG.info(_LI("Starting restore service:restore action"))
@ -72,9 +76,13 @@ class ProtectionManager(manager.Manager):
# TODO(wangliuan)
return True
def delete(self, plan):
def delete(self, context, provider_id, checkpoint_id):
# TODO(wangliuan)
pass
LOG.info(_LI("Starting protection service:delete action"))
LOG.debug('provider_id :%s checkpoint_id:%s', provider_id,
checkpoint_id)
return True
def start(self, plan):
# TODO(wangliuan)
@ -93,13 +101,68 @@ class ProtectionManager(manager.Manager):
# TODO(wangliuan)
pass
def list_checkpoints(self, list_options):
def list_checkpoints(self, context, provider_id, marker=None, limit=None,
sort_keys=None, sort_dirs=None, filters=None):
# TODO(wangliuan)
pass
LOG.info(_LI("Starting list checkpoints. "
"provider_id:%s"), provider_id)
def show_checkpoint(self, checkpoint_id):
return_stub = [
{
"id": "2220f8b1-975d-4621-a872-fa9afb43cb6c",
"project_id": "446a04d8-6ff5-4e0e-99a4-827a6389e9ff",
"status": "comitted",
"provider_id": "efc6a88b-9096-4bb6-8634-cda182a6e12a",
"protection_plan": {
"id": "2a9ce1f3-cc1a-4516-9435-0ebb13caa398",
"name": "My 3 tier application",
"resources": [
{
"id": "64e51e85-4f31-441f-9a5d-6e93e3196628",
"type": "OS::Nova::Server"
},
{
"id": "61e51e85-4f31-441f-9a5d-6e93e3196628",
"type": "OS::Cinder::Volume"
},
{
"id": "62e51e85-4f31-441f-9a5d-6e93e3196628",
"type": "OS::Cinder::Volume"
}
],
}
}
]
return return_stub
def show_checkpoint(self, context, provider_id, checkpoint_id):
# TODO(wangliuan)
pass
LOG.info(_LI("Starting show checkpoints. "
"provider_id:%s"), provider_id)
LOG.info(_LI("checkpoint_id:%s"), checkpoint_id)
return_stub = {
"id": "2220f8b1-975d-4621-a872-fa9afb43cb6c",
"project_id": "446a04d8-6ff5-4e0e-99a4-827a6389e9ff",
"status": "committed",
"protection_plan": {
"id": "2a9ce1f3-cc1a-4516-9435-0ebb13caa398",
"name": "My 3 tier application",
"resources": [
{
"id": "64e51e85-4f31-441f-9a5d-6e93e3196628",
"type": "OS::Nova::Server",
"extended_info": {
"name": "VM1",
"backup status": "done",
"available_memory": 512
}
}
]
},
"provider_id": "efc6a88b-9096-4bb6-8634-cda182a6e12a"
}
return return_stub
def delete_checkpoint(self, checkpoint_id):
# TODO(wangliuan)

View File

@ -51,6 +51,43 @@ class ProtectionAPI(object):
'restore',
restore=restore)
def protect(self, ctxt, plan=None):
cctxt = self.client.prepare(version='1.0')
return cctxt.call(
ctxt,
'protect',
plan=plan)
def delete(self, ctxt, provider_id, checkpoint_id):
cctxt = self.client.prepare(version='1.0')
return cctxt.call(
ctxt,
'delete',
provider_id=provider_id,
checkpoint_id=checkpoint_id)
def show_checkpoint(self, ctxt, provider_id, checkpoint_id):
cctxt = self.client.prepare(version='1.0')
return cctxt.call(
ctxt,
'show_checkpoint',
provider_id=provider_id,
checkpoint_id=checkpoint_id)
def list_checkpoints(self, ctxt, provider_id, marker=None,
limit=None, sort_keys=None,
sort_dirs=None, filters=None):
cctxt = self.client.prepare(version='1.0')
return cctxt.call(
ctxt,
'list_checkpoints',
provider_id=provider_id,
marker=marker,
limit=limit,
sort_keys=sort_keys,
sort_dirs=sort_dirs,
filters=filters)
def list_protectable_types(self, ctxt):
cctxt = self.client.prepare(version='1.0')
return cctxt.call(

View File

@ -49,3 +49,87 @@ class ProvidersApiTest(base.TestCase):
req = fakes.HTTPRequest.blank('/v1/providers')
self.assertRaises(exc.HTTPBadRequest, self.controller.show,
req, "1")
@mock.patch(
'smaug.services.protection.api.API.'
'show_checkpoint')
def test_checkpoint_show(self, moak_show_checkpoint):
req = fakes.HTTPRequest.blank('/v1/providers/'
'{provider_id}/checkpoints/')
moak_show_checkpoint.return_value = {
"provider_id": "efc6a88b-9096-4bb6-8634-cda182a6e12a",
"project_id": "446a04d8-6ff5-4e0e-99a4-827a6389e9ff",
"id": "2220f8b1-975d-4621-a872-fa9afb43cb6c"
}
self.controller.\
checkpoints_show(req, '2220f8b1-975d-4621-a872-fa9afb43cb6c',
'2220f8b1-975d-4621-a872-fa9afb43cb6c')
self.assertTrue(moak_show_checkpoint.called)
@mock.patch(
'smaug.services.protection.api.API.'
'show_checkpoint')
def test_checkpoint_show_Invalid(self, moak_show_checkpoint):
req = fakes.HTTPRequest.blank('/v1/providers/'
'{provider_id}/checkpoints/')
moak_show_checkpoint.return_value = {
"provider_id": "efc6a88b-9096-4bb6-8634-cda182a6e12a",
"project_id": "446a04d8-6ff5-4e0e-99a4-827a6389e9ff",
"id": "2220f8b1-975d-4621-a872-fa9afb43cb6c"
}
self.assertRaises(exc.HTTPBadRequest, self.controller.checkpoints_show,
req, '2220f8b1-975d-4621-a872-fa9afb43cb6c',
'1')
@mock.patch(
'smaug.services.protection.api.API.'
'list_checkpoints')
def test_checkpoint_index(self, moak_list_checkpoints):
req = fakes.HTTPRequest.blank('/v1/providers/'
'{provider_id}/checkpoints/')
moak_list_checkpoints.return_value = [
{
"provider_id": "efc6a88b-9096-4bb6-8634-cda182a6e12a",
"project_id": "446a04d8-6ff5-4e0e-99a4-827a6389e9ff",
"id": "2220f8b1-975d-4621-a872-fa9afb43cb6c"
}
]
self.controller.checkpoints_index(
req,
'2220f8b1-975d-4621-a872-fa9afb43cb6c')
self.assertTrue(moak_list_checkpoints.called)
@mock.patch(
'smaug.services.protection.api.API.'
'delete')
def test_checkpoints_delete(self, moak_delete):
req = fakes.HTTPRequest.blank('/v1/providers/'
'{provider_id}/checkpoints/')
self.controller.checkpoints_delete(
req,
'2220f8b1-975d-4621-a872-fa9afb43cb6c',
'2220f8b1-975d-4621-a872-fa9afb43cb6c', {})
self.assertTrue(moak_delete.called)
@mock.patch(
'smaug.services.protection.api.API.'
'protect')
@mock.patch(
'smaug.objects.plan.Plan.get_by_id')
def test_checkpoints_create(self, mock_plan_create,
mock_protect):
checkpoint = {
"plan_id": "2c3a12ee-5ea6-406a-8b64-862711ff85e6"
}
body = {"checkpoint": checkpoint}
req = fakes.HTTPRequest.blank('/v1/providers/'
'{provider_id}/checkpoints/')
mock_plan_create.return_value = {
"plan_id": "2c3a12ee-5ea6-406a-8b64-862711ff85e6"
}
self.controller.checkpoints_create(
req,
'2220f8b1-975d-4621-a872-fa9afb43cb6c',
body)
self.assertTrue(mock_plan_create.called)
self.assertTrue(mock_protect.called)