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