-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathzstack_volume_api.py
More file actions
322 lines (294 loc) · 12.3 KB
/
zstack_volume_api.py
File metadata and controls
322 lines (294 loc) · 12.3 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
#!/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_volume_api:
def __init__(self):
self.header = {"Content-Type": "application/json"}
self.base = zstack_base_api()
self.UUID = self.base.UUID
def create_dataVolume_from_diskOffering(self, name, diskOfferingUuid):
'''
request: {
"org.zstack.storage.backup.sftp.APIAddSftpBackupStorageMsg": {
"hostname": "10.0.89.18",
"username": "root",
"password": "Skt6edg",
"type": "SftpBackupStorage",
"resourceUuid": "8923e38182ca454482d820248ad8a53c",
"name": "bs-gzp5",
"description": null,
"url": "/root",
"session": {"uuid": "5c54fbf4e8784497a87eab47552a0edc"}
}
}
:param name: 资源名字;
:param hostname: Sftp服务器的主机名,一般情况需要输入服务器IP;
:param url: BS存储需要的路径,如“/home”,分区等;
:param username: Sftp服务器用户名;如“root”
:param password: Sftp服务器登录密码;
:return:
'''
content = {
"name": name,
"diskOfferingUuid": diskOfferingUuid,
"description": "Create a Data Volume %s" % name
}
rsp, rsp_code = self.base.api_call(self.UUID, "org.zstack.storage.backup.sftp.APIAddSftpBackupStorageMsg", content)
if rsp_code == 200:
# self.error_if_fail(rsp)
job_uuid = rsp['uuid']
status = self.base.query_until_done(job_uuid)
print "successfully Create a Data Volume %s From a Disk Offering, status %s" % (name, status)
else:
print "Found Error in API requests.pls check!"
def create_dataVolume_from_volume_template(self, name, imageUuid, primaryStorageUuid):
content = {
"name": name,
"imageUuid": imageUuid,
"primaryStorageUuid": primaryStorageUuid,
"description": "Create a Data Volume %s" % name
}
rsp, rsp_code = self.base.api_call(self.UUID, "org.zstack.storage.backup.sftp.APIAddSftpBackupStorageMsg", content)
if rsp_code == 200:
# self.error_if_fail(rsp)
job_uuid = rsp['uuid']
status = self.base.query_until_done(job_uuid)
print "successfully Create Data Volume %s From an Image, status %s" % (name, status)
else:
print "Found Error in API requests.pls check!"
def create_dataVolume_from_volume_template(self, name, volumeSnapshotUuid, primaryStorageUuid):
content = {
"name": name,
"volumeSnapshotUuid": volumeSnapshotUuid,
"primaryStorageUuid": primaryStorageUuid,
"description": "Create a Data Volume %s" % name
}
rsp, rsp_code = self.base.api_call(self.UUID, "org.zstack.storage.backup.sftp.APIAddSftpBackupStorageMsg", content)
if rsp_code == 200:
# self.error_if_fail(rsp)
job_uuid = rsp['uuid']
status = self.base.query_until_done(job_uuid)
print "successfully Create Data Volume %s From a Volume Snapshot, status %s" % (name, status)
else:
print "Found Error in API requests.pls check!"
def delete_dataVolume(self, uuid):
'''
request: {
"org.zstack.header.storage.backup.APIDeleteBackupStorageMsg": {
"uuid": "8923e38182ca454482d820248ad8a53c",
"session": {
"uuid": "5c54fbf4e8784497a87eab47552a0edc"
}
}
}
:param uuid: Backup Storage uuid 备份存储的uuid
:return:
'''
content = {
"uuid": uuid
}
rsp, rsp_code = self.base.api_call(self.UUID, "org.zstack.header.storage.backup.APIDeleteBackupStorageMsg", content)
if rsp_code == 200:
# self.error_if_fail(rsp)
job_uuid = rsp['uuid']
status = self.base.query_until_done(job_uuid)
print "successfully Delete Data Volume %s, status %s" % (uuid, status)
else:
print "Found Error in API requests.pls check!"
# pass
def change_volume_state(self, uuid, stateEvent):
'''
request: {
"org.zstack.header.storage.backup.APIChangeBackupStorageStateMsg": {
"session": {"uuid": "f63ca5149b154bc4942c374ca35f1209"},
"uuid": "82cf947ae8b44c479c5c30d183e7ae39",
"stateEvent": "disable"
}
}
:param uuid: 备份存储的uuid
:param stateEvent: enable/disable
:return:
'''
content = {
"uuid": uuid,
"stateEvent": stateEvent
}
rsp, rsp_code = self.base.api_call(self.UUID, "org.zstack.header.storage.backup.APIChangeBackupStorageStateMsg", content)
if rsp_code == 200:
# self.error_if_fail(rsp)
job_uuid = rsp['uuid']
status = self.base.query_until_done(job_uuid)
print "successfully Change Volume State %s to %s, status %s" % (uuid, stateEvent, status)
else:
print "Found Error in API requests.pls check!"
# pass
def attach_dataVolume_to_Vm(self, volumeUuid, vmInstanceUuid):
"""
request:{
"org.zstack.header.storage.backup.APIAttachBackupStorageToZoneMsg": {
"zoneUuid": "c9df649b419243b597b1dec99b90833f",
"backupStorageUuid": "70a82158faf241e98af898a79d906490",
"session": {
"uuid": "2f918a2b95624f88ae00f524242aeb4a"
}
}
}
:param backupStorageUuid:
:param zoneUuid:
:return:
"""
content = {
"volumeUuid": volumeUuid,
"vmInstanceUuid": vmInstanceUuid
}
rsp, rsp_code = self.base.api_call(self.UUID, "org.zstack.header.storage.backup.APIAttachBackupStorageToZoneMsg", content)
if rsp_code == 200:
# self.error_if_fail(rsp)
job_uuid = rsp['uuid']
status = self.base.query_until_done(job_uuid)
print "successfully Attach DataVolume %s To Vm %s, status %s" % (volumeUuid, vmInstanceUuid, status)
else:
print "Found Error in API requests.pls check!"
def detach_dataVolume_from_Vm(self, uuid):
"""
request:{
"org.zstack.header.storage.backup.APIDetachBackupStorageFromZoneMsg": {
"zoneUuid": "c9df649b419243b597b1dec99b90833f",
"backupStorageUuid": "70a82158faf241e98af898a79d906490",
"session": {
"uuid": "2f918a2b95624f88ae00f524242aeb4a"
}
}
}
:param backupStorageUuid:
:param zoneUuid:
:return:
"""
content = {
"uuid": uuid
}
rsp, rsp_code = self.base.api_call(self.UUID, "org.zstack.header.storage.backup.APIDetachBackupStorageFromZoneMsg",
content)
if rsp_code == 200:
# self.error_if_fail(rsp)
job_uuid = rsp['uuid']
status = self.base.query_until_done(job_uuid)
print "successfully Detach DataVolume State %s From Vm %s, status %s" % (backupStorageUuid, zoneUuid, status)
else:
print "Found Error in API requests.pls check!"
def query_backupstorage_by_uuid(self, uuid):
'''
request: {
"org.zstack.header.storage.backup.APIQueryBackupStorageMsg": {
"session": {"uuid": "f63ca5149b154bc4942c374ca35f1209"},
"conditions": [{"name": "uuid", "value": "70a82158faf241e98af898a79d906490", "op": "="},{"name": "state", "value": "enable", "op": "="}]
}
}
:param name: Backup Storage 名称
:return:
'''
content = {
"conditions": [
{
"name": "uuid",
"value": uuid,
"op": "="
}
]
}
rsp, rsp_code = self.base.api_call(self.UUID, "org.zstack.header.storage.backup.APIQueryBackupStorageMsg", content)
if rsp_code == 200:
self.base.error_if_fail(rsp)
# job_uuid = rsp['uuid']
# status = self.query_until_done(job_uuid)
print "successfully query Backup Storage by uuid %s, result: %s" % (uuid, rsp)
return rsp
else:
print "Found Error in API requests. Pls check!"
def query_backupstorage_by_name(self, name):
'''
request: {
"org.zstack.header.storage.backup.APIQueryBackupStorageMsg": {
"session": {"uuid": "f63ca5149b154bc4942c374ca35f1209"},
"conditions": [{"name": "name", "value": "zone-yu", "op": "="},{"name": "state", "value": "enable", "op": "="}]
}
}
:param name: Backup Storage 名称
:return:
'''
content = {
"conditions": [
{
"name": "name",
"value": name,
"op": "="
}
]
}
rsp, rsp_code = self.base.api_call(self.UUID, "org.zstack.header.storage.backup.APIQueryBackupStorageMsg", content)
if rsp_code == 200:
self.base.error_if_fail(rsp)
# job_uuid = rsp['uuid']
# status = self.query_until_done(job_uuid)
print "successfully query Backup Storage by name %s, result: %s" % (name, rsp)
return rsp
else:
print "Found Error in API requests. Pls check!"
# Query Backup Storage
def query_all_backupstorage(self):
'''
request: {
"org.zstack.header.storage.backup.APIQueryBackupStorageMsg": {
"session": {"uuid": "f63ca5149b154bc4942c374ca35f1209"},
"conditions": []
}
}
:return:
'''
content = {
"conditions": []
}
rsp, rsp_code = self.base.api_call(self.UUID, "org.zstack.header.storage.backup.APIQueryBackupStorageMsg", content)
if rsp_code == 200:
self.base.error_if_fail(rsp)
# job_uuid = rsp['uuid']
# status = self.query_until_done(job_uuid)
print "successfully Query All Backup Storage %s" % (rsp)
return rsp
else:
print "Found Error in API requests. Pls check!"
def tags_host(self, tags):
pass
def logout(self, session_uuid):
content = {"sessionUuid": session_uuid}
rsp, rsp_code = self.base.api_call(None, "org.zstack.header.identity.APILogOutMsg", content)
if rsp_code == 200:
self.base.error_if_fail(rsp)
print "successfully logout"
else:
print "Found Error in API requests. Pls check"
if __name__ == '__main__':
new_zs = zstack_backup_storage_api()
try:
# session_uuid = new_zs.login()
# new_zs.add_sftp_backupstorage('bs_yuan', '/root', '10.0.89.18', 'root', 'Skt6edg')
# new_zs.delete_backupstorage('0e3292931b88447cb991483890df404c')
# new_zs.change_backupstorage_state('70a82158faf241e98af898a79d906490', 'enable')
# new_zs.reconnect_backupstorage('70a82158faf241e98af898a79d906490')
# new_zs.detach_backup_storage_from_zone('70a82158faf241e98af898a79d906490', 'c9df649b419243b597b1dec99b90833f')
# new_zs.attach_backup_storage_to_zone('70a82158faf241e98af898a79d906490', 'c9df649b419243b597b1dec99b90833f')
# new_zs.query_all_backupstorage()
new_zs.query_backupstorage_by_uuid(uuid='70a82158faf241e98af898a79d906490')
# new_zs.query_backupstorage_by_name('Backup_Storage-1')
except Exception, e:
print "执行错误!", e
new_zs.logout(new_zs.UUID)