summaryrefslogtreecommitdiff
path: root/designate/central/service.py
blob: 77334dd0fa0304e77772bab1b9f5132d23895219 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
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
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
# Copyright 2012 Managed I.T.
# Copyright 2013 - 2014 Hewlett-Packard Development Company, L.P.
#
# Author: Kiall Mac Innes <kiall@managedit.ie>
#
# 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 random
from random import SystemRandom
import re
import signal
import string
import time

from dns import exception as dnsexception
from dns import zone as dnszone
from oslo_config import cfg
from oslo_log import log as logging
import oslo_messaging as messaging
from oslo_utils import timeutils

from designate.common import constants
from designate.common.decorators import lock
from designate.common.decorators import notification
from designate.common.decorators import rpc
from designate import coordination
from designate import dnsutils
from designate import exceptions
from designate import network_api
from designate import objects
from designate import policy
from designate import quota
from designate import scheduler
from designate import service
from designate import storage
from designate.storage import transaction
from designate.storage import transaction_shallow_copy
from designate import utils
from designate.worker import rpcapi as worker_rpcapi

LOG = logging.getLogger(__name__)


class Service(service.RPCService):
    RPC_API_VERSION = '6.7'

    target = messaging.Target(version=RPC_API_VERSION)

    def __init__(self):
        self.zone_lock_local = lock.ZoneLockLocal()
        self.notification_thread_local = notification.NotificationThreadLocal()

        self._scheduler = None
        self._storage = None
        self._quota = None

        super(Service, self).__init__(
            self.service_name, cfg.CONF['service:central'].topic,
            threads=cfg.CONF['service:central'].threads,
        )
        self.coordination = coordination.Coordination(
            self.service_name, self.tg, grouping_enabled=False
        )
        self.network_api = network_api.get_network_api(cfg.CONF.network_api)

    @property
    def scheduler(self):
        if not self._scheduler:
            # Get a scheduler instance
            self._scheduler = scheduler.get_scheduler(storage=self.storage)
        return self._scheduler

    @property
    def quota(self):
        if not self._quota:
            # Get a quota manager instance
            self._quota = quota.get_quota()
        return self._quota

    @property
    def storage(self):
        if not self._storage:
            # Get a storage connection
            storage_driver = cfg.CONF['service:central'].storage_driver
            self._storage = storage.get_storage(storage_driver)
        return self._storage

    @property
    def service_name(self):
        return 'central'

    def start(self):
        if (cfg.CONF['service:central'].managed_resource_tenant_id ==
                "00000000-0000-0000-0000-000000000000"):
            LOG.warning("Managed Resource Tenant ID is not properly "
                        "configured")

        super(Service, self).start()
        self.coordination.start()

    def stop(self, graceful=True):
        self.coordination.stop()
        super(Service, self).stop(graceful)

    @property
    def worker_api(self):
        return worker_rpcapi.WorkerAPI.get_instance()

    def _is_valid_zone_name(self, context, zone_name):
        # Validate zone name length
        if zone_name is None:
            raise exceptions.InvalidObject

        if len(zone_name) > cfg.CONF['service:central'].max_zone_name_len:
            raise exceptions.InvalidZoneName('Name too long')

        # Break the zone name up into its component labels
        zone_labels = zone_name.strip('.').split('.')

        # We need more than 1 label.
        if len(zone_labels) <= 1:
            raise exceptions.InvalidZoneName('More than one label is '
                                             'required')

        tlds = self.storage.find_tlds(context)
        if tlds:
            LOG.debug("Checking if %s has a valid TLD", zone_name)
            allowed = False
            for i in range(-len(zone_labels), 0):
                last_i_labels = zone_labels[i:]
                LOG.debug("Checking %s against the TLD list", last_i_labels)
                if ".".join(last_i_labels) in tlds:
                    allowed = True
                    break
            if not allowed:
                raise exceptions.InvalidZoneName('Invalid TLD')

            # Now check that the zone name is not the same as a TLD
            try:
                stripped_zone_name = zone_name.rstrip('.').lower()
                self.storage.find_tld(
                    context,
                    {'name': stripped_zone_name})
            except exceptions.TldNotFound:
                LOG.debug("%s has a valid TLD", zone_name)
            else:
                raise exceptions.InvalidZoneName(
                    'Zone name cannot be the same as a TLD')

        # Check zone name blacklist
        if self._is_blacklisted_zone_name(context, zone_name):
            # Some users are allowed bypass the blacklist.. Is this one?
            if not policy.check('use_blacklisted_zone', context,
                                do_raise=False):
                raise exceptions.InvalidZoneName('Blacklisted zone name')

        return True

    def _is_valid_recordset_name(self, context, zone, recordset_name):
        if recordset_name is None:
            raise exceptions.InvalidObject

        if not recordset_name.endswith('.'):
            raise ValueError('Please supply a FQDN')

        # Validate record name length
        max_len = cfg.CONF['service:central'].max_recordset_name_len
        if len(recordset_name) > max_len:
            raise exceptions.InvalidRecordSetName('Name too long')

        # RecordSets must be contained in the parent zone
        if (recordset_name != zone['name'] and
                not recordset_name.endswith("." + zone['name'])):
            raise exceptions.InvalidRecordSetLocation(
                'RecordSet is not contained within it\'s parent zone')

    def _is_valid_recordset_placement(self, context, zone, recordset_name,
                                      recordset_type, recordset_id=None):
        # CNAME's must not be created at the zone apex.
        if recordset_type == 'CNAME' and recordset_name == zone.name:
            raise exceptions.InvalidRecordSetLocation(
                'CNAME recordsets may not be created at the zone apex')

        # CNAME's must not share a name with other recordsets
        criterion = {
            'zone_id': zone.id,
            'name': recordset_name,
        }

        if recordset_type != 'CNAME':
            criterion['type'] = 'CNAME'

        recordsets = self.storage.find_recordsets(context, criterion)

        if ((len(recordsets) == 1 and recordsets[0].id != recordset_id) or
                len(recordsets) > 1):
            raise exceptions.InvalidRecordSetLocation(
                'CNAME recordsets may not share a name with any other records')

        return True

    def _is_valid_recordset_placement_subzone(self, context, zone,
                                              recordset_name,
                                              criterion=None):
        """
        Check that the placement of the requested rrset belongs to any of the
        zones subzones..
        """
        LOG.debug("Checking if %s belongs in any of %s subzones",
                  recordset_name, zone.name)

        criterion = criterion or {}

        context = context.elevated(all_tenants=True)

        if zone.name == recordset_name:
            return

        child_zones = self.storage.find_zones(
            context, {"parent_zone_id": zone.id})
        for child_zone in child_zones:
            try:
                self._is_valid_recordset_name(
                    context, child_zone, recordset_name)
            except Exception:
                continue
            else:
                msg = ('RecordSet belongs in a child zone: %s' %
                    child_zone['name'])
                raise exceptions.InvalidRecordSetLocation(msg)

    def _is_valid_recordset_records(self, recordset):
        """
        Check to make sure that the records in the recordset
        follow the rules, and won't blow up on the nameserver.
        """
        try:
            recordset.records
        except (AttributeError, exceptions.RelationNotLoaded):
            pass
        else:
            if len(recordset.records) > 1 and recordset.type == 'CNAME':
                raise exceptions.BadRequest(
                    'CNAME recordsets may not have more than 1 record'
                )

    def _is_blacklisted_zone_name(self, context, zone_name):
        """
        Ensures the provided zone_name is not blacklisted.
        """
        blacklists = self.storage.find_blacklists(context)

        class Timeout(Exception):
            pass

        def _handle_timeout(signum, frame):
            raise Timeout()

        signal.signal(signal.SIGALRM, _handle_timeout)

        try:
            for blacklist in blacklists:
                signal.setitimer(signal.ITIMER_REAL, 0.02)

                try:
                    if bool(re.search(blacklist.pattern, zone_name)):
                        return True
                finally:
                    signal.setitimer(signal.ITIMER_REAL, 0)

        except Timeout:
            LOG.critical(
                'Blacklist regex (%(pattern)s) took too long to evaluate '
                'against zone name (%(zone_name)s',
                {
                    'pattern': blacklist.pattern,
                    'zone_name': zone_name
                })

            return True

        return False

    def _is_subzone(self, context, zone_name, pool_id):
        """
        Ensures the provided zone_name is the subzone
        of an existing zone (checks across all tenants)
        """
        context = context.elevated(all_tenants=True)

        # Break the name up into it's component labels
        labels = zone_name.split(".")

        criterion = {"pool_id": pool_id}

        i = 1

        # Starting with label #2, search for matching zone's in the database
        while (i < len(labels)):
            name = '.'.join(labels[i:])
            criterion["name"] = name
            try:
                zone = self.storage.find_zone(context, criterion)
            except exceptions.ZoneNotFound:
                i += 1
            else:
                return zone

        return False

    def _is_superzone(self, context, zone_name, pool_id):
        """
        Ensures the provided zone_name is the parent zone
        of an existing subzone (checks across all tenants)
        """
        context = context.elevated(all_tenants=True)

        # Create wildcard term to catch all subzones
        search_term = "%%.%(name)s" % {"name": zone_name}

        criterion = {'name': search_term, "pool_id": pool_id}
        subzones = self.storage.find_zones(context, criterion)

        return subzones

    def _is_valid_ttl(self, context, ttl):
        if ttl is None:
            return
        min_ttl = cfg.CONF['service:central'].min_ttl
        if min_ttl is not None and ttl < int(min_ttl):
            try:
                policy.check('use_low_ttl', context)
            except exceptions.Forbidden:
                raise exceptions.InvalidTTL('TTL is below the minimum: %s'
                                            % min_ttl)

    def _is_valid_project_id(self, project_id):
        if project_id is None:
            raise exceptions.MissingProjectID(
                "A project ID must be specified when not using a project "
                "scoped token.")

    # SOA Recordset Methods
    @staticmethod
    def _build_soa_record(zone, ns_records):
        return '%s %s. %d %d %d %d %d' % (
            ns_records[0]['hostname'],
            zone['email'].replace('@', '.'),
            zone['serial'],
            zone['refresh'],
            zone['retry'],
            zone['expire'],
            zone['minimum']
        )

    def _create_soa(self, context, zone):
        pool_ns_records = self._get_pool_ns_records(context, zone.pool_id)
        records = objects.RecordList(objects=[
            objects.Record(
                data=self._build_soa_record(zone, pool_ns_records),
                managed=True
            )
        ])
        return self._create_recordset_in_storage(
            context, zone,
            objects.RecordSet(
                name=zone['name'],
                type='SOA',
                records=records
            ), increment_serial=False
        )[0]

    def _update_soa(self, context, zone):
        # NOTE: We should not be updating SOA records when a zone is SECONDARY.
        if zone.type != 'PRIMARY':
            return

        # Get the pool for it's list of ns_records
        pool_ns_records = self._get_pool_ns_records(context, zone.pool_id)

        soa = self.find_recordset(
            context, criterion={
                'zone_id': zone['id'],
                'type': 'SOA'
            }
        )

        soa.records[0].data = self._build_soa_record(zone, pool_ns_records)

        self._update_recordset_in_storage(
            context, zone, soa, increment_serial=False
        )

    # NS Recordset Methods
    def _create_ns(self, context, zone, ns_records):
        # NOTE: We should not be creating NS records when a zone is SECONDARY.
        if zone.type != 'PRIMARY':
            return

        # Create an NS record for each server
        recordlist = objects.RecordList(objects=[
            objects.Record(data=r, managed=True) for r in ns_records])
        values = {
            'name': zone['name'],
            'type': 'NS',
            'records': recordlist
        }
        ns, zone = self._create_recordset_in_storage(
            context, zone, objects.RecordSet(**values),
            increment_serial=False
        )

        return ns

    def _add_ns(self, context, zone, ns_record):
        # Get NS recordset
        # If the zone doesn't have an NS recordset yet, create one
        try:
            recordset = self.find_recordset(
                context,
                criterion={
                    'zone_id': zone['id'],
                    'name': zone['name'],
                    'type': 'NS'
                }
            )
        except exceptions.RecordSetNotFound:
            self._create_ns(context, zone, [ns_record])
            return

        # Add new record to recordset based on the new nameserver
        recordset.records.append(
            objects.Record(data=ns_record, managed=True)
        )

        self._update_recordset_in_storage(context, zone, recordset,
                                          set_delayed_notify=True)

    def _delete_ns(self, context, zone, ns_record):
        recordset = self.find_recordset(
            context,
            criterion={
                'zone_id': zone['id'],
                'name': zone['name'],
                'type': 'NS'
            }
        )

        for record in list(recordset.records):
            if record.data == ns_record:
                recordset.records.remove(record)

        self._update_recordset_in_storage(context, zone, recordset,
                                          set_delayed_notify=True)

    # Quota Enforcement Methods
    def _enforce_zone_quota(self, context, tenant_id):
        criterion = {'tenant_id': tenant_id}
        count = self.storage.count_zones(context, criterion)

        # Check if adding one more zone would exceed the quota
        self.quota.limit_check(context, tenant_id, zones=count + 1)

    def _enforce_recordset_quota(self, context, zone):
        # Ensure the recordsets per zone quota is OK
        criterion = {'zone_id': zone.id}
        count = self.storage.count_recordsets(context, criterion)

        # Check if adding one more recordset would exceed the quota
        self.quota.limit_check(
            context, zone.tenant_id, zone_recordsets=count + 1)

    def _enforce_record_quota(self, context, zone, recordset):
        # Quotas don't apply to managed records.
        if recordset.managed:
            return

        # Ensure the records per zone quota is OK
        zone_criterion = {
            'zone_id': zone.id,
            'managed': False,  # only include non-managed records
        }

        zone_records = self.storage.count_records(context, zone_criterion)

        recordset_criterion = {
            'recordset_id': recordset.id,
            'managed': False,  # only include non-managed records
        }
        recordset_records = self.storage.count_records(
            context, recordset_criterion)

        # We need to check the current number of zones + the
        # changes that add, so lets get +/- from our recordset
        # records based on the action
        adjusted_zone_records = (
            zone_records - recordset_records + len(recordset.records))

        self.quota.limit_check(context, zone.tenant_id,
                               zone_records=adjusted_zone_records)

        # Ensure the records per recordset quota is OK
        self.quota.limit_check(context, zone.tenant_id,
                               recordset_records=len(recordset.records))

    # Misc Methods
    @rpc.expected_exceptions()
    def get_absolute_limits(self, context):
        # NOTE(Kiall): Currently, we only have quota based limits..
        return self.quota.get_quotas(context, context.project_id)

    # Quota Methods
    @rpc.expected_exceptions()
    def get_quotas(self, context, tenant_id):
        if policy.enforce_new_defaults():
            target = {constants.RBAC_PROJECT_ID: tenant_id,
                      'all_tenants': context.all_tenants}
        else:
            target = {'tenant_id': tenant_id}
        policy.check('get_quotas', context, target)

        # TODO(johnsom) Deprecated since Wallaby, remove with legacy default
        #               policies. System scoped admin doesn't have a project_id
        if (tenant_id != context.project_id and not context.all_tenants and not
                policy.enforce_new_defaults()):
            raise exceptions.Forbidden()

        return self.quota.get_quotas(context, tenant_id)

    @rpc.expected_exceptions()
    @transaction
    def set_quota(self, context, tenant_id, resource, hard_limit):
        if policy.enforce_new_defaults():
            target = {
                constants.RBAC_PROJECT_ID: tenant_id,
                'resource': resource,
                'hard_limit': hard_limit,
            }
        else:
            target = {
                'tenant_id': tenant_id,
                'resource': resource,
                'hard_limit': hard_limit,
            }

        policy.check('set_quota', context, target)
        # TODO(johnsom) Deprecated since Wallaby, remove with legacy default
        #               policies. System scoped admin doesn't have a project_id
        if (tenant_id != context.project_id and not context.all_tenants and not
                policy.enforce_new_defaults()):
            raise exceptions.Forbidden()

        return self.quota.set_quota(context, tenant_id, resource, hard_limit)

    @transaction
    def reset_quotas(self, context, tenant_id):
        if policy.enforce_new_defaults():
            target = {constants.RBAC_PROJECT_ID: tenant_id}
        else:
            target = {'tenant_id': tenant_id}
        policy.check('reset_quotas', context, target)

        self.quota.reset_quotas(context, tenant_id)

    # TLD Methods
    @rpc.expected_exceptions()
    @notification.notify_type('dns.tld.create')
    @transaction
    def create_tld(self, context, tld):
        policy.check('create_tld', context)

        # The TLD is only created on central's storage and not on the backend.
        created_tld = self.storage.create_tld(context, tld)

        return created_tld

    @rpc.expected_exceptions()
    def find_tlds(self, context, criterion=None, marker=None, limit=None,
                  sort_key=None, sort_dir=None):
        policy.check('find_tlds', context)

        return self.storage.find_tlds(context, criterion, marker, limit,
                                      sort_key, sort_dir)

    @rpc.expected_exceptions()
    def get_tld(self, context, tld_id):
        policy.check('get_tld', context, {'tld_id': tld_id})

        return self.storage.get_tld(context, tld_id)

    @rpc.expected_exceptions()
    @notification.notify_type('dns.tld.update')
    @transaction
    def update_tld(self, context, tld):
        target = {
            'tld_id': tld.obj_get_original_value('id'),
        }
        policy.check('update_tld', context, target)

        tld = self.storage.update_tld(context, tld)

        return tld

    @rpc.expected_exceptions()
    @notification.notify_type('dns.tld.delete')
    @transaction
    def delete_tld(self, context, tld_id):
        policy.check('delete_tld', context, {'tld_id': tld_id})

        tld = self.storage.delete_tld(context, tld_id)

        return tld

    # TSIG Key Methods
    @rpc.expected_exceptions()
    @notification.notify_type('dns.tsigkey.create')
    @transaction
    def create_tsigkey(self, context, tsigkey):
        policy.check('create_tsigkey', context)

        created_tsigkey = self.storage.create_tsigkey(context, tsigkey)

        # TODO(Ron): this method needs to do more than update storage.

        return created_tsigkey

    @rpc.expected_exceptions()
    def find_tsigkeys(self, context, criterion=None, marker=None, limit=None,
                      sort_key=None, sort_dir=None):
        policy.check('find_tsigkeys', context)

        return self.storage.find_tsigkeys(context, criterion, marker,
                                          limit, sort_key, sort_dir)

    @rpc.expected_exceptions()
    def get_tsigkey(self, context, tsigkey_id):
        policy.check('get_tsigkey', context, {'tsigkey_id': tsigkey_id})

        return self.storage.get_tsigkey(context, tsigkey_id)

    @rpc.expected_exceptions()
    @notification.notify_type('dns.tsigkey.update')
    @transaction
    def update_tsigkey(self, context, tsigkey):
        target = {
            'tsigkey_id': tsigkey.obj_get_original_value('id'),
        }
        policy.check('update_tsigkey', context, target)

        tsigkey = self.storage.update_tsigkey(context, tsigkey)

        # TODO(Ron): this method needs to do more than update storage.

        return tsigkey

    @rpc.expected_exceptions()
    @notification.notify_type('dns.tsigkey.delete')
    @transaction
    def delete_tsigkey(self, context, tsigkey_id):
        policy.check('delete_tsigkey', context, {'tsigkey_id': tsigkey_id})

        tsigkey = self.storage.delete_tsigkey(context, tsigkey_id)

        # TODO(Ron): this method needs to do more than update storage.

        return tsigkey

    # Tenant Methods
    @rpc.expected_exceptions()
    def find_tenants(self, context):
        policy.check('find_tenants', context)
        return self.storage.find_tenants(context)

    @rpc.expected_exceptions()
    def get_tenant(self, context, tenant_id):
        if policy.enforce_new_defaults():
            target = {constants.RBAC_PROJECT_ID: tenant_id}
        else:
            target = {'tenant_id': tenant_id}

        policy.check('get_tenant', context, target)

        return self.storage.get_tenant(context, tenant_id)

    @rpc.expected_exceptions()
    def count_tenants(self, context):
        policy.check('count_tenants', context)
        return self.storage.count_tenants(context)

    # Zone Methods

    def _generate_soa_refresh_interval(self):
        """Generate a random refresh interval to stagger AXFRs across multiple
        zones and resolvers
        maximum val: default_soa_refresh_min
        minimum val: default_soa_refresh_max
        """
        assert cfg.CONF.default_soa_refresh_min is not None
        assert cfg.CONF.default_soa_refresh_max is not None
        dispersion = (cfg.CONF.default_soa_refresh_max -
                      cfg.CONF.default_soa_refresh_min) * random.random()
        refresh_time = cfg.CONF.default_soa_refresh_min + dispersion
        return int(refresh_time)

    def _get_pool_ns_records(self, context, pool_id):
        """Get pool ns_records using an elevated context and all_tenants = True

        :param pool_id: Pool ID
        :returns: ns_records
        """
        elevated_context = context.elevated(all_tenants=True)
        pool = self.storage.get_pool(elevated_context, pool_id)
        return pool.ns_records

    @rpc.expected_exceptions()
    @transaction
    @lock.synchronized_zone()
    def increment_zone_serial(self, context, zone):
        zone.serial = self.storage.increment_serial(context, zone.id)
        self._update_soa(context, zone)
        return zone.serial

    @rpc.expected_exceptions()
    @notification.notify_type('dns.domain.create')
    @notification.notify_type('dns.zone.create')
    @lock.synchronized_zone(new_zone=True)
    def create_zone(self, context, zone):
        """Create zone: perform checks and then call _create_zone()
        """

        # Default to creating in the current users tenant
        zone.tenant_id = zone.tenant_id or context.project_id

        if policy.enforce_new_defaults():
            target = {
                constants.RBAC_PROJECT_ID: zone.tenant_id,
                'zone_name': zone.name
            }
        else:
            target = {
                'tenant_id': zone.tenant_id,
                'zone_name': zone.name
            }

        policy.check('create_zone', context, target)

        self._is_valid_project_id(zone.tenant_id)

        # Ensure the tenant has enough quota to continue
        self._enforce_zone_quota(context, zone.tenant_id)

        # Ensure the zone name is valid
        self._is_valid_zone_name(context, zone.name)

        # Ensure TTL is above the minimum
        self._is_valid_ttl(context, zone.ttl)

        # Get a pool id
        zone.pool_id = self.scheduler.schedule_zone(context, zone)

        # Handle sub-zones appropriately
        parent_zone = self._is_subzone(
            context, zone.name, zone.pool_id)
        if parent_zone:
            if parent_zone.tenant_id == zone.tenant_id:
                # Record the Parent Zone ID
                zone.parent_zone_id = parent_zone.id
            else:
                raise exceptions.IllegalChildZone('Unable to create '
                                                  'subzone in another '
                                                  'tenants zone')

        # Handle super-zones appropriately
        subzones = self._is_superzone(context, zone.name, zone.pool_id)
        msg = ('Unable to create zone because another tenant owns a subzone '
               'of the zone')
        if subzones:
            LOG.debug("Zone '%s' is a superzone.", zone.name)
            for subzone in subzones:
                if subzone.tenant_id != zone.tenant_id:
                    raise exceptions.IllegalParentZone(msg)

        # If this succeeds, subzone parent IDs will be updated
        # after zone is created

        # NOTE(kiall): Fetch the servers before creating the zone, this way
        #              we can prevent zone creation if no servers are
        #              configured.

        pool_ns_records = self._get_pool_ns_records(context, zone.pool_id)
        if len(pool_ns_records) == 0:
            LOG.critical('No nameservers configured. Please create at least '
                         'one nameserver')
            raise exceptions.NoServersConfigured()

        # End of pre-flight checks, create zone
        return self._create_zone(context, zone, subzones)

    def _create_zone(self, context, zone, subzones):
        """Create zone straight away
        """

        if zone.type == 'SECONDARY' and zone.serial is None:
            zone.serial = 1

        # randomize the zone refresh time
        zone.refresh = self._generate_soa_refresh_interval()

        zone = self._create_zone_in_storage(context, zone)

        self.worker_api.create_zone(context, zone)

        if zone.type == 'SECONDARY':
            xfr_zone = copy.deepcopy(zone)
            xfr_zone.obj_reset_changes(recursive=True)
            self.worker_api.perform_zone_xfr(context, xfr_zone)

        # If zone is a superzone, update subzones
        # with new parent IDs
        for subzone in subzones:
            LOG.debug("Updating subzone '%s' parent ID using "
                      "superzone ID '%s'", subzone.name, zone.id)
            subzone.parent_zone_id = zone.id
            self.update_zone(context, subzone)

        return zone

    @transaction
    def _create_zone_in_storage(self, context, zone):

        zone.action = 'CREATE'
        zone.status = 'PENDING'

        zone = self.storage.create_zone(context, zone)
        pool_ns_records = self.get_zone_ns_records(context, zone['id'])

        # Create the SOA and NS recordsets for the new zone.  The SOA
        # record will always be the first 'created_at' record for a zone.
        self._create_soa(context, zone)
        self._create_ns(context, zone, [n.hostname for n in pool_ns_records])

        if zone.obj_attr_is_set('recordsets'):
            for rrset in zone.recordsets:
                # This allows eventlet to yield, as this looping operation
                # can be very long-lived.
                time.sleep(0)
                self._create_recordset_in_storage(
                    context, zone, rrset, increment_serial=False
                )

        return zone

    @rpc.expected_exceptions()
    def get_zone(self, context, zone_id, apply_tenant_criteria=True):
        """Get a zone, even if flagged for deletion
        """
        zone = self.storage.get_zone(
            context, zone_id, apply_tenant_criteria=apply_tenant_criteria)

        # Save a DB round trip if we don't need to check for shared
        zone_shared = False
        if (context.project_id != zone.tenant_id) and not context.all_tenants:
            zone_shared = self.storage.is_zone_shared_with_project(
                zone_id, context.project_id)
            if not zone_shared:
                # Maintain consistency with the previous API and _find_zones()
                # and _find() when apply_tenant_criteria is True.
                raise exceptions.ZoneNotFound(
                    "Could not find %s" % zone.obj_name())

        # TODO(johnsom) This should account for all-projects context
        # it passes today due to ADMIN
        if policy.enforce_new_defaults():
            target = {
                'zone_id': zone_id,
                'zone_name': zone.name,
                'zone_shared': zone_shared,
                constants.RBAC_PROJECT_ID: zone.tenant_id
            }
        else:
            target = {
                'zone_id': zone_id,
                'zone_name': zone.name,
                'zone_shared': zone_shared,
                'tenant_id': zone.tenant_id
            }

        policy.check('get_zone', context, target)

        return zone

    @rpc.expected_exceptions()
    def get_zone_ns_records(self, context, zone_id=None, criterion=None):

        if zone_id is None:
            policy.check('get_zone_ns_records', context)
            pool_id = cfg.CONF['service:central'].default_pool_id
        else:
            zone = self.storage.get_zone(context, zone_id)

            if policy.enforce_new_defaults():
                target = {
                    'zone_id': zone_id,
                    'zone_name': zone.name,
                    constants.RBAC_PROJECT_ID: zone.tenant_id
                }
            else:
                target = {
                    'zone_id': zone_id,
                    'zone_name': zone.name,
                    'tenant_id': zone.tenant_id
                }
            pool_id = zone.pool_id

            policy.check('get_zone_ns_records', context, target)

        # Need elevated context to get the pool
        elevated_context = context.elevated(all_tenants=True)

        # Get the pool for it's list of ns_records
        pool = self.storage.get_pool(elevated_context, pool_id)

        return pool.ns_records

    @rpc.expected_exceptions()
    def find_zones(self, context, criterion=None, marker=None, limit=None,
                   sort_key=None, sort_dir=None):
        """List existing zones including the ones flagged for deletion.
        """
        if policy.enforce_new_defaults():
            target = {constants.RBAC_PROJECT_ID: context.project_id}
        else:
            target = {'tenant_id': context.project_id}

        policy.check('find_zones', context, target)

        return self.storage.find_zones(context, criterion, marker, limit,
                                       sort_key, sort_dir)

    @rpc.expected_exceptions()
    @notification.notify_type('dns.domain.update')
    @notification.notify_type('dns.zone.update')
    @lock.synchronized_zone()
    def update_zone(self, context, zone, increment_serial=True):
        """Update zone. Perform checks and then call _update_zone()

        :returns: updated zone
        """
        if policy.enforce_new_defaults():
            target = {
                'zone_id': zone.obj_get_original_value('id'),
                'zone_name': zone.obj_get_original_value('name'),
                constants.RBAC_PROJECT_ID: (
                    zone.obj_get_original_value('tenant_id')),
            }
        else:
            target = {
                'zone_id': zone.obj_get_original_value('id'),
                'zone_name': zone.obj_get_original_value('name'),
                'tenant_id': zone.obj_get_original_value('tenant_id'),
            }

        policy.check('update_zone', context, target)

        changes = zone.obj_get_changes()

        # Ensure immutable fields are not changed
        if 'tenant_id' in changes:
            # TODO(kiall): Moving between tenants should be allowed, but the
            #              current code will not take into account that
            #              RecordSets and Records must also be moved.
            raise exceptions.BadRequest('Moving a zone between tenants is '
                                        'not allowed')

        if 'name' in changes:
            raise exceptions.BadRequest('Renaming a zone is not allowed')

        # Ensure TTL is above the minimum
        ttl = changes.get('ttl')
        self._is_valid_ttl(context, ttl)

        return self._update_zone(context, zone, increment_serial, changes)

    def _update_zone(self, context, zone, increment_serial, changes):
        """Update zone
        """
        zone = self._update_zone_in_storage(
            context, zone, increment_serial=increment_serial
        )

        # Fire off a XFR
        if 'masters' in changes:
            self.worker_api.perform_zone_xfr(context, zone)

        return zone

    @transaction
    def _update_zone_in_storage(self, context, zone,
                                increment_serial=True,
                                set_delayed_notify=False):
        zone.action = 'UPDATE'
        zone.status = 'PENDING'

        if increment_serial:
            zone.increment_serial = True
        if set_delayed_notify:
            zone.delayed_notify = True

        zone = self.storage.update_zone(context, zone)

        return zone

    @rpc.expected_exceptions()
    @notification.notify_type('dns.domain.delete')
    @notification.notify_type('dns.zone.delete')
    @lock.synchronized_zone()
    def delete_zone(self, context, zone_id):
        """Delete or abandon a zone
        On abandon, delete the zone from the DB immediately.
        Otherwise, set action to DELETE and status to PENDING and poke
        Pool Manager's "delete_zone" to update the resolvers. PM will then
        poke back to set action to NONE and status to DELETED
        """
        zone = self.storage.get_zone(context, zone_id)

        if policy.enforce_new_defaults():
            target = {
                'zone_id': zone_id,
                'zone_name': zone.name,
                constants.RBAC_PROJECT_ID: zone.tenant_id
            }
        else:
            target = {
                'zone_id': zone_id,
                'zone_name': zone.name,
                'tenant_id': zone.tenant_id
            }

        if hasattr(context, 'abandon') and context.abandon:
            policy.check('abandon_zone', context, target)
        else:
            policy.check('delete_zone', context, target)

        # Prevent the deletion of a shared zone if the delete-shares modifier
        # is not specified.
        if zone.shared and not context.delete_shares:
            raise exceptions.ZoneShared(
                'This zone is shared with other projects, please remove these '
                'shares before deletion or use the delete-shares modifier to '
                'override this warning.')

        # Prevent deletion of a zone which has child zones
        criterion = {'parent_zone_id': zone_id}

        # Look for child zones across all tenants with elevated context
        if self.storage.count_zones(context.elevated(all_tenants=True),
                                    criterion) > 0:
            raise exceptions.ZoneHasSubZone('Please delete any subzones '
                                            'before deleting this zone')

        # If the zone is shared and delete_shares was specified, remove all
        # of the zone shares in preparation for the zone delete.
        if zone.shared and context.delete_shares:
            self.storage.delete_zone_shares(zone.id)

        if hasattr(context, 'abandon') and context.abandon:
            LOG.info("Abandoning zone '%(zone)s'", {'zone': zone.name})
            zone = self.storage.delete_zone(context, zone.id)
        else:
            zone = self._delete_zone_in_storage(context, zone)
            delete_zonefile = False
            if context.hard_delete:
                delete_zonefile = True
            self.worker_api.delete_zone(context, zone,
                                        hard_delete=delete_zonefile)

        return zone

    @transaction
    def _delete_zone_in_storage(self, context, zone):
        """Set zone action to DELETE and status to PENDING
        to have the zone soft-deleted later on
        """

        zone.action = 'DELETE'
        zone.status = 'PENDING'

        zone = self.storage.update_zone(context, zone)

        return zone

    @rpc.expected_exceptions()
    def purge_zones(self, context, criterion, limit=None):
        """Purge deleted zones.
        :returns: number of purged zones
        """

        policy.check('purge_zones', context, criterion)

        LOG.debug("Performing purge with limit of %r and criterion of %r",
                  limit, criterion)

        return self.storage.purge_zones(context, criterion, limit)

    @rpc.expected_exceptions()
    def xfr_zone(self, context, zone_id):
        zone = self.storage.get_zone(context, zone_id)

        if policy.enforce_new_defaults():
            target = {
                'zone_id': zone_id,
                'zone_name': zone.name,
                constants.RBAC_PROJECT_ID: zone.tenant_id
            }
        else:
            target = {
                'zone_id': zone_id,
                'zone_name': zone.name,
                'tenant_id': zone.tenant_id
            }

        policy.check('xfr_zone', context, target)

        if zone.type != 'SECONDARY':
            msg = "Can't XFR a non Secondary zone."
            raise exceptions.BadRequest(msg)

        # Ensure the format of the servers are correct, then poll the
        # serial
        srv = random.choice(zone.masters)
        status, serial = self.worker_api.get_serial_number(
            context, zone, srv.host, srv.port)

        # Perform XFR if serial's are not equal
        if serial is not None and serial > zone.serial:
            LOG.info("Serial %(srv_serial)d is not equal to zone's "
                     "%(serial)d, performing AXFR",
                     {"srv_serial": serial, "serial": zone.serial})
            self.worker_api.perform_zone_xfr(context, zone)

    @rpc.expected_exceptions()
    def count_zones(self, context, criterion=None):
        if criterion is None:
            criterion = {}

        if policy.enforce_new_defaults():
            target = {
                constants.RBAC_PROJECT_ID: criterion.get('tenant_id', None)
            }
        else:
            target = {
                'tenant_id': criterion.get('tenant_id', None)
            }

        policy.check('count_zones', context, target)

        return self.storage.count_zones(context, criterion)

    # Report combining all the count reports based on criterion
    @rpc.expected_exceptions()
    def count_report(self, context, criterion=None):
        reports = []

        if criterion is None:
            # Get all the reports
            reports.append({'zones': self.count_zones(context),
                            'records': self.count_records(context),
                            'tenants': self.count_tenants(context)})

        elif criterion == 'zones':
            reports.append({'zones': self.count_zones(context)})

        elif criterion == 'zones_delayed_notify':
            num_zones = self.count_zones(context, criterion=dict(
                delayed_notify=True))
            reports.append({'zones_delayed_notify': num_zones})

        elif criterion == 'records':
            reports.append({'records': self.count_records(context)})

        elif criterion == 'tenants':
            reports.append({'tenants': self.count_tenants(context)})

        else:
            raise exceptions.ReportNotFound()

        return reports

    # Shared zones
    @rpc.expected_exceptions()
    @notification.notify_type('dns.zone.share')
    @transaction
    def share_zone(self, context, zone_id, shared_zone):
        # Ensure that zone exists and get the zone owner
        zone = self.storage.get_zone(context, zone_id)

        if policy.enforce_new_defaults():
            target = {constants.RBAC_PROJECT_ID: zone.tenant_id}
        else:
            target = {'tenant_id': zone.tenant_id}

        policy.check('share_zone', context, target)

        if zone.tenant_id == shared_zone.target_project_id:
            raise exceptions.BadRequest(
                'Cannot share the zone with the zone owner.')

        shared_zone['project_id'] = context.project_id
        shared_zone['zone_id'] = zone_id

        shared_zone = self.storage.share_zone(context, shared_zone)

        return shared_zone

    @rpc.expected_exceptions()
    @notification.notify_type('dns.zone.unshare')
    @transaction
    def unshare_zone(self, context, zone_id, zone_share_id):
        # Ensure the share exists and get the share owner
        shared_zone = self.get_shared_zone(context, zone_id, zone_share_id)

        if policy.enforce_new_defaults():
            target = {constants.RBAC_PROJECT_ID: shared_zone.project_id}
        else:
            target = {'tenant_id': shared_zone.project_id}

        policy.check('unshare_zone', context, target)

        # Prevent unsharing of a zone which has child zones in other tenants
        criterion = {
            'parent_zone_id': shared_zone.zone_id,
            'tenant_id': "%s" % shared_zone.target_project_id,
        }

        # Look for child zones across all tenants with elevated context
        if self.storage.count_zones(context.elevated(all_tenants=True),
                                    criterion) > 0:
            raise exceptions.SharedZoneHasSubZone(
                'Please delete all subzones owned by project %s '
                'before unsharing this zone' % shared_zone.target_project_id
            )

        # Prevent unsharing of a zone which has recordsets in other tenants
        criterion = {
            'zone_id': shared_zone.zone_id,
            'tenant_id': "%s" % shared_zone.target_project_id,
        }

        # Look for recordsets across all tenants with elevated context
        if self.storage.count_recordsets(
                context.elevated(all_tenants=True), criterion) > 0:
            raise exceptions.SharedZoneHasRecordSets(
                'Please delete all recordsets owned by project %s '
                'before unsharing this zone.' % shared_zone.target_project_id
            )

        shared_zone = self.storage.unshare_zone(
            context, zone_id, zone_share_id
        )

        return shared_zone

    @rpc.expected_exceptions()
    def find_shared_zones(self, context, criterion=None, marker=None,
                          limit=None, sort_key=None, sort_dir=None):

        # By default we will let any valid token through as the filter
        # criteria below will limit the scope of the results.
        policy.check('find_zone_shares', context)

        if not context.all_tenants and criterion:
            # Check that they are asking for another projects shares
            if policy.enforce_new_defaults():
                target = {constants.RBAC_PROJECT_ID: criterion.get(
                    'target_project_id', context.project_id)}
            else:
                target = {'tenant_id': criterion.get('target_project_id',
                                                     context.project_id)}

            policy.check('find_project_zone_share', context, target)

        shared_zones = self.storage.find_shared_zones(
            context, criterion, marker, limit, sort_key, sort_dir
        )

        return shared_zones

    @rpc.expected_exceptions()
    def get_shared_zone(self, context, zone_id, zone_share_id):
        # Ensure that share exists and get the share owner
        zone_share = self.storage.get_shared_zone(
            context, zone_id, zone_share_id)

        if policy.enforce_new_defaults():
            target = {constants.RBAC_PROJECT_ID: zone_share.project_id}
        else:
            target = {'tenant_id': zone_share.project_id}

        policy.check('get_zone_share', context, target)

        return zone_share

    def _check_zone_share_permission(self, context, zone):
        """
        Check if a request is acceptable for the requesting project ID.
        If the requestor is not the zone owner and the zone is not shared
        with them, return a 404 Not Found to match previous API versions.
        Otherwise, the later RBAC check will raise a 403 Forbidden.

        :param context: The security context for the request.
        :param zone: The zone the request is against.
        :return: If the zone is shared with the requesting project ID or not.
        """
        zone_shared = False
        if (context.project_id != zone.tenant_id) and not context.all_tenants:
            zone_shared = self.storage.is_zone_shared_with_project(
                zone.id, context.project_id)
            if not zone_shared:
                # Maintain consistency with the previous API and _find_zones()
                # and _find() when apply_tenant_criteria is True.
                raise exceptions.ZoneNotFound(
                    "Could not find %s" % zone.obj_name())
        return zone_shared

    # RecordSet Methods
    @rpc.expected_exceptions()
    @notification.notify_type('dns.recordset.create')
    def create_recordset(self, context, zone_id, recordset,
                         increment_serial=True):
        zone = self.storage.get_zone(context, zone_id,
                                     apply_tenant_criteria=False)

        # Note this call must follow the get_zone call to maintain API response
        # code behavior.
        zone_shared = self._check_zone_share_permission(context, zone)

        # Don't allow updates to zones that are being deleted
        if zone.action == 'DELETE':
            raise exceptions.BadRequest('Can not update a deleting zone')

        if policy.enforce_new_defaults():
            target = {
                'zone_id': zone_id,
                'zone_name': zone.name,
                'zone_type': zone.type,
                'zone_shared': zone_shared,
                'recordset_name': recordset.name,
                constants.RBAC_PROJECT_ID: zone.tenant_id,
            }
        else:
            target = {
                'zone_id': zone_id,
                'zone_name': zone.name,
                'zone_type': zone.type,
                'zone_shared': zone_shared,
                'recordset_name': recordset.name,
                'tenant_id': zone.tenant_id,
            }

        policy.check('create_recordset', context, target)

        # Override the context to be all_tenants here as we have already
        # passed the RBAC check for this call and context checks in lower
        # layers will fail for shared zones.
        # TODO(johnsom) Remove once context checking is removed from the lower
        #               code layers.
        context = context.elevated(all_tenants=True)

        recordset, zone = self._create_recordset_in_storage(
            context, zone, recordset, increment_serial=increment_serial
        )

        recordset.zone_name = zone.name
        recordset.obj_reset_changes(['zone_name'])

        return recordset

    def _validate_recordset(self, context, zone, recordset):

        # See if we're validating an existing or new recordset
        recordset_id = None
        if hasattr(recordset, 'id'):
            recordset_id = recordset.id

        # Ensure TTL is above the minimum
        if not recordset_id:
            ttl = getattr(recordset, 'ttl', None)
        else:
            changes = recordset.obj_get_changes()
            ttl = changes.get('ttl', None)

        self._is_valid_ttl(context, ttl)

        # Ensure the recordset name and placement is valid
        self._is_valid_recordset_name(context, zone, recordset.name)

        self._is_valid_recordset_placement(
            context, zone, recordset.name, recordset.type, recordset_id)

        self._is_valid_recordset_placement_subzone(
            context, zone, recordset.name)

        # Validate the records
        self._is_valid_recordset_records(recordset)

    @transaction_shallow_copy
    def _create_recordset_in_storage(self, context, zone, recordset,
                                     increment_serial=True):
        # Ensure the tenant has enough quota to continue
        self._enforce_recordset_quota(context, zone)
        self._validate_recordset(context, zone, recordset)

        if recordset.obj_attr_is_set('records') and recordset.records:
            # Ensure the tenant has enough zone record quotas to
            # create new records
            self._enforce_record_quota(context, zone, recordset)

            for record in recordset.records:
                record.action = 'CREATE'
                record.status = 'PENDING'
                if not increment_serial:
                    record.serial = zone.serial
                else:
                    record.serial = timeutils.utcnow_ts()

        new_recordset = self.storage.create_recordset(context, zone.id,
                                                      recordset)
        if recordset.records and increment_serial:
            # update the zone's status and increment the serial
            zone = self._update_zone_in_storage(
                context, zone, increment_serial
            )

        # Return the zone too in case it was updated
        return new_recordset, zone

    @rpc.expected_exceptions()
    def get_recordset(self, context, zone_id, recordset_id):
        # apply_tenant_criteria=False here as we will gate visibility
        # with the RBAC rules below. This allows project that share the zone
        # to see all of the records of the zone.
        if zone_id:
            recordset = self.storage.find_recordset(
                context, criterion={'id': recordset_id, 'zone_id': zone_id},
                apply_tenant_criteria=False)
            zone = self.storage.get_zone(context, zone_id,
                                         apply_tenant_criteria=False)
            # Ensure the zone_id matches the record's zone_id
            if zone.id != recordset.zone_id:
                raise exceptions.RecordSetNotFound()
        else:
            recordset = self.storage.find_recordset(
                context, criterion={'id': recordset_id},
                apply_tenant_criteria=False)
            zone = self.storage.get_zone(context, recordset.zone_id,
                                         apply_tenant_criteria=False)

        # Note this call must follow the get_zone call to maintain API response
        # code behavior.
        zone_shared = self._check_zone_share_permission(context, zone)

        # TODO(johnsom) This should account for all_projects
        if policy.enforce_new_defaults():
            target = {
                'zone_id': zone.id,
                'zone_name': zone.name,
                'zone_shared': zone_shared,
                'recordset_id': recordset.id,
                constants.RBAC_PROJECT_ID: zone.tenant_id,
            }
        else:
            target = {
                'zone_id': zone.id,
                'zone_name': zone.name,
                'zone_shared': zone_shared,
                'recordset_id': recordset.id,
                'tenant_id': zone.tenant_id,
            }

        policy.check('get_recordset', context, target)

        recordset.zone_name = zone.name
        recordset.obj_reset_changes(['zone_name'])
        recordset = recordset

        return recordset

    @rpc.expected_exceptions()
    def find_recordsets(self, context, criterion=None, marker=None, limit=None,
                        sort_key=None, sort_dir=None, force_index=False):
        zone = None
        zone_shared = False

        if criterion and criterion.get('zone_id', None):
            # NOTE: We need to ensure the zone actually exists, otherwise
            # we may return deleted recordsets instead of a zone not found
            zone = self.get_zone(context, criterion['zone_id'],
                                 apply_tenant_criteria=False)
            # Note this call must follow the get_zone call to maintain API
            # response code behavior.
            zone_shared = self._check_zone_share_permission(context, zone)

        # TODO(johnsom) Fix this to be useful
        if policy.enforce_new_defaults():
            target = {constants.RBAC_PROJECT_ID: context.project_id}
        else:
            target = {'tenant_id': context.project_id}

        policy.check('find_recordsets', context, target)

        apply_tenant_criteria = True
        # NOTE(imalinovskiy): Show all recordsets for zone owner or if the zone
        #                     is shared with this project.
        if (zone and zone.tenant_id == context.project_id) or zone_shared:
            apply_tenant_criteria = False

        recordsets = self.storage.find_recordsets(
            context, criterion, marker, limit, sort_key, sort_dir, force_index,
            apply_tenant_criteria=apply_tenant_criteria)

        return recordsets

    @rpc.expected_exceptions()
    def find_recordset(self, context, criterion=None):

        # TODO(johnsom) Fix this to be useful
        if policy.enforce_new_defaults():
            target = {constants.RBAC_PROJECT_ID: context.project_id}
        else:
            target = {'tenant_id': context.project_id}
        policy.check('find_recordset', context, target)

        recordset = self.storage.find_recordset(context, criterion)

        return recordset

    @rpc.expected_exceptions()
    def export_zone(self, context, zone_id):
        zone = self.get_zone(context, zone_id)

        criterion = {'zone_id': zone_id}
        recordsets = self.storage.find_recordsets_export(context, criterion)

        return utils.render_template('export-zone.jinja2',
                                     zone=zone,
                                     recordsets=recordsets)

    @rpc.expected_exceptions()
    @notification.notify_type('dns.recordset.update')
    def update_recordset(self, context, recordset, increment_serial=True):
        zone_id = recordset.obj_get_original_value('zone_id')
        changes = recordset.obj_get_changes()

        # Ensure immutable fields are not changed
        if 'tenant_id' in changes:
            raise exceptions.BadRequest('Moving a recordset between tenants '
                                        'is not allowed')

        if 'zone_id' in changes or 'zone_name' in changes:
            raise exceptions.BadRequest('Moving a recordset between zones '
                                        'is not allowed')

        if 'type' in changes:
            raise exceptions.BadRequest('Changing a recordsets type is not '
                                        'allowed')

        zone = self.storage.get_zone(context, zone_id,
                                     apply_tenant_criteria=False)

        # Note this call must follow the get_zone call to maintain API response
        # code behavior.
        zone_shared = self._check_zone_share_permission(context, zone)

        # Don't allow updates to zones that are being deleted
        if zone.action == 'DELETE':
            raise exceptions.BadRequest('Can not update a deleting zone')

        # TODO(johnsom) This should account for all-projects context
        # it passes today due to ADMIN
        if policy.enforce_new_defaults():
            target = {
                'recordset_id': recordset.obj_get_original_value('id'),
                'recordset_project_id': recordset.obj_get_original_value(
                    'tenant_id'),
                'zone_id': recordset.obj_get_original_value('zone_id'),
                'zone_name': zone.name,
                'zone_shared': zone_shared,
                'zone_type': zone.type,
                constants.RBAC_PROJECT_ID: zone.tenant_id
            }
        else:
            target = {
                'recordset_id': recordset.obj_get_original_value('id'),
                'recordset_project_id': recordset.obj_get_original_value(
                    'tenant_id'),
                'zone_id': recordset.obj_get_original_value('zone_id'),
                'zone_name': zone.name,
                'zone_shared': zone_shared,
                'zone_type': zone.type,
                'tenant_id': zone.tenant_id
            }

        policy.check('update_recordset', context, target)

        if recordset.managed and not context.edit_managed_records:
            raise exceptions.BadRequest('Managed records may not be updated')

        # Override the context to be all_tenants here as we have already
        # passed the RBAC check for this call and context checks in lower
        # layers will fail for shared zones.
        # TODO(johnsom) Remove once context checking is removed from the lower
        #               code layers.
        context = context.elevated(all_tenants=True)

        recordset, zone = self._update_recordset_in_storage(
            context, zone, recordset, increment_serial=increment_serial)

        return recordset

    @transaction
    def _update_recordset_in_storage(self, context, zone, recordset,
                                     increment_serial=True,
                                     set_delayed_notify=False):

        self._validate_recordset(context, zone, recordset)

        if recordset.records:
            for record in recordset.records:
                if record.action == 'DELETE':
                    continue
                record.action = 'UPDATE'
                record.status = 'PENDING'
                if not increment_serial:
                    record.serial = zone.serial
                else:
                    record.serial = timeutils.utcnow_ts()

            # Ensure the tenant has enough zone record quotas to
            # create new records
            self._enforce_record_quota(context, zone, recordset)

        # Update the recordset
        new_recordset = self.storage.update_recordset(context, recordset)

        if increment_serial:
            # update the zone's status and increment the serial
            zone = self._update_zone_in_storage(
                context, zone,
                increment_serial=increment_serial,
                set_delayed_notify=set_delayed_notify)

        return new_recordset, zone

    @rpc.expected_exceptions()
    @notification.notify_type('dns.recordset.delete')
    def delete_recordset(self, context, zone_id, recordset_id,
                         increment_serial=True):
        # apply_tenant_criteria=False here as we will gate this delete
        # with the RBAC rules below. This allows the zone owner to delete
        # all of the recordsets of the zone.
        recordset = self.storage.find_recordset(
            context,
            {"id": recordset_id, "zone_id": zone_id},
            apply_tenant_criteria=False
        )
        zone = self.storage.get_zone(context, zone_id,
                                     apply_tenant_criteria=False)

        # Don't allow updates to zones that are being deleted
        if zone.action == 'DELETE':
            raise exceptions.BadRequest('Can not update a deleting zone')

        # TODO(johnsom) should handle all_projects
        if policy.enforce_new_defaults():
            target = {
                'zone_id': zone_id,
                'zone_name': zone.name,
                'zone_type': zone.type,
                'recordset_id': recordset.id,
                'recordset_project_id': recordset.tenant_id,
                constants.RBAC_PROJECT_ID: zone.tenant_id
            }
        else:
            target = {
                'zone_id': zone_id,
                'zone_name': zone.name,
                'zone_type': zone.type,
                'recordset_id': recordset.id,
                'recordset_project_id': recordset.tenant_id,
                'tenant_id': zone.tenant_id
            }

        policy.check('delete_recordset', context, target)

        if recordset.managed and not context.edit_managed_records:
            raise exceptions.BadRequest('Managed records may not be deleted')

        # Override the context to be all_tenants here as we have already
        # passed the RBAC check for this call.
        # TODO(johnsom) Remove once context checking is removed from the lower
        #               code layers.
        context = context.elevated(all_tenants=True)

        recordset, zone = self._delete_recordset_in_storage(
            context, zone, recordset, increment_serial=increment_serial)

        recordset.zone_name = zone.name
        recordset.obj_reset_changes(['zone_name'])

        return recordset

    @transaction
    def _delete_recordset_in_storage(self, context, zone, recordset,
                                     increment_serial=True):
        if recordset.records:
            for record in recordset.records:
                record.action = 'DELETE'
                record.status = 'PENDING'
                if not increment_serial:
                    record.serial = zone.serial
                else:
                    record.serial = timeutils.utcnow_ts()

        # Update the recordset's action/status and then delete it
        self.storage.update_recordset(context, recordset)

        if increment_serial:
            # update the zone's status and increment the serial
            zone = self._update_zone_in_storage(
                context, zone, increment_serial)

        new_recordset = self.storage.delete_recordset(context, recordset.id)

        return new_recordset, zone

    @rpc.expected_exceptions()
    def count_recordsets(self, context, criterion=None):
        if criterion is None:
            criterion = {}

        if policy.enforce_new_defaults():
            target = {
                constants.RBAC_PROJECT_ID: criterion.get('tenant_id', None)
            }
        else:
            target = {'tenant_id': criterion.get('tenant_id', None)}

        policy.check('count_recordsets', context, target)

        return self.storage.count_recordsets(context, criterion)

    # Record Methods
    @rpc.expected_exceptions()
    def find_records(self, context, criterion=None, marker=None, limit=None,
                     sort_key=None, sort_dir=None):

        if policy.enforce_new_defaults():
            target = {constants.RBAC_PROJECT_ID: context.project_id}
        else:
            target = {'tenant_id': context.project_id}
        policy.check('find_records', context, target)

        return self.storage.find_records(context, criterion, marker, limit,
                                         sort_key, sort_dir)

    @rpc.expected_exceptions()
    def count_records(self, context, criterion=None):
        if criterion is None:
            criterion = {}

        if policy.enforce_new_defaults():
            target = {
                constants.RBAC_PROJECT_ID: criterion.get('tenant_id', None)
            }
        else:
            target = {'tenant_id': criterion.get('tenant_id', None)}

        policy.check('count_records', context, target)
        return self.storage.count_records(context, criterion)

    def _determine_floatingips(self, context, fips, project_id=None):
        """
        Given the context or project, and fips it returns the valid
        floating ips either with an associated record or not. Deletes invalid
        records also.

        Returns a list of tuples with FloatingIPs and its Record.
        """
        project_id = project_id or context.project_id

        elevated_context = context.elevated(all_tenants=True,
                                            edit_managed_records=True)
        criterion = {
            'managed': True,
            'managed_resource_type': 'ptr:floatingip',
        }

        records = self.find_records(elevated_context, criterion)
        records = dict([(r['managed_extra'], r) for r in records])

        invalid = []
        data = {}
        # First populate the list of FIPS.
        for fip_key, fip_values in fips.items():
            # Check if the FIP has a record
            record = records.get(fip_values['address'])

            # NOTE: Now check if it's owned by the project that actually has
            # the FIP in the external service and if not invalidate it
            # (delete it) thus not returning it with in the tuple with the FIP,
            # but None.

            if record:
                record_project = record['managed_tenant_id']

                if record_project != project_id:
                    LOG.debug(
                        'Invalid FloatingIP %s belongs to %s but record '
                        'project %s', fip_key, project_id, record_project
                    )
                    invalid.append(record)
                    record = None
            data[fip_key] = (fip_values, record)

        return data, invalid

    def _invalidate_floatingips(self, context, records):
        """
        Utility method to delete a list of records.
        """
        if not records:
            return

        elevated_context = context.elevated(all_tenants=True,
                                            edit_managed_records=True)
        for record in records:
            LOG.debug('Deleting record %s for FIP %s',
                      record['id'], record['managed_resource_id'])
            self._delete_ptr_record(
                elevated_context, record.zone_id, record.recordset_id,
                record['id']
            )

    def _list_floatingips(self, context, region=None):
        data = self.network_api.list_floatingips(context, region=region)
        return self._list_to_dict(data, keys=['region', 'id'])

    def _list_to_dict(self, data, keys=None):
        if keys is None:
            keys = ['id']
        new = {}
        for i in data:
            key = tuple([i[key] for key in keys])
            new[key] = i
        return new

    def _get_floatingip(self, context, region, floatingip_id, fips):
        if (region, floatingip_id) not in fips:
            raise exceptions.NotFound(
                'FloatingIP %s in %s is not associated for project "%s"' % (
                    floatingip_id, region, context.project_id
                )
            )
        return fips[region, floatingip_id]

    # PTR ops
    @rpc.expected_exceptions()
    def list_floatingips(self, context):
        """
        List Floating IPs PTR

        A) We have service_catalog in the context and do a lookup using the
               token pr Neutron in the SC
        B) We lookup FIPs using the configured values for this deployment.
        """
        elevated_context = context.elevated(all_tenants=True,
                                            edit_managed_records=True)

        project_floatingips = self._list_floatingips(context)

        valid, invalid = self._determine_floatingips(
            elevated_context, project_floatingips
        )

        self._invalidate_floatingips(context, invalid)

        return self._create_floating_ip_list(context, valid)

    @rpc.expected_exceptions()
    def get_floatingip(self, context, region, floatingip_id):
        """
        Get Floating IP PTR
        """
        elevated_context = context.elevated(all_tenants=True)

        tenant_fips = self._list_floatingips(context, region=region)

        fip = self._get_floatingip(context, region, floatingip_id, tenant_fips)

        result = self._list_to_dict([fip], keys=['region', 'id'])

        valid, invalid = self._determine_floatingips(
            elevated_context, result
        )

        self._invalidate_floatingips(context, invalid)

        return self._create_floating_ip_list(context, valid)[0]

    def _set_floatingip_reverse(self, context, region, floatingip_id, values):
        """
        Set the FloatingIP's PTR record based on values.
        """

        elevated_context = context.elevated(all_tenants=True,
                                            edit_managed_records=True)

        project_fips = self._list_floatingips(context, region=region)

        fip = self._get_floatingip(
            context, region, floatingip_id, project_fips
        )

        zone_name = self.network_api.address_zone(fip['address'])

        try:
            zone = self.storage.find_zone(
                elevated_context, {'name': zone_name}
            )
        except exceptions.ZoneNotFound:
            LOG.info(
                'Creating zone for %(fip_id)s:%(region)s - %(fip_addr)s '
                'zone %(zonename)s',
                {
                    'fip_id': floatingip_id,
                    'region': region,
                    'fip_addr': fip['address'],
                    'zonename': zone_name
                })

            zone = self._create_ptr_zone(elevated_context, zone_name)

        record_name = self.network_api.address_name(fip['address'])
        recordset_values = {
            'name': record_name,
            'zone_id': zone['id'],
            'type': 'PTR',
            'ttl': values.get('ttl')
        }
        record_values = {
            'data': values['ptrdname'],
            'description': values['description'],
            'managed': True,
            'managed_extra': fip['address'],
            'managed_resource_id': floatingip_id,
            'managed_resource_region': region,
            'managed_resource_type': 'ptr:floatingip',
            'managed_tenant_id': context.project_id
        }
        record = objects.Record(**record_values)
        recordset = self._replace_or_create_ptr_recordset(
            elevated_context, record,
            **recordset_values
        )
        return self._create_floating_ip(
            context, fip, record, zone=zone, recordset=recordset
        )

    def _create_ptr_zone(self, elevated_context, zone_name):
        zone_values = {
            'type': 'PRIMARY',
            'name': zone_name,
            'email': cfg.CONF['service:central'].managed_resource_email,
            'tenant_id': cfg.CONF['service:central'].managed_resource_tenant_id
        }
        try:
            zone = self.create_zone(
                elevated_context, objects.Zone(**zone_values)
            )
        except exceptions.DuplicateZone:
            # NOTE(eandersson): This code is prone to race conditions, and
            #                   it does not hurt to try to handle this if it
            #                   fails.
            zone = self.storage.find_zone(
                elevated_context, {'name': zone_name}
            )

        return zone

    def _unset_floatingip_reverse(self, context, region, floatingip_id):
        """
        Unset the FloatingIP PTR record based on the

        Service's FloatingIP ID > managed_resource_id
        Tenant ID > managed_tenant_id

        We find the record based on the criteria and delete it or raise.
        """
        elevated_context = context.elevated(all_tenants=True,
                                            edit_managed_records=True)
        criterion = {
            'managed_resource_id': floatingip_id,
            'managed_tenant_id': context.project_id
        }

        try:
            record = self.storage.find_record(
                elevated_context, criterion=criterion
            )
        except exceptions.RecordNotFound:
            msg = 'No such FloatingIP %s:%s' % (region, floatingip_id)
            raise exceptions.NotFound(msg)

        self._delete_ptr_record(
            elevated_context, record.zone_id, record.recordset_id,
            record['id']
        )

    def _create_floating_ip(self, context, fip, record,
                            zone=None, recordset=None):
        """
        Creates a FloatingIP based on floating ip and record data.
        """
        elevated_context = context.elevated(all_tenants=True)
        fip_ptr = objects.FloatingIP().from_dict({
            'address': fip['address'],
            'id': fip['id'],
            'region': fip['region'],
            'ptrdname': None,
            'ttl': None,
            'description': None,
            'action': constants.NONE,
            'status': constants.INACTIVE
        })

        # TTL population requires a present record in order to find the
        # Recordset or Zone.
        if not record:
            LOG.debug('No record information found for %s', fip['id'])
            return fip_ptr

        if not recordset:
            try:
                recordset = self.storage.find_recordset(
                    elevated_context, criterion={'id': record.recordset_id}
                )
            except exceptions.RecordSetNotFound:
                LOG.debug('No recordset found for %s', fip['id'])
                return fip_ptr

        if recordset.ttl is not None:
            fip_ptr['ttl'] = recordset.ttl
        else:
            if not zone:
                try:
                    zone = self.get_zone(
                        elevated_context, record.zone_id
                    )
                except exceptions.ZoneNotFound:
                    LOG.debug('No zone found for %s', fip['id'])
                    return fip_ptr

            fip_ptr['ttl'] = zone.ttl

        if recordset.action in constants.FLOATING_IP_ACTIONS:
            fip_ptr['action'] = recordset.action
        else:
            LOG.debug(
                'Action %s not valid for floating ip action', recordset.action
            )

        if recordset.status in constants.FLOATING_IP_STATUSES:
            fip_ptr['status'] = recordset.status
        else:
            LOG.debug(
                'Status %s not valid for floating ip status', recordset.status
            )

        fip_ptr['ptrdname'] = record.data
        fip_ptr['description'] = record.description

        return fip_ptr

    def _create_floating_ip_list(self, context, data):
        """
        Creates a FloatingIPList based on floating ips and records data.
        """
        fips = objects.FloatingIPList()
        for key, value in data.items():
            fip, record = value
            fip_ptr = self._create_floating_ip(context, fip, record)
            fips.append(fip_ptr)
        return fips

    @transaction
    def _delete_ptr_record(self, context, zone_id, recordset_id,
                           record_to_delete_id):
        try:
            recordset = self.storage.find_recordset(
                context, {'id': recordset_id, 'zone_id': zone_id}
            )
            record_ids = [record['id'] for record in recordset.records]

            if record_to_delete_id not in record_ids:
                LOG.debug(
                    'PTR Record %s not found in recordset %s',
                    record_to_delete_id, recordset_id
                )
                return

            for record in list(recordset.records):
                if record['id'] != record_to_delete_id:
                    continue
                recordset.records.remove(record)
                break

            if not recordset.records:
                self.delete_recordset(
                    context, zone_id, recordset_id
                )
                return

            recordset.validate()
            self.update_recordset(context, recordset)
        except exceptions.RecordSetNotFound:
            pass

    @transaction
    def _replace_or_create_ptr_recordset(self, context, record, zone_id,
                                         name, type, ttl=None):
        try:
            recordset = self.storage.find_recordset(context, {
                'zone_id': zone_id,
                'name': name,
                'type': type,
            })
            recordset.ttl = ttl
            recordset.records = objects.RecordList(objects=[record])
            recordset.validate()
            recordset = self.update_recordset(
                context, recordset
            )
        except exceptions.RecordSetNotFound:
            values = {
                'name': name,
                'type': type,
                'ttl': ttl,
            }
            recordset = objects.RecordSet(**values)
            recordset.records = objects.RecordList(objects=[record])
            recordset.validate()
            recordset = self.create_recordset(
                context, zone_id, recordset
            )
        return recordset

    @rpc.expected_exceptions()
    def update_floatingip(self, context, region, floatingip_id, values):
        """
        We strictly see if values['ptrdname'] is str or None and set / unset
        the requested FloatingIP's PTR record based on that.
        """
        if ('ptrdname' in values.obj_what_changed() and
                values['ptrdname'] is None):
            self._unset_floatingip_reverse(
                context, region, floatingip_id
            )
        elif isinstance(values['ptrdname'], str):
            return self._set_floatingip_reverse(
                context, region, floatingip_id, values
            )

    # Blacklisted zones
    @rpc.expected_exceptions()
    @notification.notify_type('dns.blacklist.create')
    @transaction
    def create_blacklist(self, context, blacklist):
        policy.check('create_blacklist', context)

        created_blacklist = self.storage.create_blacklist(context, blacklist)

        return created_blacklist

    @rpc.expected_exceptions()
    def get_blacklist(self, context, blacklist_id):
        policy.check('get_blacklist', context)

        blacklist = self.storage.get_blacklist(context, blacklist_id)

        return blacklist

    @rpc.expected_exceptions()
    def find_blacklists(self, context, criterion=None, marker=None,
                        limit=None, sort_key=None, sort_dir=None):
        policy.check('find_blacklists', context)

        blacklists = self.storage.find_blacklists(context, criterion,
                                                  marker, limit,
                                                  sort_key, sort_dir)

        return blacklists

    @rpc.expected_exceptions()
    @notification.notify_type('dns.blacklist.update')
    @transaction
    def update_blacklist(self, context, blacklist):
        target = {
            'blacklist_id': blacklist.id,
        }
        policy.check('update_blacklist', context, target)

        blacklist = self.storage.update_blacklist(context, blacklist)

        return blacklist

    @rpc.expected_exceptions()
    @notification.notify_type('dns.blacklist.delete')
    @transaction
    def delete_blacklist(self, context, blacklist_id):
        policy.check('delete_blacklist', context)

        blacklist = self.storage.delete_blacklist(context, blacklist_id)

        return blacklist

    # Server Pools
    @rpc.expected_exceptions()
    @notification.notify_type('dns.pool.create')
    @transaction
    def create_pool(self, context, pool):
        # Verify that there is a tenant_id
        if pool.tenant_id is None:
            pool.tenant_id = context.project_id

        policy.check('create_pool', context)

        created_pool = self.storage.create_pool(context, pool)

        return created_pool

    @rpc.expected_exceptions()
    def find_pools(self, context, criterion=None, marker=None, limit=None,
                   sort_key=None, sort_dir=None):

        policy.check('find_pools', context)

        return self.storage.find_pools(context, criterion, marker, limit,
                                       sort_key, sort_dir)

    @rpc.expected_exceptions()
    def find_pool(self, context, criterion=None):

        policy.check('find_pool', context)

        return self.storage.find_pool(context, criterion)

    @rpc.expected_exceptions()
    def get_pool(self, context, pool_id):

        policy.check('get_pool', context)

        return self.storage.get_pool(context, pool_id)

    @rpc.expected_exceptions()
    @notification.notify_type('dns.pool.update')
    @transaction
    def update_pool(self, context, pool):
        policy.check('update_pool', context)

        # If there is a nameserver, then additional steps need to be done
        # Since these are treated as mutable objects, we're only going to
        # be comparing the nameserver.value which is the FQDN
        elevated_context = context.elevated(all_tenants=True)

        # TODO(kiall): ListObjects should be able to give you their
        #              original set of values.
        original_pool_ns_records = self._get_pool_ns_records(
            context, pool.id
        )

        updated_pool = self.storage.update_pool(context, pool)

        if not pool.obj_attr_is_set('ns_records'):
            return updated_pool

        # Find the current NS hostnames
        existing_ns = set([n.hostname for n in original_pool_ns_records])

        # Find the desired NS hostnames
        request_ns = set([n.hostname for n in pool.ns_records])

        # Get the NS's to be created and deleted, ignoring the ones that
        # are in both sets, as those haven't changed.
        # TODO(kiall): Factor in priority
        create_ns = request_ns.difference(existing_ns)
        delete_ns = existing_ns.difference(request_ns)

        # After the update, handle new ns_records
        for ns_record in create_ns:
            # Create new NS recordsets for every zone
            zones = self.find_zones(
                context=elevated_context,
                criterion={'pool_id': pool.id, 'action': '!DELETE'})
            for zone in zones:
                self._add_ns(elevated_context, zone, ns_record)

        # Then handle the ns_records to delete
        for ns_record in delete_ns:
            # Cannot delete the last nameserver, so verify that first.
            if not pool.ns_records:
                raise exceptions.LastServerDeleteNotAllowed(
                    "Not allowed to delete last of servers"
                )

            # Delete the NS record for every zone
            zones = self.find_zones(
                context=elevated_context,
                criterion={'pool_id': pool.id}
            )
            for zone in zones:
                self._delete_ns(elevated_context, zone, ns_record)

        return updated_pool

    @rpc.expected_exceptions()
    @notification.notify_type('dns.pool.delete')
    @transaction
    def delete_pool(self, context, pool_id):

        policy.check('delete_pool', context)

        # Make sure that there are no existing zones in the pool
        elevated_context = context.elevated(all_tenants=True)
        zones = self.find_zones(
            context=elevated_context,
            criterion={'pool_id': pool_id, 'action': '!DELETE'})

        # If there are existing zones, do not delete the pool
        LOG.debug("Zones is None? %r", zones)
        if len(zones) == 0:
            pool = self.storage.delete_pool(context, pool_id)
        else:
            raise exceptions.InvalidOperation('pool must not contain zones')

        return pool

    # Pool Manager Integration
    @rpc.expected_exceptions()
    @notification.notify_type('dns.domain.update')
    @notification.notify_type('dns.zone.update')
    @transaction
    @lock.synchronized_zone()
    def update_status(self, context, zone_id, status, serial, action=None):
        """
        :param context: Security context information.
        :param zone_id: The ID of the designate zone.
        :param status: The status, 'SUCCESS' or 'ERROR'.
        :param serial: The consensus serial number for the zone.
        :param action: The action, 'CREATE', 'UPDATE', 'DELETE' or 'NONE'.
        :return: updated zone
        """
        zone = self.storage.get_zone(context, zone_id)
        if action is None or zone.action == action:
            if zone.action == 'DELETE' and zone.status != 'ERROR':
                status = 'NO_ZONE'
            zone = self._update_zone_or_record_status(
                zone, status, serial
            )
        else:
            LOG.debug(
                'Updated action different from current action. '
                '%(previous_action)s != %(current_action)s '
                '(%(status)s). Keeping current action %(current_action)s '
                'for %(zone_id)s',
                {
                    'previous_action': action,
                    'current_action': zone.action,
                    'status': zone.status,
                    'zone_id': zone.id,
                }
            )

        if zone.status == 'DELETED':
            LOG.debug(
                'Updated Status: Deleting %(zone_id)s',
                {
                    'zone_id': zone.id,
                }
            )
            self.storage.delete_zone(context, zone.id)
        else:
            LOG.debug(
                'Setting Zone: %(zone_id)s action: %(action)s '
                'status: %(status)s serial: %(serial)s',
                {
                    'zone_id': zone.id,
                    'action': zone.action,
                    'status': zone.status,
                    'serial': zone.serial,
                }
            )
            self.storage.update_zone(context, zone)

        self._update_record_status(context, zone_id, status, serial)

        return zone

    def _update_record_status(self, context, zone_id, status, serial):
        """Update status on every record in a zone based on `serial`
        :returns: updated records
        """
        criterion = {
            'zone_id': zone_id
        }

        if status == 'SUCCESS':
            criterion.update({
                'status': ['PENDING', 'ERROR'],
                'serial': '<=%d' % serial,
            })

        elif status == 'ERROR' and serial == 0:
            criterion.update({
                'status': 'PENDING',
            })

        elif status == 'ERROR':
            criterion.update({
                'status': 'PENDING',
                'serial': '<=%d' % serial,
            })

        records = self.storage.find_records(context, criterion=criterion)

        for record in records:
            record = self._update_zone_or_record_status(record, status, serial)

            if record.obj_what_changed():
                LOG.debug('Setting record %s, serial %s: action %s, '
                          'status %s', record.id, record.serial,
                          record.action, record.status)
                self.storage.update_record(context, record)

        return records

    @staticmethod
    def _update_zone_or_record_status(zone_or_record, status, serial):
        if status == 'SUCCESS':
            if (zone_or_record.status in ['PENDING', 'ERROR'] and
                    serial >= zone_or_record.serial):
                if zone_or_record.action in ['CREATE', 'UPDATE']:
                    zone_or_record.action = 'NONE'
                    zone_or_record.status = 'ACTIVE'
                elif zone_or_record.action == 'DELETE':
                    zone_or_record.action = 'NONE'
                    zone_or_record.status = 'DELETED'

        elif status == 'ERROR':
            if (zone_or_record.status == 'PENDING' and
                    (serial >= zone_or_record.serial or serial == 0)):
                zone_or_record.status = 'ERROR'

        elif status == 'NO_ZONE':
            if zone_or_record.action in ['CREATE', 'UPDATE']:
                zone_or_record.action = 'CREATE'
                zone_or_record.status = 'ERROR'
            elif zone_or_record.action == 'DELETE':
                zone_or_record.action = 'NONE'
                zone_or_record.status = 'DELETED'

        return zone_or_record

    # Zone Transfers
    def _transfer_key_generator(self, size=8):
        chars = string.ascii_uppercase + string.digits
        sysrand = SystemRandom()
        return ''.join(sysrand.choice(chars) for _ in range(size))

    @rpc.expected_exceptions()
    @notification.notify_type('dns.zone_transfer_request.create')
    @transaction
    def create_zone_transfer_request(self, context, zone_transfer_request):

        # get zone
        zone = self.get_zone(context, zone_transfer_request.zone_id)

        # Don't allow transfers for zones that are being deleted
        if zone.action == 'DELETE':
            raise exceptions.BadRequest('Can not transfer a deleting zone')

        if policy.enforce_new_defaults():
            target = {constants.RBAC_PROJECT_ID: zone.tenant_id}
        else:
            target = {'tenant_id': zone.tenant_id}

        policy.check('create_zone_transfer_request', context, target)

        zone_transfer_request.key = self._transfer_key_generator()

        if zone_transfer_request.tenant_id is None:
            zone_transfer_request.tenant_id = context.project_id

        self._is_valid_project_id(zone_transfer_request.tenant_id)

        created_zone_transfer_request = (
            self.storage.create_zone_transfer_request(
                context, zone_transfer_request))

        return created_zone_transfer_request

    @rpc.expected_exceptions()
    def get_zone_transfer_request(self, context, zone_transfer_request_id):

        elevated_context = context.elevated(all_tenants=True)

        # Get zone transfer request
        zone_transfer_request = self.storage.get_zone_transfer_request(
            elevated_context, zone_transfer_request_id)

        LOG.info('Target Tenant ID found - using scoped policy')
        if policy.enforce_new_defaults():
            target = {
                constants.RBAC_TARGET_PROJECT_ID: (zone_transfer_request.
                                                   target_tenant_id),
                constants.RBAC_PROJECT_ID: zone_transfer_request.tenant_id,
            }
        else:
            target = {
                'target_tenant_id': zone_transfer_request.target_tenant_id,
                'tenant_id': zone_transfer_request.tenant_id,
            }

        policy.check('get_zone_transfer_request', context, target)

        return zone_transfer_request

    @rpc.expected_exceptions()
    def find_zone_transfer_requests(self, context, criterion=None, marker=None,
                                    limit=None, sort_key=None, sort_dir=None):

        policy.check('find_zone_transfer_requests', context)

        requests = self.storage.find_zone_transfer_requests(
            context, criterion,
            marker, limit,
            sort_key, sort_dir)

        return requests

    @rpc.expected_exceptions()
    @notification.notify_type('dns.zone_transfer_request.update')
    @transaction
    def update_zone_transfer_request(self, context, zone_transfer_request):

        if 'zone_id' in zone_transfer_request.obj_what_changed():
            raise exceptions.InvalidOperation('Zone cannot be changed')

        if policy.enforce_new_defaults():
            target = {
                constants.RBAC_PROJECT_ID: zone_transfer_request.tenant_id,
            }
        else:
            target = {
                'tenant_id': zone_transfer_request.tenant_id,
            }
        policy.check('update_zone_transfer_request', context, target)
        request = self.storage.update_zone_transfer_request(
            context, zone_transfer_request)

        return request

    @rpc.expected_exceptions()
    @notification.notify_type('dns.zone_transfer_request.delete')
    @transaction
    def delete_zone_transfer_request(self, context, zone_transfer_request_id):
        # Get zone transfer request
        zone_transfer_request = self.storage.get_zone_transfer_request(
            context, zone_transfer_request_id)

        if policy.enforce_new_defaults():
            target = {
                constants.RBAC_PROJECT_ID: zone_transfer_request.tenant_id
            }
        else:
            target = {'tenant_id': zone_transfer_request.tenant_id}

        policy.check('delete_zone_transfer_request', context, target)
        return self.storage.delete_zone_transfer_request(
            context,
            zone_transfer_request_id)

    @rpc.expected_exceptions()
    @notification.notify_type('dns.zone_transfer_accept.create')
    @transaction
    def create_zone_transfer_accept(self, context, zone_transfer_accept):
        elevated_context = context.elevated(all_tenants=True)
        zone_transfer_request = self.get_zone_transfer_request(
            context, zone_transfer_accept.zone_transfer_request_id)

        zone_transfer_accept.zone_id = zone_transfer_request.zone_id

        if zone_transfer_request.status != 'ACTIVE':
            if zone_transfer_request.status == 'COMPLETE':
                raise exceptions.InvaildZoneTransfer(
                    'Zone Transfer Request has been used')
            raise exceptions.InvaildZoneTransfer(
                'Zone Transfer Request Invalid')

        if zone_transfer_request.key != zone_transfer_accept.key:
            raise exceptions.IncorrectZoneTransferKey(
                'Key does not match stored key for request')

        if policy.enforce_new_defaults():
            target = {
                constants.RBAC_TARGET_PROJECT_ID: (zone_transfer_request.
                                                   target_tenant_id)
            }
        else:
            target = {
                'target_tenant_id': zone_transfer_request.target_tenant_id
            }

        policy.check('create_zone_transfer_accept', context, target)

        if zone_transfer_accept.tenant_id is None:
            zone_transfer_accept.tenant_id = context.project_id

        self._is_valid_project_id(zone_transfer_accept.tenant_id)

        created_zone_transfer_accept = (
            self.storage.create_zone_transfer_accept(
                context, zone_transfer_accept))

        try:
            zone = self.storage.get_zone(
                elevated_context,
                zone_transfer_request.zone_id)

            # Don't allow transfers for zones that are being deleted
            if zone.action == 'DELETE':
                raise exceptions.BadRequest('Can not transfer a deleting zone')

            # Ensure the accepting tenant has enough quota to continue
            self._enforce_zone_quota(context,
                                     zone_transfer_accept.tenant_id)

            zone.tenant_id = zone_transfer_accept.tenant_id
            self.storage.update_zone(elevated_context, zone)

        except Exception:
            created_zone_transfer_accept.status = 'ERROR'
            self.storage.update_zone_transfer_accept(
                context, created_zone_transfer_accept)
            raise
        else:
            created_zone_transfer_accept.status = 'COMPLETE'
            zone_transfer_request.status = 'COMPLETE'
            self.storage.update_zone_transfer_accept(
                context, created_zone_transfer_accept)
            self.storage.update_zone_transfer_request(
                elevated_context, zone_transfer_request)

        return created_zone_transfer_accept

    @rpc.expected_exceptions()
    def get_zone_transfer_accept(self, context, zone_transfer_accept_id):
        # Get zone transfer accept

        zone_transfer_accept = self.storage.get_zone_transfer_accept(
            context, zone_transfer_accept_id)

        if policy.enforce_new_defaults():
            target = {
                constants.RBAC_PROJECT_ID: zone_transfer_accept.tenant_id
            }
        else:
            target = {
                'tenant_id': zone_transfer_accept.tenant_id
            }

        policy.check('get_zone_transfer_accept', context, target)

        return zone_transfer_accept

    @rpc.expected_exceptions()
    def find_zone_transfer_accepts(self, context, criterion=None, marker=None,
                                   limit=None, sort_key=None, sort_dir=None):
        policy.check('find_zone_transfer_accepts', context)
        return self.storage.find_zone_transfer_accepts(context, criterion,
                                                       marker, limit,
                                                       sort_key, sort_dir)

    # Zone Import Methods
    @rpc.expected_exceptions()
    @notification.notify_type('dns.zone_import.create')
    def create_zone_import(self, context, request_body):
        if policy.enforce_new_defaults():
            target = {constants.RBAC_PROJECT_ID: context.project_id}
        else:
            target = {'tenant_id': context.project_id}

        policy.check('create_zone_import', context, target)

        self._is_valid_project_id(context.project_id)

        values = {
            'status': 'PENDING',
            'message': None,
            'zone_id': None,
            'tenant_id': context.project_id,
            'task_type': 'IMPORT'
        }
        zone_import = objects.ZoneImport(**values)

        created_zone_import = self.storage.create_zone_import(context,
                                                              zone_import)

        self.tg.add_thread(self._import_zone, context, created_zone_import,
                           request_body)

        return created_zone_import

    @rpc.expected_exceptions()
    def _import_zone(self, context, zone_import, request_body):
        zone = None
        try:
            dnspython_zone = dnszone.from_text(
                request_body,
                # Don't relativize, or we end up with '@' record names.
                relativize=False,
                # Don't check origin, we allow missing NS records
                # (missing SOA records are taken care of in _create_zone).
                check_origin=False)
            zone = dnsutils.from_dnspython_zone(dnspython_zone)
            zone.type = 'PRIMARY'
            for rrset in list(zone.recordsets):
                if rrset.type == 'SOA':
                    zone.recordsets.remove(rrset)
                # subdomain NS records should be kept
                elif rrset.type == 'NS' and rrset.name == zone.name:
                    zone.recordsets.remove(rrset)
        except dnszone.UnknownOrigin:
            zone_import.message = (
                'The $ORIGIN statement is required and must be the first '
                'statement in the zonefile.'
            )
            zone_import.status = 'ERROR'
        except dnsexception.SyntaxError:
            zone_import.message = 'Malformed zonefile.'
            zone_import.status = 'ERROR'
        except exceptions.BadRequest:
            zone_import.message = 'An SOA record is required.'
            zone_import.status = 'ERROR'
        except Exception as e:
            LOG.exception('An undefined error occurred during zone import')
            zone_import.message = (
                'An undefined error occurred. %s' % str(e)[:130]
            )
            zone_import.status = 'ERROR'

        # If the zone import was valid, create the zone
        if zone_import.status != 'ERROR':
            try:
                zone = self.create_zone(context, zone)
                zone_import.status = 'COMPLETE'
                zone_import.zone_id = zone.id
                zone_import.message = (
                    '%(name)s imported' % {'name': zone.name}
                )
            except exceptions.DuplicateZone:
                zone_import.status = 'ERROR'
                zone_import.message = 'Duplicate zone.'
            except exceptions.InvalidTTL as e:
                zone_import.status = 'ERROR'
                zone_import.message = str(e)
            except exceptions.OverQuota:
                zone_import.status = 'ERROR'
                zone_import.message = 'Quota exceeded during zone import.'
            except Exception as e:
                LOG.exception(
                    'An undefined error occurred during zone import creation'
                )
                zone_import.message = (
                    'An undefined error occurred. %s' % str(e)[:130]
                )
                zone_import.status = 'ERROR'

        self.update_zone_import(context, zone_import)

    @rpc.expected_exceptions()
    @notification.notify_type('dns.zone_import.update')
    def update_zone_import(self, context, zone_import):
        if policy.enforce_new_defaults():
            target = {constants.RBAC_PROJECT_ID: zone_import.tenant_id}
        else:
            target = {'tenant_id': zone_import.tenant_id}
        policy.check('update_zone_import', context, target)

        return self.storage.update_zone_import(context, zone_import)

    @rpc.expected_exceptions()
    def find_zone_imports(self, context, criterion=None, marker=None,
                          limit=None, sort_key=None, sort_dir=None):

        if policy.enforce_new_defaults():
            target = {constants.RBAC_PROJECT_ID: context.project_id}
        else:
            target = {'tenant_id': context.project_id}

        policy.check('find_zone_imports', context, target)

        if not criterion:
            criterion = {
                'task_type': 'IMPORT'
            }
        else:
            criterion['task_type'] = 'IMPORT'

        return self.storage.find_zone_imports(context, criterion, marker,
                                      limit, sort_key, sort_dir)

    @rpc.expected_exceptions()
    def get_zone_import(self, context, zone_import_id):

        if policy.enforce_new_defaults():
            target = {constants.RBAC_PROJECT_ID: context.project_id}
        else:
            target = {'tenant_id': context.project_id}

        policy.check('get_zone_import', context, target)
        return self.storage.get_zone_import(context, zone_import_id)

    @rpc.expected_exceptions()
    @notification.notify_type('dns.zone_import.delete')
    @transaction
    def delete_zone_import(self, context, zone_import_id):

        if policy.enforce_new_defaults():
            target = {
                'zone_import_id': zone_import_id,
                constants.RBAC_PROJECT_ID: context.project_id
            }
        else:
            target = {
                'zone_import_id': zone_import_id,
                'tenant_id': context.project_id
            }

        policy.check('delete_zone_import', context, target)

        zone_import = self.storage.delete_zone_import(context, zone_import_id)

        return zone_import

    # Zone Export Methods
    @rpc.expected_exceptions()
    @notification.notify_type('dns.zone_export.create')
    def create_zone_export(self, context, zone_id):
        # Try getting the zone to ensure it exists
        zone = self.storage.get_zone(context, zone_id)

        if policy.enforce_new_defaults():
            target = {constants.RBAC_PROJECT_ID: context.project_id}
        else:
            target = {'tenant_id': context.project_id}

        policy.check('create_zone_export', context, target)

        self._is_valid_project_id(context.project_id)

        values = {
            'status': 'PENDING',
            'message': None,
            'zone_id': zone_id,
            'tenant_id': context.project_id,
            'task_type': 'EXPORT'
        }
        zone_export = objects.ZoneExport(**values)

        created_zone_export = self.storage.create_zone_export(context,
                                                              zone_export)

        export = copy.deepcopy(created_zone_export)
        self.worker_api.start_zone_export(context, zone, export)

        return created_zone_export

    @rpc.expected_exceptions()
    def find_zone_exports(self, context, criterion=None, marker=None,
                  limit=None, sort_key=None, sort_dir=None):

        if policy.enforce_new_defaults():
            target = {constants.RBAC_PROJECT_ID: context.project_id}
        else:
            target = {'tenant_id': context.project_id}
        policy.check('find_zone_exports', context, target)

        if not criterion:
            criterion = {
                'task_type': 'EXPORT'
            }
        else:
            criterion['task_type'] = 'EXPORT'

        return self.storage.find_zone_exports(context, criterion, marker,
                                      limit, sort_key, sort_dir)

    @rpc.expected_exceptions()
    def get_zone_export(self, context, zone_export_id):

        if policy.enforce_new_defaults():
            target = {constants.RBAC_PROJECT_ID: context.project_id}
        else:
            target = {'tenant_id': context.project_id}

        policy.check('get_zone_export', context, target)

        return self.storage.get_zone_export(context, zone_export_id)

    @rpc.expected_exceptions()
    @notification.notify_type('dns.zone_export.update')
    def update_zone_export(self, context, zone_export):

        if policy.enforce_new_defaults():
            target = {constants.RBAC_PROJECT_ID: zone_export.tenant_id}
        else:
            target = {'tenant_id': zone_export.tenant_id}

        policy.check('update_zone_export', context, target)

        return self.storage.update_zone_export(context, zone_export)

    @rpc.expected_exceptions()
    @notification.notify_type('dns.zone_export.delete')
    @transaction
    def delete_zone_export(self, context, zone_export_id):

        if policy.enforce_new_defaults():
            target = {
                'zone_export_id': zone_export_id,
                constants.RBAC_PROJECT_ID: context.project_id
            }
        else:
            target = {
                'zone_export_id': zone_export_id,
                'tenant_id': context.project_id
            }

        policy.check('delete_zone_export', context, target)

        zone_export = self.storage.delete_zone_export(context, zone_export_id)

        return zone_export

    @rpc.expected_exceptions()
    def find_service_statuses(self, context, criterion=None, marker=None,
                              limit=None, sort_key=None, sort_dir=None):
        """List service statuses.
        """
        policy.check('find_service_statuses', context)

        return self.storage.find_service_statuses(
            context, criterion, marker, limit, sort_key, sort_dir)

    @rpc.expected_exceptions()
    def find_service_status(self, context, criterion=None):
        policy.check('find_service_status', context)

        return self.storage.find_service_status(context, criterion)

    @rpc.expected_exceptions()
    def update_service_status(self, context, service_status):
        policy.check('update_service_status', context)

        criterion = {
            "service_name": service_status.service_name,
            "hostname": service_status.hostname
        }

        if service_status.obj_attr_is_set('id'):
            criterion["id"] = service_status.id

        try:
            db_status = self.storage.find_service_status(
                context, criterion)
            db_status.update(dict(service_status))

            return self.storage.update_service_status(context, db_status)
        except exceptions.ServiceStatusNotFound:
            LOG.info(
                "Creating new service status entry for %(service_name)s "
                "at %(hostname)s",
                {
                    'service_name': service_status.service_name,
                    'hostname': service_status.hostname
                }
            )
            return self.storage.create_service_status(
                context, service_status)