in source/backend/lambda_servers/lambda_servers.py [0:0]
def lambda_handler(event, context):
if event['httpMethod'] == 'GET':
item = scan_dynamodb_server_table()
newitem = sorted(item, key = lambda i: i['server_name'])
return {'headers': {'Access-Control-Allow-Origin': '*'},
'body': json.dumps(newitem)}
elif event['httpMethod'] == 'POST':
auth = MFAuth()
authResponse = auth.getUserResourceCrationPolicy(event)
if authResponse['action'] == 'allow':
try:
body = json.loads(event['body'])
if 'server_name' not in body:
return {'headers': {'Access-Control-Allow-Origin': '*'},
'statusCode': 400, 'body': 'attribute server_name is required'}
if 'app_id' not in body:
return {'headers': {'Access-Control-Allow-Origin': '*'},
'statusCode': 400, 'body': 'attribute app_id is required'}
if 'server_id' in body:
return {'headers': {'Access-Control-Allow-Origin': '*'},
'statusCode': 400, 'body': "You cannot create server_id, this is managed by the system"}
# Check if attribute is defined in the Server schema
server_attributes = []
for server_schema in schema_table.scan()['Items']:
if server_schema['schema_name'] == "server":
server_attributes = server_schema['attributes']
for key in body.keys():
check = False
for attribute in server_attributes:
if key == attribute['name']:
check = True
if check == False:
message = "Server attribute: " + key + " is not defined in the Server schema"
return {'headers': {'Access-Control-Allow-Origin': '*'},
'statusCode': 400, 'body': message}
# Check if attribute in the body matches the list value defined in schema
for attribute in server_attributes:
if 'listvalue' in attribute:
listvalue = attribute['listvalue'].split(',')
for key in body.keys():
if key == attribute['name']:
if body[key] not in listvalue:
message = "Server attribute " + key + " for server " + body['server_name'] + " is '" + body[key] + "', does not match the list values '" + attribute['listvalue'] + "' defined in the Server schema"
return {'headers': {'Access-Control-Allow-Origin': '*'},
'statusCode': 400, 'body': message}
except Exception as e:
print(e)
return {'headers': {'Access-Control-Allow-Origin': '*'},
'statusCode': 400, 'body': 'malformed json input'}
# Check if there is a duplicate server_name
itemlist = scan_dynamodb_server_table()
for item in itemlist:
if body['server_name'].lower() == item['server_name'].lower():
return {'headers': {'Access-Control-Allow-Origin': '*'},
'statusCode': 400, 'body': 'server_name: ' + body['server_name'] + ' already exist'}
# Validate App_id
apps = scan_dynamodb_app_table()
check = False
for app in apps:
if app['app_id'] == str(body['app_id']):
check = True
if check == False:
message = 'app Id: ' + body['app_id'] + ' does not exist'
return {'headers': {'Access-Control-Allow-Origin': '*'},
'statusCode': 400, 'body': message}
# Get vacant server_id
ids = []
for item in itemlist:
ids.append(int(item['server_id']))
ids.sort()
server_id = 1
for id in ids:
if server_id == id:
server_id += 1
body['server_id'] = str(server_id)
# Update item
resp = servers_table.put_item(
Item=body
)
if (resp['ResponseMetadata']['HTTPStatusCode'] == 200):
new_item = {}
query_resp = servers_table.query(KeyConditionExpression=Key('server_id').eq(str(server_id)))
if 'Items' in query_resp:
new_item = query_resp['Items']
else:
new_item = "Creating server " + body['server_name'] + " failed"
return {'headers': {'Access-Control-Allow-Origin': '*'},
'body': json.dumps(new_item)}
else:
return {'headers': {'Access-Control-Allow-Origin': '*'},
'statusCode': 401,
'body': json.dumps(authResponse)}