osm.py: fix bug in ns_list for old NB API
[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')
537 @click.pass_context
538 def ns_create(ctx,
539 nsd_name,
540 ns_name,
541 vim_account,
542 admin_status,
543 ssh_keys,
544 config):
545 '''creates a new NS instance'''
546 try:
547 if config:
548 check_client_version(ctx.obj, '--config', 'v1')
549 ctx.obj.ns.create(
550 nsd_name,
551 ns_name,
552 config=config,
553 ssh_keys=ssh_keys,
554 account=vim_account)
555 except ClientException as inst:
556 print(inst.message)
557 exit(1)
558
559
560 ####################
561 # UPDATE operations
562 ####################
563
564 def nsd_update(ctx, name, content):
565 try:
566 check_client_version(ctx.obj, ctx.command.name)
567 ctx.obj.nsd.update(name, content)
568 except ClientException as inst:
569 print(inst.message)
570 exit(1)
571
572 @cli.command(name='nsd-update', short_help='updates a NSD/NSpkg')
573 @click.argument('name')
574 @click.option('--content', default=None,
575 help='filename with the NSD/NSpkg replacing the current one')
576 @click.pass_context
577 def nsd_update1(ctx, name, content):
578 '''updates a NSD/NSpkg
579
580 NAME: name or ID of the NSD/NSpkg
581 '''
582 nsd_update(ctx, name, content)
583
584
585 @cli.command(name='nspkg-update', short_help='updates a NSD/NSpkg')
586 @click.argument('name')
587 @click.option('--content', default=None,
588 help='filename with the NSD/NSpkg replacing the current one')
589 @click.pass_context
590 def nsd_update2(ctx, name, content):
591 '''updates a NSD/NSpkg
592
593 NAME: name or ID of the NSD/NSpkg
594 '''
595 nsd_update(ctx, name, content)
596
597
598 def vnfd_update(ctx, name, content):
599 try:
600 check_client_version(ctx.obj, ctx.command.name)
601 ctx.obj.vnfd.update(name, content)
602 except ClientException as inst:
603 print(inst.message)
604 exit(1)
605
606
607 @cli.command(name='vnfd-update', short_help='updates a new VNFD/VNFpkg')
608 @click.argument('name')
609 @click.option('--content', default=None,
610 help='filename with the VNFD/VNFpkg replacing the current one')
611 @click.pass_context
612 def vnfd_update1(ctx, name, content):
613 '''updates a VNFD/VNFpkg
614
615 NAME: name or ID of the VNFD/VNFpkg
616 '''
617 vnfd_update(ctx, name, content)
618
619
620 @cli.command(name='vnfpkg-update', short_help='updates a VNFD/VNFpkg')
621 @click.argument('name')
622 @click.option('--content', default=None,
623 help='filename with the VNFD/VNFpkg replacing the current one')
624 @click.pass_context
625 def vnfd_update2(ctx, name, content):
626 '''updates a VNFD/VNFpkg
627
628 NAME: VNFD yaml file or VNFpkg tar.gz file
629 '''
630 vnfd_update(ctx, name, content)
631
632
633 ####################
634 # DELETE operations
635 ####################
636
637 def nsd_delete(ctx, name):
638 try:
639 ctx.obj.nsd.delete(name)
640 except ClientException as inst:
641 print(inst.message)
642 exit(1)
643
644
645 @cli.command(name='nsd-delete', short_help='deletes a NSD/NSpkg')
646 @click.argument('name')
647 @click.pass_context
648 def nsd_delete1(ctx, name):
649 '''deletes a NSD/NSpkg
650
651 NAME: name or ID of the NSD/NSpkg to be deleted
652 '''
653 nsd_delete(ctx, name)
654
655
656 @cli.command(name='nspkg-delete', short_help='deletes a NSD/NSpkg')
657 @click.argument('name')
658 @click.pass_context
659 def nsd_delete2(ctx, name):
660 '''deletes a NSD/NSpkg
661
662 NAME: name or ID of the NSD/NSpkg to be deleted
663 '''
664 nsd_delete(ctx, name)
665
666
667 def vnfd_delete(ctx, name):
668 try:
669 ctx.obj.vnfd.delete(name)
670 except ClientException as inst:
671 print(inst.message)
672 exit(1)
673
674
675 @cli.command(name='vnfd-delete', short_help='deletes a VNFD/VNFpkg')
676 @click.argument('name')
677 @click.pass_context
678 def vnfd_delete1(ctx, name):
679 '''deletes a VNFD/VNFpkg
680
681 NAME: name or ID of the VNFD/VNFpkg to be deleted
682 '''
683 vnfd_delete(ctx, name)
684
685
686 @cli.command(name='vnfpkg-delete', short_help='deletes a VNFD/VNFpkg')
687 @click.argument('name')
688 @click.pass_context
689 def vnfd_delete2(ctx, name):
690 '''deletes a VNFD/VNFpkg
691
692 NAME: name or ID of the VNFD/VNFpkg to be deleted
693 '''
694 vnfd_delete(ctx, name)
695
696
697 @cli.command(name='ns-delete', short_help='deletes a NS instance')
698 @click.argument('name')
699 @click.pass_context
700 def ns_delete(ctx, name):
701 '''deletes a NS instance
702
703 NAME: name or ID of the NS instance to be deleted
704 '''
705 try:
706 ctx.obj.ns.delete(name)
707 except ClientException as inst:
708 print(inst.message)
709 exit(1)
710
711
712 ####################
713 # VIM operations
714 ####################
715
716 @cli.command(name='vim-create')
717 @click.option('--name',
718 prompt=True,
719 help='Name to create datacenter')
720 @click.option('--user',
721 prompt=True,
722 help='VIM username')
723 @click.option('--password',
724 prompt=True,
725 hide_input=True,
726 confirmation_prompt=True,
727 help='VIM password')
728 @click.option('--auth_url',
729 prompt=True,
730 help='VIM url')
731 @click.option('--tenant',
732 prompt=True,
733 help='VIM tenant name')
734 @click.option('--config',
735 default=None,
736 help='VIM specific config parameters')
737 @click.option('--account_type',
738 default='openstack',
739 help='VIM type')
740 @click.option('--description',
741 default='no description',
742 help='human readable description')
743 @click.pass_context
744 def vim_create(ctx,
745 name,
746 user,
747 password,
748 auth_url,
749 tenant,
750 config,
751 account_type,
752 description):
753 '''creates a new VIM account
754 '''
755 vim = {}
756 vim['vim-username'] = user
757 vim['vim-password'] = password
758 vim['vim-url'] = auth_url
759 vim['vim-tenant-name'] = tenant
760 vim['config'] = config
761 vim['vim-type'] = account_type
762 vim['description'] = description
763 try:
764 ctx.obj.vim.create(name, vim)
765 except ClientException as inst:
766 print(inst.message)
767 exit(1)
768
769
770 @cli.command(name='vim-update', short_help='updates a VIM account')
771 @click.argument('name')
772 @click.option('--newname', default=None, help='New name for the VIM account')
773 @click.option('--user', default=None, help='VIM username')
774 @click.option('--password', default=None, help='VIM password')
775 @click.option('--auth_url', default=None, help='VIM url')
776 @click.option('--tenant', default=None, help='VIM tenant name')
777 @click.option('--config', default=None, help='VIM specific config parameters')
778 @click.option('--account_type', default=None, help='VIM type')
779 @click.option('--description', default=None, help='human readable description')
780 @click.pass_context
781 def vim_update(ctx,
782 name,
783 newname,
784 user,
785 password,
786 auth_url,
787 tenant,
788 config,
789 account_type,
790 description):
791 '''updates a VIM account
792
793 NAME: name or ID of the VIM account
794 '''
795 vim = {}
796 if newname:
797 vim['name'] = newname
798 vim['vim_user'] = user
799 vim['vim_password'] = password
800 vim['vim_url'] = auth_url
801 vim['vim-tenant-name'] = tenant
802 vim['config'] = config
803 vim['vim_type'] = account_type
804 vim['description'] = description
805 try:
806 check_client_version(ctx.obj, ctx.command.name)
807 ctx.obj.vim.update(name, vim)
808 except ClientException as inst:
809 print(inst.message)
810 exit(1)
811
812
813 @cli.command(name='vim-delete')
814 @click.argument('name')
815 @click.pass_context
816 def vim_delete(ctx, name):
817 '''deletes a VIM account
818
819 NAME: name or ID of the VIM account to be deleted
820 '''
821 try:
822 ctx.obj.vim.delete(name)
823 except ClientException as inst:
824 print(inst.message)
825 exit(1)
826
827
828 @cli.command(name='vim-list')
829 @click.option('--ro_update/--no_ro_update',
830 default=False,
831 help='update list from RO')
832 @click.option('--filter', default=None,
833 help='restricts the list to the VIM accounts matching the filter')
834 @click.pass_context
835 def vim_list(ctx, ro_update, filter):
836 '''list all VIM accounts'''
837 if filter:
838 check_client_version(ctx.obj, '--filter')
839 if ro_update:
840 check_client_version(ctx.obj, '--ro_update', 'v1')
841 fullclassname = ctx.obj.__module__ + "." + ctx.obj.__class__.__name__
842 if fullclassname == 'osmclient.sol005.client.Client':
843 resp = ctx.obj.vim.list(filter)
844 else:
845 resp = ctx.obj.vim.list(ro_update)
846 table = PrettyTable(['vim name', 'uuid'])
847 for vim in resp:
848 table.add_row([vim['name'], vim['uuid']])
849 table.align = 'l'
850 print(table)
851
852
853 @cli.command(name='vim-show')
854 @click.argument('name')
855 @click.pass_context
856 def vim_show(ctx, name):
857 '''shows the details of a VIM account
858
859 NAME: name or ID of the VIM account
860 '''
861 try:
862 resp = ctx.obj.vim.get(name)
863 except ClientException as inst:
864 print(inst.message)
865 exit(1)
866
867 table = PrettyTable(['key', 'attribute'])
868 for k, v in resp.items():
869 table.add_row([k, json.dumps(v, indent=2)])
870 table.align = 'l'
871 print(table)
872
873
874 ####################
875 # Other operations
876 ####################
877
878 @cli.command(name='upload-package')
879 @click.argument('filename')
880 @click.pass_context
881 def upload_package(ctx, filename):
882 '''uploads a VNF package or NS package
883
884 FILENAME: VNF or NS package file (tar.gz)
885 '''
886 try:
887 ctx.obj.package.upload(filename)
888 fullclassname = ctx.obj.__module__ + "." + ctx.obj.__class__.__name__
889 if fullclassname != 'osmclient.sol005.client.Client':
890 ctx.obj.package.wait_for_upload(filename)
891 except ClientException as inst:
892 print(inst.message)
893 exit(1)
894
895
896 @cli.command(name='ns-scaling-show')
897 @click.argument('ns_name')
898 @click.pass_context
899 def show_ns_scaling(ctx, ns_name):
900 check_client_version(ctx.obj, ctx.command.name, 'v1')
901 resp = ctx.obj.ns.list()
902
903 table = PrettyTable(
904 ['group-name',
905 'instance-id',
906 'operational status',
907 'create-time',
908 'vnfr ids'])
909
910 for ns in resp:
911 if ns_name == ns['name']:
912 nsopdata = ctx.obj.ns.get_opdata(ns['id'])
913 scaling_records = nsopdata['nsr:nsr']['scaling-group-record']
914 for record in scaling_records:
915 if 'instance' in record:
916 instances = record['instance']
917 for inst in instances:
918 table.add_row(
919 [record['scaling-group-name-ref'],
920 inst['instance-id'],
921 inst['op-status'],
922 time.strftime('%Y-%m-%d %H:%M:%S',
923 time.localtime(
924 inst['create-time'])),
925 inst['vnfrs']])
926 table.align = 'l'
927 print(table)
928
929
930 @cli.command(name='ns-scale')
931 @click.argument('ns_name')
932 @click.option('--ns_scale_group', prompt=True)
933 @click.option('--index', prompt=True)
934 @click.pass_context
935 def ns_scale(ctx, ns_name, ns_scale_group, index):
936 check_client_version(ctx.obj, ctx.command.name, 'v1')
937 ctx.obj.ns.scale(ns_name, ns_scale_group, index)
938
939
940 @cli.command(name='config-agent-list')
941 @click.pass_context
942 def config_agent_list(ctx):
943 check_client_version(ctx.obj, ctx.command.name, 'v1')
944 table = PrettyTable(['name', 'account-type', 'details'])
945 for account in ctx.obj.vca.list():
946 table.add_row(
947 [account['name'],
948 account['account-type'],
949 account['juju']])
950 table.align = 'l'
951 print(table)
952
953
954 @cli.command(name='config-agent-delete')
955 @click.argument('name')
956 @click.pass_context
957 def config_agent_delete(ctx, name):
958 try:
959 check_client_version(ctx.obj, ctx.command.name, 'v1')
960 ctx.obj.vca.delete(name)
961 except ClientException as inst:
962 print(inst.message)
963 exit(1)
964
965
966 @cli.command(name='config-agent-add')
967 @click.option('--name',
968 prompt=True)
969 @click.option('--account_type',
970 prompt=True)
971 @click.option('--server',
972 prompt=True)
973 @click.option('--user',
974 prompt=True)
975 @click.option('--secret',
976 prompt=True,
977 hide_input=True,
978 confirmation_prompt=True)
979 @click.pass_context
980 def config_agent_add(ctx, name, account_type, server, user, secret):
981 try:
982 check_client_version(ctx.obj, ctx.command.name, 'v1')
983 ctx.obj.vca.create(name, account_type, server, user, secret)
984 except ClientException as inst:
985 print(inst.message)
986 exit(1)
987
988 @cli.command(name='ro-dump')
989 @click.pass_context
990 def ro_dump(ctx):
991 check_client_version(ctx.obj, ctx.command.name, 'v1')
992 resp = ctx.obj.vim.get_resource_orchestrator()
993 table = PrettyTable(['key', 'attribute'])
994 for k, v in resp.items():
995 table.add_row([k, json.dumps(v, indent=2)])
996 table.align = 'l'
997 print(table)
998
999
1000 @cli.command(name='vcs-list')
1001 @click.pass_context
1002 def vcs_list(ctx):
1003 check_client_version(ctx.obj, ctx.command.name, 'v1')
1004 resp = ctx.obj.utils.get_vcs_info()
1005 table = PrettyTable(['component name', 'state'])
1006 for component in resp:
1007 table.add_row([component['component_name'], component['state']])
1008 table.align = 'l'
1009 print(table)
1010
1011
1012 if __name__ == '__main__':
1013 cli()