-
Notifications
You must be signed in to change notification settings - Fork 8
/
util.py
46 lines (36 loc) · 1.16 KB
/
util.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import decimal
import json
class DecimalEncoder(json.JSONEncoder):
def default(self, o):
if isinstance(o, set):
return list(o)
elif isinstance(o, decimal.Decimal):
if o % 1 > 0:
return float(o)
else:
return int(o)
return super(DecimalEncoder, self).default(o)
def validate_config_input(config):
"""Validate config input is JSON"""
try:
json.loads(config)
except ValueError as err:
print(f'K8s config is not valid json error: {err}')
def validate_unique_cluster_name(cluster_name, cluster_table):
"""Validate cluster is a unique name"""
try:
item = cluster_table.get_item(Key={"id": cluster_name})
print(f"Cluster {cluster_name} exists: {item['Item']}")
except Exception:
# XXX - should catch the correct exceptions here.
item = None
return item
def lambda_result(data, status_code=200, **kwargs):
if not isinstance(data, str):
data = json.dumps(data, cls=DecimalEncoder)
result = {
"statusCode": status_code,
"body": data
}
result.update(kwargs)
return result