This file is indexed.

/usr/lib/python2.7/dist-packages/senlinclient/v1/shell.py is in python-senlinclient 0.4.0-2.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

import logging

from openstack import exceptions as sdk_exc
from senlinclient.common import exc
from senlinclient.common.i18n import _
from senlinclient.common import utils

logger = logging.getLogger(__name__)


def do_build_info(service, args=None):
    """Retrieve build information.

    :param sc: Instance of senlinclient.
    :param args: Additional command line arguments, if any.
    """
    result = service.get_build_info()

    formatters = {
        'api': utils.json_formatter,
        'engine': utils.json_formatter,
    }
    utils.print_dict(result, formatters=formatters)


# PROFILE TYPES


def do_profile_type_list(service, args=None):
    """List the available profile types.

    :param sc: Instance of senlinclient.
    :param args: Additional command line arguments, if any.
    """
    types = service.profile_types()
    utils.print_list(types, ['name'], sortby_index=0)


@utils.arg('type_name', metavar='<TYPE_NAME>',
           help=_('Profile type to retrieve.'))
@utils.arg('-F', '--format', metavar='<FORMAT>',
           choices=utils.supported_formats.keys(),
           help=_("The template output format, one of: %s.")
                 % ', '.join(utils.supported_formats.keys()))
def do_profile_type_show(service, args):
    """Get the details about a profile type."""
    try:
        res = service.get_profile_type(args.type_name)
    except sdk_exc.ResourceNotFound:
        raise exc.CommandError(
            _('Profile Type not found: %s') % args.type_name)

    pt = res.to_dict()

    if args.format:
        print(utils.format_output(pt, format=args.format))
    else:
        print(utils.format_output(pt))


# PROFILES

@utils.arg('-f', '--filters', metavar='<KEY1=VALUE1;KEY2=VALUE2...>',
           help=_('Filter parameters to apply on returned profiles. '
                  'This can be specified multiple times, or once with '
                  'parameters separated by a semicolon.'),
           action='append')
@utils.arg('-l', '--limit', metavar='<LIMIT>',
           help=_('Limit the number of profiles returned.'))
@utils.arg('-m', '--marker', metavar='<ID>',
           help=_('Only return profiles that appear after the given ID.'))
@utils.arg('-o', '--sort', metavar='<KEY:DIR>',
           help=_('Sorting option which is a string containing a list of keys '
                  'separated by commas. Each key can be optionally appened by '
                  'a sort direction (:asc or :desc)'))
@utils.arg('-g', '--global-project', default=False, action="store_true",
           help=_('Indicate that the list should include profiles from'
                  ' all projects. This option is subject to access policy '
                  'checking. Default is False.'))
@utils.arg('-F', '--full-id', default=False, action="store_true",
           help=_('Print full IDs in list.'))
def do_profile_list(service, args=None):
    """List profiles that meet the criteria."""
    fields = ['id', 'name', 'type', 'created_at']
    queries = {
        'limit': args.limit,
        'marker': args.marker,
        'sort': args.sort,
        'global_project': args.global_project,
    }
    if args.filters:
        queries.update(utils.format_parameters(args.filters))

    sortby_index = None if args.sort else 1

    profiles = service.profiles(**queries)
    formatters = {}
    if not args.full_id:
        formatters = {
            'id': lambda x: x.id[:8],
        }
    utils.print_list(profiles, fields, formatters=formatters,
                     sortby_index=sortby_index)


def _show_profile(service, profile_id):
    try:
        profile = service.get_profile(profile_id)
    except sdk_exc.ResourceNotFound:
        raise exc.CommandError(_('Profile not found: %s') % profile_id)

    formatters = {
        'metadata': utils.json_formatter,
    }

    formatters['spec'] = utils.nested_dict_formatter(
        ['type', 'version', 'properties'],
        ['property', 'value'])

    utils.print_dict(profile.to_dict(), formatters=formatters)


@utils.arg('-s', '--spec-file', metavar='<SPEC FILE>', required=True,
           help=_('The spec file used to create the profile.'))
@utils.arg('-M', '--metadata', metavar='<KEY1=VALUE1;KEY2=VALUE2...>',
           help=_('Metadata values to be attached to the profile. '
                  'This can be specified multiple times, or once with '
                  'key-value pairs separated by a semicolon.'),
           action='append')
@utils.arg('name', metavar='<PROFILE_NAME>',
           help=_('Name of the profile to create.'))
def do_profile_create(service, args):
    """Create a profile."""

    spec = utils.get_spec_content(args.spec_file)
    type_name = spec.get('type', None)
    type_version = spec.get('version', None)
    properties = spec.get('properties', None)
    if type_name is None:
        raise exc.CommandError(_("Missing 'type' key in spec file."))
    if type_version is None:
        raise exc.CommandError(_("Missing 'version' key in spec file."))
    if properties is None:
        raise exc.CommandError(_("Missing 'properties' key in spec file."))

    if type_name == 'os.heat.stack':
        stack_properties = utils.process_stack_spec(properties)
        spec['properties'] = stack_properties

    params = {
        'name': args.name,
        'spec': spec,
        'metadata': utils.format_parameters(args.metadata),
    }

    profile = service.create_profile(**params)
    _show_profile(service, profile.id)


@utils.arg('id', metavar='<PROFILE>',
           help=_('Name or ID of profile to show.'))
def do_profile_show(service, args):
    """Show the profile details."""
    _show_profile(service, args.id)


@utils.arg('-n', '--name', metavar='<NAME>',
           help=_('The new name for the profile.'))
@utils.arg('-M', '--metadata', metavar='<KEY1=VALUE1;KEY2=VALUE2...>',
           help=_('Metadata values to be attached to the profile. '
                  'This can be specified multiple times, or once with '
                  'key-value pairs separated by a semicolon.'),
           action='append')
@utils.arg('id', metavar='<PROFILE_ID>',
           help=_('Name or ID of the profile to update.'))
def do_profile_update(service, args):
    """Update a profile."""
    params = {
        'name': args.name,
    }
    if args.metadata:
        params['metadata'] = utils.format_parameters(args.metadata)

    # Find the profile first, we need its id
    try:
        profile = service.get_profile(args.id)
    except sdk_exc.ResourceNotFound:
        raise exc.CommandError(_('Profile not found: %s') % args.id)
    service.update_profile(profile.id, **params)
    _show_profile(service, profile.id)


@utils.arg('id', metavar='<PROFILE>', nargs='+',
           help=_('Name or ID of profile(s) to delete.'))
def do_profile_delete(service, args):
    """Delete profile(s)."""
    failure_count = 0

    for pid in args.id:
        try:
            service.delete_profile(pid, False)
        except Exception as ex:
            failure_count += 1
            print(ex)
    if failure_count > 0:
        msg = _('Failed to delete some of the specified profile(s).')
        raise exc.CommandError(msg)
    print('Profile deleted: %s' % args.id)


# POLICY TYPES


def do_policy_type_list(service, args):
    """List the available policy types."""
    types = service.policy_types()
    utils.print_list(types, ['name'], sortby_index=0)


@utils.arg('type_name', metavar='<TYPE_NAME>',
           help=_('Policy type to retrieve.'))
@utils.arg('-F', '--format', metavar='<FORMAT>',
           choices=utils.supported_formats.keys(),
           help=_("The template output format, one of: %s.")
                 % ', '.join(utils.supported_formats.keys()))
def do_policy_type_show(service, args):
    """Get the details about a policy type."""
    try:
        res = service.get_policy_type(args.type_name)
    except sdk_exc.ResourceNotFound:
        raise exc.CommandError(_('Policy type not found: %s') % args.type_name)

    pt = res.to_dict()
    if args.format:
        print(utils.format_output(pt, format=args.format))
    else:
        print(utils.format_output(pt))


# POLICIES

@utils.arg('-f', '--filters', metavar='<KEY1=VALUE1;KEY2=VALUE2...>',
           help=_('Filter parameters to apply on returned policies. '
                  'This can be specified multiple times, or once with '
                  'parameters separated by a semicolon.'),
           action='append')
@utils.arg('-l', '--limit', metavar='<LIMIT>',
           help=_('Limit the number of policies returned.'))
@utils.arg('-m', '--marker', metavar='<ID>',
           help=_('Only return policies that appear after the given ID.'))
@utils.arg('-o', '--sort', metavar='<KEY:DIR>',
           help=_('Sorting option which is a string containing a list of keys '
                  'separated by commas. Each key can be optionally appened by '
                  'a sort direction (:asc or :desc)'))
@utils.arg('-g', '--global-project', default=False, action="store_true",
           help=_('Indicate that the list should include policies from'
                  ' all projects. This option is subject to access policy '
                  'checking. Default is False.'))
@utils.arg('-F', '--full-id', default=False, action="store_true",
           help=_('Print full IDs in list.'))
def do_policy_list(service, args=None):
    """List policies that meet the criteria."""
    fields = ['id', 'name', 'type', 'created_at']
    queries = {
        'limit': args.limit,
        'marker': args.marker,
        'sort': args.sort,
        'global_project': args.global_project,
    }
    if args.filters:
        queries.update(utils.format_parameters(args.filters))

    sortby_index = None if args.sort else 1
    policies = service.policies(**queries)
    formatters = {}
    if not args.full_id:
        formatters = {
            'id': lambda x: x.id[:8]
        }
    utils.print_list(policies, fields, formatters=formatters,
                     sortby_index=sortby_index)


def _show_policy(service, policy_id):
    try:
        policy = service.get_policy(policy_id)
    except sdk_exc.ResourceNotFound:
        raise exc.CommandError(_('Policy not found: %s') % policy_id)

    formatters = {
        'metadata': utils.json_formatter,
        'spec': utils.json_formatter,
    }
    utils.print_dict(policy.to_dict(), formatters=formatters)


@utils.arg('-s', '--spec-file', metavar='<SPEC_FILE>', required=True,
           help=_('The spec file used to create the policy.'))
@utils.arg('name', metavar='<NAME>',
           help=_('Name of the policy to create.'))
def do_policy_create(service, args):
    """Create a policy."""
    spec = utils.get_spec_content(args.spec_file)
    attrs = {
        'name': args.name,
        'spec': spec,
    }

    policy = service.create_policy(**attrs)
    _show_policy(service, policy.id)


@utils.arg('id', metavar='<POLICY>',
           help=_('Name of the policy to be updated.'))
def do_policy_show(service, args):
    """Show the policy details."""
    _show_policy(service, policy_id=args.id)


@utils.arg('-n', '--name', metavar='<NAME>',
           help=_('New name of the policy to be updated.'))
@utils.arg('id', metavar='<POLICY>',
           help=_('Name of the policy to be updated.'))
def do_policy_update(service, args):
    """Update a policy."""
    params = {
        'name': args.name,
    }

    policy = service.get_policy(args.id)
    if policy is not None:
        service.update_policy(policy.id, **params)
        _show_policy(service, policy_id=policy.id)


@utils.arg('id', metavar='<POLICY>', nargs='+',
           help=_('Name or ID of policy(s) to delete.'))
def do_policy_delete(service, args):
    """Delete policy(s)."""
    failure_count = 0

    for pid in args.id:
        try:
            service.delete_policy(pid, False)
        except Exception as ex:
            failure_count += 1
            print(ex)
    if failure_count > 0:
        msg = _('Failed to delete some of the specified policy(s).')
        raise exc.CommandError(msg)
    print('Policy deleted: %s' % args.id)


# CLUSTERS


@utils.arg('-f', '--filters', metavar='<KEY1=VALUE1;KEY2=VALUE2...>',
           help=_('Filter parameters to apply on returned clusters. '
                  'This can be specified multiple times, or once with '
                  'parameters separated by a semicolon.'),
           action='append')
@utils.arg('-o', '--sort', metavar='<KEY:DIR>',
           help=_('Sorting option which is a string containing a list of keys '
                  'separated by commas. Each key can be optionally appened by '
                  'a sort direction (:asc or :desc)'))
@utils.arg('-l', '--limit', metavar='<LIMIT>',
           help=_('Limit the number of clusters returned.'))
@utils.arg('-m', '--marker', metavar='<ID>',
           help=_('Only return clusters that appear after the given cluster '
                  'ID.'))
@utils.arg('-g', '--global-project', default=False, action="store_true",
           help=_('Indicate that the cluster list should include clusters from'
                  ' all projects. This option is subject to access policy '
                  'checking. Default is False.'))
@utils.arg('-F', '--full-id', default=False, action="store_true",
           help=_('Print full IDs in list.'))
def do_cluster_list(service, args=None):
    """List the user's clusters."""
    fields = ['id', 'name', 'status', 'created_at', 'updated_at']
    queries = {
        'limit': args.limit,
        'marker': args.marker,
        'sort': args.sort,
        'global_project': args.global_project,
    }
    if args.filters:
        queries.update(utils.format_parameters(args.filters))

    sortby_index = None if args.sort else 3

    clusters = service.clusters(**queries)
    formatters = {}
    if not args.full_id:
        formatters = {
            'id': lambda x: x.id[:8]
        }
    utils.print_list(clusters, fields, formatters=formatters,
                     sortby_index=sortby_index)


def _show_cluster(service, cluster_id):
    try:
        cluster = service.get_cluster(cluster_id)
    except sdk_exc.ResourceNotFound:
        raise exc.CommandError(_('Cluster not found: %s') % cluster_id)

    formatters = {
        'metadata': utils.json_formatter,
        'nodes': utils.list_formatter,
    }
    utils.print_dict(cluster.to_dict(), formatters=formatters)


@utils.arg('-p', '--profile', metavar='<PROFILE>', required=True,
           help=_('Profile Id used for this cluster.'))
@utils.arg('-n', '--min-size', metavar='<MIN-SIZE>', default=0,
           help=_('Min size of the cluster. Default to 0.'))
@utils.arg('-m', '--max-size', metavar='<MAX-SIZE>', default=-1,
           help=_('Max size of the cluster. Default to -1, means unlimited.'))
@utils.arg('-c', '--desired-capacity', metavar='<DESIRED-CAPACITY>', default=0,
           help=_('Desired capacity of the cluster. Default to min_size if '
                  'min_size is specified else 0.'))
@utils.arg('-t', '--timeout', metavar='<TIMEOUT>', type=int,
           help=_('Cluster creation timeout in seconds.'))
@utils.arg('-M', '--metadata', metavar='<KEY1=VALUE1;KEY2=VALUE2...>',
           help=_('Metadata values to be attached to the cluster. '
                  'This can be specified multiple times, or once with '
                  'key-value pairs separated by a semicolon.'),
           action='append')
@utils.arg('name', metavar='<CLUSTER_NAME>',
           help=_('Name of the cluster to create.'))
def do_cluster_create(service, args):
    """Create the cluster."""
    if args.min_size and not args.desired_capacity:
        args.desired_capacity = args.min_size
    attrs = {
        'name': args.name,
        'profile_id': args.profile,
        'min_size': args.min_size,
        'max_size': args.max_size,
        'desired_capacity': args.desired_capacity,
        'metadata': utils.format_parameters(args.metadata),
        'timeout': args.timeout
    }

    cluster = service.create_cluster(**attrs)
    _show_cluster(service, cluster.id)


@utils.arg('id', metavar='<CLUSTER>', nargs='+',
           help=_('Name or ID of cluster(s) to delete.'))
def do_cluster_delete(service, args):
    """Delete the cluster(s)."""
    failure_count = 0

    for cid in args.id:
        try:
            service.delete_cluster(cid, False)
        except Exception as ex:
            failure_count += 1
            print(ex)
    if failure_count > 0:
        msg = _('Failed to delete some of the specified clusters.')
        raise exc.CommandError(msg)
    print('Request accepted')


@utils.arg('-p', '--profile', metavar='<PROFILE>',
           help=_('ID of new profile to use.'))
@utils.arg('-t', '--timeout', metavar='<TIMEOUT>',
           help=_('New timeout (in seconds) value for the cluster.'))
@utils.arg('-M', '--metadata', metavar='<KEY1=VALUE1;KEY2=VALUE2...>',
           help=_('Metadata values to be attached to the cluster. '
                  'This can be specified multiple times, or once with '
                  'key-value pairs separated by a semicolon.'),
           action='append')
@utils.arg('-n', '--name', metavar='<NAME>',
           help=_('New name for the cluster to update.'))
@utils.arg('id', metavar='<CLUSTER>',
           help=_('Name or ID of cluster to be updated.'))
def do_cluster_update(service, args):
    """Update the cluster."""
    cluster = service.get_cluster(args.id)
    attrs = {
        'name': args.name,
        'profile_id': args.profile,
        'metadata': utils.format_parameters(args.metadata),
        'timeout': args.timeout,
    }

    service.update_cluster(cluster.id, **attrs)
    _show_cluster(service, cluster.id)


@utils.arg('id', metavar='<CLUSTER>',
           help=_('Name or ID of cluster to show.'))
def do_cluster_show(service, args):
    """Show details of the cluster."""
    _show_cluster(service, args.id)


@utils.arg('-f', '--filters', metavar='<KEY1=VALUE1;KEY2=VALUE2...>',
           help=_('Filter parameters to apply on returned nodes. '
                  'This can be specified multiple times, or once with '
                  'parameters separated by a semicolon.'),
           action='append')
@utils.arg('-l', '--limit', metavar='<LIMIT>',
           help=_('Limit the number of nodes returned.'))
@utils.arg('-m', '--marker', metavar='<ID>',
           help=_('Only return nodes that appear after the given node ID.'))
@utils.arg('-F', '--full-id', default=False, action="store_true",
           help=_('Print full IDs in list.'))
@utils.arg('id', metavar='<CLUSTER>',
           help=_('Name or ID of cluster to nodes from.'))
def do_cluster_node_list(service, args):
    """List nodes from cluster."""

    queries = {
        'cluster_id': args.id,
        'limit': args.limit,
        'marker': args.marker,
    }
    if args.filters:
        queries.update(utils.format_parameters(args.filters))

    nodes = service.nodes(**queries)
    if not args.full_id:
        formatters = {
            'id': lambda x: x.id[:8],
            'physical_id': lambda x: x.physical_id[:8] if x.physical_id else ''
        }
    else:
        formatters = {}

    fields = ['id', 'name', 'index', 'status', 'physical_id', 'created_at']
    utils.print_list(nodes, fields, formatters=formatters, sortby_index=5)


@utils.arg('-n', '--nodes', metavar='<NODES>', required=True,
           help=_('ID of nodes to be added; multiple nodes can be separated '
                  'with ","'))
@utils.arg('id', metavar='<CLUSTER>',
           help=_('Name or ID of cluster to operate on.'))
def do_cluster_node_add(service, args):
    """Add specified nodes to cluster."""
    node_ids = args.nodes.split(',')
    resp = service.cluster_add_nodes(args.id, node_ids)
    print('Request accepted by action: %s' % resp['action'])


@utils.arg('-n', '--nodes', metavar='<NODES>', required=True,
           help=_('ID of nodes to be deleted; multiple nodes can be separated '
                  'with ",".'))
@utils.arg('id', metavar='<CLUSTER>',
           help=_('Name or ID of cluster to operate on.'))
def do_cluster_node_del(service, args):
    """Delete specified nodes from cluster."""
    node_ids = args.nodes.split(',')
    resp = service.cluster_del_nodes(args.id, node_ids)
    print('Request accepted by action: %s' % resp['action'])


@utils.arg('-c', '--capacity', metavar='<CAPACITY>', type=int,
           help=_('The desired number of nodes of the cluster.'))
@utils.arg('-a', '--adjustment', metavar='<ADJUSTMENT>', type=int,
           help=_('A positive integer meaning the number of nodes to add, '
                  'or a negative integer indicating the number of nodes to '
                  'remove.'))
@utils.arg('-p', '--percentage', metavar='<PERCENTAGE>', type=float,
           help=_('A value that is interpreted as the percentage of size '
                  'adjustment. This value can be positive or negative.'))
@utils.arg('-t', '--min-step', metavar='<MIN_STEP>', type=int,
           help=_('An integer specifying the number of nodes for adjustment '
                  'when <PERCENTAGE> is specified.'))
@utils.arg('-s', '--strict',  action='store_true', default=False,
           help=_('A boolean specifying whether the resize should be '
                  'performed on a best-effort basis when the new capacity '
                  'may go beyond size constraints.'))
@utils.arg('-n', '--min-size', metavar='MIN', type=int,
           help=_('New lower bound of cluster size.'))
@utils.arg('-m', '--max-size', metavar='MAX', type=int,
           help=_('New upper bound of cluster size. A value of -1 indicates '
                  'no upper limit on cluster size.'))
@utils.arg('id', metavar='<CLUSTER>',
           help=_('Name or ID of cluster to operate on.'))
def do_cluster_resize(service, args):
    """Resize a cluster."""
    # validate parameters
    # NOTE: this will be much simpler if cliutils supports exclusive groups

    action_args = {}

    capacity = args.capacity
    adjustment = args.adjustment
    percentage = args.percentage
    min_size = args.min_size
    max_size = args.max_size
    min_step = args.min_step

    if sum(v is not None for v in (capacity, adjustment, percentage)) > 1:
        raise exc.CommandError(_("Only one of 'capacity', 'adjustment' and "
                                 "'percentage' can be specified."))

    action_args['adjustment_type'] = None
    action_args['number'] = None

    if capacity is not None:
        if capacity < 0:
            raise exc.CommandError(_('Cluster capacity must be larger than '
                                     ' or equal to zero.'))
        action_args['adjustment_type'] = 'EXACT_CAPACITY'
        action_args['number'] = capacity

    if adjustment is not None:
        if adjustment == 0:
            raise exc.CommandError(_('Adjustment cannot be zero.'))
        action_args['adjustment_type'] = 'CHANGE_IN_CAPACITY'
        action_args['number'] = adjustment

    if percentage is not None:
        if (percentage == 0 or percentage == 0.0):
            raise exc.CommandError(_('Percentage cannot be zero.'))
        action_args['adjustment_type'] = 'CHANGE_IN_PERCENTAGE'
        action_args['number'] = percentage

    if min_step is not None:
        if percentage is None:
            raise exc.CommandError(_('Min step is only used with percentage.'))

    if min_size is not None:
        if min_size < 0:
            raise exc.CommandError(_('Min size cannot be less than zero.'))
        if max_size is not None and max_size >= 0 and min_size > max_size:
            raise exc.CommandError(_('Min size cannot be larger than '
                                     'max size.'))
        if capacity is not None and min_size > capacity:
            raise exc.CommandError(_('Min size cannot be larger than the '
                                     'specified capacity'))

    if max_size is not None:
        if capacity is not None and max_size > 0 and max_size < capacity:
            raise exc.CommandError(_('Max size cannot be less than the '
                                     'specified capacity.'))
        # do a normalization
        if max_size < 0:
            max_size = -1

    action_args['min_size'] = min_size
    action_args['max_size'] = max_size
    action_args['min_step'] = min_step
    action_args['strict'] = args.strict

    resp = service.cluster_resize(args.id, **action_args)
    print('Request accepted by action: %s' % resp['action'])


@utils.arg('-c', '--count', metavar='<COUNT>',
           help=_('Number of nodes to be added to the specified cluster.'))
@utils.arg('id', metavar='<CLUSTER>',
           help=_('Name or ID of cluster to operate on.'))
def do_cluster_scale_out(service, args):
    """Scale out a cluster by the specified number of nodes."""
    resp = service.cluster_scale_out(args.id, args.count)
    print('Request accepted by action %s' % resp['action'])


@utils.arg('-c', '--count', metavar='<COUNT>',
           help=_('Number of nodes to be deleted from the specified cluster.'))
@utils.arg('id', metavar='<CLUSTER>',
           help=_('Name or ID of cluster to operate on.'))
def do_cluster_scale_in(service, args):
    """Scale in a cluster by the specified number of nodes."""
    resp = service.cluster_scale_in(args.id, args.count)
    print('Request accepted by action %s' % resp['action'])


@utils.arg('-f', '--filters', metavar='<KEY1=VALUE1;KEY2=VALUE2...>',
           help=_('Filter parameters to apply on returned results. '
                  'This can be specified multiple times, or once with '
                  'parameters separated by a semicolon.'),
           action='append')
@utils.arg('-o', '--sort', metavar='<SORT_STRING>',
           help=_('Sorting option which is a string containing a list of keys '
                  'separated by commas. Each key can be optionally appened by '
                  'a sort direction (:asc or :desc)'))
@utils.arg('-F', '--full-id', default=False, action="store_true",
           help=_('Print full IDs in list.'))
@utils.arg('id', metavar='<CLUSTER>',
           help=_('Name or ID of cluster to query on.'))
def do_cluster_policy_list(service, args):
    """List policies from cluster."""
    fields = ['policy_id', 'policy_name', 'policy_type', 'enabled']

    cluster = service.get_cluster(args.id)
    queries = {
        'sort': args.sort,
    }

    if args.filters:
        queries.update(utils.format_parameters(args.filters))

    sortby_index = None if args.sort else 3
    policies = service.cluster_policies(cluster.id, **queries)
    formatters = {}
    if not args.full_id:
        formatters = {
            'policy_id': lambda x: x.id[:8]
        }

    utils.print_list(policies, fields, formatters=formatters,
                     sortby_index=sortby_index)


@utils.arg('-p', '--policy', metavar='<POLICY>', required=True,
           help=_('ID or name of the policy to query on.'))
@utils.arg('id', metavar='<CLUSTER>',
           help=_('ID or name of the cluster to query on.'))
def do_cluster_policy_show(service, args):
    """Show a specific policy that is bound to the specified cluster."""
    binding = service.get_cluster_policy(args.policy, args.id)
    utils.print_dict(binding.to_dict())


@utils.arg('-p', '--policy', metavar='<POLICY>', required=True,
           help=_('ID or name of policy to be attached.'))
@utils.arg('-e', '--enabled', default=True, action="store_true",
           help=_('Whether the policy should be enabled once attached. '
                  'Default to enabled.'))
@utils.arg('id', metavar='<NAME or ID>',
           help=_('Name or ID of cluster to operate on.'))
def do_cluster_policy_attach(service, args):
    """Attach policy to cluster."""
    kwargs = {
        'enabled': args.enabled,
    }

    resp = service.cluster_attach_policy(args.id, args.policy, **kwargs)
    print('Request accepted by action: %s' % resp['action'])


@utils.arg('-p', '--policy', metavar='<POLICY>', required=True,
           help=_('ID or name of policy to be detached.'))
@utils.arg('id', metavar='<NAME or ID>',
           help=_('Name or ID of cluster to operate on.'))
def do_cluster_policy_detach(service, args):
    """Detach policy from cluster."""
    resp = service.cluster_detach_policy(args.id, args.policy)
    print('Request accepted by action %s' % resp['action'])


@utils.arg('-p', '--policy', metavar='<POLICY>', required=True,
           help=_('ID or name of policy to be updated.'))
@utils.arg('-e', '--enabled', metavar='<BOOLEAN>',
           help=_('Whether the policy should be enabled.'))
@utils.arg('id', metavar='<NAME or ID>',
           help=_('Name or ID of cluster to operate on.'))
def do_cluster_policy_update(service, args):
    """Update a policy's properties on a cluster."""
    kwargs = {
        'enabled': args.enabled,
    }

    resp = service.cluster_update_policy(args.id, args.policy, **kwargs)
    print('Request accepted by action: %s' % resp['action'])


# NODES


@utils.arg('-c', '--cluster', metavar='<CLUSTER>',
           help=_('ID or name of cluster from which nodes are to be listed.'))
@utils.arg('-f', '--filters', metavar='<KEY1=VALUE1;KEY2=VALUE2...>',
           help=_('Filter parameters to apply on returned nodes. '
                  'This can be specified multiple times, or once with '
                  'parameters separated by a semicolon.'),
           action='append')
@utils.arg('-o', '--sort', metavar='<KEY:DIR>',
           help=_('Sorting option which is a string containing a list of keys '
                  'separated by commas. Each key can be optionally appened by '
                  'a sort direction (:asc or :desc)'))
@utils.arg('-l', '--limit', metavar='<LIMIT>',
           help=_('Limit the number of nodes returned.'))
@utils.arg('-m', '--marker', metavar='<ID>',
           help=_('Only return nodes that appear after the given node ID.'))
@utils.arg('-g', '--global-project', default=False, action="store_true",
           help=_('Indicate that this node list should include nodes from '
                  'all projects. This option is subject to access policy '
                  'checking. Default is False.'))
@utils.arg('-F', '--full-id', default=False, action="store_true",
           help=_('Print full IDs in list.'))
def do_node_list(service, args):
    """Show list of nodes."""

    fields = ['id', 'name', 'index', 'status', 'cluster_id', 'physical_id',
              'profile_name', 'created_at', 'updated_at']
    queries = {
        'cluster_id': args.cluster,
        'sort': args.sort,
        'limit': args.limit,
        'marker': args.marker,
        'global_project': args.global_project,
    }

    if args.filters:
        queries.update(utils.format_parameters(args.filters))

    sortby_index = None if args.sort else 6

    nodes = service.nodes(**queries)

    if not args.full_id:
        formatters = {
            'id': lambda x: x.id[:8],
            'cluster_id': lambda x: x.cluster_id[:8] if x.cluster_id else '',
            'physical_id': lambda x: x.physical_id[:8] if x.physical_id else ''
        }
    else:
        formatters = {}

    utils.print_list(nodes, fields, formatters=formatters,
                     sortby_index=sortby_index)


def _show_node(service, node_id, show_details=False):
    """Show detailed info about the specified node."""

    args = {'show_details': True} if show_details else None
    try:
        node = service.get_node(node_id, args=args)
    except sdk_exc.ResourceNotFound:
        raise exc.CommandError(_('Node not found: %s') % node_id)

    formatters = {
        'metadata': utils.json_formatter,
        'data': utils.json_formatter,
    }
    data = node.to_dict()
    if show_details:
        formatters['details'] = utils.nested_dict_formatter(
            list(node['details'].keys()), ['property', 'value'])

    utils.print_dict(data, formatters=formatters)


@utils.arg('-p', '--profile', metavar='<PROFILE>', required=True,
           help=_('Profile Id used for this node.'))
@utils.arg('-c', '--cluster', metavar='<CLUSTER>',
           help=_('Cluster Id for this node.'))
@utils.arg('-r', '--role', metavar='<ROLE>',
           help=_('Role for this node in the specific cluster.'))
@utils.arg('-M', '--metadata', metavar='<KEY1=VALUE1;KEY2=VALUE2...>',
           help=_('Metadata values to be attached to the node. '
                  'This can be specified multiple times, or once with '
                  'key-value pairs separated by a semicolon.'),
           action='append')
@utils.arg('name', metavar='<NODE_NAME>',
           help=_('Name of the node to create.'))
def do_node_create(service, args):
    """Create the node."""
    attrs = {
        'name': args.name,
        'cluster_id': args.cluster,
        'profile_id': args.profile,
        'role': args.role,
        'metadata': utils.format_parameters(args.metadata),
    }

    node = service.create_node(**attrs)
    _show_node(service, node.id)


@utils.arg('-D', '--details', default=False, action="store_true",
           help=_('Include physical object details.'))
@utils.arg('id', metavar='<NODE>',
           help=_('Name or ID of the node to show the details for.'))
def do_node_show(service, args):
    """Show detailed info about the specified node."""
    _show_node(service, args.id, args.details)


@utils.arg('id', metavar='<NODE>', nargs='+',
           help=_('Name or ID of node(s) to delete.'))
def do_node_delete(service, args):
    """Delete the node(s)."""
    failure_count = 0

    for nid in args.id:
        try:
            service.delete_node(nid, False)
        except Exception as ex:
            failure_count += 1
            print(ex)
    if failure_count > 0:
        msg = _('Failed to delete some of the specified nodes.')
        raise exc.CommandError(msg)
    print('Request accepted')


@utils.arg('-n', '--name', metavar='<NAME>',
           help=_('New name for the node.'))
@utils.arg('-p', '--profile', metavar='<PROFILE ID>',
           help=_('ID of new profile to use.'))
@utils.arg('-r', '--role', metavar='<ROLE>',
           help=_('Role for this node in the specific cluster.'))
@utils.arg('-M', '--metadata', metavar='<KEY1=VALUE1;KEY2=VALUE2...>',
           help=_('Metadata values to be attached to the node. '
                  'Metadata can be specified multiple times, or once with '
                  'key-value pairs separated by a semicolon.'),
           action='append')
@utils.arg('id', metavar='<NODE>',
           help=_('Name or ID of node to update.'))
def do_node_update(service, args):
    """Update the node."""
    # Find the node first, we need its UUID
    try:
        node = service.get_node(args.id)
    except sdk_exc.ResourceNotFound:
        raise exc.CommandError(_('Node not found: %s') % args.id)

    attrs = {
        'name': args.name,
        'role': args.role,
        'profile_id': args.profile,
        'metadata': utils.format_parameters(args.metadata),
    }

    service.update_node(args.id, **attrs)
    _show_node(service, node.id)


# RECEIVERS


@utils.arg('-f', '--filters', metavar='<KEY1=VALUE1;KEY2=VALUE2...>',
           help=_('Filter parameters to apply on returned receivers. '
                  'This can be specified multiple times, or once with '
                  'parameters separated by a semicolon.'),
           action='append')
@utils.arg('-l', '--limit', metavar='<LIMIT>',
           help=_('Limit the number of receivers returned.'))
@utils.arg('-m', '--marker', metavar='<ID>',
           help=_('Only return receivers that appear after the given ID.'))
@utils.arg('-o', '--sort', metavar='<KEY:DIR>',
           help=_('Sorting option which is a string containing a list of keys '
                  'separated by commas. Each key can be optionally appened by '
                  'a sort direction (:asc or :desc)'))
@utils.arg('-g', '--global-project', default=False, action="store_true",
           help=_('Indicate that the list should include receivers from'
                  ' all projects. This option is subject to access policy '
                  'checking. Default is False.'))
@utils.arg('-F', '--full-id', default=False, action="store_true",
           help=_('Print full IDs in list.'))
def do_receiver_list(service, args):
    """List receivers that meet the criteria."""
    fields = ['id', 'name', 'type', 'cluster_id', 'action', 'created_at']
    queries = {
        'limit': args.limit,
        'marker': args.marker,
        'sort': args.sort,
        'global_project': args.global_project,
    }

    if args.filters:
        queries.update(utils.format_parameters(args.filters))

    sortby_index = None if args.sort else 0

    receivers = service.receivers(**queries)
    formatters = {}
    if not args.full_id:
        formatters = {
            'id': lambda x: x.id[:8],
            'cluster_id': lambda x: x.cluster_id[:8],
        }
    utils.print_list(receivers, fields, formatters=formatters,
                     sortby_index=sortby_index)


def _show_receiver(service, receiver_id):
    try:
        receiver = service.get_receiver(receiver_id)
    except sdk_exc.ResourceNotFound:
        raise exc.CommandError(_('Receiver not found: %s') % receiver_id)

    formatters = {
        'actor': utils.json_formatter,
        'params': utils.json_formatter,
        'channel': utils.json_formatter,
    }

    utils.print_dict(receiver.to_dict(), formatters=formatters)


@utils.arg('id', metavar='<RECEIVER>',
           help=_('Name or ID of the receiver to show.'))
def do_receiver_show(service, args):
    """Show the receiver details."""
    _show_receiver(service, receiver_id=args.id)


@utils.arg('-t', '--type', metavar='<TYPE>', default='webhook',
           help=_('Type of the receiver to create.'))
@utils.arg('-c', '--cluster', metavar='<CLUSTER>', required=True,
           help=_('Targeted cluster for this receiver.'))
@utils.arg('-a', '--action', metavar='<ACTION>', required=True,
           help=_('Name or ID of the targeted action to be triggered.'))
@utils.arg('-P', '--params', metavar='<KEY1=VALUE1;KEY2=VALUE2...>',
           help=_('A dictionary of parameters that will be passed to target '
                  'action when the receiver is triggered.'),
           action='append')
@utils.arg('name', metavar='<NAME>',
           help=_('Name of the receiver to create.'))
def do_receiver_create(service, args):
    """Create a receiver."""

    params = {
        'name': args.name,
        'type': args.type,
        'cluster_id': args.cluster,
        'action': args.action,
        'params': utils.format_parameters(args.params)
    }

    receiver = service.create_receiver(**params)
    _show_receiver(service, receiver.id)


@utils.arg('id', metavar='<RECEIVER>', nargs='+',
           help=_('Name or ID of receiver(s) to delete.'))
def do_receiver_delete(service, args):
    """Delete receiver(s)."""
    failure_count = 0

    for wid in args.id:
        try:
            service.delete_receiver(wid, False)
        except Exception as ex:
            failure_count += 1
            print(ex)
    if failure_count > 0:
        msg = _('Failed to delete some of the specified receiver(s).')
        raise exc.CommandError(msg)
    print('Receivers deleted: %s' % args.id)


# EVENTS


@utils.arg('-f', '--filters', metavar='<KEY1=VALUE1;KEY2=VALUE2...>',
           help=_('Filter parameters to apply on returned events. '
                  'This can be specified multiple times, or once with '
                  'parameters separated by a semicolon.'),
           action='append')
@utils.arg('-l', '--limit', metavar='<LIMIT>',
           help=_('Limit the number of events returned.'))
@utils.arg('-m', '--marker', metavar='<ID>',
           help=_('Only return events that appear after the given event ID.'))
@utils.arg('-o', '--sort', metavar='<KEY:DIR>',
           help=_('Sorting option which is a string containing a list of keys '
                  'separated by commas. Each key can be optionally appened by '
                  'a sort direction (:asc or :desc)'))
@utils.arg('-g', '--global-project', default=False, action="store_true",
           help=_('Whether events from all projects should be listed. '
                  ' Default to False. Setting this to True may demand '
                  'for an admin privilege.'))
@utils.arg('-F', '--full-id', default=False, action="store_true",
           help=_('Print full IDs in list.'))
def do_event_list(service, args):
    """List events."""

    fields = ['id', 'timestamp', 'obj_type', 'obj_id', 'obj_name', 'action',
              'status', 'status_reason', 'level']
    queries = {
        'sort': args.sort,
        'limit': args.limit,
        'marker': args.marker,
        'global_project': args.global_project,
    }

    if args.filters:
        queries.update(utils.format_parameters(args.filters))

    sortby_index = None if args.sort else 0
    formatters = {}
    if not args.full_id:
        formatters['id'] = lambda x: x.id[:8]
        formatters['obj_id'] = lambda x: x.obj_id[:8] if x.obj_id else ''

    events = service.events(**queries)
    utils.print_list(events, fields, formatters=formatters,
                     sortby_index=sortby_index)


@utils.arg('id', metavar='<EVENT>',
           help=_('ID of event to display details for.'))
def do_event_show(service, args):
    """Describe the event."""
    try:
        event = service.get_event(args.id)
    except sdk_exc.ResourceNotFound:
        raise exc.CommandError(_("Event not found: %s") % args.id)

    utils.print_dict(event.to_dict())


# ACTIONS


@utils.arg('-f', '--filters', metavar='<KEY1=VALUE1;KEY2=VALUE2...>',
           help=_('Filter parameters to apply on returned actions. '
                  'This can be specified multiple times, or once with '
                  'parameters separated by a semicolon.'),
           action='append')
@utils.arg('-o', '--sort', metavar='<KEY:DIR>',
           help=_('Sorting option which is a string containing a list of keys '
                  'separated by commas. Each key can be optionally appened by '
                  'a sort direction (:asc or :desc)'))
@utils.arg('-l', '--limit', metavar='<LIMIT>',
           help=_('Limit the number of actions returned.'))
@utils.arg('-m', '--marker', metavar='<ID>',
           help=_('Only return actions that appear after the given node ID.'))
@utils.arg('-F', '--full-id', default=False, action="store_true",
           help=_('Print full IDs in list.'))
def do_action_list(service, args):
    """List actions."""

    fields = ['id', 'name', 'action', 'status', 'target', 'depends_on',
              'depended_by', 'created_at']

    queries = {
        'sort': args.sort,
        'limit': args.limit,
        'marker': args.marker,
    }

    if args.filters:
        queries.update(utils.format_parameters(args.filters))

    sortby_index = None if args.sort else 0

    actions = service.actions(**queries)

    formatters = {}
    if args.full_id:
        f_depon = lambda x: '\n'.join(a for a in x.depends_on)
        f_depby = lambda x: '\n'.join(a for a in x.depended_by)

        formatters['depends_on'] = f_depon
        formatters['depended_by'] = f_depby
    else:
        formatters['id'] = lambda x: x.id[:8]
        formatters['target'] = lambda x: x.target[:8]
        f_depon = lambda x: '\n'.join(a[:8] for a in x.depends_on)
        f_depby = lambda x: '\n'.join(a[:8] for a in x.depended_by)
        formatters['depends_on'] = f_depon
        formatters['depended_by'] = f_depby

    utils.print_list(actions, fields, formatters=formatters,
                     sortby_index=sortby_index)


@utils.arg('id', metavar='<ACTION>',
           help=_('Name or ID of the action to show the details for.'))
def do_action_show(service, args):
    """Show detailed info about the specified action."""
    try:
        action = service.get_action(args.id)
    except sdk_exc.ResourceNotFound:
        raise exc.CommandError(_('Action not found: %s') % args.id)

    formatters = {
        'inputs': utils.json_formatter,
        'outputs': utils.json_formatter,
        'metadata': utils.json_formatter,
        'data': utils.json_formatter,
        'depends_on': utils.list_formatter,
        'depended_by': utils.list_formatter,
    }

    utils.print_dict(action.to_dict(), formatters=formatters)