-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathzstack_host_api.py
More file actions
302 lines (277 loc) · 10.8 KB
/
zstack_host_api.py
File metadata and controls
302 lines (277 loc) · 10.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
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
#!/usr/bin/env python
#coding=utf-8
__author__ = 'yuanbin'
import sys
import os
import config
import time
import requests
import json
from zstack_base_demo import zstack_base_api
reload(sys)
sys.setdefaultencoding('utf-8')
class zstack_host_api:
def __init__(self):
self.header = {"Content-Type": "application/json"}
self.base = zstack_base_api()
self.UUID = self.base.UUID
def add_kvm_host(self, name, clusterUuid, managementIp, username, password):
'''
request: {
"org.zstack.kvm.APIAddKVMHostMsg":
{
"username": "root",
"clusterUuid": "f6cf5efe72924ccf90b771d32f610f19",
"name": "host-yuan",
"managementIp": "10.0.3.119",
"session": {"uuid": "0962b18d361446788a635b9b904d8711"},
"password": "Skt6edg"
}
}
:param name:
:param clusterUuid:
:param managementIp:
:param username:
:param password:
:return:
'''
content = {
"name": name,
"clusterUuid": clusterUuid,
"managementIp": managementIp,
"username": username,
"password": password,
"description": "Add KVM Host: %s" % name
}
rsp = self.base.api_call(self.UUID, "org.zstack.kvm.APIAddKVMHostMsg", content)
# self.error_if_fail(rsp)
job_uuid = rsp['uuid']
status = self.base.query_until_done(job_uuid)
print "successfully created %s, status %s" % (name, status)
# pass
def delete_host(self, uuid):
'''
request: {
"org.zstack.header.host.APIDeleteHostMsg": {
"session": {"uuid": "0962b18d361446788a635b9b904d8711"},
"uuid": "848520f29011436c8685e520b87dab23"
}
}
:param uuid:
:return:
'''
content = {"uuid": uuid}
rsp = self.base.api_call(self.UUID, "org.zstack.header.host.APIDeleteHostMsg", content)
# self.error_if_fail(rsp)
job_uuid = rsp['uuid']
status = self.base.query_until_done(job_uuid)
print "successfully delete Host %s, status %s" % (uuid, status)
# return self.UUID
# pass
def change_host_state(self, uuid, stateEvent):
'''
request: {
"org.zstack.header.host.APIChangeHostStateMsg":
{
"session": {"uuid": "0962b18d361446788a635b9b904d8711"},
"uuid": "848520f29011436c8685e520b87dab23",
"stateEvent": "disable"
}
}
response: {
"org.zstack.header.host.APIChangeHostStateEvent":
{
"inventory":
{
"zoneUuid":"4cc9847534384039b323e8d2fff4b606",
"name":"host-yb",
"uuid":"848520f29011436c8685e520b87dab23",
"clusterUuid":"aac8b6ea037044b389b2849090fa9bb6",
"description":"Add KVM Host: host-yb",
"managementIp":"10.0.89.18",
"hypervisorType":"KVM",
"state":"Disabled",
"status":"Connected",
"totalCpuCapacity":40,
"availableCpuCapacity":40,
"totalMemoryCapacity":8071839744,
"availableMemoryCapacity":8071839744,"
createDate":"Sep 28, 2016 4:22:42 AM",
"lastOpDate":"Sep 28, 2016 4:31:43 AM"
},
"success":true
}
}
:param uuid: 主机的uuid
:param stateEvent: enable/disable/preMaintain
:return:
'''
content = {
"uuid": uuid,
"stateEvent": stateEvent
}
rsp = self.base.api_call(self.UUID, "org.zstack.header.host.APIChangeHostStateMsg", content)
# self.error_if_fail(rsp)
job_uuid = rsp['uuid']
status = self.base.query_until_done(job_uuid)
print "successfully Change Host State %s, status %s" % (uuid, status)
# pass
def reconnect_host(self, uuid):
'''
request: {
"org.zstack.header.host.APIReconnectHostMsg":{
"session": {"uuid": "0962b18d361446788a635b9b904d8711"},
"uuid": "848520f29011436c8685e520b87dab23"
}
}
response: {
"org.zstack.header.host.APIReconnectHostEvent":{
"success":true
}
}
:param uuid: 主机的uuid
:return:
'''
content = {
"uuid": uuid
}
rsp = self.base.api_call(self.UUID, "org.zstack.header.host.APIChangeHostStateMsg", content)
# self.error_if_fail(rsp)
job_uuid = rsp['uuid']
status = self.base.query_until_done(job_uuid)
print "successfully Reconnect Host %s, status %s" % (uuid, status)
# pass
def query_host_by_ip(self, managementIP):
'''
request: {
"org.zstack.header.host.APIQueryHostMsg": {
"session": {"uuid": "0962b18d361446788a635b9b904d8711"},
"conditions": [{"name": "managementIp", "value": "10.0.89.18", "op": "="}]
}
}
:param managementIP:
:return:
'''
content = {
"conditions": [
{
"name": "managementIp",
"value": managementIP,
"op": "="
}
]
}
rsp = self.base.api_call(self.UUID, "org.zstack.header.host.APIQueryHostMsg", content)
self.base.error_if_fail(rsp)
# job_uuid = rsp['uuid']
# status = self.query_until_done(job_uuid)
print "successfully query Host by managementIP %s, result: %s" % (managementIP, rsp)
# pass
def query_host_by_name(self, name):
'''
request: {
"org.zstack.header.host.APIQueryHostMsg": {
"session": {"uuid": "0962b18d361446788a635b9b904d8711"},
"conditions": [{"name": "name", "value": "host-yb", "op": "="}]
}
}
:param name: Host 名称NAME
:return:
'''
content = {
"conditions": [
{
"name": "name",
"value": name,
"op": "="
}
]
}
rsp = self.base.api_call(self.UUID, "org.zstack.header.host.APIQueryHostMsg", content)
self.base.error_if_fail(rsp)
# job_uuid = rsp['uuid']
# status = self.query_until_done(job_uuid)
print "successfully query Host by name %s, result: %s" % (name, rsp)
def query_all_host(self):
'''
# 查询全部Host,conditions不需要过滤任何条件
request: {
"org.zstack.header.host.APIQueryHostMsg": {
"session": {"uuid": "0962b18d361446788a635b9b904d8711"},
"conditions": []
}
}
response: {
"org.zstack.header.host.APIQueryHostReply":{
"inventories":[
{
"username":"root",
"sshPort":22,
"zoneUuid":"4cc9847534384039b323e8d2fff4b606",
"name":"host-yb",
"uuid":"848520f29011436c8685e520b87dab23",
"clusterUuid":"aac8b6ea037044b389b2849090fa9bb6",
"description":"Add KVM Host: host-yb",
"managementIp":"10.0.89.18",
"hypervisorType":"KVM",
"state":"Enabled",
"status":"Connected",
"totalCpuCapacity":40,
"availableCpuCapacity":40,
"totalMemoryCapacity":8071839744,
"availableMemoryCapacity":8071839744,
"createDate":"Sep 28, 2016 4:22:42 AM",
"lastOpDate":"Sep 28, 2016 4:42:28 AM"
},
{
"username":"root",
"sshPort":22,
"zoneUuid":"4cc9847534384039b323e8d2fff4b606",
"name":"host-221",
"uuid":"f3e90e2620f840618a8941b6688101b9",
"clusterUuid":"f6cf5efe72924ccf90b771d32f610f19",
"description":"10.0.1.221",
"managementIp":"10.0.1.221",
"hypervisorType":"KVM",
"state":"Enabled",
"status":"Connected",
"totalCpuCapacity":240,
"availableCpuCapacity":240,
"totalMemoryCapacity":540620193792,
"availableMemoryCapacity":540620193792,
"createDate":"Sep 26, 2016 11:26:02 AM",
"lastOpDate":"Sep 27, 2016 11:16:25 PM"
}
],
"success":true
}
}
:return:
'''
content = {
"conditions": []
}
rsp = self.base.api_call(self.UUID, "org.zstack.header.host.APIQueryHostMsg", content)
self.base.error_if_fail(rsp)
# job_uuid = rsp['uuid']
# status = self.query_until_done(job_uuid)
print "successfully Query All Host %s" % (rsp)
def tags_host(self, tags):
pass
def logout(self, session_uuid):
content = {"sessionUuid": session_uuid}
rsp = self.base.api_call(None, "org.zstack.header.identity.APILogOutMsg", content)
self.base.error_if_fail(rsp)
print "successfully logout"
# pass
if __name__ == '__main__':
new_zs = zstack_host_api()
# session_uuid = new_zs.login()
# new_zs.delete_host('96df40c9e1ad4622a5f5504920c628ac')
# new_zs.add_kvm_host('host-yy', '4cc9847534384039b323e8d2fff4b606', '10.0.89.18', 'root', 'Skt6edg')
# new_zs.change_host_state('848520f29011436c8685e520b87dab23', 'enable')
# new_zs.reconnect_host('848520f29011436c8685e520b87dab23')
new_zs.query_all_host()
# new_zs.query_host_by_ip('10.0.1.221')
# new_zs.query_host_by_name('host-yb')
new_zs.logout(new_zs.UUID)