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
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
|
# Copyright 2013 Hewlett-Packard Development Company, L.P.
# 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.
import copy
import datetime
from http import client as http_client
import json
from ironic_lib import metrics_utils
import jsonschema
from jsonschema import exceptions as json_schema_exc
from oslo_log import log
from oslo_utils import strutils
from oslo_utils import uuidutils
import pecan
from pecan import rest
from ironic import api
from ironic.api.controllers import link
from ironic.api.controllers.v1 import allocation
from ironic.api.controllers.v1 import bios
from ironic.api.controllers.v1 import collection
from ironic.api.controllers.v1 import notification_utils as notify
from ironic.api.controllers.v1 import port
from ironic.api.controllers.v1 import portgroup
from ironic.api.controllers.v1 import utils as api_utils
from ironic.api.controllers.v1 import versions
from ironic.api.controllers.v1 import volume
from ironic.api import method
from ironic.common import args
from ironic.common import boot_modes
from ironic.common import exception
from ironic.common.i18n import _
from ironic.common import policy
from ironic.common import states as ir_states
from ironic.conductor import steps as conductor_steps
import ironic.conf
from ironic.drivers import base as driver_base
from ironic import objects
CONF = ironic.conf.CONF
LOG = log.getLogger(__name__)
_CLEAN_STEPS_SCHEMA = {
"$schema": "http://json-schema.org/schema#",
"title": "Clean steps schema",
"type": "array",
# list of clean steps
"items": {
"type": "object",
# args is optional
"required": ["interface", "step"],
"properties": {
"interface": {
"description": "driver interface",
"enum": list(conductor_steps.CLEANING_INTERFACE_PRIORITY)
# interface value must be one of the valid interfaces
},
"step": {
"description": "name of clean step",
"type": "string",
"minLength": 1
},
"args": {
"description": "additional args",
"type": "object",
"properties": {}
},
},
# interface, step and args are the only expected keys
"additionalProperties": False
}
}
_DEPLOY_STEPS_SCHEMA = {
"$schema": "http://json-schema.org/schema#",
"title": "Deploy steps schema",
"type": "array",
"items": api_utils.DEPLOY_STEP_SCHEMA
}
METRICS = metrics_utils.get_metrics_logger(__name__)
# Vendor information for node's driver:
# key = driver name;
# value = dictionary of node vendor methods of that driver:
# key = method name.
# value = dictionary with the metadata of that method.
# NOTE(lucasagomes). This is cached for the lifetime of the API
# service. If one or more conductor services are restarted with new driver
# versions, the API service should be restarted.
_VENDOR_METHODS = {}
_DEFAULT_RETURN_FIELDS = ['instance_uuid', 'maintenance', 'power_state',
'provision_state', 'uuid', 'name']
# States where calling do_provisioning_action makes sense
PROVISION_ACTION_STATES = (ir_states.VERBS['manage'],
ir_states.VERBS['provide'],
ir_states.VERBS['abort'],
ir_states.VERBS['adopt'])
_NODES_CONTROLLER_RESERVED_WORDS = None
ALLOWED_TARGET_POWER_STATES = (ir_states.POWER_ON,
ir_states.POWER_OFF,
ir_states.REBOOT,
ir_states.SOFT_REBOOT,
ir_states.SOFT_POWER_OFF)
ALLOWED_TARGET_BOOT_MODES = (boot_modes.LEGACY_BIOS,
boot_modes.UEFI)
_NODE_DESCRIPTION_MAX_LENGTH = 4096
_NETWORK_DATA_SCHEMA = None
def network_data_schema():
global _NETWORK_DATA_SCHEMA
if _NETWORK_DATA_SCHEMA is None:
with open(CONF.api.network_data_schema) as fl:
_NETWORK_DATA_SCHEMA = json.load(fl)
return _NETWORK_DATA_SCHEMA
def node_schema():
network_data = network_data_schema()
return {
'$schema': 'http://json-schema.org/draft-07/schema#',
'type': 'object',
'properties': {
'automated_clean': {'type': ['string', 'boolean', 'null']},
'bios_interface': {'type': ['string', 'null']},
'boot_interface': {'type': ['string', 'null']},
'boot_mode': {'type': ['string', 'null']},
'chassis_uuid': {'type': ['string', 'null']},
'conductor_group': {'type': ['string', 'null']},
'console_enabled': {'type': ['string', 'boolean', 'null']},
'console_interface': {'type': ['string', 'null']},
'deploy_interface': {'type': ['string', 'null']},
'description': {'type': ['string', 'null'],
'maxLength': _NODE_DESCRIPTION_MAX_LENGTH},
'driver': {'type': 'string'},
'driver_info': {'type': ['object', 'null']},
'extra': {'type': ['object', 'null']},
'inspect_interface': {'type': ['string', 'null']},
'instance_info': {'type': ['object', 'null']},
'instance_uuid': {'type': ['string', 'null']},
'lessee': {'type': ['string', 'null']},
'management_interface': {'type': ['string', 'null']},
'maintenance': {'type': ['string', 'boolean', 'null']},
'name': {'type': ['string', 'null']},
'network_data': {'anyOf': [
{'type': 'null'},
{'type': 'object', 'additionalProperties': False},
network_data
]},
'network_interface': {'type': ['string', 'null']},
'owner': {'type': ['string', 'null']},
'power_interface': {'type': ['string', 'null']},
'properties': {'type': ['object', 'null']},
'raid_interface': {'type': ['string', 'null']},
'rescue_interface': {'type': ['string', 'null']},
'resource_class': {'type': ['string', 'null'], 'maxLength': 80},
'retired': {'type': ['string', 'boolean', 'null']},
'retired_reason': {'type': ['string', 'null']},
'secure_boot': {'type': ['string', 'boolean', 'null']},
'storage_interface': {'type': ['string', 'null']},
'uuid': {'type': ['string', 'null']},
'vendor_interface': {'type': ['string', 'null']},
},
'required': ['driver'],
'additionalProperties': False,
'definitions': network_data.get('definitions', {})
}
def node_patch_schema():
node_patch = copy.deepcopy(node_schema())
# add schema for patchable fields
node_patch['properties']['protected'] = {
'type': ['string', 'boolean', 'null']}
node_patch['properties']['protected_reason'] = {
'type': ['string', 'null']}
return node_patch
NODE_VALIDATE_EXTRA = args.dict_valid(
automated_clean=args.boolean,
chassis_uuid=args.uuid,
console_enabled=args.boolean,
instance_uuid=args.uuid,
protected=args.boolean,
maintenance=args.boolean,
retired=args.boolean,
uuid=args.uuid,
)
_NODE_VALIDATOR = None
_NODE_PATCH_VALIDATOR = None
def node_validator(name, value):
global _NODE_VALIDATOR
if _NODE_VALIDATOR is None:
_NODE_VALIDATOR = args.and_valid(
args.schema(node_schema()),
NODE_VALIDATE_EXTRA
)
return _NODE_VALIDATOR(name, value)
def node_patch_validator(name, value):
global _NODE_PATCH_VALIDATOR
if _NODE_PATCH_VALIDATOR is None:
_NODE_PATCH_VALIDATOR = args.and_valid(
args.schema(node_patch_schema()),
NODE_VALIDATE_EXTRA
)
return _NODE_PATCH_VALIDATOR(name, value)
PATCH_ALLOWED_FIELDS = [
'automated_clean',
'bios_interface',
'boot_interface',
'chassis_uuid',
'conductor_group',
'console_interface',
'deploy_interface',
'description',
'driver',
'driver_info',
'extra',
'inspect_interface',
'instance_info',
'instance_uuid',
'lessee',
'maintenance',
'management_interface',
'name',
'network_data',
'network_interface',
'owner',
'power_interface',
'properties',
'protected',
'protected_reason',
'raid_interface',
'rescue_interface',
'resource_class',
'retired',
'retired_reason',
'storage_interface',
'vendor_interface'
]
TRAITS_SCHEMA = {
'type': 'object',
'properties': {
'traits': {
'type': 'array',
'items': api_utils.TRAITS_SCHEMA
},
},
'additionalProperties': False,
}
VIF_VALIDATOR = args.and_valid(
args.schema({
'type': 'object',
'properties': {
'id': {'type': 'string'},
},
'required': ['id'],
'additionalProperties': True,
}),
args.dict_valid(id=args.uuid_or_name)
)
def get_nodes_controller_reserved_names():
global _NODES_CONTROLLER_RESERVED_WORDS
if _NODES_CONTROLLER_RESERVED_WORDS is None:
_NODES_CONTROLLER_RESERVED_WORDS = (
api_utils.get_controller_reserved_names(NodesController))
return _NODES_CONTROLLER_RESERVED_WORDS
def hide_fields_in_newer_versions(obj):
"""This method hides fields that were added in newer API versions.
Certain node fields were introduced at certain API versions.
These fields are only made available when the request's API version
matches or exceeds the versions when these fields were introduced.
"""
for field in api_utils.disallowed_fields():
obj.pop(field, None)
def reject_fields_in_newer_versions(obj):
"""When creating an object, reject fields that appear in newer versions."""
for field in api_utils.disallowed_fields():
if field == 'conductor_group':
# NOTE(jroll) this is special-cased to "" and not Unset,
# because it is used in hash ring calculations
empty_value = ''
elif field == 'name' and obj.get('name') is None:
# NOTE(dtantsur): for some reason we allow specifying name=None
# explicitly even in old API versions..
continue
else:
empty_value = None
if obj.get(field, empty_value) != empty_value:
LOG.debug('Field %(field)s is not acceptable in version %(ver)s',
{'field': field, 'ver': api.request.version})
raise exception.NotAcceptable()
def reject_patch_in_newer_versions(patch):
for field in api_utils.disallowed_fields():
value = api_utils.get_patch_values(patch, '/%s' % field)
if value:
LOG.debug('Field %(field)s is not acceptable in version %(ver)s',
{'field': field, 'ver': api.request.version})
raise exception.NotAcceptable()
def update_state_in_older_versions(obj):
"""Change provision state names for API backwards compatibility.
:param obj: The dict being returned to the API client that is
to be updated by this method.
"""
# if requested version is < 1.2, convert AVAILABLE to the old NOSTATE
if (api.request.version.minor < versions.MINOR_2_AVAILABLE_STATE
and obj.get('provision_state') == ir_states.AVAILABLE):
obj['provision_state'] = ir_states.NOSTATE
# if requested version < 1.39, convert INSPECTWAIT to INSPECTING
if (not api_utils.allow_inspect_wait_state()
and obj.get('provision_state') == ir_states.INSPECTWAIT):
obj['provision_state'] = ir_states.INSPECTING
def validate_network_data(network_data):
"""Validates node network_data field.
This method validates network data configuration against JSON
schema.
:param network_data: a network_data field to validate
:raises: Invalid if network data is not schema-compliant
"""
try:
jsonschema.validate(network_data, network_data_schema())
except json_schema_exc.ValidationError as e:
# NOTE: Even though e.message is deprecated in general, it is
# said in jsonschema documentation to use this still.
msg = _("Invalid network_data: %s ") % e.message
raise exception.Invalid(msg)
class BootDeviceController(rest.RestController):
_custom_actions = {
'supported': ['GET'],
}
def _get_boot_device(self, rpc_node, supported=False):
"""Get the current boot device or a list of supported devices.
:param rpc_node: RPC Node object.
:param supported: Boolean value. If true return a list of
supported boot devices, if false return the
current boot device. Default: False.
:returns: The current boot device or a list of the supported
boot devices.
"""
topic = api.request.rpcapi.get_topic_for(rpc_node)
if supported:
return api.request.rpcapi.get_supported_boot_devices(
api.request.context, rpc_node.uuid, topic)
else:
return api.request.rpcapi.get_boot_device(api.request.context,
rpc_node.uuid, topic)
@METRICS.timer('BootDeviceController.put')
@method.expose(status_code=http_client.NO_CONTENT)
@args.validate(node_ident=args.uuid_or_name, boot_device=args.string,
persistent=args.boolean)
def put(self, node_ident, boot_device, persistent=False):
"""Set the boot device for a node.
Set the boot device to use on next reboot of the node.
:param node_ident: the UUID or logical name of a node.
:param boot_device: the boot device, one of
:mod:`ironic.common.boot_devices`.
:param persistent: Boolean value. True if the boot device will
persist to all future boots, False if not.
Default: False.
"""
rpc_node = api_utils.check_node_policy_and_retrieve(
'baremetal:node:set_boot_device', node_ident)
topic = api.request.rpcapi.get_topic_for(rpc_node)
api.request.rpcapi.set_boot_device(api.request.context,
rpc_node.uuid,
boot_device,
persistent=persistent,
topic=topic)
@METRICS.timer('BootDeviceController.get')
@method.expose()
@args.validate(node_ident=args.uuid_or_name)
def get(self, node_ident):
"""Get the current boot device for a node.
:param node_ident: the UUID or logical name of a node.
:returns: a json object containing:
:boot_device: the boot device, one of
:mod:`ironic.common.boot_devices` or None if it is unknown.
:persistent: Whether the boot device will persist to all
future boots or not, None if it is unknown.
"""
rpc_node = api_utils.check_node_policy_and_retrieve(
'baremetal:node:get_boot_device', node_ident)
return self._get_boot_device(rpc_node)
@METRICS.timer('BootDeviceController.supported')
@method.expose()
@args.validate(node_ident=args.uuid_or_name)
def supported(self, node_ident):
"""Get a list of the supported boot devices.
:param node_ident: the UUID or logical name of a node.
:returns: A json object with the list of supported boot
devices.
"""
rpc_node = api_utils.check_node_policy_and_retrieve(
'baremetal:node:get_boot_device', node_ident)
boot_devices = self._get_boot_device(rpc_node, supported=True)
return {'supported_boot_devices': boot_devices}
class IndicatorAtComponent(object):
def __init__(self, **kwargs):
name = kwargs.get('name')
component = kwargs.get('component')
unique_name = kwargs.get('unique_name')
if name and component:
self.unique_name = name + '@' + component
self.name = name
self.component = component
elif unique_name:
try:
index = unique_name.index('@')
except ValueError:
raise exception.InvalidParameterValue(
_('Malformed indicator name "%s"') % unique_name)
self.component = unique_name[index + 1:]
self.name = unique_name[:index]
self.unique_name = unique_name
else:
raise exception.MissingParameterValue(
_('Missing indicator name "%s"'))
def indicator_convert_with_links(node_uuid, rpc_component, rpc_name,
**rpc_fields):
"""Add links to the indicator."""
url = api.request.public_url
return {
'name': rpc_name,
'component': rpc_component,
'readonly': rpc_fields.get('readonly', True),
'states': rpc_fields.get('states', []),
'links': [
link.make_link(
'self', url, 'nodes',
'%s/management/indicators/%s' % (
node_uuid, rpc_name)),
link.make_link(
'bookmark', url, 'nodes',
'%s/management/indicators/%s' % (
node_uuid, rpc_name),
bookmark=True)
]
}
def indicator_list_from_dict(node_ident, indicators):
indicator_list = []
for component, names in indicators.items():
for name, fields in names.items():
indicator_at_component = IndicatorAtComponent(
component=component, name=name)
indicator = indicator_convert_with_links(
node_ident, component, indicator_at_component.unique_name,
**fields)
indicator_list.append(indicator)
return {'indicators': indicator_list}
class IndicatorController(rest.RestController):
@METRICS.timer('IndicatorController.put')
@method.expose(status_code=http_client.NO_CONTENT)
@args.validate(node_ident=args.uuid_or_name, indicator=args.string,
state=args.string)
def put(self, node_ident, indicator, state):
"""Set node hardware component indicator to the desired state.
:param node_ident: the UUID or logical name of a node.
:param indicator: Indicator ID (as reported by
`get_supported_indicators`).
:param state: Indicator state, one of
mod:`ironic.common.indicator_states`.
"""
rpc_node = api_utils.check_node_policy_and_retrieve(
'baremetal:node:set_indicator_state',
node_ident)
topic = pecan.request.rpcapi.get_topic_for(rpc_node)
indicator_at_component = IndicatorAtComponent(unique_name=indicator)
pecan.request.rpcapi.set_indicator_state(
pecan.request.context, rpc_node.uuid,
indicator_at_component.component, indicator_at_component.name,
state, topic=topic)
@METRICS.timer('IndicatorController.get_one')
@method.expose()
@args.validate(node_ident=args.uuid_or_name, indicator=args.string)
def get_one(self, node_ident, indicator):
"""Get node hardware component indicator and its state.
:param node_ident: the UUID or logical name of a node.
:param indicator: Indicator ID (as reported by
`get_supported_indicators`).
:returns: a dict with the "state" key and one of
mod:`ironic.common.indicator_states` as a value.
"""
rpc_node = api_utils.check_node_policy_and_retrieve(
'baremetal:node:get_indicator_state',
node_ident)
topic = pecan.request.rpcapi.get_topic_for(rpc_node)
indicator_at_component = IndicatorAtComponent(unique_name=indicator)
state = pecan.request.rpcapi.get_indicator_state(
pecan.request.context, rpc_node.uuid,
indicator_at_component.component, indicator_at_component.name,
topic=topic)
return {'state': state}
@METRICS.timer('IndicatorController.get_all')
@method.expose()
@args.validate(node_ident=args.uuid_or_name)
def get_all(self, node_ident, **kwargs):
"""Get node hardware components and their indicators.
:param node_ident: the UUID or logical name of a node.
:returns: A json object of hardware components
(:mod:`ironic.common.components`) as keys with indicator IDs
(from `get_supported_indicators`) as values.
"""
rpc_node = api_utils.check_node_policy_and_retrieve(
'baremetal:node:get_indicator_state',
node_ident)
topic = pecan.request.rpcapi.get_topic_for(rpc_node)
indicators = pecan.request.rpcapi.get_supported_indicators(
pecan.request.context, rpc_node.uuid, topic=topic)
return indicator_list_from_dict(
node_ident, indicators)
class InjectNmiController(rest.RestController):
@METRICS.timer('InjectNmiController.put')
@method.expose(status_code=http_client.NO_CONTENT)
@args.validate(node_ident=args.uuid_or_name)
def put(self, node_ident):
"""Inject NMI for a node.
Inject NMI (Non Maskable Interrupt) for a node immediately.
:param node_ident: the UUID or logical name of a node.
:raises: NotFound if requested version of the API doesn't support
inject nmi.
:raises: HTTPForbidden if the policy is not authorized.
:raises: NodeNotFound if the node is not found.
:raises: NodeLocked if the node is locked by another conductor.
:raises: UnsupportedDriverExtension if the node's driver doesn't
support management or management.inject_nmi.
:raises: InvalidParameterValue when the wrong driver info is
specified or an invalid boot device is specified.
:raises: MissingParameterValue if missing supplied info.
"""
if not api_utils.allow_inject_nmi():
raise exception.NotFound()
rpc_node = api_utils.check_node_policy_and_retrieve(
'baremetal:node:inject_nmi', node_ident)
topic = api.request.rpcapi.get_topic_for(rpc_node)
api.request.rpcapi.inject_nmi(api.request.context,
rpc_node.uuid,
topic=topic)
class NodeManagementController(rest.RestController):
boot_device = BootDeviceController()
"""Expose boot_device as a sub-element of management"""
inject_nmi = InjectNmiController()
"""Expose inject_nmi as a sub-element of management"""
indicators = IndicatorController()
"""Expose indicators as a sub-element of management"""
class NodeConsoleController(rest.RestController):
@METRICS.timer('NodeConsoleController.get')
@method.expose()
@args.validate(node_ident=args.uuid_or_name)
def get(self, node_ident):
"""Get connection information about the console.
:param node_ident: UUID or logical name of a node.
"""
rpc_node = api_utils.check_node_policy_and_retrieve(
'baremetal:node:get_console', node_ident)
topic = api.request.rpcapi.get_topic_for(rpc_node)
try:
console = api.request.rpcapi.get_console_information(
api.request.context, rpc_node.uuid, topic)
console_state = True
except exception.NodeConsoleNotEnabled:
console = None
console_state = False
return {'console_enabled': console_state, 'console_info': console}
@METRICS.timer('NodeConsoleController.put')
@method.expose(status_code=http_client.ACCEPTED)
@args.validate(node_ident=args.uuid_or_name, enabled=args.boolean)
def put(self, node_ident, enabled):
"""Start and stop the node console.
:param node_ident: UUID or logical name of a node.
:param enabled: Boolean value; whether to enable or disable the
console.
"""
rpc_node = api_utils.check_node_policy_and_retrieve(
'baremetal:node:set_console_state', node_ident)
topic = api.request.rpcapi.get_topic_for(rpc_node)
api.request.rpcapi.set_console_mode(api.request.context,
rpc_node.uuid, enabled, topic)
# Set the HTTP Location Header
url_args = '/'.join([node_ident, 'states', 'console'])
api.response.location = link.build_url('nodes', url_args)
def node_states_convert(rpc_node):
attr_list = ['console_enabled', 'last_error', 'power_state',
'provision_state', 'target_power_state',
'target_provision_state', 'provision_updated_at']
if api_utils.allow_raid_config():
attr_list.extend(['raid_config', 'target_raid_config'])
if api.request.version.minor >= versions.MINOR_75_NODE_BOOT_MODE:
attr_list.extend(['boot_mode', 'secure_boot'])
states = {}
for attr in attr_list:
states[attr] = getattr(rpc_node, attr)
if isinstance(states[attr], datetime.datetime):
states[attr] = states[attr].isoformat()
update_state_in_older_versions(states)
return states
class NodeStatesController(rest.RestController):
_custom_actions = {
'boot_mode': ['PUT'],
'secure_boot': ['PUT'],
'power': ['PUT'],
'provision': ['PUT'],
'raid': ['PUT'],
}
console = NodeConsoleController()
"""Expose console as a sub-element of states"""
@METRICS.timer('NodeStatesController.get')
@method.expose()
@args.validate(node_ident=args.uuid_or_name)
def get(self, node_ident):
"""List the states of the node.
:param node_ident: the UUID or logical_name of a node.
"""
rpc_node = api_utils.check_node_policy_and_retrieve(
'baremetal:node:get_states', node_ident)
# NOTE(lucasagomes): All these state values come from the
# DB. Ironic counts with a periodic task that verify the current
# power states of the nodes and update the DB accordingly.
return node_states_convert(rpc_node)
@METRICS.timer('NodeStatesController.raid')
@method.expose(status_code=http_client.NO_CONTENT)
@method.body('target_raid_config')
@args.validate(node_ident=args.uuid_or_name,
target_raid_config=args.types(dict))
def raid(self, node_ident, target_raid_config):
"""Set the target raid config of the node.
:param node_ident: the UUID or logical name of a node.
:param target_raid_config: Desired target RAID configuration of
the node. It may be an empty dictionary as well.
:raises: UnsupportedDriverExtension, if the node's driver doesn't
support RAID configuration.
:raises: InvalidParameterValue, if validation of target raid config
fails.
:raises: NotAcceptable, if requested version of the API is less than
1.12.
"""
rpc_node = api_utils.check_node_policy_and_retrieve(
'baremetal:node:set_raid_state', node_ident)
if not api_utils.allow_raid_config():
raise exception.NotAcceptable()
topic = api.request.rpcapi.get_topic_for(rpc_node)
try:
api.request.rpcapi.set_target_raid_config(
api.request.context, rpc_node.uuid,
target_raid_config, topic=topic)
except exception.UnsupportedDriverExtension as e:
# Change error code as 404 seems appropriate because RAID is a
# standard interface and all drivers might not have it.
e.code = http_client.NOT_FOUND
raise
@METRICS.timer('NodeStatesController.power')
@method.expose(status_code=http_client.ACCEPTED)
@args.validate(node_ident=args.uuid_or_name, target=args.string,
timeout=args.integer)
def power(self, node_ident, target, timeout=None):
"""Set the power state of the node.
:param node_ident: the UUID or logical name of a node.
:param target: The desired power state of the node.
:param timeout: timeout (in seconds) positive integer (> 0) for any
power state. ``None`` indicates to use default timeout.
:raises: ClientSideError (HTTP 409) if a power operation is
already in progress.
:raises: InvalidStateRequested (HTTP 400) if the requested target
state is not valid or if the node is in CLEANING state.
:raises: NotAcceptable (HTTP 406) for soft reboot, soft power off or
timeout parameter, if requested version of the API is less than 1.27.
:raises: Invalid (HTTP 400) if timeout value is less than 1.
"""
rpc_node = api_utils.check_node_policy_and_retrieve(
'baremetal:node:set_power_state', node_ident)
# TODO(lucasagomes): Test if it's able to transition to the
# target state from the current one
topic = api.request.rpcapi.get_topic_for(rpc_node)
if ((target in [ir_states.SOFT_REBOOT, ir_states.SOFT_POWER_OFF]
or timeout) and not api_utils.allow_soft_power_off()):
raise exception.NotAcceptable()
if timeout is not None and timeout < 1:
raise exception.Invalid(
_("timeout has to be positive integer"))
if target not in ALLOWED_TARGET_POWER_STATES:
raise exception.InvalidStateRequested(
action=target, node=node_ident,
state=rpc_node.power_state)
# Don't change power state for nodes being cleaned
elif rpc_node.provision_state in (ir_states.CLEANWAIT,
ir_states.CLEANING):
raise exception.InvalidStateRequested(
action=target, node=node_ident,
state=rpc_node.provision_state)
api.request.rpcapi.change_node_power_state(api.request.context,
rpc_node.uuid, target,
timeout=timeout,
topic=topic)
# Set the HTTP Location Header
url_args = '/'.join([node_ident, 'states'])
api.response.location = link.build_url('nodes', url_args)
@METRICS.timer('NodeStatesController.boot_mode')
@method.expose(status_code=http_client.ACCEPTED)
@args.validate(node_ident=args.uuid_or_name, target=args.string)
def boot_mode(self, node_ident, target):
"""Asynchronous set the boot mode of the node.
:param node_ident: the UUID or logical name of a node.
:param target: The desired boot_mode for the node (uefi/bios).
:raises: InvalidParameterValue (HTTP 400) if the requested target
state is not valid.
:raises: NotFound (HTTP 404) if requested version of the API
is less than 1.76.
:raises: Conflict (HTTP 409) if a node is in adopting state or
another transient state.
"""
rpc_node = api_utils.check_node_policy_and_retrieve(
'baremetal:node:set_boot_mode', node_ident)
topic = api.request.rpcapi.get_topic_for(rpc_node)
if (api.request.version.minor
< versions.MINOR_76_NODE_CHANGE_BOOT_MODE):
raise exception.NotFound(
(_("This endpoint is supported starting with the API version "
"1.%(min_version)s") %
{'min_version': versions.MINOR_76_NODE_CHANGE_BOOT_MODE}))
if target not in ALLOWED_TARGET_BOOT_MODES:
msg = (_("Invalid boot mode %(mode)s requested for node. "
"Allowed boot modes are: "
"%(modes)s") %
{'mode': target,
'modes': ', '.join(ALLOWED_TARGET_BOOT_MODES)})
raise exception.InvalidParameterValue(msg)
# NOTE(cenne): This currenly includes the ADOPTING state
if rpc_node.provision_state in ir_states.UNSTABLE_STATES:
msg = _("Node is in %(state)s state. Since node is transitioning, "
"the boot mode will not be set as this may interfere "
"with ongoing changes and result in erroneous modification"
". Try again later.")
raise exception.Conflict(msg,
action=target, node=node_ident,
state=rpc_node.provision_state)
api.request.rpcapi.change_node_boot_mode(api.request.context,
rpc_node.uuid, target,
topic=topic)
# Set the HTTP Location Header
url_args = '/'.join([node_ident, 'states'])
api.response.location = link.build_url('nodes', url_args)
@METRICS.timer('NodeStatesController.secure_boot')
@method.expose(status_code=http_client.ACCEPTED)
@args.validate(node_ident=args.uuid_or_name, target=args.boolean)
def secure_boot(self, node_ident, target):
"""Asynchronous set the secure_boot state of the node.
:param node_ident: the UUID or logical name of a node.
:param target: Should secure_boot be enabled on node (True/False).
:raises: InvalidParameterValue (HTTP 400) if the requested target
state is not valid.
:raises: NotFound (HTTP 404) if requested version of the API
is less than 1.76.
:raises: Conflict (HTTP 409) if a node is in adopting state.
"""
rpc_node = api_utils.check_node_policy_and_retrieve(
'baremetal:node:set_secure_boot', node_ident)
topic = api.request.rpcapi.get_topic_for(rpc_node)
if (api.request.version.minor
< versions.MINOR_76_NODE_CHANGE_BOOT_MODE):
raise exception.NotFound(
(_("This endpoint is supported starting with the API version "
"1.%(min_version)s") %
{'min_version': versions.MINOR_76_NODE_CHANGE_BOOT_MODE}))
# NOTE(cenne): This is to exclude target=None or other invalid values
if target not in (True, False):
msg = (_("Invalid secure_boot %(state)s requested for node. "
"Allowed secure_boot states are: True, False) ") %
{'state': target})
raise exception.InvalidParameterValue(msg)
# NOTE(cenne): This currenly includes the ADOPTING state
if rpc_node.provision_state in ir_states.UNSTABLE_STATES:
msg = _("Node is in %(state)s state. Since node is transitioning, "
"the boot mode will not be set as this may interfere "
"with ongoing changes and result in erroneous modification"
". Try again later.")
raise exception.Conflict(msg,
action=target, node=node_ident,
state=rpc_node.provision_state
)
api.request.rpcapi.change_node_secure_boot(api.request.context,
rpc_node.uuid, target,
topic=topic)
# Set the HTTP Location Header
url_args = '/'.join([node_ident, 'states'])
api.response.location = link.build_url('nodes', url_args)
def _do_provision_action(self, rpc_node, target, configdrive=None,
clean_steps=None, deploy_steps=None,
rescue_password=None, disable_ramdisk=None):
topic = api.request.rpcapi.get_topic_for(rpc_node)
# Note that there is a race condition. The node state(s) could change
# by the time the RPC call is made and the TaskManager manager gets a
# lock.
if target in (ir_states.ACTIVE, ir_states.REBUILD, ir_states.DEPLOY):
rebuild = (target == ir_states.REBUILD)
if deploy_steps:
_check_deploy_steps(deploy_steps)
api.request.rpcapi.do_node_deploy(context=api.request.context,
node_id=rpc_node.uuid,
rebuild=rebuild,
configdrive=configdrive,
topic=topic,
deploy_steps=deploy_steps)
elif (target == ir_states.VERBS['unrescue']):
api.request.rpcapi.do_node_unrescue(
api.request.context, rpc_node.uuid, topic)
elif (target == ir_states.VERBS['rescue']):
if not (rescue_password and rescue_password.strip()):
msg = (_('A non-empty "rescue_password" is required when '
'setting target provision state to %s') %
ir_states.VERBS['rescue'])
raise exception.ClientSideError(
msg, status_code=http_client.BAD_REQUEST)
api.request.rpcapi.do_node_rescue(
api.request.context, rpc_node.uuid, rescue_password, topic)
elif target in (ir_states.DELETED, ir_states.UNDEPLOY):
api.request.rpcapi.do_node_tear_down(
api.request.context, rpc_node.uuid, topic)
elif target == ir_states.VERBS['inspect']:
api.request.rpcapi.inspect_hardware(
api.request.context, rpc_node.uuid, topic=topic)
elif target == ir_states.VERBS['clean']:
if not clean_steps:
msg = (_('"clean_steps" is required when setting target '
'provision state to %s') % ir_states.VERBS['clean'])
raise exception.ClientSideError(
msg, status_code=http_client.BAD_REQUEST)
_check_clean_steps(clean_steps)
api.request.rpcapi.do_node_clean(
api.request.context, rpc_node.uuid, clean_steps,
disable_ramdisk, topic=topic)
elif target in PROVISION_ACTION_STATES:
api.request.rpcapi.do_provisioning_action(
api.request.context, rpc_node.uuid, target, topic)
else:
msg = (_('The requested action "%(action)s" could not be '
'understood.') % {'action': target})
raise exception.InvalidStateRequested(message=msg)
@METRICS.timer('NodeStatesController.provision')
@method.expose(status_code=http_client.ACCEPTED)
@args.validate(node_ident=args.uuid_or_name, target=args.string,
configdrive=args.types(type(None), dict, str),
clean_steps=args.types(type(None), list),
deploy_steps=args.types(type(None), list),
rescue_password=args.string,
disable_ramdisk=args.boolean)
def provision(self, node_ident, target, configdrive=None,
clean_steps=None, deploy_steps=None,
rescue_password=None, disable_ramdisk=None):
"""Asynchronous trigger the provisioning of the node.
This will set the target provision state of the node, and a
background task will begin which actually applies the state
change. This call will return a 202 (Accepted) indicating the
request was accepted and is in progress; the client should
continue to GET the status of this node to observe the status
of the requested action.
:param node_ident: UUID or logical name of a node.
:param target: The desired provision state of the node or verb.
:param configdrive: Optional. A gzipped and base64 encoded
configdrive or a dict to build a configdrive from. Only valid when
setting provision state to "active" or "rebuild".
:param clean_steps: An ordered list of cleaning steps that will be
performed on the node. A cleaning step is a dictionary with
required keys 'interface' and 'step', and optional key 'args'. If
specified, the value for 'args' is a keyword variable argument
dictionary that is passed to the cleaning step method.::
{ 'interface': <driver_interface>,
'step': <name_of_clean_step>,
'args': {<arg1>: <value1>, ..., <argn>: <valuen>} }
For example (this isn't a real example, this cleaning step
doesn't exist)::
{ 'interface': 'deploy',
'step': 'upgrade_firmware',
'args': {'force': True} }
This is required (and only valid) when target is "clean".
:param deploy_steps: A list of deploy steps that will be performed on
the node. A deploy step is a dictionary with required keys
'interface', 'step', 'priority' and 'args'. If specified, the value
for 'args' is a keyword variable argument dictionary that is passed
to the deploy step method.::
{ 'interface': <driver_interface>,
'step': <name_of_deploy_step>,
'args': {<arg1>: <value1>, ..., <argn>: <valuen>}
'priority': <integer>}
For example (this isn't a real example, this deploy step doesn't
exist)::
{ 'interface': 'deploy',
'step': 'upgrade_firmware',
'args': {'force': True},
'priority': 90 }
This is used only when target is "active" or "rebuild" and is
optional.
:param rescue_password: A string representing the password to be set
inside the rescue environment. This is required (and only valid),
when target is "rescue".
:param disable_ramdisk: Whether to skip booting ramdisk for cleaning.
:raises: NodeLocked (HTTP 409) if the node is currently locked.
:raises: ClientSideError (HTTP 409) if the node is already being
provisioned.
:raises: InvalidParameterValue (HTTP 400), if validation of
clean_steps, deploy_steps or power driver interface fails.
:raises: InvalidStateRequested (HTTP 400) if the requested transition
is not possible from the current state.
:raises: NodeInMaintenance (HTTP 400), if operation cannot be
performed because the node is in maintenance mode.
:raises: NoFreeConductorWorker (HTTP 503) if no workers are available.
:raises: NotAcceptable (HTTP 406) if the API version specified does
not allow the requested state transition or parameters.
"""
rpc_node = api_utils.check_node_policy_and_retrieve(
'baremetal:node:set_provision_state', node_ident)
api_utils.check_allow_management_verbs(target)
if (target in (ir_states.ACTIVE, ir_states.REBUILD)
and rpc_node.maintenance):
raise exception.NodeInMaintenance(op=_('provisioning'),
node=rpc_node.uuid)
m = ir_states.machine.copy()
m.initialize(rpc_node.provision_state)
if not m.is_actionable_event(ir_states.VERBS.get(target, target)):
# Normally, we let the task manager recognize and deal with
# NodeLocked exceptions. However, that isn't done until the RPC
# calls below.
# In order to main backward compatibility with our API HTTP
# response codes, we have this check here to deal with cases where
# a node is already being operated on (DEPLOYING or such) and we
# want to continue returning 409. Without it, we'd return 400.
if rpc_node.reservation:
raise exception.NodeLocked(node=rpc_node.uuid,
host=rpc_node.reservation)
raise exception.InvalidStateRequested(
action=target, node=rpc_node.uuid,
state=rpc_node.provision_state)
api_utils.check_allow_configdrive(target, configdrive)
api_utils.check_allow_clean_disable_ramdisk(target, disable_ramdisk)
if clean_steps and target != ir_states.VERBS['clean']:
msg = (_('"clean_steps" is only valid when setting target '
'provision state to %s') % ir_states.VERBS['clean'])
raise exception.ClientSideError(
msg, status_code=http_client.BAD_REQUEST)
api_utils.check_allow_deploy_steps(target, deploy_steps)
if (rescue_password is not None
and target != ir_states.VERBS['rescue']):
msg = (_('"rescue_password" is only valid when setting target '
'provision state to %s') % ir_states.VERBS['rescue'])
raise exception.ClientSideError(
msg, status_code=http_client.BAD_REQUEST)
if (rpc_node.provision_state == ir_states.INSPECTWAIT
and target == ir_states.VERBS['abort']):
if not api_utils.allow_inspect_abort():
raise exception.NotAcceptable()
self._do_provision_action(rpc_node, target, configdrive, clean_steps,
deploy_steps, rescue_password,
disable_ramdisk)
# Set the HTTP Location Header
url_args = '/'.join([node_ident, 'states'])
api.response.location = link.build_url('nodes', url_args)
def _check_clean_steps(clean_steps):
"""Ensure all necessary keys are present and correct in steps for clean
:param clean_steps: a list of steps. For more details, see the
clean_steps parameter of :func:`NodeStatesController.provision`.
:raises: InvalidParameterValue if validation of steps fails.
"""
_check_steps(clean_steps, 'clean', _CLEAN_STEPS_SCHEMA)
def _check_deploy_steps(deploy_steps):
"""Ensure all necessary keys are present and correct in steps for deploy
:param deploy_steps: a list of steps. For more details, see the
deploy_steps parameter of :func:`NodeStatesController.provision`.
:raises: InvalidParameterValue if validation of steps fails.
"""
_check_steps(deploy_steps, 'deploy', _DEPLOY_STEPS_SCHEMA)
def _check_steps(steps, step_type, schema):
"""Ensure all necessary keys are present and correct in steps.
Check that the user-specified steps are in the expected format and include
the required information.
:param steps: a list of steps. For more details, see the
clean_steps and deploy_steps parameter of
:func:`NodeStatesController.provision`.
:param step_type: 'clean' or 'deploy' step type
:param schema: JSON schema to use for validation.
:raises: InvalidParameterValue if validation of steps fails.
"""
try:
jsonschema.validate(steps, schema)
except jsonschema.ValidationError as exc:
raise exception.InvalidParameterValue(_('Invalid %s_steps: %s') %
(step_type, exc))
def _get_chassis_uuid(node):
"""Return the UUID of a node's chassis, or None.
:param node: a Node object.
:returns: the UUID of the node's chassis, or None if the node has no
chassis set.
"""
if not node.chassis_id:
return
chassis = objects.Chassis.get_by_id(api.request.context, node.chassis_id)
return chassis.uuid
def _replace_chassis_uuid_with_id(node_dict):
chassis_uuid = node_dict.pop('chassis_uuid', None)
if not chassis_uuid:
node_dict['chassis_id'] = None
return
try:
chassis = objects.Chassis.get_by_uuid(api.request.context,
chassis_uuid)
node_dict['chassis_id'] = chassis.id
except exception.ChassisNotFound as e:
# Change error code because 404 (NotFound) is inappropriate
# response for requests acting on nodes
e.code = http_client.BAD_REQUEST # BadRequest
raise
return chassis
def _make_trait_list(context, node_id, traits):
"""Return a TraitList object for the specified node and traits.
The Trait objects will not be created in the database.
:param context: a request context.
:param node_id: the ID of a node.
:param traits: a list of trait strings to add to the TraitList.
:returns: a TraitList object.
"""
trait_objs = [objects.Trait(context, node_id=node_id, trait=t)
for t in traits]
return objects.TraitList(context, objects=trait_objs)
class NodeTraitsController(rest.RestController):
def __init__(self, node_ident):
super(NodeTraitsController, self).__init__()
self.node_ident = node_ident
@METRICS.timer('NodeTraitsController.get_all')
@method.expose()
def get_all(self):
"""List node traits."""
node = api_utils.check_node_policy_and_retrieve(
'baremetal:node:traits:list', self.node_ident)
traits = objects.TraitList.get_by_node_id(api.request.context,
node.id)
return {'traits': traits.get_trait_names()}
@METRICS.timer('NodeTraitsController.put')
@method.expose(status_code=http_client.NO_CONTENT)
@method.body('body')
@args.validate(trait=args.schema(api_utils.TRAITS_SCHEMA),
body=args.schema(TRAITS_SCHEMA))
def put(self, trait=None, body=None):
"""Add a trait to a node.
:param trait: String value; trait to add to a node, or None. Mutually
exclusive with 'traits'. If not None, adds this trait to the node.
:param traits: List of Strings; traits to set for a node, or None.
Mutually exclusive with 'trait'. If not None, replaces the node's
traits with this list.
"""
context = api.request.context
node = api_utils.check_node_policy_and_retrieve(
'baremetal:node:traits:set', self.node_ident)
traits = None
if body and 'traits' in body:
traits = body['traits']
if (trait and traits is not None) or not (trait or traits is not None):
msg = _("A single node trait may be added via PUT "
"/v1/nodes/<node identifier>/traits/<trait> with no body, "
"or all node traits may be replaced via PUT "
"/v1/nodes/<node identifier>/traits with the list of "
"traits specified in the request body.")
raise exception.Invalid(msg)
if trait:
if api.request.body and api.request.json_body:
# Ensure PUT nodes/uuid1/traits/trait1 with a non-empty body
# fails.
msg = _("No body should be provided when adding a trait")
raise exception.Invalid(msg)
traits = [trait]
replace = False
new_traits = {t.trait for t in node.traits} | {trait}
else:
replace = True
new_traits = set(traits)
# Update the node's traits to reflect the desired state.
node.traits = _make_trait_list(context, node.id, sorted(new_traits))
node.obj_reset_changes()
chassis_uuid = _get_chassis_uuid(node)
notify.emit_start_notification(context, node, 'update',
chassis_uuid=chassis_uuid)
with notify.handle_error_notification(context, node, 'update',
chassis_uuid=chassis_uuid):
topic = api.request.rpcapi.get_topic_for(node)
api.request.rpcapi.add_node_traits(
context, node.id, traits, replace=replace, topic=topic)
notify.emit_end_notification(context, node, 'update',
chassis_uuid=chassis_uuid)
if not replace:
# For single traits, set the HTTP Location Header.
url_args = '/'.join((self.node_ident, 'traits', trait))
api.response.location = link.build_url('nodes', url_args)
@METRICS.timer('NodeTraitsController.delete')
@method.expose(status_code=http_client.NO_CONTENT)
@args.validate(trait=args.string)
def delete(self, trait=None):
"""Remove one or all traits from a node.
:param trait: String value; trait to remove from a node, or None. If
None, all traits are removed.
"""
context = api.request.context
node = api_utils.check_node_policy_and_retrieve(
'baremetal:node:traits:delete', self.node_ident)
if trait:
traits = [trait]
new_traits = {t.trait for t in node.traits} - {trait}
else:
traits = None
new_traits = set()
# Update the node's traits to reflect the desired state.
node.traits = _make_trait_list(context, node.id, sorted(new_traits))
node.obj_reset_changes()
chassis_uuid = _get_chassis_uuid(node)
notify.emit_start_notification(context, node, 'update',
chassis_uuid=chassis_uuid)
with notify.handle_error_notification(context, node, 'update',
chassis_uuid=chassis_uuid):
topic = api.request.rpcapi.get_topic_for(node)
try:
api.request.rpcapi.remove_node_traits(
context, node.id, traits, topic=topic)
except exception.NodeTraitNotFound:
# NOTE(hshiina): Internal node ID should not be exposed.
raise exception.NodeTraitNotFound(node_id=node.uuid,
trait=trait)
notify.emit_end_notification(context, node, 'update',
chassis_uuid=chassis_uuid)
def _get_fields_for_node_query(fields=None):
valid_fields = ['automated_clean',
'bios_interface',
'boot_interface',
'boot_mode',
'clean_step',
'conductor_group',
'console_enabled',
'console_interface',
'deploy_interface',
'deploy_step',
'description',
'driver',
'driver_info',
'driver_internal_info',
'extra',
'fault',
'inspection_finished_at',
'inspection_started_at',
'inspect_interface',
'instance_info',
'instance_uuid',
'last_error',
'lessee',
'maintenance',
'maintenance_reason',
'management_interface',
'name',
'network_data',
'network_interface',
'owner',
'power_interface',
'power_state',
'properties',
'protected',
'protected_reason',
'provision_state',
'provision_updated_at',
'raid_config',
'raid_interface',
'rescue_interface',
'reservation',
'resource_class',
'retired',
'retired_reason',
'secure_boot',
'storage_interface',
'target_power_state',
'target_provision_state',
'target_raid_config',
'traits',
'vendor_interface']
if not fields:
return valid_fields
else:
object_fields = fields[:]
api_fulfilled_fields = ['allocation_uuid', 'chassis_uuid',
'conductor']
for api_field in api_fulfilled_fields:
if api_field in object_fields:
object_fields.remove(api_field)
query_fields = ['uuid', 'traits'] + api_fulfilled_fields \
+ valid_fields
for field in fields:
if field not in query_fields:
msg = 'Field %s is not a valid field.' % field
raise exception.Invalid(msg)
return object_fields
def node_convert_with_links(rpc_node, fields=None, sanitize=True):
# NOTE(TheJulia): This takes approximately 10% of the time to
# collect and return requests to API consumer, specifically
# for the nova sync query which is the most intense overhead
# an integrated deployment can really face.
node = api_utils.object_to_dict(
rpc_node,
link_resource='nodes',
fields=_get_fields_for_node_query(fields))
if node.get('traits') is not None:
node['traits'] = rpc_node.traits.get_trait_names()
if (api_utils.allow_expose_conductors()
and (fields is None or 'conductor' in fields)):
# NOTE(kaifeng) It is possible a node gets orphaned in certain
# circumstances, set conductor to None in such case.
try:
host = api.request.rpcapi.get_conductor_for(rpc_node)
node['conductor'] = host
except (exception.NoValidHost, exception.TemporaryFailure):
LOG.debug('Currently there is no conductor servicing node '
'%(node)s.', {'node': rpc_node.uuid})
node['conductor'] = None
# If allocations ever become the primary use path, this absolutely
# needs to become a join. :\
if (api_utils.allow_allocations()
and (fields is None or 'allocation_uuid' in fields)):
node['allocation_uuid'] = None
if rpc_node.allocation_id:
try:
allocation = objects.Allocation.get_by_id(
api.request.context,
rpc_node.allocation_id)
node['allocation_uuid'] = allocation.uuid
except exception.AllocationNotFound:
pass
if fields is None or 'chassis_uuid' in fields:
node['chassis_uuid'] = _get_chassis_uuid(rpc_node)
if fields is not None:
api_utils.check_for_invalid_fields(
fields, set(node))
show_states_links = (
api_utils.allow_links_node_states_and_driver_properties())
show_portgroups = api_utils.allow_portgroups_subcontrollers()
show_volume = api_utils.allow_volume()
url = api.request.public_url
if fields is None:
node['ports'] = [link.make_link('self', url, 'nodes',
node['uuid'] + "/ports"),
link.make_link('bookmark', url, 'nodes',
node['uuid'] + "/ports",
bookmark=True)]
if show_states_links:
node['states'] = [link.make_link('self', url, 'nodes',
node['uuid'] + "/states"),
link.make_link('bookmark', url, 'nodes',
node['uuid'] + "/states",
bookmark=True)]
if show_portgroups:
node['portgroups'] = [
link.make_link('self', url, 'nodes',
node['uuid'] + "/portgroups"),
link.make_link('bookmark', url, 'nodes',
node['uuid'] + "/portgroups",
bookmark=True)]
if show_volume:
node['volume'] = [
link.make_link('self', url, 'nodes',
node['uuid'] + "/volume"),
link.make_link('bookmark', url, 'nodes',
node['uuid'] + "/volume",
bookmark=True)]
if not sanitize:
return node
node_sanitize(node, fields)
return node
def node_sanitize(node, fields, cdict=None,
show_driver_secrets=None,
show_instance_secrets=None,
evaluate_additional_policies=None):
"""Removes sensitive and unrequested data.
Will only keep the fields specified in the ``fields`` parameter.
:param fields:
list of fields to preserve, or ``None`` to preserve them all
:type fields: list of str
:param cdict: Context dictionary for policy values evaluation.
If not provided, it will be executed by the method,
however for enumerating node lists, it is more efficent
to provide.
:param show_driver_secrets: A boolean value to allow external single
evaluation of policy instead of once per
node. Default None.
:param show_instance_secrets: A boolean value to allow external
evaluation of policy instead of once
per node. Default None.
:param evaluate_additional_policies: A boolean value to allow external
evaluation of policy instead of once
per node. Default None.
"""
# NOTE(TheJulia): As of ironic 18.0, this method is about 88% of
# the time spent preparing to return a node to. If it takes us
# ~ 4.5 seconds to get 1000 nodes, we spend approximately 4 seconds
# PER 1000 in this call. When the calling method provides
# cdict, show_driver_secrets, show_instance_secrets, and
# evaluate_additional_policies, then performance increases
# in excess of 200% as policy checks are costly.
if not cdict:
cdict = api.request.context.to_policy_values()
# We need a new target_dict for each node as owner/lessee field have
# explicit associations and target comparison.
target_dict = dict(cdict)
# These fields are node specific.
owner = node.get('owner')
lessee = node.get('lessee')
if owner:
target_dict['node.owner'] = owner
if lessee:
target_dict['node.lessee'] = lessee
# Scrub the dictionary's contents down to what was requested.
api_utils.sanitize_dict(node, fields)
# NOTE(tenbrae): the 'show_password' policy setting name exists for
# legacy purposes and can not be changed. Changing it will
# cause upgrade problems for any operators who have
# customized the value of this field
# NOTE(TheJulia): These methods use policy.check and normally return
# False in a noauth or password auth based situation, because the
# effective caller doesn't match the policy check rule.
if show_driver_secrets is None:
show_driver_secrets = policy.check("show_password",
cdict, target_dict)
if show_instance_secrets is None:
show_instance_secrets = policy.check("show_instance_secrets",
cdict, target_dict)
# TODO(TheJulia): The above checks need to be migrated in some direction,
# but until we have auditing clarity, it might not be a big deal.
# Determine if we need to do the additional checks. Keep in mind
# nova integrated with ironic is API read heavy, so it is ideal
# to keep the policy checks for say system-member based roles to
# a minimum as they are likely the regular API users as well.
# Also, the default for the filter_threshold is system-member.
if evaluate_additional_policies is None:
evaluate_additional_policies = not policy.check_policy(
"baremetal:node:get:filter_threshold",
target_dict, cdict)
node_keys = node.keys()
if evaluate_additional_policies:
# Perform extended sanitization of nodes based upon policy
# baremetal:node:get:filter_threshold
_node_sanitize_extended(node, node_keys, target_dict, cdict)
if 'driver_info' in node_keys:
if (evaluate_additional_policies
and not policy.check("baremetal:node:get:driver_info",
target_dict, cdict)):
# Guard infrastructure intenral details from being visible.
node['driver_info'] = {
'content': '** Redacted - requires baremetal:node:get:'
'driver_info permission. **'}
if not show_driver_secrets:
node['driver_info'] = strutils.mask_dict_password(
node['driver_info'], "******")
if not show_instance_secrets and 'instance_info' in node_keys:
node['instance_info'] = strutils.mask_dict_password(
node['instance_info'], "******")
# NOTE(dtantsur): configdrive may be a dict
if node['instance_info'].get('configdrive'):
node['instance_info']['configdrive'] = "******"
# NOTE(tenbrae): agent driver may store a swift temp_url on the
# instance_info, which shouldn't be exposed to non-admin users.
# Now that ironic supports additional policies, we need to hide
# it here, based on this policy.
# Related to bug #1613903
if node['instance_info'].get('image_url'):
node['instance_info']['image_url'] = "******"
if node.get('driver_internal_info', {}).get('agent_secret_token'):
node['driver_internal_info']['agent_secret_token'] = "******"
if 'provision_state' in node_keys:
# Update legacy state data for provision state, but only if
# the key is present.
update_state_in_older_versions(node)
hide_fields_in_newer_versions(node)
show_states_links = (
api_utils.allow_links_node_states_and_driver_properties())
show_portgroups = api_utils.allow_portgroups_subcontrollers()
show_volume = api_utils.allow_volume()
if not show_volume:
node.pop('volume', None)
if not show_portgroups:
node.pop('portgroups', None)
if not show_states_links:
node.pop('states', None)
def _node_sanitize_extended(node, node_keys, target_dict, cdict):
# NOTE(TheJulia): The net effect of this is that by default,
# at least matching common/policy.py defaults. is these should
# be stripped out.
if ('last_error' in node_keys
and not policy.check("baremetal:node:get:last_error",
target_dict, cdict)):
# Guard the last error from being visible as it can contain
# hostnames revealing infrastucture internal details.
node['last_error'] = ('** Value Redacted - Requires '
'baremetal:node:get:last_error '
'permission. **')
if ('reservation' in node_keys
and not policy.check("baremetal:node:get:reservation",
target_dict, cdict)):
# Guard conductor names from being visible.
node['reservation'] = ('** Redacted - requires baremetal:'
'node:get:reservation permission. **')
if ('driver_internal_info' in node_keys
and not policy.check("baremetal:node:get:driver_internal_info",
target_dict, cdict)):
# Guard conductor names from being visible.
node['driver_internal_info'] = {
'content': '** Redacted - Requires baremetal:node:get:'
'driver_internal_info permission. **'}
def node_list_convert_with_links(nodes, limit, url, fields=None, **kwargs):
cdict = api.request.context.to_policy_values()
target_dict = dict(cdict)
sanitizer_args = {
'cdict': cdict,
'show_driver_secrets': policy.check("show_password", cdict,
target_dict),
'show_instance_secrets': policy.check("show_instance_secrets",
cdict, target_dict),
'evaluate_additional_policies': not policy.check_policy(
"baremetal:node:get:filter_threshold",
target_dict, cdict),
}
return collection.list_convert_with_links(
items=[node_convert_with_links(n, fields=fields,
sanitize=False)
for n in nodes],
item_name='nodes',
limit=limit,
url=url,
fields=fields,
sanitize_func=node_sanitize,
sanitizer_args=sanitizer_args,
**kwargs
)
class NodeVendorPassthruController(rest.RestController):
"""REST controller for VendorPassthru.
This controller allow vendors to expose a custom functionality in
the Ironic API. Ironic will merely relay the message from here to the
appropriate driver, no introspection will be made in the message body.
"""
_custom_actions = {
'methods': ['GET']
}
@METRICS.timer('NodeVendorPassthruController.methods')
@method.expose()
@args.validate(node_ident=args.uuid_or_name)
def methods(self, node_ident):
"""Retrieve information about vendor methods of the given node.
:param node_ident: UUID or logical name of a node.
:returns: dictionary with <vendor method name>:<method metadata>
entries.
:raises: NodeNotFound if the node is not found.
"""
rpc_node = api_utils.check_node_policy_and_retrieve(
'baremetal:node:vendor_passthru', node_ident)
# Raise an exception if node is not found
if rpc_node.driver not in _VENDOR_METHODS:
topic = api.request.rpcapi.get_topic_for(rpc_node)
ret = api.request.rpcapi.get_node_vendor_passthru_methods(
api.request.context, rpc_node.uuid, topic=topic)
_VENDOR_METHODS[rpc_node.driver] = ret
return _VENDOR_METHODS[rpc_node.driver]
@METRICS.timer('NodeVendorPassthruController._default')
@method.expose()
@method.body('data')
@args.validate(node_ident=args.uuid_or_name, method=args.string)
def _default(self, node_ident, method, data=None):
"""Call a vendor extension.
:param node_ident: UUID or logical name of a node.
:param method: name of the method in vendor driver.
:param data: body of data to supply to the specified method.
"""
rpc_node = api_utils.check_node_policy_and_retrieve(
'baremetal:node:vendor_passthru', node_ident)
# Raise an exception if node is not found
topic = api.request.rpcapi.get_topic_for(rpc_node)
resp = api_utils.vendor_passthru(rpc_node.uuid, method, topic,
data=data)
api.response.status_code = resp.status_code
return resp.obj
class NodeMaintenanceController(rest.RestController):
def _set_maintenance(self, rpc_node, maintenance_mode, reason=None):
context = api.request.context
rpc_node.maintenance = maintenance_mode
rpc_node.maintenance_reason = reason
notify.emit_start_notification(context, rpc_node, 'maintenance_set')
with notify.handle_error_notification(context, rpc_node,
'maintenance_set'):
try:
topic = api.request.rpcapi.get_topic_for(rpc_node)
except exception.NoValidHost as e:
e.code = http_client.BAD_REQUEST
raise
new_node = api.request.rpcapi.update_node(context, rpc_node,
topic=topic)
notify.emit_end_notification(context, new_node, 'maintenance_set')
@METRICS.timer('NodeMaintenanceController.put')
@method.expose(status_code=http_client.ACCEPTED)
@args.validate(node_ident=args.uuid_or_name, reason=args.string)
def put(self, node_ident, reason=None):
"""Put the node in maintenance mode.
:param node_ident: the UUID or logical_name of a node.
:param reason: Optional, the reason why it's in maintenance.
"""
rpc_node = api_utils.check_node_policy_and_retrieve(
'baremetal:node:set_maintenance', node_ident)
self._set_maintenance(rpc_node, True, reason=reason)
@METRICS.timer('NodeMaintenanceController.delete')
@method.expose(status_code=http_client.ACCEPTED)
@args.validate(node_ident=args.uuid_or_name)
def delete(self, node_ident):
"""Remove the node from maintenance mode.
:param node_ident: the UUID or logical name of a node.
"""
rpc_node = api_utils.check_node_policy_and_retrieve(
'baremetal:node:clear_maintenance', node_ident)
self._set_maintenance(rpc_node, False)
class NodeVIFController(rest.RestController):
def __init__(self, node_ident):
self.node_ident = node_ident
def _get_node_and_topic(self, policy_name):
rpc_node = api_utils.check_node_policy_and_retrieve(
policy_name, self.node_ident)
try:
return rpc_node, api.request.rpcapi.get_topic_for(rpc_node)
except exception.NoValidHost as e:
e.code = http_client.BAD_REQUEST
raise
@METRICS.timer('NodeVIFController.get_all')
@method.expose()
def get_all(self):
"""Get a list of attached VIFs"""
rpc_node, topic = self._get_node_and_topic('baremetal:node:vif:list')
vifs = api.request.rpcapi.vif_list(api.request.context,
rpc_node.uuid, topic=topic)
return {'vifs': vifs}
@METRICS.timer('NodeVIFController.post')
@method.expose(status_code=http_client.NO_CONTENT)
@method.body('vif')
@args.validate(vif=VIF_VALIDATOR)
def post(self, vif):
"""Attach a VIF to this node
:param vif: a dictionary of information about a VIF.
It must have an 'id' key, whose value is a unique identifier
for that VIF.
"""
rpc_node, topic = self._get_node_and_topic('baremetal:node:vif:attach')
if api.request.version.minor >= versions.MINOR_67_NODE_VIF_ATTACH_PORT:
if 'port_uuid' in vif and 'portgroup_uuid' in vif:
msg = _("Cannot specify both port_uuid and portgroup_uuid")
raise exception.Invalid(msg)
api.request.rpcapi.vif_attach(api.request.context, rpc_node.uuid,
vif_info=vif, topic=topic)
@METRICS.timer('NodeVIFController.delete')
@method.expose(status_code=http_client.NO_CONTENT)
@args.validate(vif_id=args.uuid_or_name)
def delete(self, vif_id):
"""Detach a VIF from this node
:param vif_id: The ID of a VIF to detach
"""
rpc_node, topic = self._get_node_and_topic('baremetal:node:vif:detach')
api.request.rpcapi.vif_detach(api.request.context, rpc_node.uuid,
vif_id=vif_id, topic=topic)
class NodeHistoryController(rest.RestController):
detail_fields = ['uuid', 'created_at', 'severity', 'event_type',
'event', 'conductor', 'user']
standard_fields = ['uuid', 'created_at', 'severity', 'event']
def __init__(self, node_ident):
super(NodeHistoryController).__init__()
self.node_ident = node_ident
def _history_event_convert_with_links(self, node_uuid, event,
detail=False):
"""Add link and convert history event"""
url = api.request.public_url
if not detail:
fields = self.standard_fields
else:
fields = self.detail_fields
event_entry = api_utils.object_to_dict(
event,
link_resource='nodes',
fields=fields)
if not detail:
# The spec for this feature calls to truncate the event
# field if not detailed, which makes sense in some environments
# with many events, espescialy if the event text is particullarlly
# long.
entry_len = len(event_entry['event'])
if entry_len > 255:
event_entry['event'] = event_entry['event'][0:251] + '...'
else:
event_entry['event'] = event_entry['event'][0:entry_len]
# These records cannot be changed by the API consumer,
# and updated_at gets handed up from the db model
# regardless if we want it or not. As such, strip from
# the reply.
event_entry.pop('updated_at')
event_entry['links'] = [
link.make_link(
'self', url,
'nodes',
'%s/history/%s' % (node_uuid, event.uuid)
)
]
return event_entry
@METRICS.timer('NodeHistoryController.get_all')
@method.expose()
@args.validate(detail=args.boolean, marker=args.uuid, limit=args.integer)
def get_all(self, detail=False, marker=None, limit=None):
"""List node history."""
node = api_utils.check_node_policy_and_retrieve(
'baremetal:node:history:get', self.node_ident)
fields = self.detail_fields if detail else self.standard_fields
marker_obj = None
if marker:
marker_obj = objects.NodeHistory.get_by_uuid(api.request.context,
marker)
limit = api_utils.validate_limit(limit)
events = objects.NodeHistory.list_by_node_id(api.request.context,
node.id,
marker=marker_obj,
limit=limit)
return collection.list_convert_with_links(
items=[
self._history_event_convert_with_links(
node.uuid, event, detail=detail) for event in events
],
item_name='history',
url=f'nodes/{self.node_ident}/history',
fields=fields,
marker=marker_obj,
limit=limit,
)
@METRICS.timer('NodeHistoryController.get_one')
@method.expose()
@args.validate(event=args.uuid_or_name)
def get_one(self, event):
"""Get a node history entry"""
node = api_utils.check_node_policy_and_retrieve(
'baremetal:node:history:get', self.node_ident)
# TODO(TheJulia): Need to check policy to make sure if policy
# check fails, that the entry cannot be found.
event = objects.NodeHistory.get_by_uuid(api.request.context,
event)
return self._history_event_convert_with_links(
node.uuid, event, detail=True)
class NodesController(rest.RestController):
"""REST controller for Nodes."""
# NOTE(lucasagomes): For future reference. If we happen
# to need to add another sub-controller in this class let's
# try to make it a parameter instead of an endpoint due
# https://bugs.launchpad.net/ironic/+bug/1572651, e.g, instead of
# v1/nodes/(ident)/detail we could have v1/nodes/(ident)?detail=True
states = NodeStatesController()
"""Expose the state controller action as a sub-element of nodes"""
vendor_passthru = NodeVendorPassthruController()
"""A resource used for vendors to expose a custom functionality in
the API"""
management = NodeManagementController()
"""Expose management as a sub-element of nodes"""
maintenance = NodeMaintenanceController()
"""Expose maintenance as a sub-element of nodes"""
from_chassis = False
"""A flag to indicate if the requests to this controller are coming
from the top-level resource Chassis"""
_custom_actions = {
'detail': ['GET'],
'validate': ['GET'],
}
invalid_sort_key_list = ['properties', 'driver_info', 'extra',
'instance_info', 'driver_internal_info',
'clean_step', 'deploy_step',
'raid_config', 'target_raid_config',
'traits', 'network_data']
_subcontroller_map = {
'ports': port.PortsController,
'portgroups': portgroup.PortgroupsController,
'vifs': NodeVIFController,
'volume': volume.VolumeController,
'traits': NodeTraitsController,
'bios': bios.NodeBiosController,
'allocation': allocation.NodeAllocationController,
'history': NodeHistoryController,
}
@pecan.expose()
def _lookup(self, ident, *remainder):
if ident in self._subcontroller_map:
pecan.abort(http_client.NOT_FOUND)
try:
ident = args.uuid_or_name('node', ident)
except exception.InvalidParameterValue as e:
pecan.abort(http_client.BAD_REQUEST, e.args[0])
if not remainder:
return
if ((remainder[0] == 'portgroups'
and not api_utils.allow_portgroups_subcontrollers())
or (remainder[0] == 'vifs'
and not api_utils.allow_vifs_subcontroller())
or (remainder[0] == 'bios'
and not api_utils.allow_bios_interface())
or (remainder[0] == 'allocation'
and not api_utils.allow_allocations())
or (remainder[0] == 'history'
and not api_utils.allow_node_history())):
pecan.abort(http_client.NOT_FOUND)
if remainder[0] == 'traits' and not api_utils.allow_traits():
# NOTE(mgoddard): Returning here will ensure we exhibit the
# behaviour of previous releases for microversions without this
# endpoint.
return
subcontroller = self._subcontroller_map.get(remainder[0])
if subcontroller:
return subcontroller(node_ident=ident), remainder[1:]
def _filter_by_conductor(self, nodes, conductor):
filtered_nodes = []
for n in nodes:
try:
host = api.request.rpcapi.get_conductor_for(n)
if host == conductor:
filtered_nodes.append(n)
except (exception.NoValidHost, exception.TemporaryFailure):
# NOTE(kaifeng) Node gets orphaned in case some conductor
# offline or all conductors are offline.
pass
return filtered_nodes
def _get_nodes_collection(self, chassis_uuid, instance_uuid, associated,
maintenance, retired, provision_state, marker,
limit, sort_key, sort_dir, driver=None,
resource_class=None, resource_url=None,
fields=None, fault=None, conductor_group=None,
detail=None, conductor=None, owner=None,
lessee=None, project=None,
description_contains=None):
if self.from_chassis and not chassis_uuid:
raise exception.MissingParameterValue(
_("Chassis id not specified."))
limit = api_utils.validate_limit(limit)
sort_dir = api_utils.validate_sort_dir(sort_dir)
if sort_key in self.invalid_sort_key_list:
raise exception.InvalidParameterValue(
_("The sort_key value %(key)s is an invalid field for "
"sorting") % {'key': sort_key})
marker_obj = None
if marker:
marker_obj = objects.Node.get_by_uuid(api.request.context,
marker)
# The query parameters for the 'next' URL
parameters = {}
possible_filters = {
'maintenance': maintenance,
'chassis_uuid': chassis_uuid,
'associated': associated,
'provision_state': provision_state,
'driver': driver,
'resource_class': resource_class,
'fault': fault,
'conductor_group': conductor_group,
'owner': owner,
'lessee': lessee,
'project': project,
'description_contains': description_contains,
'retired': retired,
'instance_uuid': instance_uuid
}
filters = {}
for key, value in possible_filters.items():
if value is not None:
filters[key] = value
if fields:
obj_fields = fields[:]
required_object_fields = ('allocation_id', 'chassis_id',
'uuid', 'owner', 'lessee',
'created_at', 'updated_at')
for req_field in required_object_fields:
if req_field not in obj_fields:
obj_fields.append(req_field)
else:
# map the name for the call, as we did not pickup a specific
# list of fields to return.
obj_fields = fields
# NOTE(TheJulia): When a data set of the nodeds list is being
# requested, this method takes approximately 3-3.5% of the time
# when requesting specific fields aligning with Nova's sync
# process. (Local DB though)
nodes = objects.Node.list(api.request.context, limit, marker_obj,
sort_key=sort_key, sort_dir=sort_dir,
filters=filters, fields=obj_fields)
# Special filtering on results based on conductor field
if conductor:
nodes = self._filter_by_conductor(nodes, conductor)
parameters = {'sort_key': sort_key, 'sort_dir': sort_dir}
if associated:
parameters['associated'] = associated
if maintenance:
parameters['maintenance'] = maintenance
if retired:
parameters['retired'] = retired
if detail is not None:
parameters['detail'] = detail
if instance_uuid:
# NOTE(rloo) if limit==1 and len(nodes)==1 (see
# Collection.has_next()), a 'next' link will
# be generated, which we don't want.
# NOTE(TheJulia): This is done after the query as
# instance_uuid is a unique constraint in the DB
# and we cannot pass a limit of 0 to sqlalchemy
# and expect a response.
limit = 0
return node_list_convert_with_links(nodes, limit,
url=resource_url,
fields=fields,
**parameters)
def _check_names_acceptable(self, names, error_msg):
"""Checks all node 'name's are acceptable, it does not return a value.
This function will raise an exception for unacceptable names.
:param names: list of node names to check
:param error_msg: error message in case of exception.ClientSideError,
should contain %(name)s placeholder.
:raises: exception.NotAcceptable
:raises: exception.ClientSideError
"""
if not api_utils.allow_node_logical_names():
raise exception.NotAcceptable()
reserved_names = get_nodes_controller_reserved_names()
for name in names:
if not api_utils.is_valid_node_name(name):
raise exception.ClientSideError(
error_msg % {'name': name},
status_code=http_client.BAD_REQUEST)
if name in reserved_names:
raise exception.ClientSideError(
'The word "%(name)s" is reserved and can not be used as a '
'node name. Reserved words are: %(reserved)s.' %
{'name': name,
'reserved': ', '.join(reserved_names)},
status_code=http_client.BAD_REQUEST)
def _update_changed_fields(self, node, rpc_node):
"""Update rpc_node based on changed fields in a node.
"""
original_chassis_id = rpc_node.chassis_id
chassis = _replace_chassis_uuid_with_id(node)
# conductor_group is case-insensitive, and we use it to
# calculate the conductor to send an update too. lowercase
# it here instead of just before saving so we calculate
# correctly.
node['conductor_group'] = node['conductor_group'].lower()
# Node object protected field is not nullable
if node.get('protected') is None:
node['protected'] = False
# NOTE(mgoddard): Traits cannot be updated via a node PATCH.
api_utils.patch_update_changed_fields(
node, rpc_node,
fields=set(objects.Node.fields) - {'traits'},
schema=node_patch_schema(),
id_map={'chassis_id': chassis and chassis.id or None}
)
if original_chassis_id and not rpc_node.chassis_id:
if not api_utils.allow_remove_chassis_uuid():
raise exception.NotAcceptable()
def _check_driver_changed_and_console_enabled(self, rpc_node, node_ident):
"""Checks if the driver and the console is enabled in a node.
If it does, is necessary to prevent updating it because the new driver
will not be able to stop a console started by the previous one.
:param rpc_node: RPC Node object to be verified.
:param node_ident: the UUID or logical name of a node.
:raises: exception.ClientSideError
"""
delta = rpc_node.obj_what_changed()
if 'driver' in delta and rpc_node.console_enabled:
raise exception.ClientSideError(
_("Node %s can not update the driver while the console is "
"enabled. Please stop the console first.") % node_ident,
status_code=http_client.CONFLICT)
@METRICS.timer('NodesController.get_all')
@method.expose()
@args.validate(chassis_uuid=args.uuid, instance_uuid=args.uuid,
associated=args.boolean, maintenance=args.boolean,
retired=args.boolean, provision_state=args.string,
marker=args.uuid, limit=args.integer, sort_key=args.string,
sort_dir=args.string, driver=args.string,
fields=args.string_list, resource_class=args.string,
fault=args.string, conductor_group=args.string,
detail=args.boolean, conductor=args.string,
owner=args.string, description_contains=args.string,
lessee=args.string, project=args.string)
def get_all(self, chassis_uuid=None, instance_uuid=None, associated=None,
maintenance=None, retired=None, provision_state=None,
marker=None, limit=None, sort_key='id', sort_dir='asc',
driver=None, fields=None, resource_class=None, fault=None,
conductor_group=None, detail=None, conductor=None,
owner=None, description_contains=None, lessee=None,
project=None):
"""Retrieve a list of nodes.
:param chassis_uuid: Optional UUID of a chassis, to get only nodes for
that chassis.
:param instance_uuid: Optional UUID of an instance, to find the node
associated with that instance.
:param associated: Optional boolean whether to return a list of
associated or unassociated nodes. May be combined
with other parameters.
:param maintenance: Optional boolean value that indicates whether
to get nodes in maintenance mode ("True"), or not
in maintenance mode ("False").
:param retired: Optional boolean value that indicates whether
to get retired nodes.
:param provision_state: Optional string value to get only nodes in
that provision state.
:param marker: pagination marker for large data sets.
:param limit: maximum number of resources to return in a single result.
This value cannot be larger than the value of max_limit
in the [api] section of the ironic configuration, or only
max_limit resources will be returned.
:param sort_key: column to sort results by. Default: id.
:param sort_dir: direction to sort. "asc" or "desc". Default: asc.
:param driver: Optional string value to get only nodes using that
driver.
:param resource_class: Optional string value to get only nodes with
that resource_class.
:param conductor_group: Optional string value to get only nodes with
that conductor_group.
:param conductor: Optional string value to get only nodes managed by
that conductor.
:param owner: Optional string value that set the owner whose nodes
are to be retrurned.
:param lessee: Optional string value that set the lessee whose nodes
are to be returned.
:param project: Optional string value that set the project - lessee or
owner - whose nodes are to be returned.
:param fields: Optional, a list with a specified set of fields
of the resource to be returned.
:param fault: Optional string value to get only nodes with that fault.
:param description_contains: Optional string value to get only nodes
with description field contains matching
value.
"""
project = api_utils.check_list_policy('node', project)
api_utils.check_allow_specify_fields(fields)
api_utils.check_allowed_fields(fields)
api_utils.check_allowed_fields([sort_key])
api_utils.check_for_invalid_state_and_allow_filter(provision_state)
api_utils.check_allow_specify_driver(driver)
api_utils.check_allow_specify_resource_class(resource_class)
api_utils.check_allow_filter_by_fault(fault)
api_utils.check_allow_filter_by_conductor_group(conductor_group)
api_utils.check_allow_filter_by_conductor(conductor)
api_utils.check_allow_filter_by_owner(owner)
api_utils.check_allow_filter_by_lessee(lessee)
fields = api_utils.get_request_return_fields(fields, detail,
_DEFAULT_RETURN_FIELDS)
extra_args = {'description_contains': description_contains}
return self._get_nodes_collection(chassis_uuid, instance_uuid,
associated, maintenance, retired,
provision_state, marker,
limit, sort_key, sort_dir,
driver=driver,
resource_class=resource_class,
resource_url='nodes',
fields=fields, fault=fault,
conductor_group=conductor_group,
detail=detail,
conductor=conductor,
owner=owner, lessee=lessee,
project=project,
**extra_args)
@METRICS.timer('NodesController.detail')
@method.expose()
@args.validate(chassis_uuid=args.uuid, instance_uuid=args.uuid,
associated=args.boolean, maintenance=args.boolean,
retired=args.boolean, provision_state=args.string,
marker=args.uuid, limit=args.integer, sort_key=args.string,
sort_dir=args.string, driver=args.string,
resource_class=args.string, fault=args.string,
conductor_group=args.string, conductor=args.string,
owner=args.string, description_contains=args.string,
lessee=args.string, project=args.string)
def detail(self, chassis_uuid=None, instance_uuid=None, associated=None,
maintenance=None, retired=None, provision_state=None,
marker=None, limit=None, sort_key='id', sort_dir='asc',
driver=None, resource_class=None, fault=None,
conductor_group=None, conductor=None, owner=None,
description_contains=None, lessee=None, project=None):
"""Retrieve a list of nodes with detail.
:param chassis_uuid: Optional UUID of a chassis, to get only nodes for
that chassis.
:param instance_uuid: Optional UUID of an instance, to find the node
associated with that instance.
:param associated: Optional boolean whether to return a list of
associated or unassociated nodes. May be combined
with other parameters.
:param maintenance: Optional boolean value that indicates whether
to get nodes in maintenance mode ("True"), or not
in maintenance mode ("False").
:param retired: Optional boolean value that indicates whether
to get nodes which are retired.
:param provision_state: Optional string value to get only nodes in
that provision state.
:param marker: pagination marker for large data sets.
:param limit: maximum number of resources to return in a single result.
This value cannot be larger than the value of max_limit
in the [api] section of the ironic configuration, or only
max_limit resources will be returned.
:param sort_key: column to sort results by. Default: id.
:param sort_dir: direction to sort. "asc" or "desc". Default: asc.
:param driver: Optional string value to get only nodes using that
driver.
:param resource_class: Optional string value to get only nodes with
that resource_class.
:param fault: Optional string value to get only nodes with that fault.
:param conductor_group: Optional string value to get only nodes with
that conductor_group.
:param owner: Optional string value that set the owner whose nodes
are to be retrurned.
:param lessee: Optional string value that set the lessee whose nodes
are to be returned.
:param project: Optional string value that set the project - lessee or
owner - whose nodes are to be returned.
:param description_contains: Optional string value to get only nodes
with description field contains matching
value.
"""
project = api_utils.check_list_policy('node', project)
api_utils.check_for_invalid_state_and_allow_filter(provision_state)
api_utils.check_allow_specify_driver(driver)
api_utils.check_allow_specify_resource_class(resource_class)
api_utils.check_allow_filter_by_fault(fault)
api_utils.check_allow_filter_by_conductor_group(conductor_group)
api_utils.check_allow_filter_by_owner(owner)
api_utils.check_allow_filter_by_lessee(lessee)
api_utils.check_allowed_fields([sort_key])
# /detail should only work against collections
parent = api.request.path.split('/')[:-1][-1]
if parent != "nodes":
raise exception.HTTPNotFound()
api_utils.check_allow_filter_by_conductor(conductor)
extra_args = {'description_contains': description_contains}
return self._get_nodes_collection(chassis_uuid, instance_uuid,
associated, maintenance, retired,
provision_state, marker,
limit, sort_key, sort_dir,
driver=driver,
resource_class=resource_class,
resource_url='nodes/detail',
fault=fault,
conductor_group=conductor_group,
conductor=conductor,
owner=owner, lessee=lessee,
project=project,
**extra_args)
@METRICS.timer('NodesController.validate')
@method.expose()
@args.validate(node=args.uuid_or_name, node_uuid=args.uuid)
def validate(self, node=None, node_uuid=None):
"""Validate the driver interfaces, using the node's UUID or name.
Note that the 'node_uuid' interface is deprecated in favour
of the 'node' interface
:param node: UUID or name of a node.
:param node_uuid: UUID of a node.
"""
if node is not None:
# We're invoking this interface using positional notation, or
# explicitly using 'node'. Try and determine which one.
if (not api_utils.allow_node_logical_names()
and not uuidutils.is_uuid_like(node)):
raise exception.NotAcceptable()
rpc_node = api_utils.check_node_policy_and_retrieve(
'baremetal:node:validate', node_uuid or node)
topic = api.request.rpcapi.get_topic_for(rpc_node)
return api.request.rpcapi.validate_driver_interfaces(
api.request.context, rpc_node.uuid, topic)
@METRICS.timer('NodesController.get_one')
@method.expose()
@args.validate(node_ident=args.uuid_or_name, fields=args.string_list)
def get_one(self, node_ident, fields=None):
"""Retrieve information about the given node.
:param node_ident: UUID or logical name of a node.
:param fields: Optional, a list with a specified set of fields
of the resource to be returned.
"""
if self.from_chassis:
raise exception.OperationNotPermitted()
rpc_node = api_utils.check_node_policy_and_retrieve(
'baremetal:node:get', node_ident, with_suffix=True)
api_utils.check_allow_specify_fields(fields)
api_utils.check_allowed_fields(fields)
return node_convert_with_links(rpc_node, fields=fields)
@METRICS.timer('NodesController.post')
@method.expose(status_code=http_client.CREATED)
@method.body('node')
@args.validate(node=node_validator)
def post(self, node):
"""Create a new node.
:param node: a node within the request body.
**Example Node creation request:**
.. literalinclude::
../../../../api-ref/source/samples/node-create-request-dynamic.json
:language: javascript
"""
if self.from_chassis:
raise exception.OperationNotPermitted()
context = api.request.context
api_utils.check_policy('baremetal:node:create')
reject_fields_in_newer_versions(node)
# NOTE(tenbrae): get_topic_for checks if node.driver is in the hash
# ring and raises NoValidHost if it is not.
# We need to ensure that node has a UUID before it can
# be mapped onto the hash ring.
if not node.get('uuid'):
node['uuid'] = uuidutils.generate_uuid()
# NOTE(jroll) this is special-cased to "" and not None,
# because it is used in hash ring calculations
if not node.get('conductor_group'):
node['conductor_group'] = ''
if node.get('name') is not None:
error_msg = _("Cannot create node with invalid name '%(name)s'")
self._check_names_acceptable([node['name']], error_msg)
node['provision_state'] = api_utils.initial_node_provision_state()
if not node.get('resource_class'):
node['resource_class'] = CONF.default_resource_class
chassis = _replace_chassis_uuid_with_id(node)
chassis_uuid = chassis and chassis.uuid or None
new_node = objects.Node(context, **node)
try:
topic = api.request.rpcapi.get_topic_for(new_node)
except exception.NoValidHost as e:
# NOTE(tenbrae): convert from 404 to 400 because client can see
# list of available drivers and shouldn't request
# one that doesn't exist.
e.code = http_client.BAD_REQUEST
raise
notify.emit_start_notification(context, new_node, 'create',
chassis_uuid=chassis_uuid)
with notify.handle_error_notification(context, new_node, 'create',
chassis_uuid=chassis_uuid):
new_node = api.request.rpcapi.create_node(context,
new_node, topic)
# Set the HTTP Location Header
api.response.location = link.build_url('nodes', new_node.uuid)
api_node = node_convert_with_links(new_node)
chassis_uuid = api_node.get('chassis_uuid')
notify.emit_end_notification(context, new_node, 'create',
chassis_uuid=chassis_uuid)
return api_node
def _validate_patch(self, patch, reset_interfaces):
if self.from_chassis:
raise exception.OperationNotPermitted()
api_utils.patch_validate_allowed_fields(patch, PATCH_ALLOWED_FIELDS)
reject_patch_in_newer_versions(patch)
traits = api_utils.get_patch_values(patch, '/traits')
if traits:
msg = _("Cannot update node traits via node patch. Node traits "
"should be updated via the node traits API.")
raise exception.Invalid(msg)
driver = api_utils.get_patch_values(patch, '/driver')
if reset_interfaces and not driver:
msg = _("The reset_interfaces parameter can only be used when "
"changing the node's driver.")
raise exception.Invalid(msg)
description = api_utils.get_patch_values(patch, '/description')
if description and len(description[0]) > _NODE_DESCRIPTION_MAX_LENGTH:
msg = _("Cannot update node with description exceeding %s "
"characters") % _NODE_DESCRIPTION_MAX_LENGTH
raise exception.Invalid(msg)
network_data_fields = api_utils.get_patch_values(
patch, '/network_data')
for network_data in network_data_fields:
validate_network_data(network_data)
def _authorize_patch_and_get_node(self, node_ident, patch):
# deal with attribute-specific policy rules
policy_checks = []
generic_update = False
for p in patch:
if p['path'].startswith('/instance_info'):
policy_checks.append('baremetal:node:update_instance_info')
elif p['path'].startswith('/extra'):
policy_checks.append('baremetal:node:update_extra')
elif (p['path'].startswith('/automated_clean')
and strutils.bool_from_string(p['value'], default=None)
is False):
policy_checks.append('baremetal:node:disable_cleaning')
elif p['path'].startswith('/driver_info'):
policy_checks.append('baremetal:node:update:driver_info')
elif p['path'].startswith('/properties'):
policy_checks.append('baremetal:node:update:properties')
elif p['path'].startswith('/chassis_uuid'):
policy_checks.append('baremetal:node:update:chassis_uuid')
elif p['path'].startswith('/instance_uuid'):
policy_checks.append('baremetal:node:update:instance_uuid')
elif p['path'].startswith('/lessee'):
policy_checks.append('baremetal:node:update:lessee')
elif p['path'].startswith('/owner'):
policy_checks.append('baremetal:node:update:owner')
elif p['path'].startswith('/driver'):
policy_checks.append('baremetal:node:update:driver_interfaces')
elif ((p['path'].lstrip('/').rsplit(sep="_", maxsplit=1)[0]
in driver_base.ALL_INTERFACES)
and (p['path'].lstrip('/').rsplit(sep="_", maxsplit=1)[-1]
== "interface")):
# TODO(TheJulia): Replace the above check with something like
# elif (p['path'].lstrip('/').removesuffix('_interface')
# when the minimum supported version is Python 3.9.
policy_checks.append('baremetal:node:update:driver_interfaces')
elif p['path'].startswith('/network_data'):
policy_checks.append('baremetal:node:update:network_data')
elif p['path'].startswith('/conductor_group'):
policy_checks.append('baremetal:node:update:conductor_group')
elif p['path'].startswith('/name'):
policy_checks.append('baremetal:node:update:name')
elif p['path'].startswith('/retired'):
policy_checks.append('baremetal:node:update:retired')
else:
generic_update = True
# always do at least one check
if generic_update or not policy_checks:
policy_checks.append('baremetal:node:update')
return api_utils.check_multiple_node_policies_and_retrieve(
policy_checks, node_ident, with_suffix=True)
@METRICS.timer('NodesController.patch')
@method.expose()
@method.body('patch')
@args.validate(node_ident=args.uuid_or_name, reset_interfaces=args.boolean,
patch=args.patch)
def patch(self, node_ident, reset_interfaces=None, patch=None):
"""Update an existing node.
:param node_ident: UUID or logical name of a node.
:param reset_interfaces: whether to reset hardware interfaces to their
defaults. Only valid when updating the driver field.
:param patch: a json PATCH document to apply to this node.
"""
if (reset_interfaces is not None and not
api_utils.allow_reset_interfaces()):
raise exception.NotAcceptable()
self._validate_patch(patch, reset_interfaces)
context = api.request.context
rpc_node = self._authorize_patch_and_get_node(node_ident, patch)
remove_inst_uuid_patch = [{'op': 'remove', 'path': '/instance_uuid'}]
if rpc_node.maintenance and patch == remove_inst_uuid_patch:
LOG.debug('Removing instance uuid %(instance)s from node %(node)s',
{'instance': rpc_node.instance_uuid,
'node': rpc_node.uuid})
# Check if node is transitioning state, although nodes in some states
# can be updated.
elif (rpc_node.target_provision_state and rpc_node.provision_state
not in ir_states.UPDATE_ALLOWED_STATES):
msg = _("Node %s can not be updated while a state transition "
"is in progress.")
raise exception.ClientSideError(
msg % node_ident, status_code=http_client.CONFLICT)
elif (rpc_node.provision_state == ir_states.INSPECTING
and api_utils.allow_inspect_wait_state()):
msg = _('Cannot update node "%(node)s" while it is in state '
'"%(state)s".') % {'node': rpc_node.uuid,
'state': ir_states.INSPECTING}
raise exception.ClientSideError(msg,
status_code=http_client.CONFLICT)
elif api_utils.get_patch_values(patch, '/owner'):
# check if updating a provisioned node's owner is allowed
if rpc_node.provision_state == ir_states.ACTIVE:
try:
api_utils.check_owner_policy(
'node',
'baremetal:node:update_owner_provisioned',
rpc_node['owner'], rpc_node['lessee'])
except exception.HTTPForbidden:
msg = _('Cannot update owner of node "%(node)s" while it '
'is in state "%(state)s".') % {
'node': rpc_node.uuid,
'state': ir_states.ACTIVE}
raise exception.ClientSideError(
msg, status_code=http_client.CONFLICT)
# check if node has an associated allocation with an owner
if rpc_node.allocation_id:
try:
allocation = objects.Allocation.get_by_id(
context,
rpc_node.allocation_id)
if allocation.owner is not None:
msg = _('Cannot update owner of node "%(node)s" while '
'it is allocated to an allocation with an '
' owner.') % {'node': rpc_node.uuid}
raise exception.ClientSideError(
msg, status_code=http_client.CONFLICT)
except exception.AllocationNotFound:
pass
names = api_utils.get_patch_values(patch, '/name')
if len(names):
error_msg = (_("Node %s: Cannot change name to invalid name ")
% node_ident)
error_msg += "'%(name)s'"
self._check_names_acceptable(names, error_msg)
node_dict = rpc_node.as_dict()
# NOTE(lucasagomes):
# 1) Remove chassis_id because it's an internal value and
# not present in the API object
# 2) Add chassis_uuid
node_dict['chassis_uuid'] = _get_chassis_uuid(rpc_node)
node_dict = api_utils.apply_jsonpatch(node_dict, patch)
api_utils.patched_validate_with_schema(
node_dict, node_patch_schema(), node_patch_validator)
self._update_changed_fields(node_dict, rpc_node)
# NOTE(tenbrae): we calculate the rpc topic here in case node.driver
# has changed, so that update is sent to the
# new conductor, not the old one which may fail to
# load the new driver.
try:
topic = api.request.rpcapi.get_topic_for(rpc_node)
except exception.NoValidHost as e:
# NOTE(tenbrae): convert from 404 to 400 because client can see
# list of available drivers and shouldn't request
# one that doesn't exist.
e.code = http_client.BAD_REQUEST
raise
self._check_driver_changed_and_console_enabled(rpc_node, node_ident)
chassis_uuid = _get_chassis_uuid(rpc_node)
notify.emit_start_notification(context, rpc_node, 'update',
chassis_uuid=chassis_uuid)
with notify.handle_error_notification(context, rpc_node, 'update',
chassis_uuid=chassis_uuid):
new_node = api.request.rpcapi.update_node(context,
rpc_node, topic,
reset_interfaces)
api_node = node_convert_with_links(new_node)
chassis_uuid = api_node.get('chassis_uuid')
notify.emit_end_notification(context, new_node, 'update',
chassis_uuid=chassis_uuid)
return api_node
@METRICS.timer('NodesController.delete')
@method.expose(status_code=http_client.NO_CONTENT)
@args.validate(node_ident=args.uuid_or_name)
def delete(self, node_ident, *args):
"""Delete a node.
:param node_ident: UUID or logical name of a node.
"""
# occurs when deleting traits with an old API version
if args:
raise exception.NotFound()
if self.from_chassis:
raise exception.OperationNotPermitted()
context = api.request.context
rpc_node = api_utils.check_node_policy_and_retrieve(
'baremetal:node:delete', node_ident, with_suffix=True)
chassis_uuid = _get_chassis_uuid(rpc_node)
notify.emit_start_notification(context, rpc_node, 'delete',
chassis_uuid=chassis_uuid)
with notify.handle_error_notification(context, rpc_node, 'delete',
chassis_uuid=chassis_uuid):
try:
topic = api.request.rpcapi.get_topic_for(rpc_node)
except exception.NoValidHost as e:
e.code = http_client.BAD_REQUEST
raise
api.request.rpcapi.destroy_node(context, rpc_node.uuid, topic)
notify.emit_end_notification(context, rpc_node, 'delete',
chassis_uuid=chassis_uuid)
|