-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathzstack_cluster_api.py
More file actions
409 lines (379 loc) · 15.7 KB
/
zstack_cluster_api.py
File metadata and controls
409 lines (379 loc) · 15.7 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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
#!/usr/bin/env python
#coding=utf-8
__author__ = 'yuanbin'
import sys
from zstack_base_demo import zstack_base_api
reload(sys)
sys.setdefaultencoding('utf-8')
class zstack_cluster_api:
def __init__(self):
self.header = {"Content-Type": "application/json"}
self.base = zstack_base_api()
self.UUID = self.base.UUID
"""
zoneUuid 父区域的uuid
name 资源名称
resourceUuid 资源uuid
description 资源描述
hypervisorType 虚拟机管理类型,zstack仅支持KVM
type 保留域,不要使用
"""
def create_cluster(self, name='cluster-default', zoneUuid=""):
'''request:{
"org.zstack.header.cluster.APICreateClusterMsg":
{
"session": {"uuid": "e360e25b28cb49eca00ea9117e23018b"},
"zoneUuid": "46627d3eafb9462dbafa35c96d9db302",
"hypervisorType": "KVM",
"name": "cluster-look"
}
}
response: {
"org.zstack.header.cluster.APICreateClusterEvent":
{
"inventory":
{
"name":"cluster-look",
"uuid":"73a5d13f020d450e997f2aa04350ac3c",
"state":"Enabled",
"hypervisorType":"KVM",
"createDate":"Sep 27, 2016 10:10:40 PM",
"lastOpDate":"Sep 27, 2016 10:10:40 PM",
"zoneUuid":"46627d3eafb9462dbafa35c96d9db302",
"type":"zstack"
},
"success":true
}
}'''
if len(zoneUuid) == 0:
print "zoneUuid is null, can not continue to create cluster."
return {}
content = {
"name": name,
"hypervisorType": "KVM",
"zoneUuid": zoneUuid,
# "resourceUuid": resourceUuid,
"description": "with ops, auto create " + name,
# "type": type
}
rsp = self.base.api_call(self.UUID, "org.zstack.header.cluster.APICreateClusterMsg", 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_cluster(self, uuid):
'''
request: {
"org.zstack.header.cluster.APIDeleteClusterMsg":
{
"session": {"uuid": "b1de35faa6eb4c4298dd92b89119516e"},
"uuid": "1e3aedbc71d44d1cafa5ff123c988cb5"
}
}
response: {
"org.zstack.header.cluster.APIDeleteClusterEvent":
{"success":true}
}
:param uuid: Cluster UUID
:return:
'''
content = {"uuid": uuid}
rsp = self.base.api_call(self.UUID, "org.zstack.header.cluster.APIDeleteClusterMsg", content)
# self.error_if_fail(rsp)
job_uuid = rsp['uuid']
status = self.base.query_until_done(job_uuid)
print "successfully delete %s, status %s" % (uuid, status)
# return self.UUID
# pass
def change_cluster_state(self, uuid, stateEvent):
'''
request: {
"org.zstack.header.cluster.APIChangeClusterStateMsg":
{
"session": {"uuid": "b1de35faa6eb4c4298dd92b89119516e"},
"uuid": "73a5d13f020d450e997f2aa04350ac3c",
"stateEvent": "disable"
}
}
response: {
"org.zstack.header.cluster.APIChangeClusterStateEvent":
{
"inventory":
{
"name":"cluster-look",
"uuid":"73a5d13f020d450e997f2aa04350ac3c",
"state":"Disabled",
"hypervisorType":"KVM",
"createDate":"Sep 27, 2016 10:10:40 PM",
"lastOpDate":"Sep 27, 2016 11:08:43 PM",
"zoneUuid":"46627d3eafb9462dbafa35c96d9db302",
"type":"zstack"
},
"success":true
}
}
:param uuid: 集群的uuid
:param stateEvent: enable/disable
:return:
'''
content = {
"uuid": uuid,
"stateEvent": stateEvent
}
rsp = self.base.api_call(self.UUID, "org.zstack.header.cluster.APIChangeClusterStateMsg", content)
# self.error_if_fail(rsp)
job_uuid = rsp['uuid']
status = self.base.query_until_done(job_uuid)
print "successfully change cluster state %s, status %s" % (uuid, status)
def attach_primary_storage_to_cluster(self, uuid, psuuid):
'''
request: {
"org.zstack.header.storage.primary.APIAttachPrimaryStorageToClusterMsg":
{
"primaryStorageUuid": "8438a03dc6e04be38dae8d56207508b2",
"session": {"uuid": "b1de35faa6eb4c4298dd92b89119516e"},
"clusterUuid": "f6cf5efe72924ccf90b771d32f610f19"
}
}
response: {
"org.zstack.header.storage.primary.APIAttachPrimaryStorageToClusterEvent":
{
"inventory":
{
"uuid":"8438a03dc6e04be38dae8d56207508b2",
"zoneUuid":"4cc9847534384039b323e8d2fff4b606",
"name":"Primary_Storage-1",
"url":"/data",
"description":"???01 - 10.0.1.221 ????",
"totalCapacity":11998530109440,
"availableCapacity":11998530109440,
"totalPhysicalCapacity":11998530109440,
"availablePhysicalCapacity":11940231933952,
"systemUsedCapacity":58298175488,
"type":"LocalStorage",
"state":"Enabled",
"status":"Connected",
"mountPath":"/data",
"createDate":"Sep 26, 2016 11:28:56 AM",
"lastOpDate":"Sep 27, 2016 10:37:10 PM",
"attachedClusterUuids":["f6cf5efe72924ccf90b771d32f610f19"]
},
"success":true
}
}
:param uuid: 集群(Cluster)的uuid
:param psuuid: 主存储(Primary Storage)的uuid
:return:
'''
content = {
"clusterUuid": uuid,
"primaryStorageUuid": psuuid
}
rsp = self.base.api_call(self.UUID, "org.zstack.header.storage.primary.APIAttachPrimaryStorageToClusterMsg", content)
# self.error_if_fail(rsp)
job_uuid = rsp['uuid']
status = self.base.query_until_done(job_uuid)
print "successfully Attach Primary Sstorage %s, status %s" % (uuid, status)
def detach_primary_storage_from_cluster(self, uuid, psuuid):
'''
:param uuid: 集群(Cluster)的uuid
:param psuuid: 主存储(Primary Storage)的uuid
:return:
'''
content = {
"clusterUuid": uuid,
"primaryStorageUuid": psuuid
}
rsp = self.base.api_call(self.UUID, "org.zstack.header.storage.primary.APIDetachPrimaryStorageFromClusterMsg", content)
# self.error_if_fail(rsp)
job_uuid = rsp['uuid']
status = self.base.query_until_done(job_uuid)
print "successfully Detach Primary Storage %s, status %s" % (uuid, status)
def attach_l2network_to_cluster(self, uuid, l2uuid):
content = {
"clusterUuid": uuid,
"l2NetworkUuid": l2uuid
}
rsp = self.base.api_call(self.UUID, "org.zstack.header.network.primary.APIAttachL2NetworkToClusterMsg", content)
# self.error_if_fail(rsp)
job_uuid = rsp['uuid']
status = self.base.query_until_done(job_uuid)
print "successfully Attach L2Network To Cluster %s, status %s" % (uuid, status)
# pass
def detach_l2network_from_cluster(self, uuid, l2uuid):
content = {
"clusterUuid": uuid,
"l2NetworkUuid": l2uuid
}
rsp = self.base.api_call(self.UUID, "org.zstack.header.network.primary.APIDetachL2NetworkFromClusterMsg",
content)
# self.error_if_fail(rsp)
job_uuid = rsp['uuid']
status = self.base.query_until_done(job_uuid)
print "successfully Detach L2Network From Cluster %s, status %s" % (uuid, status)
# pass
def query_cluster_by_clusteruuid(self, clusterUuid):
'''
request: {
"org.zstack.header.cluster.APIQueryClusterMsg":
{
"session":{"uuid": "68b7f784500640feac28c09c2015a0b4"},
"conditions": [{"name": "uuid", "value": "f6cf5efe72924ccf90b771d32f610f19", "op": "="}]
}
}
response: {
"org.zstack.header.cluster.APIQueryClusterReply":
{
"inventories":
[
{
"name":"cluster-zb01",
"uuid":"f6cf5efe72924ccf90b771d32f610f19",
"description":"?????",
"state":"Enabled",
"hypervisorType":"KVM",
"createDate":"Sep 26, 2016 11:25:28 AM",
"lastOpDate":"Sep 27, 2016 11:16:25 PM",
"zoneUuid":"4cc9847534384039b323e8d2fff4b606",
"type":"zstack"
}
],
"success":true
}
}
:param clusterUuid:
:return:
'''
content = {
"conditions": [
{
"name": "uuid",
"value": clusterUuid,
"op": "="
}
]
}
rsp = self.base.api_call(self.UUID, "org.zstack.header.cluster.APIQueryClusterMsg",
content)
self.base.error_if_fail(rsp)
# job_uuid = rsp['uuid']
# status = self.query_until_done(job_uuid)
print "successfully Query Cluster by cluster uuid %s" % (clusterUuid)
print rsp
def query_cluster_by_clustername(self, clusterName):
'''
request: {
"org.zstack.header.cluster.APIQueryClusterMsg":
{
"session":{"uuid": "68b7f784500640feac28c09c2015a0b4"},
"conditions": [{"name": "name", "value": "cluster-zb", "op": "="}]
}
}
response: {
"org.zstack.header.cluster.APIQueryClusterReply":
{
"inventories":
[
{
"name":"cluster-zb01",
"uuid":"f6cf5efe72924ccf90b771d32f610f19",
"description":"?????",
"state":"Enabled",
"hypervisorType":"KVM",
"createDate":"Sep 26, 2016 11:25:28 AM",
"lastOpDate":"Sep 27, 2016 11:16:25 PM",
"zoneUuid":"4cc9847534384039b323e8d2fff4b606",
"type":"zstack"
}
],
"success":true
}
}
:param clusterUuid:
:return:
'''
content = {
"conditions": [
{
"name": "name",
"value": clusterName,
"op": "="
}
]
}
rsp = self.base.api_call(self.UUID, "org.zstack.header.cluster.APIQueryClusterMsg",content)
self.base.error_if_fail(rsp)
# job_uuid = rsp['uuid']
# status = self.query_until_done(job_uuid)
print "successfully Query Cluster by cluster name %s" % (clusterName)
print rsp
def query_cluster_by_zoneuuid(self, zoneUuid):
'''
request: {
"org.zstack.header.cluster.APIQueryClusterMsg":
{
"session":{"uuid": "68b7f784500640feac28c09c2015a0b4"},
"conditions": [{"name": "zoneUuid", "value": "4cc9847534384039b323e8d2fff4b606", "op": "="}]
}
}
response: {
"org.zstack.header.cluster.APIQueryClusterReply":
{
"inventories":
[
{
"name":"cluster-zb01",
"uuid":"f6cf5efe72924ccf90b771d32f610f19",
"description":"?????",
"state":"Enabled",
"hypervisorType":"KVM",
"createDate":"Sep 26, 2016 11:25:28 AM",
"lastOpDate":"Sep 27, 2016 11:16:25 PM",
"zoneUuid":"4cc9847534384039b323e8d2fff4b606",
"type":"zstack"
}
],
"success":true
}
}
:param clusterUuid:
:return:
'''
content = {
"conditions": [
{
"name": "zoneUuid",
"value": zoneUuid,
"op": "="
}
]
}
rsp = self.base.api_call(self.UUID, "org.zstack.header.cluster.APIQueryClusterMsg",
content)
self.base.error_if_fail(rsp)
# job_uuid = rsp['uuid']
# status = self.query_until_done(job_uuid)
print "successfully Query Cluster by zone uuid %s" % (zoneUuid)
print rsp
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_cluster_api()
try:
# session_uuid = new_zs.login()
new_zs.create_cluster('cluster-16lou', 'c9df649b419243b597b1dec99b90833f')
# new_zs.delete_cluster('6c965d8775f440efa18471dec8e3b12a')
# new_zs.change_cluster_state('f6cf5efe72924ccf90b771d32f610f19', 'enable')
# new_zs.attach_primary_storage_to_cluster('f6cf5efe72924ccf90b771d32f610f19', '8438a03dc6e04be38dae8d56207508b2')
# new_zs.detach_primary_storage_from_cluster('f6cf5efe72924ccf90b771d32f610f19', '8438a03dc6e04be38dae8d56207508b2')
# zone_list = new_zs.query_cluster_by_clustername('cluster-zb01')
# zone_list = new_zs.query_cluster_by_clusteruuid('f6cf5efe72924ccf90b771d32f610f19')
# new_zs.attach_l2network_to_cluster('f6cf5efe72924ccf90b771d32f610f19', 'c75585d91b4b4ce5a251dd3ef471302d')
except Exception, e:
print "执行出错!", e
new_zs.logout(new_zs.UUID)