forked from karmab/samplecontroller
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathui.py
More file actions
86 lines (70 loc) · 2.8 KB
/
ui.py
File metadata and controls
86 lines (70 loc) · 2.8 KB
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
#!/usr/bin/python
from flask import Flask, render_template, request, jsonify
from kubernetes import client, config
import os
DOMAIN = 'kool.karmalabs.local'
VERSION = 'v1'
NAMESPACE = os.environ['GUITAR_NAMESPACE'] if 'GUITAR_NAMESPACE' in os.environ else 'guitarcenter'
goodbrands = ['coleclark', 'fender', 'gibson', 'ibanez', 'martin', 'seagull', 'squier', 'washburn']
badbrands = ['epiphone', 'guild', 'gretsch', 'jackson', 'ovation', 'prs', 'rickenbauer', 'taylor', 'yamaha']
app = Flask(__name__)
@app.route("/guitaradd", methods=['POST'])
def guitaradd():
name = request.form['name'].lower()
brand = request.form['brand']
body = {'kind': 'Guitar', 'spec': {'brand': brand, 'review': False}, 'apiVersion': '%s/%s' % (DOMAIN, VERSION), 'metadata': {'name': name, 'namespace': NAMESPACE}}
crds = client.CustomObjectsApi()
try:
crds.create_namespaced_custom_object(DOMAIN, VERSION, NAMESPACE, 'guitars', body)
result = {'result': 'success'}
# code = 200
except Exception as e:
message = [x.split(':')[1] for x in e.body.split(',') if 'message' in x][0].replace('"', '')
result = {'result': 'failure', 'reason': message}
# code = e.status
response = jsonify(result)
# response.status_code = code
return response
@app.route("/guitardelete", methods=['POST'])
def guitardelete():
name = request.form['name']
crds = client.CustomObjectsApi()
try:
crds.delete_namespaced_custom_object(DOMAIN, VERSION, NAMESPACE, 'guitars', name, client.V1DeleteOptions())
result = {'result': 'success'}
# code = 200
except Exception as e:
message = [x.split(':')[1] for x in e.body.split(',') if 'message' in x][0].replace('"', '')
result = {'result': 'failure', 'reason': message}
# code = e.status
response = jsonify(result)
# response.status_code = code
return response
@app.route("/guitarform")
def guitarform():
return render_template("guitarform.html", title="Add Your Guitar", brands=sorted(goodbrands + badbrands))
@app.route("/guitarlist")
def guitarlist():
"""
display guitars
"""
crds = client.CustomObjectsApi()
guitars = crds.list_cluster_custom_object(DOMAIN, VERSION, 'guitars')["items"]
guitars = sorted(guitars, key=lambda x: (x.get("spec")["brand"], x.get("metadata")["name"]))
return render_template("guitarlist.html", title="Guitars", guitars=guitars)
@app.route("/")
def index():
"""
display guitars
"""
return render_template("index.html", title="Guitars")
def run():
if 'KUBERNETES_PORT' in os.environ:
# os.environ['KUBERNETES_SERVICE_HOST'] = 'kubernetes'
config.load_incluster_config()
else:
config.load_kube_config()
app.run(host="0.0.0.0", port=9000)
run()
if __name__ == '__main__':
run()