-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.py
121 lines (91 loc) · 2.85 KB
/
init.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#!/usr/bin/env python
from flask import Flask, request, jsonify
from functools import wraps
from os import path
from time import asctime as localtime
import logging
import ecmanaged.loadb
app = Flask(__name__)
ecm = ecmanaged.loadb.ECMLoadBalancer()
# Logger
cwd = path.dirname(path.abspath( __file__ ))
file_handler = logging.FileHandler(cwd + '/loadb_api.log')
app.logger.addHandler(file_handler)
app.logger.setLevel(logging.DEBUG)
# Read secret
SECRET = ecm.secret_read()
def output_json(data, code = 200, headers=None):
message = {
'status': code,
'message': data,
}
resp = jsonify(message)
resp.status_code = code
resp.headers.extend(headers or {})
# Log event
log_str = '%s (%s): request: %s - data: %s' % (localtime(),request.method,request.url,data)
if code == 200: app.logger.info ('INFO - %s' % log_str)
elif code >= 500: app.logger.error ('WARN - %s' % log_str)
elif code >= 400: app.logger.warning('CRIT - %s' % log_str)
return resp
def check_auth(username, password):
return username == 'admin' and password == SECRET
def authenticate(msg):
headers = {}
headers['WWW-Authenticate'] = 'Basic realm="private"'
return output_json(msg,401,headers)
def requires_auth(f):
@wraps(f)
def decorated(*args, **kwargs):
auth = request.authorization
if not auth:
return authenticate("Authenticate")
elif not check_auth(auth.username, auth.password):
return authenticate("Authentication failed")
return f(*args, **kwargs)
return decorated
@app.errorhandler(404)
def output_not_found(error=None):
return output_json('Not Found: ' + request.url, 404)
@app.errorhandler(500)
def output_error(error=None):
return output_json('Error: ' + str(error), 500)
#
# API Resources
#
@app.route('/', methods=['GET',])
def config_get():
return output_json(ecm.config_get())
@app.route('/config/', methods=['POST',])
@app.route('/main/', methods=['POST',])
@requires_auth
def config_set():
retval = None
if request.method == 'POST':
section = str(request.url_rule).replace('/','')
retval = ecm.config_set(request.json,section)
return output_json(retval)
@app.route('/service/<id>/', methods=['GET','POST','DELETE'])
@requires_auth
def service_id(id = None):
retval = None
if request.method == 'GET':
retval = ecm.service_get(id)
elif request.method == 'POST':
retval = ecm.service_add(id,request.json)
elif request.method == 'DELETE':
retval = ecm.service_delete(id)
return output_json(retval)
@app.route('/service/<id>/<node>/', methods=['GET','POST','DELETE'])
@requires_auth
def node(id=None, node=None):
retval = None
if request.method == 'GET':
retval = ecm.node_get(id,node)
elif request.method == 'POST':
retval = ecm.node_add_into_service(id,node,request.json)
elif request.method == 'DELETE':
retval = ecm.node_delete_from_service(id,node)
return output_json(retval)
if __name__ == '__main__':
app.run(port=5002, debug=True)