Browse Source

API services/restore

New API for restoring all the services on a board.

Change-Id: Iceac65b1672a90d8662de6ef4df8ce51980339c8
tags/0.2.0
Fabio Verboso 1 year ago
parent
commit
53a3b766e2

+ 23
- 3
iotronic/api/controllers/v1/board.py View File

@@ -322,8 +322,10 @@ class BoardPluginsController(rest.RestController):
322 322
 
323 323
 
324 324
 class BoardServicesController(rest.RestController):
325
+
325 326
     _custom_actions = {
326 327
         'action': ['POST'],
328
+        'restore': ['GET']
327 329
     }
328 330
 
329 331
     def __init__(self, board_ident):
@@ -358,9 +360,6 @@ class BoardServicesController(rest.RestController):
358 360
             raise exception.MissingParameterValue(
359 361
                 ("Action is not specified."))
360 362
 
361
-        if not ServiceAction.parameters:
362
-            ServiceAction.parameters = {}
363
-
364 363
         rpc_board = api_utils.get_rpc_board(self.board_ident)
365 364
         rpc_service = api_utils.get_rpc_service(service_ident)
366 365
 
@@ -380,6 +379,27 @@ class BoardServicesController(rest.RestController):
380 379
                                                      ServiceAction.action)
381 380
         return result
382 381
 
382
+    @expose.expose(ExposedCollection,
383
+                   status_code=200)
384
+    def restore(self):
385
+        rpc_board = api_utils.get_rpc_board(self.board_ident)
386
+
387
+        try:
388
+            cdict = pecan.request.context.to_policy_values()
389
+            cdict['owner'] = rpc_board.owner
390
+            policy.authorize('iot:service_action:post', cdict, cdict)
391
+
392
+        except exception:
393
+            return exception
394
+
395
+        rpc_board.check_if_online()
396
+
397
+        pecan.request.rpcapi.restore_services_on_board(
398
+            pecan.request.context,
399
+            rpc_board.uuid)
400
+
401
+        return self._get_services_on_board_collection(rpc_board.uuid)
402
+
383 403
 
384 404
 class BoardsController(rest.RestController):
385 405
     """REST controller for Boards."""

+ 4
- 0
iotronic/common/exception.py View File

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

+ 44
- 60
iotronic/conductor/endpoints.py View File

@@ -305,12 +305,12 @@ class ConductorEndpoint(object):
305 305
         return serializer.serialize_entity(ctx, service)
306 306
 
307 307
     def action_service(self, ctx, service_uuid, board_uuid, action):
308
-        LOG.info('Enable service with id %s into the board %s',
309
-                 service_uuid, board_uuid)
310 308
         service = objects.Service.get(ctx, service_uuid)
311 309
         objects.service.is_valid_action(action)
312 310
 
313 311
         if action == "ServiceEnable":
312
+            LOG.info('Enabling service with id %s into the board %s',
313
+                     service_uuid, board_uuid)
314 314
             try:
315 315
                 objects.ExposedService.get(ctx,
316 316
                                            board_uuid,
@@ -348,6 +348,8 @@ class ConductorEndpoint(object):
348 348
                 return res.message
349 349
 
350 350
         elif action == "ServiceDisable":
351
+            LOG.info('Disabling service with id %s into the board %s',
352
+                     service_uuid, board_uuid)
351 353
             exposed = objects.ExposedService.get(ctx,
352 354
                                                  board_uuid,
353 355
                                                  service_uuid)
@@ -361,12 +363,11 @@ class ConductorEndpoint(object):
361 363
             return result
362 364
 
363 365
         elif action == "ServiceRestore":
364
-
366
+            LOG.info('Restoring service with id %s into the board %s',
367
+                     service_uuid, board_uuid)
365 368
             exposed = objects.ExposedService.get(ctx, board_uuid,
366 369
                                                  service_uuid)
367 370
 
368
-            print(exposed)
369
-
370 371
             res = self.execute_on_board(ctx, board_uuid, action,
371 372
                                         (service.name, exposed.public_port,
372 373
                                          service.port, exposed.pid))
@@ -396,58 +397,41 @@ class ConductorEndpoint(object):
396 397
             LOG.debug(res.message)
397 398
             return res.message
398 399
 
399
-            # try:
400
-            #
401
-            #
402
-            #     return exception.ServiceAlreadyExposed(uuid=service_uuid)
403
-            # except:
404
-            #     name=service.name
405
-            #     public_port=random_public_port()
406
-            #     port=service.port
407
-            #
408
-            #     res = self.execute_on_board(ctx, board_uuid, action,
409
-            #                                 (name, public_port, port))
410
-            #
411
-            #     if res.result == wm.SUCCESS:
412
-            #         pid = res.message[0]
413
-            #
414
-            #         exp_data = {
415
-            #             'board_uuid': board_uuid,
416
-            #             'service_uuid': service_uuid,
417
-            #             'public_port': public_port,
418
-            #             'pid': pid,
419
-            #         }
420
-            #         exposed = objects.ExposedService(ctx, **exp_data)
421
-            #         exposed.create()
422
-            #
423
-            #         res.message = res.message[1]
424
-            #     elif res.result == wm.ERROR:
425
-            #         LOG.error('Error in the execution of %s on %s: %s',
426
-            #                   action,
427
-            #                   board_uuid, res.message)
428
-            #         raise exception.ErrorExecutionOnBoard(call=action,
429
-            #                                               board=board_uuid,
430
-            #                                               error=res.message)
431
-            #     LOG.debug(res.message)
432
-            #     return res.message
433
-            #
434
-            #
435
-            #
436
-            #
437
-            #
438
-            #
439
-            #
440
-            #
441
-            #
442
-            #
443
-            #
444
-            # exposed = objects.ExposedService.get(ctx, board_uuid,
445
-            #                                          service_uuid)
446
-            #
447
-            # res = self.execute_on_board(ctx, board_uuid, action,
448
-            #                             (service.name, exposed.pid))
449
-            #
450
-            # result=manage_result(res,action,board_uuid)
451
-            # LOG.debug(res.message)
452
-            # exposed.destroy()
453
-            # return result
400
+    def restore_services_on_board(self, ctx, board_uuid):
401
+        LOG.info('Restoring the services into the board %s',
402
+                 board_uuid)
403
+
404
+        exposed_list = objects.ExposedService.get_by_board_uuid(ctx,
405
+                                                                board_uuid)
406
+
407
+        # response = []
408
+        for exposed in exposed_list:
409
+            service = objects.Service.get_by_uuid(ctx, exposed.service_uuid)
410
+            res = self.execute_on_board(ctx, board_uuid, "ServiceRestore",
411
+                                        (service.name, exposed.public_port,
412
+                                         service.port, exposed.pid))
413
+
414
+            if res.result == wm.SUCCESS:
415
+                pid = res.message[0]
416
+
417
+                exp_data = {
418
+                    'id': exposed.id,
419
+                    'board_uuid': exposed.board_uuid,
420
+                    'service_uuid': exposed.service_uuid,
421
+                    'public_port': exposed.public_port,
422
+                    'pid': pid,
423
+                }
424
+
425
+                exposed = objects.ExposedService(ctx, **exp_data)
426
+                exposed.save()
427
+
428
+                # response.append(exposed)
429
+            elif res.result == wm.ERROR:
430
+                LOG.error('Error in restoring %s on %s: %s',
431
+                          service.name,
432
+                          board_uuid, res.message)
433
+                raise exception.ErrorExecutionOnBoard(call="ServiceRestore",
434
+                                                      board=board_uuid,
435
+                                                      error=res.message)
436
+
437
+        return 0

+ 13
- 0
iotronic/conductor/rpcapi.py View File

@@ -263,3 +263,16 @@ class ConductorAPI(object):
263 263
 
264 264
         return cctxt.call(context, 'action_service', service_uuid=service_uuid,
265 265
                           board_uuid=board_uuid, action=action)
266
+
267
+    def restore_services_on_board(self, context,
268
+                                  board_uuid, topic=None):
269
+        """Restore all the services on a board.
270
+
271
+        :param context: request context.
272
+        :param board_uuid: board id or uuid.
273
+
274
+        """
275
+        cctxt = self.client.prepare(topic=topic or self.topic, version='1.0')
276
+
277
+        return cctxt.call(context, 'restore_services_on_board',
278
+                          board_uuid=board_uuid)

+ 1
- 1
iotronic/db/api.py View File

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

+ 3
- 3
iotronic/db/sqlalchemy/api.py View File

@@ -764,14 +764,14 @@ class Connection(api.Connection):
764 764
 
765 765
     # EXPOSED SERVICE api
766 766
 
767
-    def get_exposed_service_by_board_uuid(self, board_uuid):
767
+    def get_exposed_services_by_board_uuid(self, board_uuid):
768 768
         query = model_query(
769 769
             models.ExposedService).filter_by(
770 770
             board_uuid=board_uuid)
771 771
         try:
772
-            return query.one()
772
+            return query.all()
773 773
         except NoResultFound:
774
-            raise exception.ExposedServiceNotFound()
774
+            raise exception.NoExposedServices(uuid=board_uuid)
775 775
 
776 776
     def create_exposed_service(self, values):
777 777
         # ensure defaults are present for new services

+ 12
- 8
iotronic/objects/exposedservice.py View File

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

Loading…
Cancel
Save