-
Notifications
You must be signed in to change notification settings - Fork 0
/
vmware_api.py
136 lines (112 loc) · 3.76 KB
/
vmware_api.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
from flask import Blueprint, request, abort, url_for, jsonify, session, g, redirect
import vmware_automate
from vmware_automate import ESXI_Host, ESXI_Guest
from pyVmomi import vim
vms = Blueprint('vms', __name__, url_prefix='/vms')
vmware = vmware_automate.setup()
print(vmware)
host = ESXI_Host(vmware['esxi']['host'], vmware['esxi']['user'], vmware['esxi']['password'], naming_scheme=vmware['naming_scheme'])
guests = []
vmware['esxi'] = {'host': host, 'guests': []}
for vm in host.lsVMs():
guest = ESXI_Guest(host, vm, vmware['guest']['user'], vmware['guest']['password'])
guests.append(guest)
vmware['esxi'] = {'host': host, 'guests': guests}
def findVM(vm_name):
global vmware
guests = vmware['esxi']['guests']
vm = list(filter(lambda x: x.guest == vm_name, guests))
if len(vm) > 0:
vm = vm.pop()
else:
vm = None
return vm
#### VMWARE ESXI API INTERFACE ####
@vms.route('/', methods=['GET'])
def vm_main():
global vmware
return jsonify({'vms': vmware['esxi']['host'].lsVMs(), 'naming scheme': vmware['naming_scheme']})
@vms.route('/<vm_name>', methods=['GET'])
@vms.route('/<vm_name>/', methods=['GET'])
def vm_get(vm_name):
vm = findVM(vm_name)
print(vm)
if not vm:
vm_name = None
return jsonify({'vm': vm.as_dict() if vm else None})
@vms.route('/<vm_name>/upload', methods=['POST'])
def vm_upload(vm_name):
files = request.files
if len(files) == 0:
return jsonify({'vm': vm_name, 'error': 'no files found in uploaded data'})
data = {}
vm = findVM(vm_name)
data['vm'] = vm_name
if vm:
for file in files:
vm.upload(files[file].read(), filename=files[file].filename)
data['uploaded'] = True
else:
vm_name = None
return jsonify(data)
@vms.route('/<vm_name>/snapshots', methods=['GET'])
def vm_snapshots(vm_name):
vm = findVM(vm_name)
if vm:
snapshots = vm.makeJSON(vm.getSnapshots())
else:
snapshots = None
vm_name = None
return jsonify({'vm': vm_name, 'snapshots': snapshots})
@vms.route('/<vm_name>/snapshots/create', methods=['GET'])
def vm_snapshots_create(vm_name):
args = request.args
if args.get('name', None) is None:
return jsonify({'vm': vm_name, 'error': 'missing required name argument'})
else:
name = args.get('name')
memory = args.get('memory', False)
memory = bool(memory)
pause = args.get('pause', True)
pause = bool(pause)
vm = findVM(vm_name)
if vm:
snapshot = vm.createSnapshot(name, memory=memory, pause=pause)
snapshot = str(snapshot)
else:
snapshots = None
vm_name = None
return jsonify({'vm': vm_name, 'snapshot': snapshot})
@vms.route('/<vm_name>/snapshots/<name>/revert', methods=['GET'])
def vm_snapshots_revert(vm_name, name):
vm = findVM(vm_name)
results = vm.revertToSnapshot(name)
return jsonify({'vm': vm_name, 'snapshot': name, 'results': vm.makeJSON(results)})
@vms.route('/<vm_name>/power', methods=['GET'])
def vm_power_list(vm_name):
vm = findVM(vm_name)
if vm:
modes = vm.getPowerModes()
else:
modes = None
vm_name = None
return jsonify({'vm': vm_name, 'modes': modes})
@vms.route('/<vm_name>/power/<mode>', methods=['GET'])
def vm_power(vm_name, mode):
vm = findVM(vm_name)
if vm:
status = vm.power(mode)
else:
status = None
vm_name = None
res = {'vm': vm_name}
res.update(status)
return jsonify(res)
@vms.route('/<vm_name>/reset', methods=['GET'])
def vm_reset(vm_name):
vm = findVM(vm_name)
if not vm:
return '', 404
status = vm.power('off')
results = vm.revertToSnapshot('clean')
return jsonify({'vm': vm_name, 'power_status': status, 'results': vm.makeJSON(results)})