-
Notifications
You must be signed in to change notification settings - Fork 0
/
psar.py
378 lines (281 loc) · 13.8 KB
/
psar.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
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
from requests import get, put, delete, patch, post
import urllib
from keystoneclient.v2_0 import client
import argparse
'''
Client for the PSAR API. All methods return a Response object.
TO-DO: Currently only tested without authentication.
'''
class Client:
def __init__(self,base_url):
self.base_url=base_url+'/v1/'
def get_token(self,user,password,tenant,auth_URL):
keystone = client.Client(username=user, password=password, tenant_name=tenant, auth_url=auth_URL)
return keystone.get_raw_token_from_identity_service(auth_url=auth_URL,username=user, password=password,tenant_name=tenant).auth_token
def add_token_param(self,token,first_param): #TO-DO:Change authentication so that it goes on the header instead of url
return ('?' if first_param else '&')+urllib.urlencode({'auth_token':token})
#Status
def get_status(self,token=None):
url = self.base_url+'status'
if token:
url += add_token_param(token=token,first_param=True)
return get(url)
#PSA
def create_psa(self,name=None,token=None):
url = self.base_url+'PSA/images/'
if token:
url +=add_token_param(token=token,first_param=True)
if name:
url += '&name='+name
return post(url)
def delete_psa(self,image_id,token=None):
url = self.base_url+'PSA/images/'+image_id+'/'
if token:
url +=add_token_param(token=token,first_param=True)
return delete(url)
def get_image_list(self, id=None,token=None):
url = self.base_url+'PSA/images/'
if id:
url += '?id='+id
if token:
url += add_token_param(token=token,first_param=not id)
return get(url)
#Manifest
def get_manifest(self, image_id,token=None):
url = self.base_url+'PSA/manifest/'+image_id
if token:
url += add_token_param(token=token,first_param=True)
return get(url)
def delete_manifest (self,image_id,token=None):
url = self.base_url+'PSA/manifest/'+image_id+'/file'
if token:
url += add_token_param(token=token,first_param=True)
return delete(url)
def put_manifest_file(self,image_id,path,token=None):
with open(path,'rb') as f:
url=self.base_url+'PSA/manifest/'+str(image_id)+'/file'
if token:
url += add_token_param(token=token,first_param=True)
files={'file':f}
return put(url,files=files)
#Images
def get_image_file(self,image_id, path,token=None):
url = self.base_url+'PSA/images/'+image_id+'/file'
if token:
url += add_token_param(token=token,first_param=True)
r=get(url)
if r.status_code == 200:
with open(path, 'wb') as f:
for chunk in r.iter_content(chunk_size=1024):
f.write(chunk)
return r
def put_image_file(self, image_id, path, disk_format, container_format,token=None):
url=self.base_url+'PSA/images/'+image_id+'/file?'+urllib.urlencode({"disk_format":disk_format,'container_format':container_format})
if token:
url += add_token_param(token=token,first_param=False)
with open (path, 'rb') as f:
files={'file':f}
return put(url,files=files)
def put_image(self,image_id,name=None,token=None):
url = self.base_url+'PSA/images/'+image_id+'/'
if token:
url += add_token_param(token=token,first_param=True)
if name:
url += '&name='+name
return put(url)
def delete_image(self, image_id,token=None):
url = self.base_url+'PSA/images/'+image_id+'/file'
if token:
url += add_token_param(token=token,first_param=True)
return delete(url)
def get_image_location (self,image_id,token=None):
url = self.base_url+'PSA/images/'+image_id+'/image_location'
if token:
url += add_token_param(token=token,first_param=True)
return get(url)
def patch_image(self, image_id, new_status,token=None):
url = self.base_url+'PSA/images/'+image_id+'/?status='+new_status
if token:
url += add_token_param(token=token,first_param=False)
return patch(url)
#Plugin
def get_plugin_file(self,image_id,path,token=None):
url =self.base_url+'PSA/M2Lplugins/'+image_id+'/'
if token:
url += add_token_param(token=token,first_param=True)
r=get(url)
if r.status_code == 200:
with open(path, 'wb') as f:
for chunk in r.iter_content(chunk_size=1024):
f.write(chunk)
return r
def put_plugin_file(self,image_id,path,token=None):
url=self.base_url+'PSA/M2Lplugins/'+image_id+'/file'
if token:
url += add_token_param(token=token,first_param=True)
with open(path,'rb') as f:
files={'file':f}
return put(url,files=files)
def put_plugin(self, image_id,name=None,new_url=None,token=None):
url=self.base_url+'PSA/M2Lplugins/'+image_id+'/'
if name:
url += '?name='+name
if token:
url += add_token_param(token=token,first_param=not(name))
if new_url:
url+='&url='+new_url
def delete_plugin(self, image_id,token=None):
url = self.base_url+'PSA/M2Lplugins/'+image_id+'/file'
if token:
url += add_token_param(token=token,first_param=True)
return delete(url)
def get_plugin_location (self, image_id,token=None):
url = self.base_url+'PSA/M2Lplugins/'+image_id+'/plugin_location'
if token:
url += add_token_param(token=token,first_param=True)
return get(url)
#PSARL
def put_psarl_location(self, psarl_id,new_location,token=None):
url = self.base_url+'PSARLs/'+psarl_id+'/?location='+new_location
if token:
url += add_token_param(token=token,first_param=False)
return put(url)
if __name__=='__main__':
#TO-DO: Take arguments (such as the url of the psar) from environment
#Functions
def list_images(args):
c=Client(args.url)
r=c.get_image_list(token=args.token)
print r.content
def download_image(args):
c=Client(args.url)
c.get_image_file(path=args.path,image_id=args.id,token=args.token)
def upload_image(args):
c=Client(args.url)
c.put_image_file(image_id=args.id,path=args.path,disk_format=args.disk_format,container_format=args.container_format,token=args.token)
def delete_image(args):
c=Client(args.url)
c.delete_image(image_id=args.id,token=args.token)
def download_manifest(args):
c=Client(args.url)
r=c.get_manifest(image_id=args.id,token=args.token)
with open(args.path,'w') as f:
f.write(r.content)
def upload_manifest(args):
c=Client(args.url)
c.put_manifest_file(image_id=str(args.id),path=args.path,token=args.token)
def delete_manifest(args):
c=Client(args.url)
c.delete_manifest_file(image_id=args.id,token=args.token)
def download_plugin(args):
c=Client(args.url)
c.get_plugin_file(path=args.path,image_id=args.id,token=args.token)
def upload_plugin(args):
c=Client(args.url)
c.put_plugin_file(image_id=args.id,path=args.path,token=args.token)
def delete_plugin(args):
c=Client(args.url)
c.delete_plugin_file(image_id=args.id,token=args.token)
def create_psa(args):
c=Client(args.url)
r=c.create_psa()
print r.text
def delete_psa(args):
c=Client(args.url)
r=c.delete_psa(args.id)
#General
parser=argparse.ArgumentParser(description="Command line client for the PSAR API")
subparsers = parser.add_subparsers(help='groups')
create_parser=subparsers.add_parser('create',help='Creates a new empty PSA')
create_parser.add_argument('--url',action='store', required=True, help='URL of the PSAR')
create_parser.add_argument('--token',action='store', help='Authentication token')
create_parser.set_defaults(func=create_psa)
delete_parser=subparsers.add_parser('delete',help='Deletes the PSA')
delete_parser.add_argument('--url',action='store', required=True, help='URL of the PSAR')
delete_parser.add_argument('id',action='store', help='ID of the PSA to delete')
delete_parser.add_argument('--token',action='store', help='Authentication token')
delete_parser.set_defaults(func=delete_psa)
#Images
image_parser=subparsers.add_parser('image',help='Interacts with the images')
image_subparser=image_parser.add_subparsers(help='commands')
# List
list_image_parser = image_subparser.add_parser('list', help='List images stored')
list_image_parser.add_argument('--id', action='store', help='Returns only matching PSAs')
list_image_parser.add_argument('--url',action='store', required=True, help='URL of the PSAR')
list_image_parser.add_argument('--token',action='store', help='Authentication token')
list_image_parser.set_defaults(func=list_images)
# Download
download_image_parser=image_subparser.add_parser('download',help='Download an image')
download_image_parser.add_argument('id', action='store', help='PSA to download')
download_image_parser.add_argument('path', action='store', help='Path')
download_image_parser.add_argument('--url',action='store', required=True, help='URL of the PSAR')
download_image_parser.add_argument('--token',action='store', help='Authentication token')
download_image_parser.set_defaults(func=download_image)
# Upload
upload_image_parser=image_subparser.add_parser('upload', help='Upload an image')
upload_image_parser.add_argument('id', action='store', help='PSA to upload')
upload_image_parser.add_argument('path', action='store', help='Path')
upload_image_parser.add_argument('--disk_format', required=True, choices=['qcow2', 'raw', 'vhd', 'vmdk', 'vdi', 'iso', 'aki','ari','ami'] , action='store', help='Disk format')
upload_image_parser.add_argument('--container_format', required=True, choices=['bare', 'ovf', 'aki', 'ari', 'ami'], action='store', help='Container format')
upload_image_parser.add_argument('--name', action='store', help='Name')
upload_image_parser.add_argument('--status', action='store', help='status')
upload_image_parser.add_argument('--manifest_id', action='store', help='Manifest ID')
upload_image_parser.add_argument('--storage_id', action='store', help='Storage ID')
upload_image_parser.add_argument('--plugin_id', action='store', help='Plugin ID')
upload_image_parser.add_argument('--url',action='store', required=True, help='URL of the PSAR')
upload_image_parser.add_argument('--token',action='store', help='Authentication token')
upload_image_parser.set_defaults(func=upload_image)
# Delete
delete_image_parser=image_subparser.add_parser('delete', help='Delete an image')
delete_image_parser.add_argument('id', action='store', help='PSA to delete')
delete_image_parser.add_argument('--url',action='store', required=True, help='URL of the PSAR')
delete_image_parser.add_argument('--token',action='store', help='Authentication token')
delete_image_parser.set_defaults(func=delete_image)
#Manifest
manifest_parser=subparsers.add_parser('manifest', help='Interacts with the manifests')
manifest_subparser=manifest_parser.add_subparsers(help='commands')
# Download
download_manifest_parser=manifest_subparser.add_parser('download',help='Download a manifest')
download_manifest_parser.add_argument('id', action='store', help='ID of the PSA which manifest is to be downloaded')
download_manifest_parser.add_argument('path', action='store', help='Path')
download_manifest_parser.add_argument('--url',action='store', required=True, help='URL of the PSAR')
download_manifest_parser.add_argument('--token',action='store', help='Authentication token')
download_manifest_parser.set_defaults(func=download_manifest)
# Upload
upload_manifest_parser=manifest_subparser.add_parser('upload', help='Upload a manifest')
upload_manifest_parser.add_argument('id', action='store', help='ID of the PSA which manifest is to be uploaded')
upload_manifest_parser.add_argument('path', action='store', help='Path')
upload_manifest_parser.add_argument('--url',action='store', required=True, help='URL of the PSAR')
upload_manifest_parser.add_argument('--token',action='store', help='Authentication token')
upload_manifest_parser.set_defaults(func=upload_manifest)
# Delete
delete_manifest_parser=manifest_subparser.add_parser('delete', help='Delete a manifest')
delete_manifest_parser.add_argument('id', action='store', help='ID of the PSA which manifest is to be deleted')
delete_manifest_parser.add_argument('--url',action='store', required=True, help='URL of the PSAR')
delete_manifest_parser.add_argument('--token',action='store', help='Authentication token')
delete_manifest_parser.set_defaults(func=delete_manifest)
#Plugins
plugin_parser=subparsers.add_parser('plugin', help='Interacts with the plugins')
plugin_subparser=plugin_parser.add_subparsers(help='commands')
# Download
download_plugin_parser=plugin_subparser.add_parser('download',help='Download a plugin')
download_plugin_parser.add_argument('id', action='store', help='PSA to download')
download_plugin_parser.add_argument('path', action='store', help='Path')
download_plugin_parser.add_argument('--url',action='store', required=True, help='URL of the PSAR')
download_plugin_parser.add_argument('--token',action='store', help='Authentication token')
download_plugin_parser.set_defaults(func=download_plugin)
# Upload
upload_plugin_parser=plugin_subparser.add_parser('upload', help='Upload a plugin')
upload_plugin_parser.add_argument('id', action='store', help='PSA to upload')
upload_plugin_parser.add_argument('path', action='store', help='Path')
upload_plugin_parser.set_defaults(func=upload_plugin)
upload_plugin_parser.add_argument('--url',action='store', required=True, help='URL of the PSAR')
upload_plugin_parser.add_argument('--token',action='store', help='Authentication token')
# Delete
delete_plugin_parser=plugin_subparser.add_parser('delete', help='Delete a plugin')
delete_plugin_parser.add_argument('id', action='store', help='PSA to delete')
delete_plugin_parser.add_argument('--url',action='store', required=True, help='URL of the PSAR')
delete_plugin_parser.add_argument('--token',action='store', help='Authentication token')
delete_plugin_parser.set_defaults(func=delete_plugin)
args= parser.parse_args()
args.func(args)