def new_project(event, context=None): """ End-point: Creates a new CBR application (project). """ result = {} statusCode = 201 proj = json.loads(event['body']) # parameters in request body # create ES index for Projects if it does not exist es = getESConn() if not es.indices.exists(index=projects_db): project_mapping = project.getProjectMapping() es.indices.create(index=projects_db, body=project_mapping) # create config db if it does not exist utility.createOrUpdateGlobalConfig(es, config_db=config_db) if 'casebase' not in proj or "" == proj['casebase'] or "" == proj['name']: result = "A new project has to specify a name and a casebase." statusCode = 400 elif utility.indexHasDocWithFieldVal( es, index=projects_db, field='casebase', value=proj['casebase']): # (index, field, value) result = "Casebase already exists. Choose a different name for the casebase." statusCode = 400 else: proj['attributes'] = [] proj['hasCasebase'] = False # print(proj) result = es.index(index=projects_db, body=proj) response = { "statusCode": statusCode, "headers": headers, "body": json.dumps(result) } return response
def re_create_config(event, context=None): """ End-point: (Temporary) To re-create the config after changes are made programmatically """ # get config. configuration index has 1 document result = [] es = getESConn() utility.createOrUpdateGlobalConfig(es, config_db=config_db) time.sleep(0.3) # 0.3 sec wait to allow time for created index to be ready query = {"query": retrieve.MatchAll()} res = es.search(index=config_db, body=query) if (res['hits']['total']['value'] > 0): result = res['hits']['hits'][0]['_source'] response = { "statusCode": 200, "headers": headers, "body": json.dumps(result) } return response
def get_config(event, context=None): """ End-point: Retrieves configuration """ # get config. configuration index has 1 document result = [] es = getESConn() if not es.indices.exists(index=config_db): # create config db if it does not exist utility.createOrUpdateGlobalConfig(es, config_db=config_db) time.sleep(0.3) # 0.3 sec wait to allow time for created index to be ready query = {"query": retrieve.MatchAll()} res = es.search(index=config_db, body=query) if (res['hits']['total']['value'] > 0): result = res['hits']['hits'][0]['_source'] response = { "statusCode": 200, "headers": headers, "body": json.dumps(result) } return response
def update_config(event, context=None): """ End-point: Updates configuration """ res = utility.createOrUpdateGlobalConfig(getESConn(), config_db=config_db, globalConfig=json.loads(event['body'])) msg = "Configuration updated" if res else "Configuration not updated" body = { "result": res, "message": msg } response = { "statusCode": 201, "headers": headers, "body": json.dumps(body) } return response