Merge "API services/restore"

This commit is contained in:
Zuul 2018-02-19 18:06:59 +00:00 committed by Gerrit Code Review
commit 5d5218cab8
7 changed files with 100 additions and 75 deletions

View File

@ -322,8 +322,10 @@ class BoardPluginsController(rest.RestController):
class BoardServicesController(rest.RestController): class BoardServicesController(rest.RestController):
_custom_actions = { _custom_actions = {
'action': ['POST'], 'action': ['POST'],
'restore': ['GET']
} }
def __init__(self, board_ident): def __init__(self, board_ident):
@ -358,9 +360,6 @@ class BoardServicesController(rest.RestController):
raise exception.MissingParameterValue( raise exception.MissingParameterValue(
("Action is not specified.")) ("Action is not specified."))
if not ServiceAction.parameters:
ServiceAction.parameters = {}
rpc_board = api_utils.get_rpc_board(self.board_ident) rpc_board = api_utils.get_rpc_board(self.board_ident)
rpc_service = api_utils.get_rpc_service(service_ident) rpc_service = api_utils.get_rpc_service(service_ident)
@ -380,6 +379,27 @@ class BoardServicesController(rest.RestController):
ServiceAction.action) ServiceAction.action)
return result return result
@expose.expose(ExposedCollection,
status_code=200)
def restore(self):
rpc_board = api_utils.get_rpc_board(self.board_ident)
try:
cdict = pecan.request.context.to_policy_values()
cdict['owner'] = rpc_board.owner
policy.authorize('iot:service_action:post', cdict, cdict)
except exception:
return exception
rpc_board.check_if_online()
pecan.request.rpcapi.restore_services_on_board(
pecan.request.context,
rpc_board.uuid)
return self._get_services_on_board_collection(rpc_board.uuid)
class BoardsController(rest.RestController): class BoardsController(rest.RestController):
"""REST controller for Boards.""" """REST controller for Boards."""

View File

@ -609,3 +609,7 @@ class ServiceAlreadyExposed(Conflict):
class ExposedServiceNotFound(NotFound): class ExposedServiceNotFound(NotFound):
message = _("ExposedService %(uuid)s could not be found.") message = _("ExposedService %(uuid)s could not be found.")
class NoExposedServices(NotFound):
message = _("No exposed services on the board %(uuid)s.")

View File

@ -305,12 +305,12 @@ class ConductorEndpoint(object):
return serializer.serialize_entity(ctx, service) return serializer.serialize_entity(ctx, service)
def action_service(self, ctx, service_uuid, board_uuid, action): def action_service(self, ctx, service_uuid, board_uuid, action):
LOG.info('Enable service with id %s into the board %s',
service_uuid, board_uuid)
service = objects.Service.get(ctx, service_uuid) service = objects.Service.get(ctx, service_uuid)
objects.service.is_valid_action(action) objects.service.is_valid_action(action)
if action == "ServiceEnable": if action == "ServiceEnable":
LOG.info('Enabling service with id %s into the board %s',
service_uuid, board_uuid)
try: try:
objects.ExposedService.get(ctx, objects.ExposedService.get(ctx,
board_uuid, board_uuid,
@ -348,6 +348,8 @@ class ConductorEndpoint(object):
return res.message return res.message
elif action == "ServiceDisable": elif action == "ServiceDisable":
LOG.info('Disabling service with id %s into the board %s',
service_uuid, board_uuid)
exposed = objects.ExposedService.get(ctx, exposed = objects.ExposedService.get(ctx,
board_uuid, board_uuid,
service_uuid) service_uuid)
@ -361,12 +363,11 @@ class ConductorEndpoint(object):
return result return result
elif action == "ServiceRestore": elif action == "ServiceRestore":
LOG.info('Restoring service with id %s into the board %s',
service_uuid, board_uuid)
exposed = objects.ExposedService.get(ctx, board_uuid, exposed = objects.ExposedService.get(ctx, board_uuid,
service_uuid) service_uuid)
print(exposed)
res = self.execute_on_board(ctx, board_uuid, action, res = self.execute_on_board(ctx, board_uuid, action,
(service.name, exposed.public_port, (service.name, exposed.public_port,
service.port, exposed.pid)) service.port, exposed.pid))
@ -396,58 +397,41 @@ class ConductorEndpoint(object):
LOG.debug(res.message) LOG.debug(res.message)
return res.message return res.message
# try: def restore_services_on_board(self, ctx, board_uuid):
# LOG.info('Restoring the services into the board %s',
# board_uuid)
# return exception.ServiceAlreadyExposed(uuid=service_uuid)
# except: exposed_list = objects.ExposedService.get_by_board_uuid(ctx,
# name=service.name board_uuid)
# public_port=random_public_port()
# port=service.port # response = []
# for exposed in exposed_list:
# res = self.execute_on_board(ctx, board_uuid, action, service = objects.Service.get_by_uuid(ctx, exposed.service_uuid)
# (name, public_port, port)) res = self.execute_on_board(ctx, board_uuid, "ServiceRestore",
# (service.name, exposed.public_port,
# if res.result == wm.SUCCESS: service.port, exposed.pid))
# pid = res.message[0]
# if res.result == wm.SUCCESS:
# exp_data = { pid = res.message[0]
# 'board_uuid': board_uuid,
# 'service_uuid': service_uuid, exp_data = {
# 'public_port': public_port, 'id': exposed.id,
# 'pid': pid, 'board_uuid': exposed.board_uuid,
# } 'service_uuid': exposed.service_uuid,
# exposed = objects.ExposedService(ctx, **exp_data) 'public_port': exposed.public_port,
# exposed.create() 'pid': pid,
# }
# res.message = res.message[1]
# elif res.result == wm.ERROR: exposed = objects.ExposedService(ctx, **exp_data)
# LOG.error('Error in the execution of %s on %s: %s', exposed.save()
# action,
# board_uuid, res.message) # response.append(exposed)
# raise exception.ErrorExecutionOnBoard(call=action, elif res.result == wm.ERROR:
# board=board_uuid, LOG.error('Error in restoring %s on %s: %s',
# error=res.message) service.name,
# LOG.debug(res.message) board_uuid, res.message)
# return res.message raise exception.ErrorExecutionOnBoard(call="ServiceRestore",
# board=board_uuid,
# error=res.message)
#
# return 0
#
#
#
#
#
#
#
# exposed = objects.ExposedService.get(ctx, board_uuid,
# service_uuid)
#
# res = self.execute_on_board(ctx, board_uuid, action,
# (service.name, exposed.pid))
#
# result=manage_result(res,action,board_uuid)
# LOG.debug(res.message)
# exposed.destroy()
# return result

View File

@ -263,3 +263,16 @@ class ConductorAPI(object):
return cctxt.call(context, 'action_service', service_uuid=service_uuid, return cctxt.call(context, 'action_service', service_uuid=service_uuid,
board_uuid=board_uuid, action=action) board_uuid=board_uuid, action=action)
def restore_services_on_board(self, context,
board_uuid, topic=None):
"""Restore all the services on a board.
:param context: request context.
:param board_uuid: board id or uuid.
"""
cctxt = self.client.prepare(topic=topic or self.topic, version='1.0')
return cctxt.call(context, 'restore_services_on_board',
board_uuid=board_uuid)

View File

@ -475,7 +475,7 @@ class Connection(object):
""" """
@abc.abstractmethod @abc.abstractmethod
def get_exposed_service_by_board_uuid(self, board_uuid): def get_exposed_services_by_board_uuid(self, board_uuid):
"""get an exposed of a service using a board_uuid """get an exposed of a service using a board_uuid
:param board_uuid: The id or uuid of a board. :param board_uuid: The id or uuid of a board.

View File

@ -764,14 +764,14 @@ class Connection(api.Connection):
# EXPOSED SERVICE api # EXPOSED SERVICE api
def get_exposed_service_by_board_uuid(self, board_uuid): def get_exposed_services_by_board_uuid(self, board_uuid):
query = model_query( query = model_query(
models.ExposedService).filter_by( models.ExposedService).filter_by(
board_uuid=board_uuid) board_uuid=board_uuid)
try: try:
return query.one() return query.all()
except NoResultFound: except NoResultFound:
raise exception.ExposedServiceNotFound() raise exception.NoExposedServices(uuid=board_uuid)
def create_exposed_service(self, values): def create_exposed_service(self, values):
# ensure defaults are present for new services # ensure defaults are present for new services

View File

@ -55,16 +55,20 @@ class ExposedService(base.IotronicObject):
@base.remotable_classmethod @base.remotable_classmethod
def get_by_board_uuid(cls, context, board_uuid): def get_by_board_uuid(cls, context, board_uuid):
"""Find a exposed_service based on uuid and return a Board object. """Return a list of ExposedService objects.
:param context: Security context.
:param limit: maximum number of resources to return in a single result.
:param marker: pagination marker for large data sets.
:param sort_key: column to sort results by.
:param sort_dir: direction to sort. "asc" or "desc".
:param filters: Filters to apply.
:returns: a list of :class:`ExposedService` object.
:param board_uuid: the uuid of a exposed_service.
:returns: a :class:`exposed_service` object.
""" """
db_exp_service = cls.dbapi.get_exposed_service_by_board_uuid( db_exps = cls.dbapi.get_exposed_services_by_board_uuid(board_uuid)
board_uuid) return [ExposedService._from_db_object(cls(context), obj)
exp_service = ExposedService._from_db_object(cls(context), for obj in db_exps]
db_exp_service)
return exp_service
@base.remotable_classmethod @base.remotable_classmethod
def get_by_service_uuid(cls, context, service_uuid): def get_by_service_uuid(cls, context, service_uuid):