summaryrefslogtreecommitdiff
path: root/cinderclient/v3/shell.py
blob: 8c95b76d66be2ce26eef03674fd4c8621ae2a630 (plain)
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
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
# Copyright (c) 2013-2014 OpenStack Foundation
# All Rights Reserved.
#
# 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.

from __future__ import print_function

import argparse
import collections
import os

from oslo_utils import strutils
import six

import cinderclient
from cinderclient import api_versions
from cinderclient import base
from cinderclient import exceptions
from cinderclient import shell_utils
from cinderclient import utils

from cinderclient.v2.shell import *  # noqa
from cinderclient.v2.shell import CheckSizeArgForCreate

FILTER_DEPRECATED = ("This option is deprecated and will be removed in "
                     "newer release. Please use '--filters' option which "
                     "is introduced since 3.33 instead.")


class AppendFilters(argparse.Action):

    filters = []

    def __call__(self, parser, namespace, values, option_string):
        AppendFilters.filters.append(values[0])


@api_versions.wraps('3.33')
@utils.arg('--resource',
           metavar='<resource>',
           default=None,
           help='Show enabled filters for specified resource. Default=None.')
def do_list_filters(cs, args):
    """List enabled filters.

    Symbol '~' after filter key means it supports inexact filtering.
    """
    filters = cs.resource_filters.list(resource=args.resource)
    shell_utils.print_resource_filter_list(filters)


@utils.arg('--filters',
           action=AppendFilters,
           type=six.text_type,
           nargs='*',
           start_version='3.52',
           metavar='<key=value>',
           default=None,
           help="Filter key and value pairs. Admin only.")
def do_type_list(cs, args):
    """Lists available 'volume types'.

    (Only admin and tenant users will see private types)
    """
    # pylint: disable=function-redefined
    search_opts = {}
    # Update search option with `filters`
    if AppendFilters.filters:
        search_opts.update(shell_utils.extract_filters(AppendFilters.filters))
    vtypes = cs.volume_types.list(search_opts=search_opts)
    shell_utils.print_volume_type_list(vtypes)

    with cs.volume_types.completion_cache(
            'uuid',
            cinderclient.v3.volume_types.VolumeType,
            mode="w"):
        for vtype in vtypes:
            cs.volume_types.write_to_completion_cache('uuid', vtype.id)
    with cs.volume_types.completion_cache(
            'name',
            cinderclient.v3.volume_types.VolumeType,
            mode="w"):
        for vtype in vtypes:
            cs.volume_types.write_to_completion_cache('name', vtype.name)
    AppendFilters.filters = []


@utils.arg('--all-tenants',
           metavar='<all_tenants>',
           nargs='?',
           type=int,
           const=1,
           default=0,
           help='Shows details for all tenants. Admin only.')
@utils.arg('--all_tenants',
           nargs='?',
           type=int,
           const=1,
           help=argparse.SUPPRESS)
@utils.arg('--name',
           metavar='<name>',
           default=None,
           help="Filters results by a name. Default=None. "
                "%s" % FILTER_DEPRECATED)
@utils.arg('--status',
           metavar='<status>',
           default=None,
           help="Filters results by a status. Default=None. "
                "%s" % FILTER_DEPRECATED)
@utils.arg('--volume-id',
           metavar='<volume-id>',
           default=None,
           help="Filters results by a volume ID. Default=None. "
                "%s" % FILTER_DEPRECATED)
@utils.arg('--volume_id',
           help=argparse.SUPPRESS)
@utils.arg('--marker',
           metavar='<marker>',
           default=None,
           help='Begin returning backups that appear later in the backup '
                'list than that represented by this id. '
                'Default=None.')
@utils.arg('--limit',
           metavar='<limit>',
           default=None,
           help='Maximum number of backups to return. Default=None.')
@utils.arg('--sort',
           metavar='<key>[:<direction>]',
           default=None,
           help=(('Comma-separated list of sort keys and directions in the '
                  'form of <key>[:<asc|desc>]. '
                  'Valid keys: %s. '
                  'Default=None.') % ', '.join(base.SORT_KEY_VALUES)))
@utils.arg('--filters',
           action=AppendFilters,
           type=six.text_type,
           nargs='*',
           start_version='3.33',
           metavar='<key=value>',
           default=None,
           help="Filter key and value pairs. Please use 'cinder list-filters' "
                "to check enabled filters from server. Use 'key~=value' for "
                "inexact filtering if the key supports. Default=None.")
@utils.arg('--with-count',
           type=bool,
           default=False,
           const=True,
           nargs='?',
           start_version='3.45',
           metavar='<True|False>',
           help="Show total number of backup entities. This is useful when "
                "pagination is applied in the request.")
def do_backup_list(cs, args):
    """Lists all backups."""
    # pylint: disable=function-redefined

    show_count = True if hasattr(
        args, 'with_count') and args.with_count else False
    search_opts = {
        'all_tenants': args.all_tenants,
        'name': args.name,
        'status': args.status,
        'volume_id': args.volume_id,
    }

    # Update search option with `filters`
    if AppendFilters.filters:
        search_opts.update(shell_utils.extract_filters(AppendFilters.filters))

    total_count = 0
    if show_count:
        search_opts['with_count'] = args.with_count
        backups, total_count = cs.backups.list(search_opts=search_opts,
                                               marker=args.marker,
                                               limit=args.limit,
                                               sort=args.sort)
    else:
        backups = cs.backups.list(search_opts=search_opts,
                                  marker=args.marker,
                                  limit=args.limit,
                                  sort=args.sort)
    shell_utils.translate_volume_snapshot_keys(backups)
    columns = ['ID', 'Volume ID', 'Status', 'Name', 'Size', 'Object Count',
               'Container']
    if cs.api_version >= api_versions.APIVersion('3.56'):
        columns.append('User ID')

    if args.sort:
        sortby_index = None
    else:
        sortby_index = 0
    utils.print_list(backups, columns, sortby_index=sortby_index)
    if show_count:
        print("Backup in total: %s" % total_count)

    with cs.backups.completion_cache(
            'uuid',
            cinderclient.v3.volume_backups.VolumeBackup,
            mode="w"):
        for backup in backups:
            cs.backups.write_to_completion_cache('uuid', backup.id)
    with cs.backups.completion_cache(
            'name',
            cinderclient.v3.volume_backups.VolumeBackup,
            mode='w'):
        for backup in backups:
            if backup.name is not None:
                cs.backups.write_to_completion_cache('name', backup.name)
    AppendFilters.filters = []


@utils.arg('--detail',
           action='store_true',
           help='Show detailed information about pools.')
@utils.arg('--filters',
           action=AppendFilters,
           type=six.text_type,
           nargs='*',
           start_version='3.33',
           metavar='<key=value>',
           default=None,
           help="Filter key and value pairs. Please use 'cinder list-filters' "
                "to check enabled filters from server, Default=None.")
def do_get_pools(cs, args):
    """Show pool information for backends. Admin only."""
    # pylint: disable=function-redefined
    search_opts = {}
    # Update search option with `filters`
    if AppendFilters.filters:
        search_opts.update(shell_utils.extract_filters(AppendFilters.filters))
    if cs.api_version >= api_versions.APIVersion("3.33"):
        pools = cs.volumes.get_pools(args.detail, search_opts)
    else:
        pools = cs.volumes.get_pools(args.detail)
    infos = dict()
    infos.update(pools._info)

    for info in infos['pools']:
        backend = dict()
        backend['name'] = info['name']
        if args.detail:
            backend.update(info['capabilities'])
        utils.print_dict(backend)
    AppendFilters.filters = []


RESET_STATE_RESOURCES = {'volume': utils.find_volume,
                         'backup': shell_utils.find_backup,
                         'snapshot': shell_utils.find_volume_snapshot,
                         'group': shell_utils.find_group,
                         'group-snapshot': shell_utils.find_group_snapshot}


@utils.arg('--group_id',
           metavar='<group_id>',
           default=None,
           help="Filters results by a group_id. Default=None."
                "%s" % FILTER_DEPRECATED,
           start_version='3.10')
@utils.arg('--all-tenants',
           dest='all_tenants',
           metavar='<0|1>',
           nargs='?',
           type=int,
           const=1,
           default=0,
           help='Shows details for all tenants. Admin only.')
@utils.arg('--all_tenants',
           nargs='?',
           type=int,
           const=1,
           help=argparse.SUPPRESS)
@utils.arg('--name',
           metavar='<name>',
           default=None,
           help="Filters results by a name. Default=None. "
                "%s" % FILTER_DEPRECATED)
@utils.arg('--display-name',
           help=argparse.SUPPRESS)
@utils.arg('--status',
           metavar='<status>',
           default=None,
           help="Filters results by a status. Default=None. "
                "%s" % FILTER_DEPRECATED)
@utils.arg('--bootable',
           metavar='<True|true|False|false>',
           const=True,
           nargs='?',
           choices=['True', 'true', 'False', 'false'],
           help="Filters results by bootable status. Default=None. "
                "%s" % FILTER_DEPRECATED)
@utils.arg('--migration_status',
           metavar='<migration_status>',
           default=None,
           help="Filters results by a migration status. Default=None. "
                "Admin only. "
                "%s" % FILTER_DEPRECATED)
@utils.arg('--metadata',
           nargs='*',
           metavar='<key=value>',
           default=None,
           help="Filters results by a metadata key and value pair. "
                "Default=None. "
                "%s" % FILTER_DEPRECATED)
@utils.arg('--image_metadata',
           nargs='*',
           metavar='<key=value>',
           default=None,
           start_version='3.4',
           help="Filters results by a image metadata key and value pair. "
                "Require volume api version >=3.4. Default=None."
                "%s" % FILTER_DEPRECATED)
@utils.arg('--marker',
           metavar='<marker>',
           default=None,
           help='Begin returning volumes that appear later in the volume '
                'list than that represented by this volume id. '
                'Default=None.')
@utils.arg('--limit',
           metavar='<limit>',
           default=None,
           help='Maximum number of volumes to return. Default=None.')
@utils.arg('--fields',
           default=None,
           metavar='<fields>',
           help='Comma-separated list of fields to display. '
                'Use the show command to see which fields are available. '
                'Unavailable/non-existent fields will be ignored. '
                'Default=None.')
@utils.arg('--sort',
           metavar='<key>[:<direction>]',
           default=None,
           help=(('Comma-separated list of sort keys and directions in the '
                  'form of <key>[:<asc|desc>]. '
                  'Valid keys: %s. '
                  'Default=None.') % ', '.join(base.SORT_KEY_VALUES)))
@utils.arg('--tenant',
           type=str,
           dest='tenant',
           nargs='?',
           metavar='<tenant>',
           help='Display information from single tenant (Admin only).')
@utils.arg('--filters',
           action=AppendFilters,
           type=six.text_type,
           nargs='*',
           start_version='3.33',
           metavar='<key=value>',
           default=None,
           help="Filter key and value pairs. Please use 'cinder list-filters' "
                "to check enabled filters from server. Use 'key~=value' "
                "for inexact filtering if the key supports. Default=None.")
@utils.arg('--with-count',
           type=bool,
           default=False,
           const=True,
           nargs='?',
           start_version='3.45',
           metavar='<True|False>',
           help="Show total number of volume entities. This is useful when "
                "pagination is applied in the request.")
def do_list(cs, args):
    """Lists all volumes."""
    # pylint: disable=function-redefined
    # NOTE(thingee): Backwards-compatibility with v1 args
    if args.display_name is not None:
        args.name = args.display_name
    show_count = True if hasattr(
        args, 'with_count') and args.with_count else False
    all_tenants = 1 if args.tenant else \
        int(os.environ.get("ALL_TENANTS", args.all_tenants))
    search_opts = {
        'all_tenants': all_tenants,
        'project_id': args.tenant,
        'name': args.name,
        'status': args.status,
        'bootable': args.bootable,
        'migration_status': args.migration_status,
        'metadata': shell_utils.extract_metadata(args)
        if args.metadata else None,
        'glance_metadata': shell_utils.extract_metadata(args,
                                                        type='image_metadata')
        if hasattr(args, 'image_metadata') and args.image_metadata else None,
        'group_id': getattr(args, 'group_id', None),
    }
    # Update search option with `filters`
    if AppendFilters.filters:
        search_opts.update(shell_utils.extract_filters(AppendFilters.filters))

    # If unavailable/non-existent fields are specified, these fields will
    # be removed from key_list at the print_list() during key validation.
    field_titles = []
    if args.fields:
        for field_title in args.fields.split(','):
            field_titles.append(field_title)

    total_count = 0
    if show_count:
        search_opts['with_count'] = args.with_count
        volumes, total_count = cs.volumes.list(
            search_opts=search_opts, marker=args.marker,
            limit=args.limit, sort=args.sort)
    else:
        volumes = cs.volumes.list(search_opts=search_opts, marker=args.marker,
                                  limit=args.limit, sort=args.sort)
    shell_utils.translate_volume_keys(volumes)

    # Create a list of servers to which the volume is attached
    for vol in volumes:
        servers = [s.get('server_id') for s in vol.attachments]
        setattr(vol, 'attached_to', ','.join(map(str, servers)))

    with cs.volumes.completion_cache('uuid',
                                     cinderclient.v3.volumes.Volume,
                                     mode="w"):
        for vol in volumes:
            cs.volumes.write_to_completion_cache('uuid', vol.id)

    with cs.volumes.completion_cache('name',
                                     cinderclient.v3.volumes.Volume,
                                     mode="w"):
        for vol in volumes:
            if vol.name is None:
                continue
            cs.volumes.write_to_completion_cache('name', vol.name)

    if field_titles:
        # Remove duplicate fields
        key_list = ['ID']
        unique_titles = [k for k in collections.OrderedDict.fromkeys(
            [x.title().strip() for x in field_titles]) if k != 'Id']
        key_list.extend(unique_titles)
    else:
        key_list = ['ID', 'Status', 'Name', 'Size', 'Volume Type',
                    'Bootable', 'Attached to']
        # If all_tenants is specified, print
        # Tenant ID as well.
        if search_opts['all_tenants']:
            key_list.insert(1, 'Tenant ID')

    if args.sort:
        sortby_index = None
    else:
        sortby_index = 0
    utils.print_list(volumes, key_list, exclude_unavailable=True,
                     sortby_index=sortby_index)
    if show_count:
        print("Volume in total: %s" % total_count)
    AppendFilters.filters = []


@utils.arg('entity', metavar='<entity>', nargs='+',
           help='Name or ID of entity to update.')
@utils.arg('--type', metavar='<type>', default='volume',
           choices=RESET_STATE_RESOURCES.keys(),
           help="Type of entity to update. Available resources "
                "are: 'volume', 'snapshot', 'backup', "
                "'group' (since 3.20) and "
                "'group-snapshot' (since 3.19), Default=volume.")
@utils.arg('--state', metavar='<state>', default=None,
           help=("The state to assign to the entity. "
                 "NOTE: This command simply changes the state of the "
                 "entity in the database with no regard to actual status, "
                 "exercise caution when using. Default=None, that means the "
                 "state is unchanged."))
@utils.arg('--attach-status', metavar='<attach-status>', default=None,
           help=('This is only used for a volume entity. The attach status '
                 'to assign to the volume in the database, with no regard '
                 'to the actual status. Valid values are "attached" and '
                 '"detached". Default=None, that means the status '
                 'is unchanged.'))
@utils.arg('--reset-migration-status',
           action='store_true',
           help=('This is only used for a volume entity. Clears the migration '
                 'status of the volume in the DataBase that indicates the '
                 'volume is source or destination of volume migration, '
                 'with no regard to the actual status.'))
def do_reset_state(cs, args):
    """Explicitly updates the entity state in the Cinder database.

    Being a database change only, this has no impact on the true state of the
    entity and may not match the actual state. This can render a entity
    unusable in the case of changing to the 'available' state.
    """
    # pylint: disable=function-redefined
    failure_count = 0
    single = (len(args.entity) == 1)

    migration_status = 'none' if args.reset_migration_status else None
    collector = RESET_STATE_RESOURCES[args.type]
    argument = (args.state,)
    if args.type == 'volume':
        argument += (args.attach_status, migration_status)

    for entity in args.entity:
        try:
            collector(cs, entity).reset_state(*argument)
        except Exception as e:
            print(e)
            failure_count += 1
            msg = "Reset state for entity %s failed: %s" % (entity, e)
            if not single:
                print(msg)

    if failure_count == len(args.entity):
        msg = "Unable to reset the state for the specified entity(s)."
        raise exceptions.CommandError(msg)


@utils.arg('size',
           metavar='<size>',
           nargs='?',
           type=int,
           action=CheckSizeArgForCreate,
           help='Size of volume, in GiBs. (Required unless '
                'snapshot-id/source-volid/backup-id is specified).')
@utils.arg('--consisgroup-id',
           metavar='<consistencygroup-id>',
           default=None,
           help='ID of a consistency group where the new volume belongs to. '
                'Default=None.')
@utils.arg('--group-id',
           metavar='<group-id>',
           default=None,
           help='ID of a group where the new volume belongs to. '
                'Default=None.',
           start_version='3.13')
@utils.arg('--snapshot-id',
           metavar='<snapshot-id>',
           default=None,
           help='Creates volume from snapshot ID. Default=None.')
@utils.arg('--snapshot_id',
           help=argparse.SUPPRESS)
@utils.arg('--source-volid',
           metavar='<source-volid>',
           default=None,
           help='Creates volume from volume ID. Default=None.')
@utils.arg('--source_volid',
           help=argparse.SUPPRESS)
@utils.arg('--image-id',
           metavar='<image-id>',
           default=None,
           help='Creates volume from image ID. Default=None.')
@utils.arg('--image_id',
           help=argparse.SUPPRESS)
@utils.arg('--image',
           metavar='<image>',
           default=None,
           help='Creates a volume from image (ID or name). Default=None.')
@utils.arg('--backup-id',
           metavar='<backup-id>',
           default=None,
           start_version='3.47',
           help='Creates a volume from backup ID. Default=None.')
@utils.arg('--image_ref',
           help=argparse.SUPPRESS)
@utils.arg('--name',
           metavar='<name>',
           default=None,
           help='Volume name. Default=None.')
@utils.arg('--display-name',
           help=argparse.SUPPRESS)
@utils.arg('--display_name',
           help=argparse.SUPPRESS)
@utils.arg('--description',
           metavar='<description>',
           default=None,
           help='Volume description. Default=None.')
@utils.arg('--display-description',
           help=argparse.SUPPRESS)
@utils.arg('--display_description',
           help=argparse.SUPPRESS)
@utils.arg('--volume-type',
           metavar='<volume-type>',
           default=None,
           help='Volume type. Default=None.')
@utils.arg('--volume_type',
           help=argparse.SUPPRESS)
@utils.arg('--availability-zone',
           metavar='<availability-zone>',
           default=None,
           help='Availability zone for volume. Default=None.')
@utils.arg('--availability_zone',
           help=argparse.SUPPRESS)
@utils.arg('--metadata',
           nargs='*',
           metavar='<key=value>',
           default=None,
           help='Metadata key and value pairs. Default=None.')
@utils.arg('--hint',
           metavar='<key=value>',
           dest='scheduler_hints',
           action='append',
           default=[],
           help='Scheduler hint, similar to nova. Repeat option to set '
                'multiple hints. Values with the same key will be stored '
                'as a list.')
@utils.arg('--poll',
           action="store_true",
           help=('Wait for volume creation until it completes.'))
def do_create(cs, args):
    """Creates a volume."""

    # NOTE(thingee): Backwards-compatibility with v1 args
    if args.display_name is not None:
        args.name = args.display_name

    if args.display_description is not None:
        args.description = args.display_description

    volume_metadata = None
    if args.metadata is not None:
        volume_metadata = shell_utils.extract_metadata(args)

    # NOTE(N.S.): take this piece from novaclient
    hints = {}
    if args.scheduler_hints:
        for hint in args.scheduler_hints:
            key, _sep, value = hint.partition('=')
            # NOTE(vish): multiple copies of same hint will
            #             result in a list of values
            if key in hints:
                if isinstance(hints[key], six.string_types):
                    hints[key] = [hints[key]]
                hints[key] += [value]
            else:
                hints[key] = value
    # NOTE(N.S.): end of taken piece

    # Keep backward compatibility with image_id, favoring explicit ID
    image_ref = args.image_id or args.image or args.image_ref

    try:
        group_id = args.group_id
    except AttributeError:
        group_id = None

    backup_id = args.backup_id if hasattr(args, 'backup_id') else None

    volume = cs.volumes.create(args.size,
                               args.consisgroup_id,
                               group_id,
                               args.snapshot_id,
                               args.source_volid,
                               args.name,
                               args.description,
                               args.volume_type,
                               availability_zone=args.availability_zone,
                               imageRef=image_ref,
                               metadata=volume_metadata,
                               scheduler_hints=hints,
                               backup_id=backup_id)

    info = dict()
    volume = cs.volumes.get(volume.id)
    info.update(volume._info)

    if 'readonly' in info['metadata']:
        info['readonly'] = info['metadata']['readonly']

    info.pop('links', None)

    if args.poll:
        timeout_period = os.environ.get("POLL_TIMEOUT_PERIOD", 3600)
        shell_utils._poll_for_status(
            cs.volumes.get, volume.id, info, 'creating', ['available'],
            timeout_period, cs.client.global_request_id, cs.messages)
        volume = cs.volumes.get(volume.id)
        info.update(volume._info)

    utils.print_dict(info)

    with cs.volumes.completion_cache('uuid',
                                     cinderclient.v3.volumes.Volume,
                                     mode="a"):
        cs.volumes.write_to_completion_cache('uuid', volume.id)
    if volume.name is not None:
        with cs.volumes.completion_cache('name',
                                         cinderclient.v3.volumes.Volume,
                                         mode="a"):
            cs.volumes.write_to_completion_cache('name', volume.name)


@utils.arg('volume',
           metavar='<volume>',
           help='Name or ID of volume for which to update metadata.')
@utils.arg('action',
           metavar='<action>',
           choices=['set', 'unset'],
           help='The action. Valid values are "set" or "unset."')
@utils.arg('metadata',
           metavar='<key=value>',
           nargs='+',
           default=[],
           end_version='3.14',
           help='Metadata key and value pair to set or unset. '
                'For unset, specify only the key.')
@utils.arg('metadata',
           metavar='<key=value>',
           nargs='+',
           default=[],
           start_version='3.15',
           help='Metadata key and value pair to set or unset. '
                'For unset, specify only the key(s): <key key>')
def do_metadata(cs, args):
    """Sets or deletes volume metadata."""
    volume = utils.find_volume(cs, args.volume)
    metadata = shell_utils.extract_metadata(args)

    if args.action == 'set':
        cs.volumes.set_metadata(volume, metadata)
    elif args.action == 'unset':
        # NOTE(zul): Make sure py2/py3 sorting is the same
        cs.volumes.delete_metadata(volume, sorted(metadata.keys(),
                                   reverse=True))


@api_versions.wraps('3.12')
@utils.arg('--all-tenants',
           dest='all_tenants',
           metavar='<0|1>',
           nargs='?',
           type=int,
           const=1,
           default=utils.env('ALL_TENANTS', default=0),
           help='Shows details for all tenants. Admin only.')
def do_summary(cs, args):
    """Get volumes summary."""
    all_tenants = args.all_tenants
    info = cs.volumes.summary(all_tenants)

    formatters = ['total_size', 'total_count']
    if cs.api_version >= api_versions.APIVersion("3.36"):
        formatters.append('metadata')

    utils.print_dict(info['volume-summary'], formatters=formatters)


@api_versions.wraps('3.11')
@utils.arg('--filters',
           action=AppendFilters,
           type=six.text_type,
           nargs='*',
           start_version='3.52',
           metavar='<key=value>',
           default=None,
           help="Filter key and value pairs. Admin only.")
def do_group_type_list(cs, args):
    """Lists available 'group types'. (Admin only will see private types)"""
    search_opts = {}
    # Update search option with `filters`
    if AppendFilters.filters:
        search_opts.update(shell_utils.extract_filters(AppendFilters.filters))
    gtypes = cs.group_types.list(search_opts=search_opts)
    shell_utils.print_group_type_list(gtypes)
    AppendFilters.filters = []


@api_versions.wraps('3.11')
def do_group_type_default(cs, args):
    """List the default group type."""
    gtype = cs.group_types.default()
    shell_utils.print_group_type_list([gtype])


@api_versions.wraps('3.11')
@utils.arg('group_type',
           metavar='<group_type>',
           help='Name or ID of the group type.')
def do_group_type_show(cs, args):
    """Show group type details."""
    gtype = shell_utils.find_gtype(cs, args.group_type)
    info = dict()
    info.update(gtype._info)

    info.pop('links', None)
    utils.print_dict(info, formatters=['group_specs'])


@api_versions.wraps('3.11')
@utils.arg('id',
           metavar='<id>',
           help='ID of the group type.')
@utils.arg('--name',
           metavar='<name>',
           help='Name of the group type.')
@utils.arg('--description',
           metavar='<description>',
           help='Description of the group type.')
@utils.arg('--is-public',
           metavar='<is-public>',
           help='Make type accessible to the public or not.')
def do_group_type_update(cs, args):
    """Updates group type name, description, and/or is_public."""
    is_public = strutils.bool_from_string(args.is_public)
    gtype = cs.group_types.update(args.id, args.name, args.description,
                                  is_public)
    shell_utils.print_group_type_list([gtype])


@api_versions.wraps('3.11')
def do_group_specs_list(cs, args):
    """Lists current group types and specs."""
    gtypes = cs.group_types.list()
    utils.print_list(gtypes, ['ID', 'Name', 'group_specs'])


@api_versions.wraps('3.11')
@utils.arg('name',
           metavar='<name>',
           help='Name of new group type.')
@utils.arg('--description',
           metavar='<description>',
           help='Description of new group type.')
@utils.arg('--is-public',
           metavar='<is-public>',
           default=True,
           help='Make type accessible to the public (default true).')
def do_group_type_create(cs, args):
    """Creates a group type."""
    is_public = strutils.bool_from_string(args.is_public)
    gtype = cs.group_types.create(args.name, args.description, is_public)
    shell_utils.print_group_type_list([gtype])


@api_versions.wraps('3.11')
@utils.arg('group_type',
           metavar='<group_type>', nargs='+',
           help='Name or ID of group type or types to delete.')
def do_group_type_delete(cs, args):
    """Deletes group type or types."""
    failure_count = 0
    for group_type in args.group_type:
        try:
            gtype = shell_utils.find_group_type(cs, group_type)
            cs.group_types.delete(gtype)
            print("Request to delete group type %s has been accepted."
                  % group_type)
        except Exception as e:
            failure_count += 1
            print("Delete for group type %s failed: %s" % (group_type, e))
    if failure_count == len(args.group_type):
        raise exceptions.CommandError("Unable to delete any of the "
                                      "specified types.")


@api_versions.wraps('3.11')
@utils.arg('gtype',
           metavar='<gtype>',
           help='Name or ID of group type.')
@utils.arg('action',
           metavar='<action>',
           choices=['set', 'unset'],
           help='The action. Valid values are "set" or "unset."')
@utils.arg('metadata',
           metavar='<key=value>',
           nargs='+',
           default=[],
           help='The group specs key and value pair to set or unset. '
                'For unset, specify only the key.')
def do_group_type_key(cs, args):
    """Sets or unsets group_spec for a group type."""
    gtype = shell_utils.find_group_type(cs, args.gtype)
    keypair = shell_utils.extract_metadata(args)

    if args.action == 'set':
        gtype.set_keys(keypair)
    elif args.action == 'unset':
        gtype.unset_keys(list(keypair))


@utils.arg('tenant',
           metavar='<tenant_id>',
           help='ID of tenant for which to set quotas.')
@utils.arg('--volumes',
           metavar='<volumes>',
           type=int, default=None,
           help='The new "volumes" quota value. Default=None.')
@utils.arg('--snapshots',
           metavar='<snapshots>',
           type=int, default=None,
           help='The new "snapshots" quota value. Default=None.')
@utils.arg('--gigabytes',
           metavar='<gigabytes>',
           type=int, default=None,
           help='The new "gigabytes" quota value. Default=None.')
@utils.arg('--backups',
           metavar='<backups>',
           type=int, default=None,
           help='The new "backups" quota value. Default=None.')
@utils.arg('--backup-gigabytes',
           metavar='<backup_gigabytes>',
           type=int, default=None,
           help='The new "backup_gigabytes" quota value. Default=None.')
@utils.arg('--groups',
           metavar='<groups>',
           type=int, default=None,
           help='The new "groups" quota value. Default=None.',
           start_version='3.13')
@utils.arg('--volume-type',
           metavar='<volume_type_name>',
           default=None,
           help='Volume type. Default=None.')
@utils.arg('--per-volume-gigabytes',
           metavar='<per_volume_gigabytes>',
           type=int, default=None,
           help='Set max volume size limit. Default=None.')
@utils.arg('--skip-validation',
           metavar='<skip_validation>',
           default=False,
           help='Skip validate the existing resource quota. Default=False.')
def do_quota_update(cs, args):
    """Updates quotas for a tenant."""

    shell_utils.quota_update(cs.quotas, args.tenant, args)


@utils.arg('volume',
           metavar='<volume>',
           help='Name or ID of volume to snapshot.')
@utils.arg('--force',
           metavar='<True|False>',
           const=True,
           nargs='?',
           default=False,
           help='Enables or disables upload of '
           'a volume that is attached to an instance. '
           'Default=False. '
           'This option may not be supported by your cloud.')
@utils.arg('--container-format',
           metavar='<container-format>',
           default='bare',
           help='Container format type. '
                'Default is bare.')
@utils.arg('--container_format',
           help=argparse.SUPPRESS)
@utils.arg('--disk-format',
           metavar='<disk-format>',
           default='raw',
           help='Disk format type. '
                'Default is raw.')
@utils.arg('--disk_format',
           help=argparse.SUPPRESS)
@utils.arg('image_name',
           metavar='<image-name>',
           help='The new image name.')
@utils.arg('--image_name',
           help=argparse.SUPPRESS)
@utils.arg('--visibility',
           metavar='<public|private|community|shared>',
           help='Set image visibility to public, private, community or '
                'shared. Default=private.',
           default='private',
           start_version='3.1')
@utils.arg('--protected',
           metavar='<True|False>',
           help='Prevents image from being deleted. Default=False.',
           default=False,
           start_version='3.1')
def do_upload_to_image(cs, args):
    """Uploads volume to Image Service as an image."""
    volume = utils.find_volume(cs, args.volume)
    if cs.api_version >= api_versions.APIVersion("3.1"):
        (resp, body) = volume.upload_to_image(args.force,
                                              args.image_name,
                                              args.container_format,
                                              args.disk_format,
                                              args.visibility,
                                              args.protected)

        shell_utils.print_volume_image((resp, body))
    else:
        (resp, body) = volume.upload_to_image(args.force,
                                              args.image_name,
                                              args.container_format,
                                              args.disk_format)
        shell_utils.print_volume_image((resp, body))


@utils.arg('volume', metavar='<volume>', help='ID of volume to migrate.')
# NOTE(geguileo): host is positional but optional in order to maintain backward
# compatibility even with mutually exclusive arguments.  If version is < 3.16
# then only host positional argument will be possible, and since the
# exclusive_arg group has required=True it will be required even if it's
# optional.
@utils.exclusive_arg('destination', 'host', required=True, nargs='?',
                     metavar='<host>', help='Destination host. Takes the '
                     'form: host@backend-name#pool')
@utils.exclusive_arg('destination', '--cluster', required=True,
                     help='Destination cluster. Takes the form: '
                     'cluster@backend-name#pool',
                     start_version='3.16')
@utils.arg('--force-host-copy', metavar='<True|False>',
           choices=['True', 'False'],
           required=False,
           const=True,
           nargs='?',
           default=False,
           help='Enables or disables generic host-based '
           'force-migration, which bypasses driver '
           'optimizations. Default=False.')
@utils.arg('--lock-volume', metavar='<True|False>',
           choices=['True', 'False'],
           required=False,
           const=True,
           nargs='?',
           default=False,
           help='Enables or disables the termination of volume migration '
           'caused by other commands. This option applies to the '
           'available volume. True means it locks the volume '
           'state and does not allow the migration to be aborted. The '
           'volume status will be in maintenance during the '
           'migration. False means it allows the volume migration '
           'to be aborted. The volume status is still in the original '
           'status. Default=False.')
def do_migrate(cs, args):
    """Migrates volume to a new host."""
    volume = utils.find_volume(cs, args.volume)
    try:
        volume.migrate_volume(args.host, args.force_host_copy,
                              args.lock_volume, getattr(args, 'cluster', None))
        print("Request to migrate volume %s has been accepted." % (volume.id))
    except Exception as e:
        print("Migration for volume %s failed: %s." % (volume.id,
                                                       six.text_type(e)))


@api_versions.wraps('3.9')
@utils.arg('backup', metavar='<backup>',
           help='Name or ID of backup to rename.')
@utils.arg('--name', nargs='?', metavar='<name>',
           help='New name for backup.')
@utils.arg('--description', metavar='<description>',
           help='Backup description. Default=None.')
@utils.arg('--metadata',
           nargs='*',
           metavar='<key=value>',
           default=None,
           help='Metadata key and value pairs. Default=None.',
           start_version='3.43')
def do_backup_update(cs, args):
    """Updates a backup."""
    kwargs = {}

    if args.name is not None:
        kwargs['name'] = args.name

    if args.description is not None:
        kwargs['description'] = args.description

    if cs.api_version >= api_versions.APIVersion("3.43"):
        if args.metadata is not None:
            kwargs['metadata'] = shell_utils.extract_metadata(args)

    if not kwargs:
        msg = 'Must supply at least one: name, description or metadata.'
        raise exceptions.ClientException(code=1, message=msg)

    shell_utils.find_backup(cs, args.backup).update(**kwargs)
    print("Request to update backup '%s' has been accepted." % args.backup)


@api_versions.wraps('3.7')
@utils.arg('--name', metavar='<name>', default=None,
           help='Filter by cluster name, without backend will list all '
                'clustered services from the same cluster. Default=None.')
@utils.arg('--binary', metavar='<binary>', default=None,
           help='Cluster binary. Default=None.')
@utils.arg('--is-up', metavar='<True|true|False|false>', default=None,
           choices=('True', 'true', 'False', 'false'),
           help='Filter by up/dow status. Default=None.')
@utils.arg('--disabled', metavar='<True|true|False|false>', default=None,
           choices=('True', 'true', 'False', 'false'),
           help='Filter by disabled status. Default=None.')
@utils.arg('--num-hosts', metavar='<num-hosts>', default=None,
           help='Filter by number of hosts in the cluster.')
@utils.arg('--num-down-hosts', metavar='<num-down-hosts>', default=None,
           help='Filter by number of hosts that are down.')
@utils.arg('--detailed', dest='detailed', default=False,
           help='Get detailed clustered service information (Default=False).',
           action='store_true')
def do_cluster_list(cs, args):
    """Lists clustered services with optional filtering."""
    clusters = cs.clusters.list(name=args.name, binary=args.binary,
                                is_up=args.is_up, disabled=args.disabled,
                                num_hosts=args.num_hosts,
                                num_down_hosts=args.num_down_hosts,
                                detailed=args.detailed)

    columns = ['Name', 'Binary', 'State', 'Status']
    if args.detailed:
        columns.extend(('Num Hosts', 'Num Down Hosts', 'Last Heartbeat',
                        'Disabled Reason', 'Created At', 'Updated at'))
    utils.print_list(clusters, columns)


@api_versions.wraps('3.7')
@utils.arg('binary', metavar='<binary>', nargs='?', default='cinder-volume',
           help='Binary to filter by.  Default: cinder-volume.')
@utils.arg('name', metavar='<cluster-name>',
           help='Name of the clustered service to show.')
def do_cluster_show(cs, args):
    """Show detailed information on a clustered service."""
    cluster = cs.clusters.show(args.name, args.binary)
    utils.print_dict(cluster.to_dict())


@api_versions.wraps('3.7')
@utils.arg('binary', metavar='<binary>', nargs='?', default='cinder-volume',
           help='Binary to filter by.  Default: cinder-volume.')
@utils.arg('name', metavar='<cluster-name>',
           help='Name of the clustered services to update.')
def do_cluster_enable(cs, args):
    """Enables clustered services."""
    cluster = cs.clusters.update(args.name, args.binary, disabled=False)
    utils.print_dict(cluster.to_dict())


@api_versions.wraps('3.7')
@utils.arg('binary', metavar='<binary>', nargs='?', default='cinder-volume',
           help='Binary to filter by.  Default: cinder-volume.')
@utils.arg('name', metavar='<cluster-name>',
           help='Name of the clustered services to update.')
@utils.arg('--reason', metavar='<reason>', default=None,
           help='Reason for disabling clustered service.')
def do_cluster_disable(cs, args):
    """Disables clustered services."""
    cluster = cs.clusters.update(args.name, args.binary, disabled=True,
                                 disabled_reason=args.reason)
    utils.print_dict(cluster.to_dict())


@api_versions.wraps('3.24')
@utils.arg('--cluster', metavar='<cluster-name>', default=None,
           help='Cluster name. Default=None.')
@utils.arg('--host', metavar='<hostname>', default=None,
           help='Service host name. Default=None.')
@utils.arg('--binary', metavar='<binary>', default=None,
           help='Service binary. Default=None.')
@utils.arg('--is-up', metavar='<True|true|False|false>', dest='is_up',
           default=None, choices=('True', 'true', 'False', 'false'),
           help='Filter by up/down status, if set to true services need to be'
                ' up, if set to false services need to be down.  Default is '
                'None, which means up/down status is ignored.')
@utils.arg('--disabled', metavar='<True|true|False|false>', default=None,
           choices=('True', 'true', 'False', 'false'),
           help='Filter by disabled status. Default=None.')
@utils.arg('--resource-id', metavar='<resource-id>', default=None,
           help='UUID of a resource to cleanup. Default=None.')
@utils.arg('--resource-type', metavar='<Volume|Snapshot>', default=None,
           choices=('Volume', 'Snapshot'),
           help='Type of resource to cleanup.')
@utils.arg('--service-id',
           metavar='<service-id>',
           type=int,
           default=None,
           help='The service id field from the DB, not the uuid of the'
                ' service. Default=None.')
def do_work_cleanup(cs, args):
    """Request cleanup of services with optional filtering."""
    filters = dict(cluster_name=args.cluster, host=args.host,
                   binary=args.binary, is_up=args.is_up,
                   disabled=args.disabled, resource_id=args.resource_id,
                   resource_type=args.resource_type,
                   service_id=args.service_id)

    filters = {k: v for k, v in filters.items() if v is not None}

    cleaning, unavailable = cs.workers.clean(**filters)

    columns = ('ID', 'Cluster Name', 'Host', 'Binary')

    if cleaning:
        print('Following services will be cleaned:')
        utils.print_list(cleaning, columns)

    if unavailable:
        print('There are no alternative nodes to do cleanup for the following '
              'services:')
        utils.print_list(unavailable, columns)

    if not (cleaning or unavailable):
        print('No cleanable services matched cleanup criteria.')


@utils.arg('host',
           metavar='<host>',
           help='Cinder host on which the existing volume resides; '
                'takes the form: host@backend-name#pool')
@utils.arg('--cluster',
           help='Cinder cluster on which the existing volume resides; '
                'takes the form: cluster@backend-name#pool',
           start_version='3.16')
@utils.arg('identifier',
           metavar='<identifier>',
           help='Name or other Identifier for existing volume')
@utils.arg('--id-type',
           metavar='<id-type>',
           default='source-name',
           help='Type of backend device identifier provided, '
                'typically source-name or source-id (Default=source-name)')
@utils.arg('--name',
           metavar='<name>',
           help='Volume name (Default=None)')
@utils.arg('--description',
           metavar='<description>',
           help='Volume description (Default=None)')
@utils.arg('--volume-type',
           metavar='<volume-type>',
           help='Volume type (Default=None)')
@utils.arg('--availability-zone',
           metavar='<availability-zone>',
           help='Availability zone for volume (Default=None)')
@utils.arg('--metadata',
           type=str,
           nargs='*',
           metavar='<key=value>',
           help='Metadata key=value pairs (Default=None)')
@utils.arg('--bootable',
           action='store_true',
           help='Specifies that the newly created volume should be'
                ' marked as bootable')
def do_manage(cs, args):
    """Manage an existing volume."""
    volume_metadata = None
    if args.metadata is not None:
        volume_metadata = shell_utils.extract_metadata(args)

    # Build a dictionary of key/value pairs to pass to the API.
    ref_dict = {args.id_type: args.identifier}

    # The recommended way to specify an existing volume is by ID or name, and
    # have the Cinder driver look for 'source-name' or 'source-id' elements in
    # the ref structure.  To make things easier for the user, we have special
    # --source-name and --source-id CLI options that add the appropriate
    # element to the ref structure.
    #
    # Note how argparse converts hyphens to underscores.  We use hyphens in the
    # dictionary so that it is consistent with what the user specified on the
    # CLI.

    if hasattr(args, 'source_name') and args.source_name is not None:
        ref_dict['source-name'] = args.source_name
    if hasattr(args, 'source_id') and args.source_id is not None:
        ref_dict['source-id'] = args.source_id

    volume = cs.volumes.manage(host=args.host,
                               ref=ref_dict,
                               name=args.name,
                               description=args.description,
                               volume_type=args.volume_type,
                               availability_zone=args.availability_zone,
                               metadata=volume_metadata,
                               bootable=args.bootable,
                               cluster=getattr(args, 'cluster', None))

    info = {}
    volume = cs.volumes.get(volume.id)
    info.update(volume._info)
    info.pop('links', None)
    utils.print_dict(info)


@api_versions.wraps('3.8')
# NOTE(geguileo): host is positional but optional in order to maintain backward
# compatibility even with mutually exclusive arguments.  If version is < 3.16
# then only host positional argument will be possible, and since the
# exclusive_arg group has required=True it will be required even if it's
# optional.
@utils.exclusive_arg('source', 'host', required=True, nargs='?',
                     metavar='<host>',
                     help='Cinder host on which to list manageable volumes; '
                          'takes the form: host@backend-name#pool')
@utils.exclusive_arg('source', '--cluster', required=True,
                     metavar='CLUSTER',
                     help='Cinder cluster on which to list manageable '
                     'volumes; takes the form: cluster@backend-name#pool',
                     start_version='3.17')
@utils.arg('--detailed',
           metavar='<detailed>',
           default=True,
           help='Returned detailed information (default true).')
@utils.arg('--marker',
           metavar='<marker>',
           default=None,
           help='Begin returning volumes that appear later in the volume '
                'list than that represented by this reference. This reference '
                'should be json like. '
                'Default=None.')
@utils.arg('--limit',
           metavar='<limit>',
           default=None,
           help='Maximum number of volumes to return. Default=None.')
@utils.arg('--offset',
           metavar='<offset>',
           default=None,
           help='Number of volumes to skip after marker. Default=None.')
@utils.arg('--sort',
           metavar='<key>[:<direction>]',
           default=None,
           help=(('Comma-separated list of sort keys and directions in the '
                  'form of <key>[:<asc|desc>]. '
                  'Valid keys: %s. '
                  'Default=None.'
                  ) % ', '.join(base.SORT_MANAGEABLE_KEY_VALUES)))
def do_manageable_list(cs, args):
    """Lists all manageable volumes."""
    # pylint: disable=function-redefined
    detailed = strutils.bool_from_string(args.detailed)
    cluster = getattr(args, 'cluster', None)
    volumes = cs.volumes.list_manageable(host=args.host, detailed=detailed,
                                         marker=args.marker, limit=args.limit,
                                         offset=args.offset, sort=args.sort,
                                         cluster=cluster)
    columns = ['reference', 'size', 'safe_to_manage']
    if detailed:
        columns.extend(['reason_not_safe', 'cinder_id', 'extra_info'])
    utils.print_list(volumes, columns, sortby_index=None)


@api_versions.wraps('3.13')
@utils.arg('--all-tenants',
           dest='all_tenants',
           metavar='<0|1>',
           nargs='?',
           type=int,
           const=1,
           default=utils.env('ALL_TENANTS', default=None),
           help='Shows details for all tenants. Admin only.')
@utils.arg('--filters',
           action=AppendFilters,
           type=six.text_type,
           nargs='*',
           start_version='3.33',
           metavar='<key=value>',
           default=None,
           help="Filter key and value pairs. Please use 'cinder list-filters' "
                "to check enabled filters from server. Use 'key~=value' "
                "for inexact filtering if the key supports. Default=None.")
def do_group_list(cs, args):
    """Lists all groups."""
    search_opts = {'all_tenants': args.all_tenants}

    # Update search option with `filters`
    if AppendFilters.filters:
        search_opts.update(shell_utils.extract_filters(AppendFilters.filters))

    groups = cs.groups.list(search_opts=search_opts)

    columns = ['ID', 'Status', 'Name']
    utils.print_list(groups, columns)

    with cs.groups.completion_cache(
            'uuid',
            cinderclient.v3.groups.Group,
            mode='w'):
        for group in groups:
            cs.groups.write_to_completion_cache('uuid', group.id)
    with cs.groups.completion_cache('name',
                                    cinderclient.v3.groups.Group,
                                    mode='w'):
        for group in groups:
            if group.name is None:
                continue
            cs.groups.write_to_completion_cache('name', group.name)
    AppendFilters.filters = []


@api_versions.wraps('3.13')
@utils.arg('--list-volume',
           dest='list_volume',
           metavar='<False|True>',
           nargs='?',
           type=bool,
           const=True,
           default=False,
           help='Shows volumes included in the group.',
           start_version='3.25')
@utils.arg('group',
           metavar='<group>',
           help='Name or ID of a group.')
def do_group_show(cs, args):
    """Shows details of a group."""
    info = dict()
    if getattr(args, 'list_volume', None):
        group = shell_utils.find_group(cs, args.group,
                                       list_volume=args.list_volume)
    else:
        group = shell_utils.find_group(cs, args.group)
    info.update(group._info)

    info.pop('links', None)
    utils.print_dict(info)


@api_versions.wraps('3.13')
@utils.arg('grouptype',
           metavar='<group-type>',
           help='Group type.')
@utils.arg('volumetypes',
           metavar='<volume-types>',
           help='Comma-separated list of volume types.')
@utils.arg('--name',
           metavar='<name>',
           help='Name of a group.')
@utils.arg('--description',
           metavar='<description>',
           default=None,
           help='Description of a group. Default=None.')
@utils.arg('--availability-zone',
           metavar='<availability-zone>',
           default=None,
           help='Availability zone for group. Default=None.')
def do_group_create(cs, args):
    """Creates a group."""

    group = cs.groups.create(
        args.grouptype,
        args.volumetypes,
        args.name,
        args.description,
        availability_zone=args.availability_zone)

    info = dict()
    group = cs.groups.get(group.id)
    info.update(group._info)

    info.pop('links', None)
    utils.print_dict(info)

    with cs.groups.completion_cache('uuid',
                                    cinderclient.v3.groups.Group,
                                    mode='a'):
        cs.groups.write_to_completion_cache('uuid', group.id)

    if group.name is not None:
        with cs.groups.completion_cache('name',
                                        cinderclient.v3.groups.Group,
                                        mode='a'):
            cs.groups.write_to_completion_cache('name', group.name)


@api_versions.wraps('3.14')
@utils.arg('--group-snapshot',
           metavar='<group-snapshot>',
           help='Name or ID of a group snapshot. Default=None.')
@utils.arg('--source-group',
           metavar='<source-group>',
           help='Name or ID of a source group. Default=None.')
@utils.arg('--name',
           metavar='<name>',
           help='Name of a group. Default=None.')
@utils.arg('--description',
           metavar='<description>',
           help='Description of a group. Default=None.')
def do_group_create_from_src(cs, args):
    """Creates a group from a group snapshot or a source group."""
    if not args.group_snapshot and not args.source_group:
        msg = ('Cannot create group because neither '
               'group snapshot nor source group is provided.')
        raise exceptions.ClientException(code=1, message=msg)
    if args.group_snapshot and args.source_group:
        msg = ('Cannot create group because both '
               'group snapshot and source group are provided.')
        raise exceptions.ClientException(code=1, message=msg)
    group_snapshot = None
    if args.group_snapshot:
        group_snapshot = shell_utils.find_group_snapshot(cs,
                                                         args.group_snapshot)
    source_group = None
    if args.source_group:
        source_group = shell_utils.find_group(cs, args.source_group)
    info = cs.groups.create_from_src(
        group_snapshot.id if group_snapshot else None,
        source_group.id if source_group else None,
        args.name,
        args.description)

    info.pop('links', None)
    utils.print_dict(info)


@api_versions.wraps('3.13')
@utils.arg('group',
           metavar='<group>', nargs='+',
           help='Name or ID of one or more groups '
                'to be deleted.')
@utils.arg('--delete-volumes',
           action='store_true',
           default=False,
           help='Allows or disallows groups to be deleted '
                'if they are not empty. If the group is empty, '
                'it can be deleted without the delete-volumes flag. '
                'If the group is not empty, the delete-volumes '
                'flag is required for it to be deleted. If True, '
                'all volumes in the group will also be deleted.')
def do_group_delete(cs, args):
    """Removes one or more groups."""
    failure_count = 0
    for group in args.group:
        try:
            shell_utils.find_group(cs, group).delete(args.delete_volumes)
        except Exception as e:
            failure_count += 1
            print("Delete for group %s failed: %s" %
                  (group, e))
    if failure_count == len(args.group):
        raise exceptions.CommandError("Unable to delete any of the specified "
                                      "groups.")


@api_versions.wraps('3.13')
@utils.arg('group',
           metavar='<group>',
           help='Name or ID of a group.')
@utils.arg('--name', metavar='<name>',
           help='New name for group. Default=None.')
@utils.arg('--description', metavar='<description>',
           help='New description for group. Default=None.')
@utils.arg('--add-volumes',
           metavar='<uuid1,uuid2,......>',
           help='UUID of one or more volumes '
                'to be added to the group, '
                'separated by commas. Default=None.')
@utils.arg('--remove-volumes',
           metavar='<uuid3,uuid4,......>',
           help='UUID of one or more volumes '
                'to be removed from the group, '
                'separated by commas. Default=None.')
def do_group_update(cs, args):
    """Updates a group."""
    kwargs = {}

    if args.name is not None:
        kwargs['name'] = args.name

    if args.description is not None:
        kwargs['description'] = args.description

    if args.add_volumes is not None:
        kwargs['add_volumes'] = args.add_volumes

    if args.remove_volumes is not None:
        kwargs['remove_volumes'] = args.remove_volumes

    if not kwargs:
        msg = ('At least one of the following args must be supplied: '
               'name, description, add-volumes, remove-volumes.')
        raise exceptions.ClientException(code=1, message=msg)

    shell_utils.find_group(cs, args.group).update(**kwargs)
    print("Request to update group '%s' has been accepted." % args.group)


@api_versions.wraps('3.38')
@utils.arg('group',
           metavar='<group>',
           help='Name or ID of the group.')
def do_group_enable_replication(cs, args):
    """Enables replication for group."""

    shell_utils.find_group(cs, args.group).enable_replication()


@api_versions.wraps('3.38')
@utils.arg('group',
           metavar='<group>',
           help='Name or ID of the group.')
def do_group_disable_replication(cs, args):
    """Disables replication for group."""

    shell_utils.find_group(cs, args.group).disable_replication()


@api_versions.wraps('3.38')
@utils.arg('group',
           metavar='<group>',
           help='Name or ID of the group.')
@utils.arg('--allow-attached-volume',
           action='store_true',
           default=False,
           help='Allows or disallows group with '
                'attached volumes to be failed over.')
@utils.arg('--secondary-backend-id',
           metavar='<secondary_backend_id>',
           help='Secondary backend id. Default=None.')
def do_group_failover_replication(cs, args):
    """Fails over replication for group."""

    shell_utils.find_group(cs, args.group).failover_replication(
        allow_attached_volume=args.allow_attached_volume,
        secondary_backend_id=args.secondary_backend_id)


@api_versions.wraps('3.38')
@utils.arg('group',
           metavar='<group>',
           help='Name or ID of the group.')
def do_group_list_replication_targets(cs, args):
    """Lists replication targets for group.

    Example value for replication_targets:

    .. code-block: json

        {
            'replication_targets': [{'backend_id': 'vendor-id-1',
                                     'unique_key': 'val1',
                                     ......},
                                    {'backend_id': 'vendor-id-2',
                                     'unique_key': 'val2',
                                     ......}]
        }
    """

    rc, replication_targets = shell_utils.find_group(
        cs, args.group).list_replication_targets()
    rep_targets = replication_targets.get('replication_targets')
    if rep_targets and len(rep_targets) > 0:
        utils.print_list(rep_targets, [key for key in rep_targets[0].keys()])


@api_versions.wraps('3.14')
@utils.arg('--all-tenants',
           dest='all_tenants',
           metavar='<0|1>',
           nargs='?',
           type=int,
           const=1,
           default=utils.env('ALL_TENANTS', default=None),
           help='Shows details for all tenants. Admin only.')
@utils.arg('--status',
           metavar='<status>',
           default=None,
           help="Filters results by a status. Default=None. "
                "%s" % FILTER_DEPRECATED)
@utils.arg('--group-id',
           metavar='<group_id>',
           default=None,
           help="Filters results by a group ID. Default=None. "
                "%s" % FILTER_DEPRECATED)
@utils.arg('--filters',
           action=AppendFilters,
           type=six.text_type,
           nargs='*',
           start_version='3.33',
           metavar='<key=value>',
           default=None,
           help="Filter key and value pairs. Please use 'cinder list-filters' "
                "to check enabled filters from server. Use 'key~=value' "
                "for inexact filtering if the key supports. Default=None.")
def do_group_snapshot_list(cs, args):
    """Lists all group snapshots."""

    search_opts = {
        'all_tenants': args.all_tenants,
        'status': args.status,
        'group_id': args.group_id,
    }
    # Update search option with `filters`
    if AppendFilters.filters:
        search_opts.update(shell_utils.extract_filters(AppendFilters.filters))

    group_snapshots = cs.group_snapshots.list(search_opts=search_opts)

    columns = ['ID', 'Status', 'Name']
    utils.print_list(group_snapshots, columns)
    AppendFilters.filters = []


@api_versions.wraps('3.14')
@utils.arg('group_snapshot',
           metavar='<group_snapshot>',
           help='Name or ID of group snapshot.')
def do_group_snapshot_show(cs, args):
    """Shows group snapshot details."""
    info = dict()
    group_snapshot = shell_utils.find_group_snapshot(cs, args.group_snapshot)
    info.update(group_snapshot._info)

    info.pop('links', None)
    utils.print_dict(info)


@api_versions.wraps('3.14')
@utils.arg('group',
           metavar='<group>',
           help='Name or ID of a group.')
@utils.arg('--name',
           metavar='<name>',
           default=None,
           help='Group snapshot name. Default=None.')
@utils.arg('--description',
           metavar='<description>',
           default=None,
           help='Group snapshot description. Default=None.')
def do_group_snapshot_create(cs, args):
    """Creates a group snapshot."""
    group = shell_utils.find_group(cs, args.group)
    group_snapshot = cs.group_snapshots.create(
        group.id,
        args.name,
        args.description)

    info = dict()
    group_snapshot = cs.group_snapshots.get(group_snapshot.id)
    info.update(group_snapshot._info)

    info.pop('links', None)
    utils.print_dict(info)


@api_versions.wraps('3.14')
@utils.arg('group_snapshot',
           metavar='<group_snapshot>', nargs='+',
           help='Name or ID of one or more group snapshots to be deleted.')
def do_group_snapshot_delete(cs, args):
    """Removes one or more group snapshots."""
    failure_count = 0
    for group_snapshot in args.group_snapshot:
        try:
            shell_utils.find_group_snapshot(cs, group_snapshot).delete()
        except Exception as e:
            failure_count += 1
            print("Delete for group snapshot %s failed: %s" %
                  (group_snapshot, e))
    if failure_count == len(args.group_snapshot):
        raise exceptions.CommandError("Unable to delete any of the specified "
                                      "group snapshots.")


@api_versions.wraps('3.0')
@utils.arg('--host', metavar='<hostname>', default=None,
           help='Host name. Default=None.')
@utils.arg('--binary', metavar='<binary>', default=None,
           help='Service binary. Default=None.')
@utils.arg('--withreplication',
           metavar='<True|False>',
           const=True,
           nargs='?',
           default=False,
           start_version='3.7',
           help='Enables or disables display of '
                'Replication info for c-vol services. Default=False.')
def do_service_list(cs, args):
    """Lists all services. Filter by host and service binary."""
    if hasattr(args, 'withreplication'):
        replication = strutils.bool_from_string(args.withreplication,
                                                strict=True)
    else:
        replication = False

    result = cs.services.list(host=args.host, binary=args.binary)
    columns = ["Binary", "Host", "Zone", "Status", "State", "Updated_at"]
    if cs.api_version.matches('3.7'):
        columns.append('Cluster')
    if replication:
        columns.extend(["Replication Status", "Active Backend ID", "Frozen"])
    # NOTE(jay-lau-513): we check if the response has disabled_reason
    # so as not to add the column when the extended ext is not enabled.
    if result and hasattr(result[0], 'disabled_reason'):
        columns.append("Disabled Reason")
    if cs.api_version.matches('3.49'):
        columns.extend(["Backend State"])
    utils.print_list(result, columns)


@api_versions.wraps('3.8')
# NOTE(geguileo): host is positional but optional in order to maintain backward
# compatibility even with mutually exclusive arguments.  If version is < 3.16
# then only host positional argument will be possible, and since the
# exclusive_arg group has required=True it will be required even if it's
# optional.
@utils.exclusive_arg('source', 'host', required=True, nargs='?',
                     metavar='<host>',
                     help='Cinder host on which to list manageable snapshots; '
                     'takes the form: host@backend-name#pool')
@utils.exclusive_arg('source', '--cluster', required=True,
                     help='Cinder cluster on which to list manageable '
                     'snapshots; takes the form: cluster@backend-name#pool',
                     start_version='3.17')
@utils.arg('--detailed',
           metavar='<detailed>',
           default=True,
           help='Returned detailed information (default true).')
@utils.arg('--marker',
           metavar='<marker>',
           default=None,
           help='Begin returning volumes that appear later in the volume '
                'list than that represented by this reference. This reference '
                'should be json like. '
                'Default=None.')
@utils.arg('--limit',
           metavar='<limit>',
           default=None,
           help='Maximum number of volumes to return. Default=None.')
@utils.arg('--offset',
           metavar='<offset>',
           default=None,
           help='Number of volumes to skip after marker. Default=None.')
@utils.arg('--sort',
           metavar='<key>[:<direction>]',
           default=None,
           help=(('Comma-separated list of sort keys and directions in the '
                  'form of <key>[:<asc|desc>]. '
                  'Valid keys: %s. '
                  'Default=None.'
                  ) % ', '.join(base.SORT_MANAGEABLE_KEY_VALUES)))
def do_snapshot_manageable_list(cs, args):
    """Lists all manageable snapshots."""
    # pylint: disable=function-redefined
    detailed = strutils.bool_from_string(args.detailed)
    cluster = getattr(args, 'cluster', None)
    snapshots = cs.volume_snapshots.list_manageable(host=args.host,
                                                    detailed=detailed,
                                                    marker=args.marker,
                                                    limit=args.limit,
                                                    offset=args.offset,
                                                    sort=args.sort,
                                                    cluster=cluster)
    columns = ['reference', 'size', 'safe_to_manage', 'source_reference']
    if detailed:
        columns.extend(['reason_not_safe', 'cinder_id', 'extra_info'])
    utils.print_list(snapshots, columns, sortby_index=None)


@api_versions.wraps("3.0")
def do_api_version(cs, args):
    """Display the server API version information."""
    columns = ['ID', 'Status', 'Version', 'Min_version']
    response = cs.services.server_api_version()
    utils.print_list(response, columns)


@api_versions.wraps("3.40")
@utils.arg(
    'snapshot',
    metavar='<snapshot>',
    help='Name or ID of the snapshot to restore. The snapshot must be the '
         'most recent one known to cinder.')
def do_revert_to_snapshot(cs, args):
    """Revert a volume to the specified snapshot."""
    snapshot = shell_utils.find_volume_snapshot(cs, args.snapshot)
    volume = utils.find_volume(cs, snapshot.volume_id)
    volume.revert_to_snapshot(snapshot)


@api_versions.wraps("3.3")
@utils.arg('--marker',
           metavar='<marker>',
           default=None,
           start_version='3.5',
           help='Begin returning message that appear later in the message '
                'list than that represented by this id. '
                'Default=None.')
@utils.arg('--limit',
           metavar='<limit>',
           default=None,
           start_version='3.5',
           help='Maximum number of messages to return. Default=None.')
@utils.arg('--sort',
           metavar='<key>[:<direction>]',
           default=None,
           start_version='3.5',
           help=(('Comma-separated list of sort keys and directions in the '
                  'form of <key>[:<asc|desc>]. '
                  'Valid keys: %s. '
                  'Default=None.') % ', '.join(base.SORT_KEY_VALUES)))
@utils.arg('--resource_uuid',
           metavar='<resource_uuid>',
           default=None,
           help="Filters results by a resource uuid. Default=None. "
                "%s" % FILTER_DEPRECATED)
@utils.arg('--resource_type',
           metavar='<type>',
           default=None,
           help="Filters results by a resource type. Default=None. "
                "%s" % FILTER_DEPRECATED)
@utils.arg('--event_id',
           metavar='<id>',
           default=None,
           help="Filters results by event id. Default=None. "
                "%s" % FILTER_DEPRECATED)
@utils.arg('--request_id',
           metavar='<request_id>',
           default=None,
           help="Filters results by request id. Default=None. "
                "%s" % FILTER_DEPRECATED)
@utils.arg('--level',
           metavar='<level>',
           default=None,
           help="Filters results by the message level. Default=None. "
                "%s" % FILTER_DEPRECATED)
@utils.arg('--filters',
           action=AppendFilters,
           type=six.text_type,
           nargs='*',
           start_version='3.33',
           metavar='<key=value>',
           default=None,
           help="Filter key and value pairs. Please use 'cinder list-filters' "
                "to check enabled filters from server. Use 'key~=value' "
                "for inexact filtering if the key supports. Default=None.")
def do_message_list(cs, args):
    """Lists all messages."""
    search_opts = {
        'resource_uuid': args.resource_uuid,
        'event_id': args.event_id,
        'request_id': args.request_id,
    }
    # Update search option with `filters`
    if AppendFilters.filters:
        search_opts.update(shell_utils.extract_filters(AppendFilters.filters))
    if args.resource_type:
        search_opts['resource_type'] = args.resource_type.upper()
    if args.level:
        search_opts['message_level'] = args.level.upper()

    marker = args.marker if hasattr(args, 'marker') else None
    limit = args.limit if hasattr(args, 'limit') else None
    sort = args.sort if hasattr(args, 'sort') else None

    messages = cs.messages.list(search_opts=search_opts,
                                marker=marker,
                                limit=limit,
                                sort=sort)

    columns = ['ID', 'Resource Type', 'Resource UUID', 'Event ID',
               'User Message']
    if sort:
        sortby_index = None
    else:
        sortby_index = 0
    utils.print_list(messages, columns, sortby_index=sortby_index)
    AppendFilters.filters = []


@api_versions.wraps("3.3")
@utils.arg('message',
           metavar='<message>',
           help='ID of message.')
def do_message_show(cs, args):
    """Shows message details."""
    info = dict()
    message = shell_utils.find_message(cs, args.message)
    info.update(message._info)
    info.pop('links', None)
    utils.print_dict(info)


@api_versions.wraps("3.3")
@utils.arg('message',
           metavar='<message>', nargs='+',
           help='ID of one or more message to be deleted.')
def do_message_delete(cs, args):
    """Removes one or more messages."""
    failure_count = 0
    for message in args.message:
        try:
            shell_utils.find_message(cs, message).delete()
        except Exception as e:
            failure_count += 1
            print("Delete for message %s failed: %s" % (message, e))
    if failure_count == len(args.message):
        raise exceptions.CommandError("Unable to delete any of the specified "
                                      "messages.")


@utils.arg('--all-tenants',
           dest='all_tenants',
           metavar='<0|1>',
           nargs='?',
           type=int,
           const=1,
           default=0,
           help='Shows details for all tenants. Admin only.')
@utils.arg('--all_tenants',
           nargs='?',
           type=int,
           const=1,
           help=argparse.SUPPRESS)
@utils.arg('--name',
           metavar='<name>',
           default=None,
           help="Filters results by a name. Default=None. "
                "%s" % FILTER_DEPRECATED)
@utils.arg('--display-name',
           help=argparse.SUPPRESS)
@utils.arg('--display_name',
           help=argparse.SUPPRESS)
@utils.arg('--status',
           metavar='<status>',
           default=None,
           help="Filters results by a status. Default=None. "
                "%s" % FILTER_DEPRECATED)
@utils.arg('--volume-id',
           metavar='<volume-id>',
           default=None,
           help="Filters results by a volume ID. Default=None. "
                "%s" % FILTER_DEPRECATED)
@utils.arg('--volume_id',
           help=argparse.SUPPRESS)
@utils.arg('--marker',
           metavar='<marker>',
           default=None,
           help='Begin returning snapshots that appear later in the snapshot '
                'list than that represented by this id. '
                'Default=None.')
@utils.arg('--limit',
           metavar='<limit>',
           default=None,
           help='Maximum number of snapshots to return. Default=None.')
@utils.arg('--sort',
           metavar='<key>[:<direction>]',
           default=None,
           help=(('Comma-separated list of sort keys and directions in the '
                  'form of <key>[:<asc|desc>]. '
                  'Valid keys: %s. '
                  'Default=None.') % ', '.join(base.SORT_KEY_VALUES)))
@utils.arg('--tenant',
           type=str,
           dest='tenant',
           nargs='?',
           metavar='<tenant>',
           help='Display information from single tenant (Admin only).')
@utils.arg('--metadata',
           nargs='*',
           metavar='<key=value>',
           default=None,
           start_version='3.22',
           help="Filters results by a metadata key and value pair. Require "
                "volume api version >=3.22. Default=None. "
                "%s" % FILTER_DEPRECATED)
@utils.arg('--filters',
           action=AppendFilters,
           type=six.text_type,
           nargs='*',
           start_version='3.33',
           metavar='<key=value>',
           default=None,
           help="Filter key and value pairs. Please use 'cinder list-filters' "
                "to check enabled filters from server. Use 'key~=value' "
                "for inexact filtering if the key supports. Default=None.")
@utils.arg('--with-count',
           type=bool,
           default=False,
           const=True,
           nargs='?',
           start_version='3.45',
           metavar='<True|False>',
           help="Show total number of snapshot entities. This is useful when "
                "pagination is applied in the request.")
def do_snapshot_list(cs, args):
    """Lists all snapshots."""
    # pylint: disable=function-redefined
    show_count = True if hasattr(
        args, 'with_count') and args.with_count else False
    all_tenants = (1 if args.tenant else
                   int(os.environ.get("ALL_TENANTS", args.all_tenants)))

    if args.display_name is not None:
        args.name = args.display_name

    metadata = None
    try:
        if args.metadata:
            metadata = shell_utils.extract_metadata(args)
    except AttributeError:
        pass

    search_opts = {
        'all_tenants': all_tenants,
        'name': args.name,
        'status': args.status,
        'volume_id': args.volume_id,
        'project_id': args.tenant,
        'metadata': metadata
    }

    # Update search option with `filters`
    if AppendFilters.filters:
        search_opts.update(shell_utils.extract_filters(AppendFilters.filters))

    total_count = 0
    if show_count:
        search_opts['with_count'] = args.with_count
        snapshots, total_count = cs.volume_snapshots.list(
            search_opts=search_opts,
            marker=args.marker,
            limit=args.limit,
            sort=args.sort)
    else:
        snapshots = cs.volume_snapshots.list(search_opts=search_opts,
                                             marker=args.marker,
                                             limit=args.limit,
                                             sort=args.sort)

    shell_utils.translate_volume_snapshot_keys(snapshots)
    sortby_index = None if args.sort else 0
    if cs.api_version >= api_versions.APIVersion("3.41"):
        utils.print_list(snapshots,
                         ['ID', 'Volume ID', 'Status',
                          'Name', 'Size', 'User ID'],
                         sortby_index=sortby_index)
    else:
        utils.print_list(snapshots,
                         ['ID', 'Volume ID', 'Status', 'Name', 'Size'],
                         sortby_index=sortby_index)
    if show_count:
        print("Snapshot in total: %s" % total_count)

    with cs.volume_snapshots.completion_cache(
            'uuid',
            cinderclient.v3.volume_snapshots.Snapshot,
            mode='w'):
        for snapshot in snapshots:
            cs.volume_snapshots.write_to_completion_cache('uuid', snapshot.id)
    AppendFilters.filters = []


@api_versions.wraps('3.27')
@utils.arg('--all-tenants',
           dest='all_tenants',
           metavar='<0|1>',
           nargs='?',
           type=int,
           const=1,
           default=utils.env('ALL_TENANTS', default=0),
           help='Shows details for all tenants. Admin only.')
@utils.arg('--volume-id',
           metavar='<volume-id>',
           default=None,
           help="Filters results by a volume ID. Default=None. "
                "%s" % FILTER_DEPRECATED)
@utils.arg('--status',
           metavar='<status>',
           default=None,
           help="Filters results by a status. Default=None. "
                "%s" % FILTER_DEPRECATED)
@utils.arg('--marker',
           metavar='<marker>',
           default=None,
           help='Begin returning attachments that appear later in '
                'attachment list than that represented by this id. '
                'Default=None.')
@utils.arg('--limit',
           metavar='<limit>',
           default=None,
           help='Maximum number of attachments to return. Default=None.')
@utils.arg('--sort',
           metavar='<key>[:<direction>]',
           default=None,
           help=(('Comma-separated list of sort keys and directions in the '
                  'form of <key>[:<asc|desc>]. '
                  'Valid keys: %s. '
                  'Default=None.') % ', '.join(base.SORT_KEY_VALUES)))
@utils.arg('--tenant',
           type=str,
           dest='tenant',
           nargs='?',
           metavar='<tenant>',
           help='Display information from single tenant (Admin only).')
@utils.arg('--filters',
           action=AppendFilters,
           type=six.text_type,
           nargs='*',
           start_version='3.33',
           metavar='<key=value>',
           default=None,
           help="Filter key and value pairs. Please use 'cinder list-filters' "
                "to check enabled filters from server. Use 'key~=value' "
                "for inexact filtering if the key supports. Default=None.")
def do_attachment_list(cs, args):
    """Lists all attachments."""
    search_opts = {
        'all_tenants': 1 if args.tenant else args.all_tenants,
        'project_id': args.tenant,
        'status': args.status,
        'volume_id': args.volume_id,
    }
    # Update search option with `filters`
    if AppendFilters.filters:
        search_opts.update(shell_utils.extract_filters(AppendFilters.filters))

    attachments = cs.attachments.list(search_opts=search_opts,
                                      marker=args.marker,
                                      limit=args.limit,
                                      sort=args.sort)
    for attachment in attachments:
        setattr(attachment, 'server_id', getattr(attachment, 'instance', None))
    columns = ['ID', 'Volume ID', 'Status', 'Server ID']
    if args.sort:
        sortby_index = None
    else:
        sortby_index = 0
    utils.print_list(attachments, columns, sortby_index=sortby_index)
    AppendFilters.filters = []


@api_versions.wraps('3.27')
@utils.arg('attachment',
           metavar='<attachment>',
           help='ID of attachment.')
def do_attachment_show(cs, args):
    """Show detailed information for attachment."""
    attachment = cs.attachments.show(args.attachment)
    attachment_dict = attachment.to_dict()
    connection_dict = attachment_dict.pop('connection_info', {})
    utils.print_dict(attachment_dict)

    # TODO(jdg): Need to add checks here like admin/policy for displaying the
    # connection_info, this is still experimental so we'll leave it enabled for
    # now
    if connection_dict:
        utils.print_dict(connection_dict)


@api_versions.wraps('3.27')
@utils.arg('volume',
           metavar='<volume>',
           help='Name or ID of volume or volumes to attach.')
@utils.arg('server_id',
           metavar='<server_id>',
           help='ID of server attaching to.')
@utils.arg('--connect',
           metavar='<connect>',
           default=False,
           help='Make an active connection using provided connector info '
                '(True or False).')
@utils.arg('--initiator',
           metavar='<initiator>',
           default=None,
           help='iqn of the initiator attaching to.  Default=None.')
@utils.arg('--ip',
           metavar='<ip>',
           default=None,
           help='ip of the system attaching to.  Default=None.')
@utils.arg('--host',
           metavar='<host>',
           default=None,
           help='Name of the host attaching to. Default=None.')
@utils.arg('--platform',
           metavar='<platform>',
           default='x86_64',
           help='Platform type. Default=x86_64.')
@utils.arg('--ostype',
           metavar='<ostype>',
           default='linux2',
           help='OS type. Default=linux2.')
@utils.arg('--multipath',
           metavar='<multipath>',
           default=False,
           help='Use multipath. Default=False.')
@utils.arg('--mountpoint',
           metavar='<mountpoint>',
           default=None,
           help='Mountpoint volume will be attached at. Default=None.')
@utils.arg('--mode',
           metavar='<mode>',
           default='null',
           start_version='3.54',
           help='Mode of attachment, rw, ro and null, where null '
                'indicates we want to honor any existing '
                'admin-metadata settings.  Default=null.')
def do_attachment_create(cs, args):
    """Create an attachment for a cinder volume."""

    connector = {}
    if strutils.bool_from_string(args.connect, strict=True):
        # FIXME(jdg): Add in all the options when they're finalized
        connector = {'initiator': args.initiator,
                     'ip': args.ip,
                     'platform': args.platform,
                     'host': args.host,
                     'os_type': args.ostype,
                     'multipath': args.multipath,
                     'mountpoint': args.mountpoint}
    volume = utils.find_volume(cs, args.volume)
    mode = getattr(args, 'mode', 'null')
    attachment = cs.attachments.create(volume.id,
                                       connector,
                                       args.server_id,
                                       mode)

    connector_dict = attachment.pop('connection_info', None)
    utils.print_dict(attachment)
    if connector_dict:
        utils.print_dict(connector_dict)


@api_versions.wraps('3.27')
@utils.arg('attachment',
           metavar='<attachment>',
           help='ID of attachment.')
@utils.arg('--initiator',
           metavar='<initiator>',
           default=None,
           help='iqn of the initiator attaching to.  Default=None.')
@utils.arg('--ip',
           metavar='<ip>',
           default=None,
           help='ip of the system attaching to.  Default=None.')
@utils.arg('--host',
           metavar='<host>',
           default=None,
           help='Name of the host attaching to. Default=None.')
@utils.arg('--platform',
           metavar='<platform>',
           default='x86_64',
           help='Platform type. Default=x86_64.')
@utils.arg('--ostype',
           metavar='<ostype>',
           default='linux2',
           help='OS type. Default=linux2.')
@utils.arg('--multipath',
           metavar='<multipath>',
           default=False,
           help='Use multipath. Default=False.')
@utils.arg('--mountpoint',
           metavar='<mountpoint>',
           default=None,
           help='Mountpoint volume will be attached at. Default=None.')
def do_attachment_update(cs, args):
    """Update an attachment for a cinder volume.
    This call is designed to be more of an attachment completion than anything
    else.  It expects the value of a connector object to notify the driver that
    the volume is going to be connected and where it's being connected to.
    """
    connector = {'initiator': args.initiator,
                 'ip': args.ip,
                 'platform': args.platform,
                 'host': args.host,
                 'os_type': args.ostype,
                 'multipath': args.multipath,
                 'mountpoint': args.mountpoint}
    attachment = cs.attachments.update(args.attachment,
                                       connector)
    attachment_dict = attachment.to_dict()
    connector_dict = attachment_dict.pop('connection_info', None)
    utils.print_dict(attachment_dict)
    if connector_dict:
        utils.print_dict(connector_dict)


@api_versions.wraps('3.27')
@utils.arg('attachment',
           metavar='<attachment>', nargs='+',
           help='ID of attachment or attachments to delete.')
def do_attachment_delete(cs, args):
    """Delete an attachment for a cinder volume."""
    for attachment in args.attachment:
        cs.attachments.delete(attachment)


@api_versions.wraps('3.44')
@utils.arg('attachment',
           metavar='<attachment>', nargs='+',
           help='ID of attachment or attachments to delete.')
def do_attachment_complete(cs, args):
    """Complete an attachment for a cinder volume."""
    for attachment in args.attachment:
        cs.attachments.complete(attachment)


@api_versions.wraps('3.0')
def do_version_list(cs, args):
    """List all API versions."""
    result = cs.services.server_api_version()
    if 'min_version' in dir(result[0]):
        columns = ["Id", "Status", "Updated", "Min Version", "Version"]
    else:
        columns = ["Id", "Status", "Updated"]

    print("Client supported API versions:")
    print("Minimum version %(v)s" %
          {'v': api_versions.MIN_VERSION})
    print("Maximum version %(v)s" %
          {'v': api_versions.MAX_VERSION})

    print("\nServer supported API versions:")
    utils.print_list(result, columns)


@api_versions.wraps('3.32')
@utils.arg('level',
           metavar='<log-level>',
           choices=('INFO', 'WARNING', 'ERROR', 'DEBUG',
                    'info', 'warning', 'error', 'debug'),
           help='Desired log level.')
@utils.arg('--binary',
           choices=('', '*', 'cinder-api', 'cinder-volume', 'cinder-scheduler',
                    'cinder-backup'),
           default='',
           help='Binary to change.')
@utils.arg('--server',
           default='',
           help='Host or cluster value for service.')
@utils.arg('--prefix',
           default='',
           help='Prefix for the log. ie: "cinder.volume.drivers.".')
def do_service_set_log(cs, args):
    cs.services.set_log_levels(args.level, args.binary, args.server,
                               args.prefix)


@api_versions.wraps('3.32')
@utils.arg('--binary',
           choices=('', '*', 'cinder-api', 'cinder-volume', 'cinder-scheduler',
                    'cinder-backup'),
           default='',
           help='Binary to query.')
@utils.arg('--server',
           default='',
           help='Host or cluster value for service.')
@utils.arg('--prefix',
           default='',
           help='Prefix for the log. ie: "sqlalchemy.".')
def do_service_get_log(cs, args):
    log_levels = cs.services.get_log_levels(args.binary, args.server,
                                            args.prefix)
    columns = ('Binary', 'Host', 'Prefix', 'Level')
    utils.print_list(log_levels, columns)


@utils.arg('volume', metavar='<volume>',
           help='Name or ID of volume to backup.')
@utils.arg('--container', metavar='<container>',
           default=None,
           help='Backup container name. Default=None.')
@utils.arg('--display-name',
           help=argparse.SUPPRESS)
@utils.arg('--name', metavar='<name>',
           default=None,
           help='Backup name. Default=None.')
@utils.arg('--display-description',
           help=argparse.SUPPRESS)
@utils.arg('--description',
           metavar='<description>',
           default=None,
           help='Backup description. Default=None.')
@utils.arg('--incremental',
           action='store_true',
           help='Incremental backup. Default=False.',
           default=False)
@utils.arg('--force',
           action='store_true',
           help='Allows or disallows backup of a volume '
           'when the volume is attached to an instance. '
           'If set to True, backs up the volume whether '
           'its status is "available" or "in-use". The backup '
           'of an "in-use" volume means your data is crash '
           'consistent. Default=False.',
           default=False)
@utils.arg('--snapshot-id',
           metavar='<snapshot-id>',
           default=None,
           help='ID of snapshot to backup. Default=None.')
@utils.arg('--metadata',
           nargs='*',
           metavar='<key=value>',
           default=None,
           start_version='3.43',
           help='Metadata key and value pairs. Default=None.')
@utils.arg('--availability-zone',
           default=None,
           start_version='3.51',
           help='AZ where the backup should be stored, by default it will be '
           'the same as the source.')
def do_backup_create(cs, args):
    """Creates a volume backup."""
    if args.display_name is not None:
        args.name = args.display_name

    if args.display_description is not None:
        args.description = args.display_description

    kwargs = {}
    if getattr(args, 'metadata', None):
        kwargs['metadata'] = shell_utils.extract_metadata(args)
    az = getattr(args, 'availability_zone', None)
    if az:
        kwargs['availability_zone'] = az

    volume = utils.find_volume(cs, args.volume)
    backup = cs.backups.create(volume.id,
                               args.container,
                               args.name,
                               args.description,
                               args.incremental,
                               args.force,
                               args.snapshot_id,
                               **kwargs)
    info = {"volume_id": volume.id}
    info.update(backup._info)

    if 'links' in info:
        info.pop('links')

    utils.print_dict(info)

    with cs.backups.completion_cache(
            'uuid',
            cinderclient.v3.volume_backups.VolumeBackup,
            mode="a"):
        cs.backups.write_to_completion_cache('uuid', backup.id)


@utils.arg('volume', metavar='<volume>',
           help='Name or ID of volume to transfer.')
@utils.arg('--name',
           metavar='<name>',
           default=None,
           help='Transfer name. Default=None.')
@utils.arg('--display-name',
           help=argparse.SUPPRESS)
@utils.arg('--no-snapshots',
           action='store_true',
           help='Allows or disallows transfer volumes without snapshots. '
                'Default=False.',
           start_version='3.55',
           default=False)
def do_transfer_create(cs, args):
    """Creates a volume transfer."""
    if args.display_name is not None:
        args.name = args.display_name

    kwargs = {}
    no_snapshots = getattr(args, 'no_snapshots', None)
    if no_snapshots is not None:
        kwargs['no_snapshots'] = no_snapshots

    volume = utils.find_volume(cs, args.volume)
    transfer = cs.transfers.create(volume.id,
                                   args.name,
                                   **kwargs)
    info = dict()
    info.update(transfer._info)

    info.pop('links', None)
    utils.print_dict(info)


@utils.arg('--all-tenants',
           dest='all_tenants',
           metavar='<0|1>',
           nargs='?',
           type=int,
           const=1,
           default=0,
           help='Shows details for all tenants. Admin only.')
@utils.arg('--all_tenants',
           nargs='?',
           type=int,
           const=1,
           help=argparse.SUPPRESS)
@utils.arg('--sort',
           metavar='<key>[:<direction>]',
           default=None,
           help='Sort keys and directions in the form of <key>[:<asc|desc>].',
           start_version='3.59')
@utils.arg('--filters',
           action=AppendFilters,
           type=six.text_type,
           nargs='*',
           start_version='3.52',
           metavar='<key=value>',
           default=None,
           help="Filter key and value pairs.")
def do_transfer_list(cs, args):
    """Lists all transfers."""
    all_tenants = int(os.environ.get("ALL_TENANTS", args.all_tenants))
    search_opts = {
        'all_tenants': all_tenants,
    }
    if AppendFilters.filters:
        search_opts.update(shell_utils.extract_filters(AppendFilters.filters))

    sort = getattr(args, 'sort', None)
    if sort:
        sort_args = sort.split(':')
        if len(sort_args) > 2:
            raise exceptions.CommandError(
                'Invalid sort parameter provided. Argument must be in the '
                'form "key[:<asc|desc>]".')

    transfers = cs.transfers.list(search_opts=search_opts, sort=sort)
    columns = ['ID', 'Volume ID', 'Name']
    utils.print_list(transfers, columns)
    AppendFilters.filters = []