def test_delete(self): proxy, service = self._create_proxy() proxy.delete() with self.assertRaises( KeyError, msg='deleting a proxy, should delete the real service'): scol.get_by_guid(proxy.guid)
def AddTaskToListHandler(service_guid): """ Add a task to the task list It is handler for POST /services/<service_guid>/task_list """ inputs = request.get_json() try: TaskCreate_schema_validator.validate(inputs) except jsonschema.ValidationError as e: return jsonify(code=400, message=str(e)), 400 try: service = scol.get_by_guid(service_guid) except KeyError: return jsonify(code=404, message="service with guid '%s' not found" % service_guid), 404 args = inputs.get("args", None) try: task = service.schedule_action(action=inputs["action_name"], args=args) except ActionNotFoundError: return jsonify(code=400, message="action '%s' not found" % inputs["action_name"]), 400 except BadActionArgumentError as err: return jsonify(code=400, message=str(err)), 400 return jsonify(task_view(task, service)), 201
def AddTaskToListHandler(service_guid): ''' Add a task to the task list It is handler for POST /services/<service_guid>/task_list ''' inputs = request.get_json() try: TaskCreate_schema_validator.validate(inputs) except jsonschema.ValidationError as e: return jsonify(code=400, message=str(e)), 400 try: service = scol.get_by_guid(service_guid) except KeyError: return jsonify(code=404, message="service with guid '%s' not found" % service_guid), 404 args = inputs.get('args', None) try: task = service.schedule_action(action=inputs['action_name'], args=args) except ActionNotFoundError: return jsonify(code=400, message="action '%s' not found" % inputs['action_name']), 400 except BadActionArgumentError: return jsonify(code=400, message="the argument passed in the requests, doesn't match the signature of the action"), 400 return jsonify(task_view(task, service)), 201
def test_service_create(self): with self.assertRaises(TemplateNotFoundError, msg='TemplateNotFoundError should be raise\ if trying to create service from a non existing template' ): self.cl.services.create( 'github.com/threefoldtech/0-robot/notexits/0.0.1', 'foo') data = {'ip': '127.0.0.1'} node = self.cl.services.create( 'github.com/threefoldtech/0-robot/node/0.0.1', 'node1', data) self.assertEqual(type(node), ServiceProxy, 'service type should be ServiceProxy') # ensure the services actually exists scol.get_by_name(node.name) node = scol.get_by_guid(node.guid) self.assertEqual( node.data['ip'], data['ip'], "data send during creation of the service should be set to the actual service" ) self.assertEqual(len(self.cl.services.names), 1, "listing of service per name should return 1") self.assertEqual(len(self.cl.services.guids), 1, "listing of service per guid should return 1")
def instantiate_services(services): services_created = [] err_msg = None err_code = None for service in services: try: service = _instantiate_service(service) if service: # add secret to new created service view = service_view(service) try: view["secret"] = auth.user_jwt.create( {"service_guid": service.guid}) services_created.append(view) except auth.user_jwt.SigningKeyNotFoundError as err: err_code = 500 err_msg = "error creating user secret: no signing key available" break except Exception as err: err_code = 500 err_msg = "error creating user secret: %s" % str(err) break except TemplateNotFoundError: err_code = 404 err_msg = "template '%s' not found" % service["template"] break except TemplateConflictError as err: err_code = 400 err_msg = err.args[0] break except Exception as err: err_code = 500 err_msg = str(err) break if err_code or err_msg: # means we had an error during the creation of services # clean up all created service in this blueprint for service in services_created: scol.get_by_guid(service["guid"]).delete() return services_created, err_code, err_msg
def test_add_get_service(self): service = FakeService('1234567890', 's1') scol.add(service) self.assertEqual(service, scol.get_by_guid('1234567890')) self.assertEqual(service, scol.get_by_name('s1')) self.assertEqual(service, scol.find(name='s1')[0]) with self.assertRaises(scol.ServiceNotFoundError): scol.get_by_name("nan")
def DeleteServiceHandler(service_guid): ''' Delete a service It is handler for DELETE /services/<service_guid> ''' try: service = scol.get_by_guid(service_guid) service.delete() except scol.ServiceNotFoundError: pass return "", 204, {"Content-type": 'application/json'}
def GetServiceHandler(service_guid): ''' Get the detail of a service It is handler for GET /services/<service_guid> ''' try: service = scol.get_by_guid(service_guid) except KeyError: return json.dumps({'code': 404, 'message': "service with guid '%s' not found" % service_guid}), \ 404, {"Content-type": 'application/json'} return json.dumps(service_view(service)), 200, { "Content-type": 'application/json' }
def ListActionsHandler(service_guid): ''' List all the possible action a service can do. It is handler for GET /services/<service_guid>/actions ''' try: service = scol.get_by_guid(service_guid) except KeyError: return json.dumps({'code': 404, 'message': "service with guid '%s' not found" % service_guid}), \ 404, {"Content-type": 'application/json'} actions = get_actions_list(service) return json.dumps(actions), 200, {"Content-type": 'application/json'}
def test_service_save_delete(self): Node = self.load_template('node') srv = tcol.instantiate_service(Node, 'testnode') srv.save() srv_dir = os.path.join(config.data_repo.path, srv.template_uid.host, srv.template_uid.account, srv.template_uid.repo, srv.template_uid.name, srv.name, srv.guid) self.assertTrue(os.path.exists(srv_dir), "directory of the saved service should exists") for x in ['service.yaml', 'data.yaml', 'state.yaml']: self.assertTrue(os.path.exists(os.path.join(srv_dir, x)), "%s file service should exists" % x) service_info = j.data.serializer.yaml.load( os.path.join(srv_dir, 'service.yaml')) for k in ['template', 'guid', 'name', 'version']: self.assertTrue(k in service_info, "%s should be present in service.yaml" % k) srv.delete() self.assertFalse( os.path.exists(os.path.dirname(srv_dir)), "directory of the saved service not should exists anymore") log_file_pattern = os.path.join(j.dirs.LOGDIR, 'zrobot', srv.guid) + '*' log_files = glob.glob(log_file_pattern) self.assertEqual( len(log_files), 0, "log files of the services should not should exists anymore") with self.assertRaises( scol.ServiceNotFoundError, message='service should not be found in memory anymore'): scol.get_by_guid(srv.guid)
def GetTaskHandler(task_guid, service_guid): ''' Retrieve the detail of a task It is handler for GET /services/<service_guid>/task_list/<task_guid> ''' try: service = scol.get_by_guid(service_guid) except scol.ServiceNotFoundError: return jsonify(code=404, message="service with guid '%s' not found" % service_guid), 404 try: task = service.task_list.get_task_by_guid(task_guid) except TaskNotFoundError: return jsonify(code=404, message="task with guid '%s' not found" % task_guid), 404 return jsonify(task_view(task, service)), 200
def CancelTaskHandler(task_guid, service_guid): try: service = scol.get_by_guid(service_guid) except scol.ServiceNotFoundError: return jsonify(code=404, message="service with guid '%s' not found" % service_guid), 404 try: task = service.task_list.get_task_by_guid(task_guid) task._cancel() except TaskNotFoundError: return jsonify(code=404, message="task with guid '%s' not found" % task_guid), 404 return '', 204
def GetLogsHandler(service_guid): if not auth.god_jwt.check_god_token(request): return jsonify( code=400, message= 'god mode is not enabled on the service, you cannot read the logs' ), 400 try: service = scol.get_by_guid(service_guid) except KeyError: return jsonify(code=404, message="service with guid '%s' not found" % service_guid), 404 log_file = os.path.join(j.dirs.LOGDIR, 'zrobot', service.guid) if not os.path.exists(log_file): return jsonify(logs=''), 200 with open(log_file) as f: return jsonify(logs=f.read()), 200
def GetLogsHandler(service_guid): if config.god is False: return jsonify( code=400, message= "god mode is not enable on the 0-robot, logs are not accessible" ), 400 try: service = scol.get_by_guid(service_guid) except KeyError: return jsonify(code=404, message="service with guid '%s' not found" % service_guid), 404 log_file = os.path.join(j.dirs.LOGDIR, 'zrobot', service.guid) if not os.path.exists(log_file): return jsonify(logs=''), 200 with open(log_file) as f: return jsonify(logs=f.read()), 200
def getTaskListHandler(service_guid): ''' Return all the action in the task list It is handler for GET /services/<service_guid>/task_list ''' try: service = scol.get_by_guid(service_guid) except scol.ServiceNotFoundError: return jsonify(code=404, message="service with guid '%s' not found" % service_guid), 404 # return only task waiting or all existing task for this service all_task = request.args.get('all') if all_task is not None: all_task = j.data.types.bool.fromString(all_task) tasks = [ task_view(t, service) for t in service.task_list.list_tasks(all=all_task) ] return jsonify(tasks), 200
def create_proxy(self, client, public=False): template = 'github.com/zero-os/0-robot/node/0.0.1' name = 'node1' proxy = client.services.create(template, name, public=public) service = scol.get_by_guid(proxy.guid) return (proxy, service)
def _create_proxy(self, public=False): template = 'github.com/threefoldtech/0-robot/node/0.0.1' name = 'node1' proxy = self.cl.services.create(template, name, public=public) service = scol.get_by_guid(proxy.guid) return (proxy, service)