osm.py: fixed typo when checking client version
[osm/osmclient.git] / osmclient / scripts / osm.py
1 # Copyright 2017-2018 Sandvine
2 # Copyright 2018 Telefonica
3 #
4 # All Rights Reserved.
5 #
6 # Licensed under the Apache License, Version 2.0 (the "License"); you may
7 # not use this file except in compliance with the License. You may obtain
8 # a copy of the License at
9 #
10 # http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15 # License for the specific language governing permissions and limitations
16 # under the License.
17 """
18 OSM shell/cli
19 """
20
21 import click
22 from osmclient import client
23 from osmclient.common.exceptions import ClientException
24 from prettytable import PrettyTable
25 import yaml
26 import json
27 import time
28
29 def check_client_version(obj, what, version='sol005'):
30 '''
31 Checks the version of the client object and raises error if it not the expected.
32
33 :param obj: the client object
34 :what: the function or command under evaluation (used when an error is raised)
35 :return: -
36 :raises ClientError: if the specified version does not match the client version
37 '''
38 fullclassname = obj.__module__ + "." + obj.__class__.__name__
39 message = 'The following commands or options are only supported with the option "--sol005": {}'.format(what)
40 if version == 'v1':
41 message = 'The following commands or options are not supported when using option "--sol005": {}'.format(what)
42 if fullclassname != 'osmclient.{}.client.Client'.format(version):
43 raise ClientException(message)
44 return
45
46 @click.group()
47 @click.option('--hostname',
48 default=None,
49 envvar='OSM_HOSTNAME',
50 help='hostname of server. ' +
51 'Also can set OSM_HOSTNAME in environment')
52 @click.option('--so-port',
53 default=None,
54 envvar='OSM_SO_PORT',
55 help='hostname of server. ' +
56 'Also can set OSM_SO_PORT in environment')
57 @click.option('--so-project',
58 default=None,
59 envvar='OSM_SO_PROJECT',
60 help='Project Name in SO. ' +
61 'Also can set OSM_SO_PROJECT in environment')
62 @click.option('--ro-hostname',
63 default=None,
64 envvar='OSM_RO_HOSTNAME',
65 help='hostname of RO server. ' +
66 'Also can set OSM_RO_HOSTNAME in environment')
67 @click.option('--ro-port',
68 default=9090,
69 envvar='OSM_RO_PORT',
70 help='hostname of RO server. ' +
71 'Also can set OSM_RO_PORT in environment')
72 @click.option('--sol005',
73 is_flag=True,
74 envvar='OSM_SOL005',
75 help='Use ETSI NFV SOL005 API')
76 @click.pass_context
77 def cli(ctx, hostname, so_port, so_project, ro_hostname, ro_port, sol005):
78 if hostname is None:
79 print(
80 "either hostname option or OSM_HOSTNAME " +
81 "environment variable needs to be specified")
82 exit(1)
83 kwargs={}
84 if so_port is not None:
85 kwargs['so_port']=so_port
86 if so_project is not None:
87 kwargs['so_project']=so_project
88 if ro_hostname is not None:
89 kwargs['ro_host']=ro_hostname
90 if ro_port is not None:
91 kwargs['ro_port']=ro_port
92
93 ctx.obj = client.Client(host=hostname, sol005=sol005, **kwargs)
94
95
96 ####################
97 # LIST operations
98 ####################
99
100 @cli.command(name='ns-list')
101 @click.option('--filter', default=None,
102 help='restricts the list to the NS instances matching the filter')
103 @click.pass_context
104 def ns_list(ctx, filter):
105 '''list all NS instances'''
106 if filter:
107 check_client_version(ctx.obj, '--filter')
108 resp = ctx.obj.ns.list(filter)
109 else:
110 resp = ctx.obj.ns.list()
111 table = PrettyTable(
112 ['ns instance name',
113 'id',
114 'operational status',
115 'config status'])
116 for ns in resp:
117 fullclassname = ctx.obj.__module__ + "." + ctx.obj.__class__.__name__
118 if fullclassname == 'osmclient.sol005.client.Client':
119 nsr = ns
120 nsr_name = nsr['name']
121 nsr_id = nsr['_id']
122 else:
123 nsopdata = ctx.obj.ns.get_opdata(ns['id'])
124 nsr = nsopdata['nsr:nsr']
125 nsr_name = nsr['name-ref']
126 nsr_id = nsr['ns-instance-config-ref']
127 opstatus = nsr['operational-status'] if 'operational-status' in nsr else 'Not found'
128 configstatus = nsr['config-status'] if 'config-status' in nsr else 'Not found'
129 if configstatus == "config_not_needed":
130 configstatus = "configured (no charms)"
131 table.add_row(
132 [nsr_name,
133 nsr_id,
134 opstatus,
135 configstatus])
136 table.align = 'l'
137 print(table)
138
139
140 def nsd_list(ctx, filter):
141 if filter:
142 check_client_version(ctx.obj, '--filter')
143 resp = ctx.obj.nsd.list(filter)
144 else:
145 resp = ctx.obj.nsd.list()
146 #print yaml.safe_dump(resp)
147 table = PrettyTable(['nsd name', 'id'])
148 fullclassname = ctx.obj.__module__ + "." + ctx.obj.__class__.__name__
149 if fullclassname == 'osmclient.sol005.client.Client':
150 for ns in resp:
151 name = ns['name'] if 'name' in ns else '-'
152 table.add_row([name, ns['_id']])
153 else:
154 for ns in resp:
155 table.add_row([ns['name'], ns['id']])
156 table.align = 'l'
157 print(table)
158
159
160 @cli.command(name='nsd-list')
161 @click.option('--filter', default=None,
162 help='restricts the list to the NSD/NSpkg matching the filter')
163 @click.pass_context
164 def nsd_list1(ctx, filter):
165 '''list all NSD/NSpkg in the system'''
166 nsd_list(ctx,filter)
167
168
169 @cli.command(name='nspkg-list')
170 @click.option('--filter', default=None,
171 help='restricts the list to the NSD/NSpkg matching the filter')
172 @click.pass_context
173 def nsd_list2(ctx, filter):
174 '''list all NSD/NSpkg in the system'''
175 nsd_list(ctx,filter)
176
177
178 def vnfd_list(ctx, filter):
179 if filter:
180 check_client_version(ctx.obj, '--filter')
181 resp = ctx.obj.vnfd.list(filter)
182 else:
183 resp = ctx.obj.vnfd.list()
184 #print yaml.safe_dump(resp)
185 table = PrettyTable(['vnfd name', 'id'])
186 fullclassname = ctx.obj.__module__ + "." + ctx.obj.__class__.__name__
187 if fullclassname == 'osmclient.sol005.client.Client':
188 for vnfd in resp:
189 name = vnfd['name'] if 'name' in vnfd else '-'
190 table.add_row([name, vnfd['_id']])
191 else:
192 for vnfd in resp:
193 table.add_row([vnfd['name'], vnfd['id']])
194 table.align = 'l'
195 print(table)
196
197
198 @cli.command(name='vnfd-list')
199 @click.option('--filter', default=None,
200 help='restricts the list to the VNFD/VNFpkg matching the filter')
201 @click.pass_context
202 def vnfd_list1(ctx, filter):
203 '''list all VNFD/VNFpkg in the system'''
204 vnfd_list(ctx,filter)
205
206
207 @cli.command(name='vnfpkg-list')
208 @click.option('--filter', default=None,
209 help='restricts the list to the VNFD/VNFpkg matching the filter')
210 @click.pass_context
211 def vnfd_list2(ctx, filter):
212 '''list all VNFD/VNFpkg in the system'''
213 vnfd_list(ctx,filter)
214
215
216 @cli.command(name='vnf-list')
217 @click.pass_context
218 def vnf_list(ctx):
219 ''' list all VNF instances'''
220 resp = ctx.obj.vnf.list()
221 table = PrettyTable(
222 ['vnf name',
223 'id',
224 'operational status',
225 'config status'])
226 for vnfr in resp:
227 if 'mgmt-interface' not in vnfr:
228 vnfr['mgmt-interface'] = {}
229 vnfr['mgmt-interface']['ip-address'] = None
230 table.add_row(
231 [vnfr['name'],
232 vnfr['id'],
233 vnfr['operational-status'],
234 vnfr['config-status']])
235 table.align = 'l'
236 print(table)
237
238
239 ####################
240 # SHOW operations
241 ####################
242
243 def nsd_show(ctx, name, literal):
244 try:
245 resp = ctx.obj.nsd.get(name)
246 #resp = ctx.obj.nsd.get_individual(name)
247 except ClientException as inst:
248 print(inst.message)
249 exit(1)
250
251 if literal:
252 print yaml.safe_dump(resp)
253 return
254
255 table = PrettyTable(['field', 'value'])
256 for k, v in resp.items():
257 table.add_row([k, json.dumps(v, indent=2)])
258 table.align = 'l'
259 print(table)
260
261
262 @cli.command(name='nsd-show', short_help='shows the content of a NSD')
263 @click.option('--literal', is_flag=True,
264 help='print literally, no pretty table')
265 @click.argument('name')
266 @click.pass_context
267 def nsd_show1(ctx, name, literal):
268 '''shows the content of a NSD
269
270 NAME: name or ID of the NSD/NSpkg
271 '''
272 nsd_show(ctx, name, literal)
273
274
275 @cli.command(name='nspkg-show', short_help='shows the content of a NSD')
276 @click.option('--literal', is_flag=True,
277 help='print literally, no pretty table')
278 @click.argument('name')
279 @click.pass_context
280 def nsd_show2(ctx, name, literal):
281 '''shows the content of a NSD
282
283 NAME: name or ID of the NSD/NSpkg
284 '''
285 nsd_show(ctx, name, literal)
286
287
288 def vnfd_show(ctx, name, literal):
289 try:
290 resp = ctx.obj.vnfd.get(name)
291 #resp = ctx.obj.vnfd.get_individual(name)
292 except ClientException as inst:
293 print(inst.message)
294 exit(1)
295
296 if literal:
297 print yaml.safe_dump(resp)
298 return
299
300 table = PrettyTable(['field', 'value'])
301 for k, v in resp.items():
302 table.add_row([k, json.dumps(v, indent=2)])
303 table.align = 'l'
304 print(table)
305
306
307 @cli.command(name='vnfd-show', short_help='shows the content of a VNFD')
308 @click.option('--literal', is_flag=True,
309 help='print literally, no pretty table')
310 @click.argument('name')
311 @click.pass_context
312 def vnfd_show1(ctx, name, literal):
313 '''shows the content of a VNFD
314
315 NAME: name or ID of the VNFD/VNFpkg
316 '''
317 vnfd_show(ctx, name, literal)
318
319
320 @cli.command(name='vnfpkg-show', short_help='shows the content of a VNFD')
321 @click.option('--literal', is_flag=True,
322 help='print literally, no pretty table')
323 @click.argument('name')
324 @click.pass_context
325 def vnfd_show2(ctx, name, literal):
326 '''shows the content of a VNFD
327
328 NAME: name or ID of the VNFD/VNFpkg
329 '''
330 vnfd_show(ctx, name, literal)
331
332
333 @cli.command(name='ns-show', short_help='shows the info of a NS instance')
334 @click.argument('name')
335 @click.option('--literal', is_flag=True,
336 help='print literally, no pretty table')
337 @click.option('--filter', default=None)
338 @click.pass_context
339 def ns_show(ctx, name, literal, filter):
340 '''shows the info of a NS instance
341
342 NAME: name or ID of the NS instance
343 '''
344 try:
345 ns = ctx.obj.ns.get(name)
346 except ClientException as inst:
347 print(inst.message)
348 exit(1)
349
350 if literal:
351 print yaml.safe_dump(resp)
352 return
353
354 table = PrettyTable(['field', 'value'])
355
356 for k, v in ns.items():
357 if filter is None or filter in k:
358 table.add_row([k, json.dumps(v, indent=2)])
359
360 fullclassname = ctx.obj.__module__ + "." + ctx.obj.__class__.__name__
361 if fullclassname != 'osmclient.sol005.client.Client':
362 nsopdata = ctx.obj.ns.get_opdata(ns['id'])
363 nsr_optdata = nsopdata['nsr:nsr']
364 for k, v in nsr_optdata.items():
365 if filter is None or filter in k:
366 table.add_row([k, json.dumps(v, indent=2)])
367 table.align = 'l'
368 print(table)
369
370
371 @cli.command(name='vnf-show', short_help='shows the info of a VNF instance')
372 @click.argument('name')
373 @click.option('--literal', is_flag=True,
374 help='print literally, no pretty table')
375 @click.option('--filter', default=None)
376 @click.pass_context
377 def vnf_show(ctx, name, literal, filter):
378 '''shows the info of a VNF instance
379
380 NAME: name or ID of the VNF instance
381 '''
382 try:
383 check_client_version(ctx.obj, ctx.command.name, 'v1')
384 resp = ctx.obj.vnf.get(name)
385 except ClientException as inst:
386 print(inst.message)
387 exit(1)
388
389 if literal:
390 print yaml.safe_dump(resp)
391 return
392
393 table = PrettyTable(['field', 'value'])
394 for k, v in resp.items():
395 if filter is None or filter in k:
396 table.add_row([k, json.dumps(v, indent=2)])
397 table.align = 'l'
398 print(table)
399
400
401 @cli.command(name='vnf-monitoring-show')
402 @click.argument('vnf_name')
403 @click.pass_context
404 def vnf_monitoring_show(ctx, vnf_name):
405 try:
406 check_client_version(ctx.obj, ctx.command.name, 'v1')
407 resp = ctx.obj.vnf.get_monitoring(vnf_name)
408 except ClientException as inst:
409 print(inst.message)
410 exit(1)
411
412 table = PrettyTable(['vnf name', 'monitoring name', 'value', 'units'])
413 if resp is not None:
414 for monitor in resp:
415 table.add_row(
416 [vnf_name,
417 monitor['name'],
418 monitor['value-integer'],
419 monitor['units']])
420 table.align = 'l'
421 print(table)
422
423
424 @cli.command(name='ns-monitoring-show')
425 @click.argument('ns_name')
426 @click.pass_context
427 def ns_monitoring_show(ctx, ns_name):
428 try:
429 check_client_version(ctx.obj, ctx.command.name, 'v1')
430 resp = ctx.obj.ns.get_monitoring(ns_name)
431 except ClientException as inst:
432 print(inst.message)
433 exit(1)
434
435 table = PrettyTable(['vnf name', 'monitoring name', 'value', 'units'])
436 for key, val in resp.items():
437 for monitor in val:
438 table.add_row(
439 [key,
440 monitor['name'],
441 monitor['value-integer'],
442 monitor['units']])
443 table.align = 'l'
444 print(table)
445
446
447 ####################
448 # CREATE operations
449 ####################
450
451 def nsd_create(ctx, filename, overwrite):
452 try:
453 check_client_version(ctx.obj, ctx.command.name)
454 ctx.obj.nsd.create(filename, overwrite)
455 except ClientException as inst:
456 print(inst.message)
457 exit(1)
458
459
460 @cli.command(name='nsd-create', short_help='creates a new NSD/NSpkg')
461 @click.argument('filename')
462 @click.option('--overwrite', default=None,
463 help='overwrites some fields in NSD')
464 @click.pass_context
465 def nsd_create1(ctx, filename, overwrite):
466 '''creates a new NSD/NSpkg
467
468 FILENAME: NSD yaml file or NSpkg tar.gz file
469 '''
470 nsd_create(ctx, filename, overwrite)
471
472
473 @cli.command(name='nspkg-create', short_help='creates a new NSD/NSpkg')
474 @click.argument('filename')
475 @click.option('--overwrite', default=None,
476 help='overwrites some fields in NSD')
477 @click.pass_context
478 def nsd_create2(ctx, filename, overwrite):
479 '''creates a new NSD/NSpkg
480
481 FILENAME: NSD yaml file or NSpkg tar.gz file
482 '''
483 nsd_create(ctx, filename, overwrite)
484
485
486 def vnfd_create(ctx, filename, overwrite):
487 try:
488 check_client_version(ctx.obj, ctx.command.name)
489 ctx.obj.vnfd.create(filename, overwrite)
490 except ClientException as inst:
491 print(inst.message)
492 exit(1)
493
494
495 @cli.command(name='vnfd-create', short_help='creates a new VNFD/VNFpkg')
496 @click.argument('filename')
497 @click.option('--overwrite', default=None,
498 help='overwrites some fields in VNFD')
499 @click.pass_context
500 def vnfd_create1(ctx, filename, overwrite):
501 '''creates a new VNFD/VNFpkg
502
503 FILENAME: VNFD yaml file or VNFpkg tar.gz file
504 '''
505 vnfd_create(ctx, filename, overwrite)
506
507
508 @cli.command(name='vnfpkg-create', short_help='creates a new VNFD/VNFpkg')
509 @click.argument('filename')
510 @click.option('--overwrite', default=None,
511 help='overwrites some fields in VNFD')
512 @click.pass_context
513 def vnfd_create2(ctx, filename, overwrite):
514 '''creates a new VNFD/VNFpkg
515
516 FILENAME: VNFD yaml file or VNFpkg tar.gz file
517 '''
518 vnfd_create(ctx, filename, overwrite)
519
520
521 @cli.command(name='ns-create')
522 @click.option('--ns_name',
523 prompt=True)
524 @click.option('--nsd_name',
525 prompt=True)
526 @click.option('--vim_account',
527 prompt=True)
528 @click.option('--admin_status',
529 default='ENABLED',
530 help='administration status')
531 @click.option('--ssh_keys',
532 default=None,
533 help='comma separated list of keys to inject to vnfs')
534 @click.option('--config',
535 default=None,
536 help='ns specific yaml configuration:\nvnf: [member-vnf-index: TEXT, vim_account: TEXT]\n'
537 'vld: [name: TEXT, vim-network-name: TEXT or DICT with vim_account, vim_net entries]')
538 @click.pass_context
539 def ns_create(ctx,
540 nsd_name,
541 ns_name,
542 vim_account,
543 admin_status,
544 ssh_keys,
545 config):
546 '''creates a new NS instance'''
547 try:
548 # if config:
549 # check_client_version(ctx.obj, '--config', 'v1')
550 ctx.obj.ns.create(
551 nsd_name,
552 ns_name,
553 config=config,
554 ssh_keys=ssh_keys,
555 account=vim_account)
556 except ClientException as inst:
557 print(inst.message)
558 exit(1)
559
560
561 ####################
562 # UPDATE operations
563 ####################
564
565 def nsd_update(ctx, name, content):
566 try:
567 check_client_version(ctx.obj, ctx.command.name)
568 ctx.obj.nsd.update(name, content)
569 except ClientException as inst:
570 print(inst.message)
571 exit(1)
572
573 @cli.command(name='nsd-update', short_help='updates a NSD/NSpkg')
574 @click.argument('name')
575 @click.option('--content', default=None,
576 help='filename with the NSD/NSpkg replacing the current one')
577 @click.pass_context
578 def nsd_update1(ctx, name, content):
579 '''updates a NSD/NSpkg
580
581 NAME: name or ID of the NSD/NSpkg
582 '''
583 nsd_update(ctx, name, content)
584
585
586 @cli.command(name='nspkg-update', short_help='updates a NSD/NSpkg')
587 @click.argument('name')
588 @click.option('--content', default=None,
589 help='filename with the NSD/NSpkg replacing the current one')
590 @click.pass_context
591 def nsd_update2(ctx, name, content):
592 '''updates a NSD/NSpkg
593
594 NAME: name or ID of the NSD/NSpkg
595 '''
596 nsd_update(ctx, name, content)
597
598
599 def vnfd_update(ctx, name, content):
600 try:
601 check_client_version(ctx.obj, ctx.command.name)
602 ctx.obj.vnfd.update(name, content)
603 except ClientException as inst:
604 print(inst.message)
605 exit(1)
606
607
608 @cli.command(name='vnfd-update', short_help='updates a new VNFD/VNFpkg')
609 @click.argument('name')
610 @click.option('--content', default=None,
611 help='filename with the VNFD/VNFpkg replacing the current one')
612 @click.pass_context
613 def vnfd_update1(ctx, name, content):
614 '''updates a VNFD/VNFpkg
615
616 NAME: name or ID of the VNFD/VNFpkg
617 '''
618 vnfd_update(ctx, name, content)
619
620
621 @cli.command(name='vnfpkg-update', short_help='updates a VNFD/VNFpkg')
622 @click.argument('name')
623 @click.option('--content', default=None,
624 help='filename with the VNFD/VNFpkg replacing the current one')
625 @click.pass_context
626 def vnfd_update2(ctx, name, content):
627 '''updates a VNFD/VNFpkg
628
629 NAME: VNFD yaml file or VNFpkg tar.gz file
630 '''
631 vnfd_update(ctx, name, content)
632
633
634 ####################
635 # DELETE operations
636 ####################
637
638 def nsd_delete(ctx, name):
639 try:
640 ctx.obj.nsd.delete(name)
641 except ClientException as inst:
642 print(inst.message)
643 exit(1)
644
645
646 @cli.command(name='nsd-delete', short_help='deletes a NSD/NSpkg')
647 @click.argument('name')
648 @click.pass_context
649 def nsd_delete1(ctx, name):
650 '''deletes a NSD/NSpkg
651
652 NAME: name or ID of the NSD/NSpkg to be deleted
653 '''
654 nsd_delete(ctx, name)
655
656
657 @cli.command(name='nspkg-delete', short_help='deletes a NSD/NSpkg')
658 @click.argument('name')
659 @click.pass_context
660 def nsd_delete2(ctx, name):
661 '''deletes a NSD/NSpkg
662
663 NAME: name or ID of the NSD/NSpkg to be deleted
664 '''
665 nsd_delete(ctx, name)
666
667
668 def vnfd_delete(ctx, name):
669 try:
670 ctx.obj.vnfd.delete(name)
671 except ClientException as inst:
672 print(inst.message)
673 exit(1)
674
675
676 @cli.command(name='vnfd-delete', short_help='deletes a VNFD/VNFpkg')
677 @click.argument('name')
678 @click.pass_context
679 def vnfd_delete1(ctx, name):
680 '''deletes a VNFD/VNFpkg
681
682 NAME: name or ID of the VNFD/VNFpkg to be deleted
683 '''
684 vnfd_delete(ctx, name)
685
686
687 @cli.command(name='vnfpkg-delete', short_help='deletes a VNFD/VNFpkg')
688 @click.argument('name')
689 @click.pass_context
690 def vnfd_delete2(ctx, name):
691 '''deletes a VNFD/VNFpkg
692
693 NAME: name or ID of the VNFD/VNFpkg to be deleted
694 '''
695 vnfd_delete(ctx, name)
696
697
698 @cli.command(name='ns-delete', short_help='deletes a NS instance')
699 @click.argument('name')
700 @click.pass_context
701 def ns_delete(ctx, name):
702 '''deletes a NS instance
703
704 NAME: name or ID of the NS instance to be deleted
705 '''
706 try:
707 ctx.obj.ns.delete(name)
708 except ClientException as inst:
709 print(inst.message)
710 exit(1)
711
712
713 ####################
714 # VIM operations
715 ####################
716
717 @cli.command(name='vim-create')
718 @click.option('--name',
719 prompt=True,
720 help='Name to create datacenter')
721 @click.option('--user',
722 prompt=True,
723 help='VIM username')
724 @click.option('--password',
725 prompt=True,
726 hide_input=True,
727 confirmation_prompt=True,
728 help='VIM password')
729 @click.option('--auth_url',
730 prompt=True,
731 help='VIM url')
732 @click.option('--tenant',
733 prompt=True,
734 help='VIM tenant name')
735 @click.option('--config',
736 default=None,
737 help='VIM specific config parameters')
738 @click.option('--account_type',
739 default='openstack',
740 help='VIM type')
741 @click.option('--description',
742 default='no description',
743 help='human readable description')
744 @click.pass_context
745 def vim_create(ctx,
746 name,
747 user,
748 password,
749 auth_url,
750 tenant,
751 config,
752 account_type,
753 description):
754 '''creates a new VIM account
755 '''
756 vim = {}
757 vim['vim-username'] = user
758 vim['vim-password'] = password
759 vim['vim-url'] = auth_url
760 vim['vim-tenant-name'] = tenant
761 vim['config'] = config
762 vim['vim-type'] = account_type
763 vim['description'] = description
764 try:
765 ctx.obj.vim.create(name, vim)
766 except ClientException as inst:
767 print(inst.message)
768 exit(1)
769
770
771 @cli.command(name='vim-update', short_help='updates a VIM account')
772 @click.argument('name')
773 @click.option('--newname', default=None, help='New name for the VIM account')
774 @click.option('--user', default=None, help='VIM username')
775 @click.option('--password', default=None, help='VIM password')
776 @click.option('--auth_url', default=None, help='VIM url')
777 @click.option('--tenant', default=None, help='VIM tenant name')
778 @click.option('--config', default=None, help='VIM specific config parameters')
779 @click.option('--account_type', default=None, help='VIM type')
780 @click.option('--description', default=None, help='human readable description')
781 @click.pass_context
782 def vim_update(ctx,
783 name,
784 newname,
785 user,
786 password,
787 auth_url,
788 tenant,
789 config,
790 account_type,
791 description):
792 '''updates a VIM account
793
794 NAME: name or ID of the VIM account
795 '''
796 vim = {}
797 if newname:
798 vim['name'] = newname
799 vim['vim_user'] = user
800 vim['vim_password'] = password
801 vim['vim_url'] = auth_url
802 vim['vim-tenant-name'] = tenant
803 vim['config'] = config
804 vim['vim_type'] = account_type
805 vim['description'] = description
806 try:
807 check_client_version(ctx.obj, ctx.command.name)
808 ctx.obj.vim.update(name, vim)
809 except ClientException as inst:
810 print(inst.message)
811 exit(1)
812
813
814 @cli.command(name='vim-delete')
815 @click.argument('name')
816 @click.pass_context
817 def vim_delete(ctx, name):
818 '''deletes a VIM account
819
820 NAME: name or ID of the VIM account to be deleted
821 '''
822 try:
823 ctx.obj.vim.delete(name)
824 except ClientException as inst:
825 print(inst.message)
826 exit(1)
827
828
829 @cli.command(name='vim-list')
830 @click.option('--ro_update/--no_ro_update',
831 default=False,
832 help='update list from RO')
833 @click.option('--filter', default=None,
834 help='restricts the list to the VIM accounts matching the filter')
835 @click.pass_context
836 def vim_list(ctx, ro_update, filter):
837 '''list all VIM accounts'''
838 if filter:
839 check_client_version(ctx.obj, '--filter')
840 if ro_update:
841 check_client_version(ctx.obj, '--ro_update', 'v1')
842 fullclassname = ctx.obj.__module__ + "." + ctx.obj.__class__.__name__
843 if fullclassname == 'osmclient.sol005.client.Client':
844 resp = ctx.obj.vim.list(filter)
845 else:
846 resp = ctx.obj.vim.list(ro_update)
847 table = PrettyTable(['vim name', 'uuid'])
848 for vim in resp:
849 table.add_row([vim['name'], vim['uuid']])
850 table.align = 'l'
851 print(table)
852
853
854 @cli.command(name='vim-show')
855 @click.argument('name')
856 @click.pass_context
857 def vim_show(ctx, name):
858 '''shows the details of a VIM account
859
860 NAME: name or ID of the VIM account
861 '''
862 try:
863 resp = ctx.obj.vim.get(name)
864 except ClientException as inst:
865 print(inst.message)
866 exit(1)
867
868 table = PrettyTable(['key', 'attribute'])
869 for k, v in resp.items():
870 table.add_row([k, json.dumps(v, indent=2)])
871 table.align = 'l'
872 print(table)
873
874
875 ####################
876 # Other operations
877 ####################
878
879 @cli.command(name='upload-package')
880 @click.argument('filename')
881 @click.pass_context
882 def upload_package(ctx, filename):
883 '''uploads a VNF package or NS package
884
885 FILENAME: VNF or NS package file (tar.gz)
886 '''
887 try:
888 ctx.obj.package.upload(filename)
889 fullclassname = ctx.obj.__module__ + "." + ctx.obj.__class__.__name__
890 if fullclassname != 'osmclient.sol005.client.Client':
891 ctx.obj.package.wait_for_upload(filename)
892 except ClientException as inst:
893 print(inst.message)
894 exit(1)
895
896
897 @cli.command(name='ns-scaling-show')
898 @click.argument('ns_name')
899 @click.pass_context
900 def show_ns_scaling(ctx, ns_name):
901 check_client_version(ctx.obj, ctx.command.name, 'v1')
902 resp = ctx.obj.ns.list()
903
904 table = PrettyTable(
905 ['group-name',
906 'instance-id',
907 'operational status',
908 'create-time',
909 'vnfr ids'])
910
911 for ns in resp:
912 if ns_name == ns['name']:
913 nsopdata = ctx.obj.ns.get_opdata(ns['id'])
914 scaling_records = nsopdata['nsr:nsr']['scaling-group-record']
915 for record in scaling_records:
916 if 'instance' in record:
917 instances = record['instance']
918 for inst in instances:
919 table.add_row(
920 [record['scaling-group-name-ref'],
921 inst['instance-id'],
922 inst['op-status'],
923 time.strftime('%Y-%m-%d %H:%M:%S',
924 time.localtime(
925 inst['create-time'])),
926 inst['vnfrs']])
927 table.align = 'l'
928 print(table)
929
930
931 @cli.command(name='ns-scale')
932 @click.argument('ns_name')
933 @click.option('--ns_scale_group', prompt=True)
934 @click.option('--index', prompt=True)
935 @click.pass_context
936 def ns_scale(ctx, ns_name, ns_scale_group, index):
937 check_client_version(ctx.obj, ctx.command.name, 'v1')
938 ctx.obj.ns.scale(ns_name, ns_scale_group, index)
939
940
941 @cli.command(name='config-agent-list')
942 @click.pass_context
943 def config_agent_list(ctx):
944 check_client_version(ctx.obj, ctx.command.name, 'v1')
945 table = PrettyTable(['name', 'account-type', 'details'])
946 for account in ctx.obj.vca.list():
947 table.add_row(
948 [account['name'],
949 account['account-type'],
950 account['juju']])
951 table.align = 'l'
952 print(table)
953
954
955 @cli.command(name='config-agent-delete')
956 @click.argument('name')
957 @click.pass_context
958 def config_agent_delete(ctx, name):
959 try:
960 check_client_version(ctx.obj, ctx.command.name, 'v1')
961 ctx.obj.vca.delete(name)
962 except ClientException as inst:
963 print(inst.message)
964 exit(1)
965
966
967 @cli.command(name='config-agent-add')
968 @click.option('--name',
969 prompt=True)
970 @click.option('--account_type',
971 prompt=True)
972 @click.option('--server',
973 prompt=True)
974 @click.option('--user',
975 prompt=True)
976 @click.option('--secret',
977 prompt=True,
978 hide_input=True,
979 confirmation_prompt=True)
980 @click.pass_context
981 def config_agent_add(ctx, name, account_type, server, user, secret):
982 try:
983 check_client_version(ctx.obj, ctx.command.name, 'v1')
984 ctx.obj.vca.create(name, account_type, server, user, secret)
985 except ClientException as inst:
986 print(inst.message)
987 exit(1)
988
989 @cli.command(name='ro-dump')
990 @click.pass_context
991 def ro_dump(ctx):
992 check_client_version(ctx.obj, ctx.command.name, 'v1')
993 resp = ctx.obj.vim.get_resource_orchestrator()
994 table = PrettyTable(['key', 'attribute'])
995 for k, v in resp.items():
996 table.add_row([k, json.dumps(v, indent=2)])
997 table.align = 'l'
998 print(table)
999
1000
1001 @cli.command(name='vcs-list')
1002 @click.pass_context
1003 def vcs_list(ctx):
1004 check_client_version(ctx.obj, ctx.command.name, 'v1')
1005 resp = ctx.obj.utils.get_vcs_info()
1006 table = PrettyTable(['component name', 'state'])
1007 for component in resp:
1008 table.add_row([component['component_name'], component['state']])
1009 table.align = 'l'
1010 print(table)
1011
1012
1013 if __name__ == '__main__':
1014 cli()