osm.py: print error message when using vnf-list with --sol005
[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 try:
221 check_client_version(ctx.obj, ctx.command.name, 'v1')
222 resp = ctx.obj.vnf.list()
223 except ClientException as inst:
224 print(inst.message)
225 exit(1)
226 table = PrettyTable(
227 ['vnf name',
228 'id',
229 'operational status',
230 'config status'])
231 for vnfr in resp:
232 if 'mgmt-interface' not in vnfr:
233 vnfr['mgmt-interface'] = {}
234 vnfr['mgmt-interface']['ip-address'] = None
235 table.add_row(
236 [vnfr['name'],
237 vnfr['id'],
238 vnfr['operational-status'],
239 vnfr['config-status']])
240 table.align = 'l'
241 print(table)
242
243
244 ####################
245 # SHOW operations
246 ####################
247
248 def nsd_show(ctx, name, literal):
249 try:
250 resp = ctx.obj.nsd.get(name)
251 #resp = ctx.obj.nsd.get_individual(name)
252 except ClientException as inst:
253 print(inst.message)
254 exit(1)
255
256 if literal:
257 print yaml.safe_dump(resp)
258 return
259
260 table = PrettyTable(['field', 'value'])
261 for k, v in resp.items():
262 table.add_row([k, json.dumps(v, indent=2)])
263 table.align = 'l'
264 print(table)
265
266
267 @cli.command(name='nsd-show', short_help='shows the content of a NSD')
268 @click.option('--literal', is_flag=True,
269 help='print literally, no pretty table')
270 @click.argument('name')
271 @click.pass_context
272 def nsd_show1(ctx, name, literal):
273 '''shows the content of a NSD
274
275 NAME: name or ID of the NSD/NSpkg
276 '''
277 nsd_show(ctx, name, literal)
278
279
280 @cli.command(name='nspkg-show', short_help='shows the content of a NSD')
281 @click.option('--literal', is_flag=True,
282 help='print literally, no pretty table')
283 @click.argument('name')
284 @click.pass_context
285 def nsd_show2(ctx, name, literal):
286 '''shows the content of a NSD
287
288 NAME: name or ID of the NSD/NSpkg
289 '''
290 nsd_show(ctx, name, literal)
291
292
293 def vnfd_show(ctx, name, literal):
294 try:
295 resp = ctx.obj.vnfd.get(name)
296 #resp = ctx.obj.vnfd.get_individual(name)
297 except ClientException as inst:
298 print(inst.message)
299 exit(1)
300
301 if literal:
302 print yaml.safe_dump(resp)
303 return
304
305 table = PrettyTable(['field', 'value'])
306 for k, v in resp.items():
307 table.add_row([k, json.dumps(v, indent=2)])
308 table.align = 'l'
309 print(table)
310
311
312 @cli.command(name='vnfd-show', short_help='shows the content of a VNFD')
313 @click.option('--literal', is_flag=True,
314 help='print literally, no pretty table')
315 @click.argument('name')
316 @click.pass_context
317 def vnfd_show1(ctx, name, literal):
318 '''shows the content of a VNFD
319
320 NAME: name or ID of the VNFD/VNFpkg
321 '''
322 vnfd_show(ctx, name, literal)
323
324
325 @cli.command(name='vnfpkg-show', short_help='shows the content of a VNFD')
326 @click.option('--literal', is_flag=True,
327 help='print literally, no pretty table')
328 @click.argument('name')
329 @click.pass_context
330 def vnfd_show2(ctx, name, literal):
331 '''shows the content of a VNFD
332
333 NAME: name or ID of the VNFD/VNFpkg
334 '''
335 vnfd_show(ctx, name, literal)
336
337
338 @cli.command(name='ns-show', short_help='shows the info of a NS instance')
339 @click.argument('name')
340 @click.option('--literal', is_flag=True,
341 help='print literally, no pretty table')
342 @click.option('--filter', default=None)
343 @click.pass_context
344 def ns_show(ctx, name, literal, filter):
345 '''shows the info of a NS instance
346
347 NAME: name or ID of the NS instance
348 '''
349 try:
350 ns = ctx.obj.ns.get(name)
351 except ClientException as inst:
352 print(inst.message)
353 exit(1)
354
355 if literal:
356 print yaml.safe_dump(resp)
357 return
358
359 table = PrettyTable(['field', 'value'])
360
361 for k, v in ns.items():
362 if filter is None or filter in k:
363 table.add_row([k, json.dumps(v, indent=2)])
364
365 fullclassname = ctx.obj.__module__ + "." + ctx.obj.__class__.__name__
366 if fullclassname != 'osmclient.sol005.client.Client':
367 nsopdata = ctx.obj.ns.get_opdata(ns['id'])
368 nsr_optdata = nsopdata['nsr:nsr']
369 for k, v in nsr_optdata.items():
370 if filter is None or filter in k:
371 table.add_row([k, json.dumps(v, indent=2)])
372 table.align = 'l'
373 print(table)
374
375
376 @cli.command(name='vnf-show', short_help='shows the info of a VNF instance')
377 @click.argument('name')
378 @click.option('--literal', is_flag=True,
379 help='print literally, no pretty table')
380 @click.option('--filter', default=None)
381 @click.pass_context
382 def vnf_show(ctx, name, literal, filter):
383 '''shows the info of a VNF instance
384
385 NAME: name or ID of the VNF instance
386 '''
387 try:
388 check_client_version(ctx.obj, ctx.command.name, 'v1')
389 resp = ctx.obj.vnf.get(name)
390 except ClientException as inst:
391 print(inst.message)
392 exit(1)
393
394 if literal:
395 print yaml.safe_dump(resp)
396 return
397
398 table = PrettyTable(['field', 'value'])
399 for k, v in resp.items():
400 if filter is None or filter in k:
401 table.add_row([k, json.dumps(v, indent=2)])
402 table.align = 'l'
403 print(table)
404
405
406 @cli.command(name='vnf-monitoring-show')
407 @click.argument('vnf_name')
408 @click.pass_context
409 def vnf_monitoring_show(ctx, vnf_name):
410 try:
411 check_client_version(ctx.obj, ctx.command.name, 'v1')
412 resp = ctx.obj.vnf.get_monitoring(vnf_name)
413 except ClientException as inst:
414 print(inst.message)
415 exit(1)
416
417 table = PrettyTable(['vnf name', 'monitoring name', 'value', 'units'])
418 if resp is not None:
419 for monitor in resp:
420 table.add_row(
421 [vnf_name,
422 monitor['name'],
423 monitor['value-integer'],
424 monitor['units']])
425 table.align = 'l'
426 print(table)
427
428
429 @cli.command(name='ns-monitoring-show')
430 @click.argument('ns_name')
431 @click.pass_context
432 def ns_monitoring_show(ctx, ns_name):
433 try:
434 check_client_version(ctx.obj, ctx.command.name, 'v1')
435 resp = ctx.obj.ns.get_monitoring(ns_name)
436 except ClientException as inst:
437 print(inst.message)
438 exit(1)
439
440 table = PrettyTable(['vnf name', 'monitoring name', 'value', 'units'])
441 for key, val in resp.items():
442 for monitor in val:
443 table.add_row(
444 [key,
445 monitor['name'],
446 monitor['value-integer'],
447 monitor['units']])
448 table.align = 'l'
449 print(table)
450
451
452 ####################
453 # CREATE operations
454 ####################
455
456 def nsd_create(ctx, filename, overwrite):
457 try:
458 check_client_version(ctx.obj, ctx.command.name)
459 ctx.obj.nsd.create(filename, overwrite)
460 except ClientException as inst:
461 print(inst.message)
462 exit(1)
463
464
465 @cli.command(name='nsd-create', short_help='creates a new NSD/NSpkg')
466 @click.argument('filename')
467 @click.option('--overwrite', default=None,
468 help='overwrites some fields in NSD')
469 @click.pass_context
470 def nsd_create1(ctx, filename, overwrite):
471 '''creates a new NSD/NSpkg
472
473 FILENAME: NSD yaml file or NSpkg tar.gz file
474 '''
475 nsd_create(ctx, filename, overwrite)
476
477
478 @cli.command(name='nspkg-create', short_help='creates a new NSD/NSpkg')
479 @click.argument('filename')
480 @click.option('--overwrite', default=None,
481 help='overwrites some fields in NSD')
482 @click.pass_context
483 def nsd_create2(ctx, filename, overwrite):
484 '''creates a new NSD/NSpkg
485
486 FILENAME: NSD yaml file or NSpkg tar.gz file
487 '''
488 nsd_create(ctx, filename, overwrite)
489
490
491 def vnfd_create(ctx, filename, overwrite):
492 try:
493 check_client_version(ctx.obj, ctx.command.name)
494 ctx.obj.vnfd.create(filename, overwrite)
495 except ClientException as inst:
496 print(inst.message)
497 exit(1)
498
499
500 @cli.command(name='vnfd-create', short_help='creates a new VNFD/VNFpkg')
501 @click.argument('filename')
502 @click.option('--overwrite', default=None,
503 help='overwrites some fields in VNFD')
504 @click.pass_context
505 def vnfd_create1(ctx, filename, overwrite):
506 '''creates a new VNFD/VNFpkg
507
508 FILENAME: VNFD yaml file or VNFpkg tar.gz file
509 '''
510 vnfd_create(ctx, filename, overwrite)
511
512
513 @cli.command(name='vnfpkg-create', short_help='creates a new VNFD/VNFpkg')
514 @click.argument('filename')
515 @click.option('--overwrite', default=None,
516 help='overwrites some fields in VNFD')
517 @click.pass_context
518 def vnfd_create2(ctx, filename, overwrite):
519 '''creates a new VNFD/VNFpkg
520
521 FILENAME: VNFD yaml file or VNFpkg tar.gz file
522 '''
523 vnfd_create(ctx, filename, overwrite)
524
525
526 @cli.command(name='ns-create')
527 @click.option('--ns_name',
528 prompt=True)
529 @click.option('--nsd_name',
530 prompt=True)
531 @click.option('--vim_account',
532 prompt=True)
533 @click.option('--admin_status',
534 default='ENABLED',
535 help='administration status')
536 @click.option('--ssh_keys',
537 default=None,
538 help='comma separated list of keys to inject to vnfs')
539 @click.option('--config',
540 default=None,
541 help='ns specific yaml configuration:\nvnf: [member-vnf-index: TEXT, vim_account: TEXT]\n'
542 'vld: [name: TEXT, vim-network-name: TEXT or DICT with vim_account, vim_net entries]')
543 @click.pass_context
544 def ns_create(ctx,
545 nsd_name,
546 ns_name,
547 vim_account,
548 admin_status,
549 ssh_keys,
550 config):
551 '''creates a new NS instance'''
552 try:
553 # if config:
554 # check_client_version(ctx.obj, '--config', 'v1')
555 ctx.obj.ns.create(
556 nsd_name,
557 ns_name,
558 config=config,
559 ssh_keys=ssh_keys,
560 account=vim_account)
561 except ClientException as inst:
562 print(inst.message)
563 exit(1)
564
565
566 ####################
567 # UPDATE operations
568 ####################
569
570 def nsd_update(ctx, name, content):
571 try:
572 check_client_version(ctx.obj, ctx.command.name)
573 ctx.obj.nsd.update(name, content)
574 except ClientException as inst:
575 print(inst.message)
576 exit(1)
577
578 @cli.command(name='nsd-update', short_help='updates a NSD/NSpkg')
579 @click.argument('name')
580 @click.option('--content', default=None,
581 help='filename with the NSD/NSpkg replacing the current one')
582 @click.pass_context
583 def nsd_update1(ctx, name, content):
584 '''updates a NSD/NSpkg
585
586 NAME: name or ID of the NSD/NSpkg
587 '''
588 nsd_update(ctx, name, content)
589
590
591 @cli.command(name='nspkg-update', short_help='updates a NSD/NSpkg')
592 @click.argument('name')
593 @click.option('--content', default=None,
594 help='filename with the NSD/NSpkg replacing the current one')
595 @click.pass_context
596 def nsd_update2(ctx, name, content):
597 '''updates a NSD/NSpkg
598
599 NAME: name or ID of the NSD/NSpkg
600 '''
601 nsd_update(ctx, name, content)
602
603
604 def vnfd_update(ctx, name, content):
605 try:
606 check_client_version(ctx.obj, ctx.command.name)
607 ctx.obj.vnfd.update(name, content)
608 except ClientException as inst:
609 print(inst.message)
610 exit(1)
611
612
613 @cli.command(name='vnfd-update', short_help='updates a new VNFD/VNFpkg')
614 @click.argument('name')
615 @click.option('--content', default=None,
616 help='filename with the VNFD/VNFpkg replacing the current one')
617 @click.pass_context
618 def vnfd_update1(ctx, name, content):
619 '''updates a VNFD/VNFpkg
620
621 NAME: name or ID of the VNFD/VNFpkg
622 '''
623 vnfd_update(ctx, name, content)
624
625
626 @cli.command(name='vnfpkg-update', short_help='updates a VNFD/VNFpkg')
627 @click.argument('name')
628 @click.option('--content', default=None,
629 help='filename with the VNFD/VNFpkg replacing the current one')
630 @click.pass_context
631 def vnfd_update2(ctx, name, content):
632 '''updates a VNFD/VNFpkg
633
634 NAME: VNFD yaml file or VNFpkg tar.gz file
635 '''
636 vnfd_update(ctx, name, content)
637
638
639 ####################
640 # DELETE operations
641 ####################
642
643 def nsd_delete(ctx, name):
644 try:
645 ctx.obj.nsd.delete(name)
646 except ClientException as inst:
647 print(inst.message)
648 exit(1)
649
650
651 @cli.command(name='nsd-delete', short_help='deletes a NSD/NSpkg')
652 @click.argument('name')
653 @click.pass_context
654 def nsd_delete1(ctx, name):
655 '''deletes a NSD/NSpkg
656
657 NAME: name or ID of the NSD/NSpkg to be deleted
658 '''
659 nsd_delete(ctx, name)
660
661
662 @cli.command(name='nspkg-delete', short_help='deletes a NSD/NSpkg')
663 @click.argument('name')
664 @click.pass_context
665 def nsd_delete2(ctx, name):
666 '''deletes a NSD/NSpkg
667
668 NAME: name or ID of the NSD/NSpkg to be deleted
669 '''
670 nsd_delete(ctx, name)
671
672
673 def vnfd_delete(ctx, name):
674 try:
675 ctx.obj.vnfd.delete(name)
676 except ClientException as inst:
677 print(inst.message)
678 exit(1)
679
680
681 @cli.command(name='vnfd-delete', short_help='deletes a VNFD/VNFpkg')
682 @click.argument('name')
683 @click.pass_context
684 def vnfd_delete1(ctx, name):
685 '''deletes a VNFD/VNFpkg
686
687 NAME: name or ID of the VNFD/VNFpkg to be deleted
688 '''
689 vnfd_delete(ctx, name)
690
691
692 @cli.command(name='vnfpkg-delete', short_help='deletes a VNFD/VNFpkg')
693 @click.argument('name')
694 @click.pass_context
695 def vnfd_delete2(ctx, name):
696 '''deletes a VNFD/VNFpkg
697
698 NAME: name or ID of the VNFD/VNFpkg to be deleted
699 '''
700 vnfd_delete(ctx, name)
701
702
703 @cli.command(name='ns-delete', short_help='deletes a NS instance')
704 @click.argument('name')
705 @click.pass_context
706 def ns_delete(ctx, name):
707 '''deletes a NS instance
708
709 NAME: name or ID of the NS instance to be deleted
710 '''
711 try:
712 ctx.obj.ns.delete(name)
713 except ClientException as inst:
714 print(inst.message)
715 exit(1)
716
717
718 ####################
719 # VIM operations
720 ####################
721
722 @cli.command(name='vim-create')
723 @click.option('--name',
724 prompt=True,
725 help='Name to create datacenter')
726 @click.option('--user',
727 prompt=True,
728 help='VIM username')
729 @click.option('--password',
730 prompt=True,
731 hide_input=True,
732 confirmation_prompt=True,
733 help='VIM password')
734 @click.option('--auth_url',
735 prompt=True,
736 help='VIM url')
737 @click.option('--tenant',
738 prompt=True,
739 help='VIM tenant name')
740 @click.option('--config',
741 default=None,
742 help='VIM specific config parameters')
743 @click.option('--account_type',
744 default='openstack',
745 help='VIM type')
746 @click.option('--description',
747 default='no description',
748 help='human readable description')
749 @click.pass_context
750 def vim_create(ctx,
751 name,
752 user,
753 password,
754 auth_url,
755 tenant,
756 config,
757 account_type,
758 description):
759 '''creates a new VIM account
760 '''
761 vim = {}
762 vim['vim-username'] = user
763 vim['vim-password'] = password
764 vim['vim-url'] = auth_url
765 vim['vim-tenant-name'] = tenant
766 vim['config'] = config
767 vim['vim-type'] = account_type
768 vim['description'] = description
769 try:
770 ctx.obj.vim.create(name, vim)
771 except ClientException as inst:
772 print(inst.message)
773 exit(1)
774
775
776 @cli.command(name='vim-update', short_help='updates a VIM account')
777 @click.argument('name')
778 @click.option('--newname', default=None, help='New name for the VIM account')
779 @click.option('--user', default=None, help='VIM username')
780 @click.option('--password', default=None, help='VIM password')
781 @click.option('--auth_url', default=None, help='VIM url')
782 @click.option('--tenant', default=None, help='VIM tenant name')
783 @click.option('--config', default=None, help='VIM specific config parameters')
784 @click.option('--account_type', default=None, help='VIM type')
785 @click.option('--description', default=None, help='human readable description')
786 @click.pass_context
787 def vim_update(ctx,
788 name,
789 newname,
790 user,
791 password,
792 auth_url,
793 tenant,
794 config,
795 account_type,
796 description):
797 '''updates a VIM account
798
799 NAME: name or ID of the VIM account
800 '''
801 vim = {}
802 if newname:
803 vim['name'] = newname
804 vim['vim_user'] = user
805 vim['vim_password'] = password
806 vim['vim_url'] = auth_url
807 vim['vim-tenant-name'] = tenant
808 vim['config'] = config
809 vim['vim_type'] = account_type
810 vim['description'] = description
811 try:
812 check_client_version(ctx.obj, ctx.command.name)
813 ctx.obj.vim.update(name, vim)
814 except ClientException as inst:
815 print(inst.message)
816 exit(1)
817
818
819 @cli.command(name='vim-delete')
820 @click.argument('name')
821 @click.pass_context
822 def vim_delete(ctx, name):
823 '''deletes a VIM account
824
825 NAME: name or ID of the VIM account to be deleted
826 '''
827 try:
828 ctx.obj.vim.delete(name)
829 except ClientException as inst:
830 print(inst.message)
831 exit(1)
832
833
834 @cli.command(name='vim-list')
835 @click.option('--ro_update/--no_ro_update',
836 default=False,
837 help='update list from RO')
838 @click.option('--filter', default=None,
839 help='restricts the list to the VIM accounts matching the filter')
840 @click.pass_context
841 def vim_list(ctx, ro_update, filter):
842 '''list all VIM accounts'''
843 if filter:
844 check_client_version(ctx.obj, '--filter')
845 if ro_update:
846 check_client_version(ctx.obj, '--ro_update', 'v1')
847 fullclassname = ctx.obj.__module__ + "." + ctx.obj.__class__.__name__
848 if fullclassname == 'osmclient.sol005.client.Client':
849 resp = ctx.obj.vim.list(filter)
850 else:
851 resp = ctx.obj.vim.list(ro_update)
852 table = PrettyTable(['vim name', 'uuid'])
853 for vim in resp:
854 table.add_row([vim['name'], vim['uuid']])
855 table.align = 'l'
856 print(table)
857
858
859 @cli.command(name='vim-show')
860 @click.argument('name')
861 @click.pass_context
862 def vim_show(ctx, name):
863 '''shows the details of a VIM account
864
865 NAME: name or ID of the VIM account
866 '''
867 try:
868 resp = ctx.obj.vim.get(name)
869 except ClientException as inst:
870 print(inst.message)
871 exit(1)
872
873 table = PrettyTable(['key', 'attribute'])
874 for k, v in resp.items():
875 table.add_row([k, json.dumps(v, indent=2)])
876 table.align = 'l'
877 print(table)
878
879
880 ####################
881 # Other operations
882 ####################
883
884 @cli.command(name='upload-package')
885 @click.argument('filename')
886 @click.pass_context
887 def upload_package(ctx, filename):
888 '''uploads a VNF package or NS package
889
890 FILENAME: VNF or NS package file (tar.gz)
891 '''
892 try:
893 ctx.obj.package.upload(filename)
894 fullclassname = ctx.obj.__module__ + "." + ctx.obj.__class__.__name__
895 if fullclassname != 'osmclient.sol005.client.Client':
896 ctx.obj.package.wait_for_upload(filename)
897 except ClientException as inst:
898 print(inst.message)
899 exit(1)
900
901
902 @cli.command(name='ns-scaling-show')
903 @click.argument('ns_name')
904 @click.pass_context
905 def show_ns_scaling(ctx, ns_name):
906 check_client_version(ctx.obj, ctx.command.name, 'v1')
907 resp = ctx.obj.ns.list()
908
909 table = PrettyTable(
910 ['group-name',
911 'instance-id',
912 'operational status',
913 'create-time',
914 'vnfr ids'])
915
916 for ns in resp:
917 if ns_name == ns['name']:
918 nsopdata = ctx.obj.ns.get_opdata(ns['id'])
919 scaling_records = nsopdata['nsr:nsr']['scaling-group-record']
920 for record in scaling_records:
921 if 'instance' in record:
922 instances = record['instance']
923 for inst in instances:
924 table.add_row(
925 [record['scaling-group-name-ref'],
926 inst['instance-id'],
927 inst['op-status'],
928 time.strftime('%Y-%m-%d %H:%M:%S',
929 time.localtime(
930 inst['create-time'])),
931 inst['vnfrs']])
932 table.align = 'l'
933 print(table)
934
935
936 @cli.command(name='ns-scale')
937 @click.argument('ns_name')
938 @click.option('--ns_scale_group', prompt=True)
939 @click.option('--index', prompt=True)
940 @click.pass_context
941 def ns_scale(ctx, ns_name, ns_scale_group, index):
942 check_client_version(ctx.obj, ctx.command.name, 'v1')
943 ctx.obj.ns.scale(ns_name, ns_scale_group, index)
944
945
946 @cli.command(name='config-agent-list')
947 @click.pass_context
948 def config_agent_list(ctx):
949 check_client_version(ctx.obj, ctx.command.name, 'v1')
950 table = PrettyTable(['name', 'account-type', 'details'])
951 for account in ctx.obj.vca.list():
952 table.add_row(
953 [account['name'],
954 account['account-type'],
955 account['juju']])
956 table.align = 'l'
957 print(table)
958
959
960 @cli.command(name='config-agent-delete')
961 @click.argument('name')
962 @click.pass_context
963 def config_agent_delete(ctx, name):
964 try:
965 check_client_version(ctx.obj, ctx.command.name, 'v1')
966 ctx.obj.vca.delete(name)
967 except ClientException as inst:
968 print(inst.message)
969 exit(1)
970
971
972 @cli.command(name='config-agent-add')
973 @click.option('--name',
974 prompt=True)
975 @click.option('--account_type',
976 prompt=True)
977 @click.option('--server',
978 prompt=True)
979 @click.option('--user',
980 prompt=True)
981 @click.option('--secret',
982 prompt=True,
983 hide_input=True,
984 confirmation_prompt=True)
985 @click.pass_context
986 def config_agent_add(ctx, name, account_type, server, user, secret):
987 try:
988 check_client_version(ctx.obj, ctx.command.name, 'v1')
989 ctx.obj.vca.create(name, account_type, server, user, secret)
990 except ClientException as inst:
991 print(inst.message)
992 exit(1)
993
994 @cli.command(name='ro-dump')
995 @click.pass_context
996 def ro_dump(ctx):
997 check_client_version(ctx.obj, ctx.command.name, 'v1')
998 resp = ctx.obj.vim.get_resource_orchestrator()
999 table = PrettyTable(['key', 'attribute'])
1000 for k, v in resp.items():
1001 table.add_row([k, json.dumps(v, indent=2)])
1002 table.align = 'l'
1003 print(table)
1004
1005
1006 @cli.command(name='vcs-list')
1007 @click.pass_context
1008 def vcs_list(ctx):
1009 check_client_version(ctx.obj, ctx.command.name, 'v1')
1010 resp = ctx.obj.utils.get_vcs_info()
1011 table = PrettyTable(['component name', 'state'])
1012 for component in resp:
1013 table.add_row([component['component_name'], component['state']])
1014 table.align = 'l'
1015 print(table)
1016
1017
1018 if __name__ == '__main__':
1019 cli()