def get_destination(self, request, router_id, destination_id): """Get the config and status of a destination""" router = yield Router.from_id(self, router_id) destination = router.get_destination(destination_id) returnValue(response( request, 'destination found', (yield destination.status()) ))
def delete_router_destination(self, request, router_id, destination_id): """Delete and stop the router destination""" router = yield Router.from_id(self, router_id) destination = router.get_destination(destination_id) yield router.stop() yield destination.delete() router.start(self.service) returnValue(response(request, 'destination deleted', {}))
def test_from_id(self): """from_id should be able to restore a router, given just the id""" config = self.create_router_config() router = Router(self.api, config) yield router.save() router.start(self.api.service) restored_router = yield Router.from_id(self.api, router.router_config['id']) self.assertEqual(router.router_config, restored_router.router_config) self.assertEqual(router.router_worker, restored_router.router_worker)
def test_from_id(self): """from_id should be able to restore a router, given just the id""" config = self.create_router_config() router = Router(self.api, config) yield router.save() router.start(self.api.service) restored_router = yield Router.from_id( self.api, router.router_config['id']) self.assertEqual(router.router_config, restored_router.router_config) self.assertEqual(router.router_worker, restored_router.router_worker)
def update_router_config(self, request, body, router_id): """Update the router config with the one specified""" router = yield Router.from_id(self, router_id) router.router_config.update(body) yield router.validate_config() # Stop and start the router for the worker to get the new config yield router.stop() router.start(self.service) yield router.save() returnValue(response( request, 'router updated', (yield router.status())))
def create_router_destination(self, request, body, router_id): """Create a new destination for the router""" router = yield Router.from_id(self, router_id) yield router.validate_destination_config(body['config']) destination = router.add_destination(body) yield router.stop() router.start(self.service) yield destination.save() returnValue(response( request, 'destination created', (yield destination.status()), code=http.CREATED ))
def replace_router_config(self, request, body, router_id): """Replace the router config with the one specified""" router = yield Router.from_id(self, router_id) for field in ['type', 'label', 'config', 'metadata']: router.router_config.pop(field, None) router.router_config.update(body) yield router.validate_config() # Stop and start the router for the worker to get the new config yield router.stop() router.start(self.service) yield router.save() returnValue(response( request, 'router updated', (yield router.status())))
def update_router_destination( self, request, body, router_id, destination_id): """Update the config of a router destination""" router = yield Router.from_id(self, router_id) if 'config' in body: yield router.validate_destination_config(body['config']) destination = router.get_destination(destination_id) destination.destination_config.update(body) # Stop and start the router for the worker to get the new config yield router.stop() router.start(self.service) yield destination.save() returnValue(response( request, 'destination updated', (yield destination.status())))
def test_destinations_restored_on_router_from_id(self): """Creating a router object from id should also restore the destinations for that router""" router_config = self.create_router_config() router = Router(self.api, router_config) router.start(self.api.service) destination_config = self.create_destination_config() destination = router.add_destination(destination_config) yield router.save() restored_router = yield Router.from_id(self.api, router.id) self.assertEqual(router.destinations.keys(), restored_router.destinations.keys()) self.assertEqual( router.destinations[destination.id].destination_config, restored_router.destinations[destination.id].destination_config)
def test_destinations_restored_on_router_from_id(self): """Creating a router object from id should also restore the destinations for that router""" router_config = self.create_router_config() router = Router(self.api, router_config) router.start(self.api.service) destination_config = self.create_destination_config() destination = router.add_destination(destination_config) yield router.save() restored_router = yield Router.from_id(self.api, router.id) self.assertEqual( router.destinations.keys(), restored_router.destinations.keys()) self.assertEqual( router.destinations[destination.id].destination_config, restored_router.destinations[destination.id].destination_config)
def get_router_destination_list(self, request, router_id): """Get the list of destinations for a router""" d = Router.from_id(self, router_id) d.addCallback(lambda router: router.get_destination_list()) d.addCallback(partial(response, request, 'destinations retrieved')) return d
def delete_router(self, request, router_id): router = yield Router.from_id(self, router_id) yield router.stop() yield router.delete() returnValue(response(request, 'router deleted', {}))
def get_router(self, request, router_id): """Get the configuration details and status of a specific router""" d = Router.from_id(self, router_id) d.addCallback(lambda router: router.status()) d.addCallback(partial(response, request, 'router found')) return d
def test_from_id_doesnt_exist(self): """If we don't have a router for the specified ID, then we should raise the appropriate error""" with self.assertRaises(RouterNotFound): yield Router.from_id(self.api, 'bad-router-id')