def get_one_service(serviceId): """ Return info for one service """ db_session = orm.get_session() try: q = db_session.query(models.URL).get(serviceId) except orm.ORMException as e: return Error(status=500, title='Error connecting to database', detail=str(e)), 500 if not q: return Error(title="No such service found", detail="No result for service " + str(serviceId), status=404), 404 service = get_service_info(q.url) if not service: return Error(title="Service not available", detail="Could not connect to service " + str(serviceId), status=404), 404 # overwrite ID and name with local values service.id = q.id service.name = q.name service_as_dict = orm.dump(service) service_as_dict['url'] = q.url external_service = ExternalService(**service_as_dict) return orm.dump(external_service), 200, { 'Access-Control-Allow-Origin': '*' }
def list_services(): """ Return all known and active services """ db_session = orm.get_session() try: urls = URL().query.all() except orm.ORMException as e: return Error(status=500, title='Error connecting to database', detail=str(e)), 500 external_services = [] for url in urls: service = get_service_info(url.url) if not service: continue # overwrite ID and name with local values service.id = url.id service.name = url.name service_as_dict = orm.dump(service) service_as_dict['url'] = url.url external_services.append(ExternalService(**service_as_dict)) return [orm.dump(ext_svc) for ext_svc in external_services], 200, { 'Access-Control-Allow-Origin': '*' }
def empty_db(db_filename="services.db"): # pylint: disable=too-many-locals """ Create an empty db """ # delete db if already exists try: os.remove(db_filename) except OSError: pass init_db('sqlite:///' + db_filename) session = get_session(expire_on_commit=False) session.commit()
def main(args=None): """The main routine.""" if args is None: args = sys.argv[1:] parser = argparse.ArgumentParser('Run service registry') parser.add_argument('--database', default="./data/services.sqlite") parser.add_argument('name', type=str) parser.add_argument('url', type=str) args = parser.parse_args(args) init_db(uri="sqlite:///" + args.database) db_session = get_session() new_url = URL(url=args.url, name=args.name, id=uuid.uuid4()) db_session.add(new_url) db_session.commit()
def list_service_types(): """ Return all known and active services """ db_session = orm.get_session() try: urls = URL().query.all() except orm.ORMException as e: return Error(status=500, title='Error connecting to database', detail=str(e)), 500 service_types = [] for url in urls: service = get_service_info(url.url) if not service: continue service_type = ServiceType(**service.type) if not service_type in service_types: service_types.append(service_type) return [orm.dump(svc_type) for svc_type in service_types], 200, { 'Access-Control-Allow-Origin': '*' }