-
Notifications
You must be signed in to change notification settings - Fork 7
/
myapi.py
63 lines (48 loc) · 1.64 KB
/
myapi.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
from flask import Flask, request, jsonify
import methods
import os
app = Flask(__name__)
# Uncomment if you need to debug the site
app.debug = True
port = int(os.getenv('VCAP_APP_PORT', 8080))
# with open('config.json') as data_file:
# data = json.load(data_file)
#
# host=data["host"]
# user=data["username"]
# pwd=data["password"]
@app.route('/')
def index():
return 'Welcome to the VMware REST API!'
@app.route('/debug')
def debug():
return methods.debugger()
@app.route('/vms/', methods=['GET', 'POST'])
def get_vms():
if request.method == 'POST':
specs = request.get_json()
return jsonify(vm=methods.create_new_vm(specs))
else:
return jsonify(vm=methods.get_all_vm_info())
@app.route('/vms/<uuid>/', methods=['GET', 'PUT', 'DELETE'])
def get_vm(uuid):
if request.method == 'DELETE':
return methods.delete_vm_from_server(uuid)
elif request.method == 'PUT':
specs = request.get_json()
return methods.change_vm_stats(uuid, specs)
else:
return jsonify(methods.find_vm_by_uuid(uuid))
@app.route('/vms/<uuid>/<attr>/', methods=['GET', 'PUT'])
def get_attr(uuid, attr):
if request.method == 'GET':
return methods.get_vm_attribute(uuid, attr)
elif request.method == 'PUT' and attr.lower() == "pxeboot":
specs = request.get_json()
return methods.force_pxe_boot(uuid, specs)
@app.route('/vms/<uuid>/<root_attr>/<attr>', methods=['GET'])
def get_deep_attr(uuid, root_attr, attr):
if request.method == 'GET':
return methods.get_vm_attribute(uuid, attr, root_attr)
if __name__ == '__main__':
app.run(host='0.0.0.0', port=port)