-
Notifications
You must be signed in to change notification settings - Fork 1
/
zstack_PS_api.py
238 lines (209 loc) · 8.31 KB
/
zstack_PS_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
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
#!/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_primarystorage_api:
def __init__(self):
self.header = {"Content-Type": "application/json"}
self.base = zstack_base_api()
self.UUID = self.base.UUID
def add_nfs_primarystorage(self, name, zoneUuid, url):
content = {
"name": name,
"url": url,
"zoneUuid": zoneUuid,
"description": "Add NFS Primary Storage %s" % name
}
rsp = self.base.api_call(self.UUID, "org.zstack.storage.primary.local.APIAddNfsPrimaryStorageMsg", content)
# self.error_if_fail(rsp)
job_uuid = rsp['uuid']
status = self.base.query_until_done(job_uuid)
print "successfully Add NFS Primary Storage %s, status %s" % (name, status)
# pass
def add_local_primarystorage(self, name, zoneUuid, url):
'''
request: {
"org.zstack.storage.primary.local.APIAddLocalPrimaryStorageMsg": {
"url": "/home",
"zoneUuid": "4cc9847534384039b323e8d2fff4b606",
"name": "localPrimarySotrage01",
"session": {"uuid": "f63ca5149b154bc4942c374ca35f1209"}
}
}
:param name: 资源名字;
:param zoneUuid: 父区域的uuid;
:param url: 本地存储需要的路径,如“/home”,分区等;
:return:
'''
content = {
"name": name,
"url": url,
"zoneUuid": zoneUuid,
"description": "Add local Primary Storage %s" % name
}
rsp = self.base.api_call(self.UUID, "org.zstack.storage.primary.local.APIAddLocalPrimaryStorageMsg", content)
# self.error_if_fail(rsp)
job_uuid = rsp['uuid']
status = self.base.query_until_done(job_uuid)
print "successfully Add local Primary Storage %s, status %s" % (name, status)
def delete_primarystorage(self, uuid):
'''
request: {
"org.zstack.header.storage.primary.APIDeletePrimaryStorageMsg": {
"uuid": "77b820cf4bc247048c4495207be5a34e",
"session": {"uuid": "460369df1444440c8fe8560af425bd81"}
}
}
:param uuid: Primary Storage uuid
:return:
'''
content = {
"uuid": uuid
}
rsp = self.base.api_call(self.UUID, "org.zstack.header.storage.primary.APIDeletePrimaryStorageMsg", content)
# self.error_if_fail(rsp)
job_uuid = rsp['uuid']
status = self.base.query_until_done(job_uuid)
print "successfully delete Primary Storage %s, status %s" % (uuid, status)
# return self.UUID
# pass
def change_primarystorage_state(self, uuid, stateEvent):
'''
request: {
"org.zstack.header.storage.primary.APIChangePrimaryStorageStateMsg": {
"session": {"uuid": "f63ca5149b154bc4942c374ca35f1209"},
"uuid": "82cf947ae8b44c479c5c30d183e7ae39",
"stateEvent": "disable"
}
}
:param uuid: 主机的uuid
:param stateEvent: enable/disable
:return:
'''
content = {
"uuid": uuid,
"stateEvent": stateEvent
}
rsp = self.base.api_call(self.UUID, "org.zstack.header.storage.primary.APIChangePrimaryStorageStateMsg", content)
# self.error_if_fail(rsp)
job_uuid = rsp['uuid']
status = self.base.query_until_done(job_uuid)
print "successfully Change Primary Storage State %s to %s, status %s" % (uuid, stateEvent, status)
# pass
def reconnect_primarystorage(self, uuid):
'''
request: {
"org.zstack.header.storage.primary.APIReconnectPrimaryStorageMsg": {
"uuid": "82cf947ae8b44c479c5c30d183e7ae39",
"session": {"uuid": "460369df1444440c8fe8560af425bd81"}
}
}
:param uuid: 主存储的uuid
:return:
'''
content = {
"uuid": uuid
}
rsp = self.base.api_call(self.UUID, "org.zstack.header.storage.primary.APIReconnectPrimaryStorageMsg", content)
# self.error_if_fail(rsp)
job_uuid = rsp['uuid']
status = self.base.query_until_done(job_uuid)
print "successfully Reconnect Primary Storage %s, status %s" % (uuid, status)
# pass
def query_primarystorage_by_uuid(self, uuid):
'''
request: {
"org.zstack.header.storage.primary.APIQueryPrimaryStorageMsg": {
"session": {"uuid": "f925c7ebb98245ac9a78d0e7a37cb890"},
"conditions": [{"name": "uuid", "value": "82cf947ae8b44c479c5c30d183e7ae39", "op": "="}]
}
}
:param managementIP:
:return:
'''
content = {
"conditions": [
{
"name": "uuid",
"value": uuid,
"op": "="
}
]
}
rsp = self.base.api_call_other(self.UUID, "org.zstack.header.storage.primary.APIQueryPrimaryStorageMsg", content)
self.base.error_if_fail(rsp)
# job_uuid = rsp['uuid']
# status = self.query_until_done(job_uuid)
print "successfully query Primary Storage by uuid %s, result: %s" % (uuid, rsp)
return rsp
# pass
def query_primarystorage_by_name(self, name):
'''
request: {
"org.zstack.header.storage.primary.APIQueryPrimaryStorageMsg": {
"session": {"uuid": "f925c7ebb98245ac9a78d0e7a37cb890"},
"conditions": [{"name": "name", "value": "ps-yuan", "op": "="}]
}
}
:param name: Primary Storage 名称
:return:
'''
content = {
"conditions": [
{
"name": "name",
"value": name,
"op": "="
}
]
}
rsp = self.base.api_call(self.UUID, "org.zstack.header.storage.primary.APIQueryPrimaryStorageMsg", content)
self.base.error_if_fail(rsp)
# job_uuid = rsp['uuid']
# status = self.query_until_done(job_uuid)
print "successfully query Primary Storage by name %s, result: %s" % (name, rsp)
return rsp
def query_all_primarystorage(self):
'''
request: {
"org.zstack.header.storage.primary.APIQueryPrimaryStorageMsg": {
"session": {"uuid": "f63ca5149b154bc4942c374ca35f1209"},
"conditions": []
}
}
:return:
'''
content = {
"conditions": []
}
rsp = self.base.api_call(self.UUID, "org.zstack.header.storage.primary.APIQueryPrimaryStorageMsg", content)
self.base.error_if_fail(rsp)
# job_uuid = rsp['uuid']
# status = self.query_until_done(job_uuid)
print "successfully Query All Primary Storage %s" % (rsp)
return 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_primarystorage_api()
try:
# session_uuid = new_zs.login()
# new_zs.add_local_primarystorage('ps-yuan', 'c9df649b419243b597b1dec99b90833f', url='/home')
# new_zs.delete_primarystorage('8352683f89fa46ad843986d54d2bbc31')
# new_zs.change_primarystorage_state('82cf947ae8b44c479c5c30d183e7ae39', 'enable')
# new_zs.reconnect_primarystorage('82cf947ae8b44c479c5c30d183e7ae39')
# new_zs.query_all_primarystorage()
new_zs.query_primarystorage_by_name('ps-yuan')
new_zs.query_primarystorage_by_uuid('82cf947ae8b44c479c5c30d183e7ae39')
except Exception,e:
print "执行错误!", e
new_zs.logout(new_zs.UUID)