File: validators.py

package info (click to toggle)
python-moto 5.1.18-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 116,520 kB
  • sloc: python: 636,725; javascript: 181; makefile: 39; sh: 3
file content (1184 lines) | stat: -rw-r--r-- 28,884 bytes parent folder | download
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
import re
from datetime import datetime, timedelta, timezone
from enum import Enum
from ipaddress import IPv4Address, IPv6Address, ip_address
from typing import Any, Optional

from moto.core.common_models import BaseModel
from moto.moto_api._internal import mock_random
from moto.route53domains.exceptions import UnsupportedTLDException

DOMAIN_OPERATION_STATUSES = (
    "SUBMITTED",
    "IN_PROGRESS",
    "ERROR",
    "SUCCESSFUL",
    "FAILED",
)

DOMAIN_OPERATION_TYPES = (
    "REGISTER_DOMAIN",
    "DELETE_DOMAIN",
    "TRANSFER_IN_DOMAIN",
    "UPDATE_DOMAIN_CONTACT",
    "UPDATE_NAMESERVER",
    "CHANGE_PRIVACY_PROTECTION",
    "DOMAIN_LOCK",
    "ENABLE_AUTORENEW",
    "DISABLE_AUTORENEW",
    "ADD_DNSSEC",
    "REMOVE_DNSSEC",
    "EXPIRE_DOMAIN",
    "TRANSFER_OUT_DOMAIN",
    "CHANGE_DOMAIN_OWNER",
    "RENEW_DOMAIN",
    "PUSH_DOMAIN",
    "INTERNAL_TRANSFER_OUT_DOMAIN",
    "INTERNAL_TRANSFER_IN_DOMAIN",
)

DOMAIN_OPERATION_STATUS_FLAGS = (
    "PENDING_ACCEPTANCE",
    "PENDING_CUSTOMER_ACTION",
    "PENDING_AUTHORIZATION",
    "PENDING_PAYMENT_VERIFICATION",
    "PENDING_SUPPORT_CASE",
)

DOMAIN_CONTACT_DETAIL_CONTACT_TYPES = (
    "PERSON",
    "COMPANY",
    "ASSOCIATION",
    "PUBLIC_BODY",
    "RESELLER",
    "ORGANIZATION",
)

DOMAIN_CONTACT_DETAIL_COUNTRY_CODES = (
    "AC",
    "AD",
    "AE",
    "AF",
    "AG",
    "AI",
    "AL",
    "AM",
    "AN",
    "AO",
    "AQ",
    "AR",
    "AS",
    "AT",
    "AU",
    "AW",
    "AX",
    "AZ",
    "BA",
    "BB",
    "BD",
    "BE",
    "BF",
    "BG",
    "BH",
    "BI",
    "BJ",
    "BL",
    "BM",
    "BN",
    "BO",
    "BQ",
    "BR",
    "BS",
    "BT",
    "BV",
    "BW",
    "BY",
    "BZ",
    "CA",
    "CC",
    "CD",
    "CF",
    "CG",
    "CH",
    "CI",
    "CK",
    "CL",
    "CM",
    "CN",
    "CO",
    "CR",
    "CU",
    "CV",
    "CW",
    "CX",
    "CY",
    "CZ",
    "DE",
    "DJ",
    "DK",
    "DM",
    "DO",
    "DZ",
    "EC",
    "EE",
    "EG",
    "EH",
    "ER",
    "ES",
    "ET",
    "FI",
    "FJ",
    "FK",
    "FM",
    "FO",
    "FR",
    "GA",
    "GB",
    "GD",
    "GE",
    "GF",
    "GG",
    "GH",
    "GI",
    "GL",
    "GM",
    "GN",
    "GP",
    "GQ",
    "GR",
    "GS",
    "GT",
    "GU",
    "GW",
    "GY",
    "HK",
    "HM",
    "HN",
    "HR",
    "HT",
    "HU",
    "ID",
    "IE",
    "IL",
    "IM",
    "IN",
    "IO",
    "IQ",
    "IR",
    "IS",
    "IT",
    "JE",
    "JM",
    "JO",
    "JP",
    "KE",
    "KG",
    "KH",
    "KI",
    "KM",
    "KN",
    "KP",
    "KR",
    "KW",
    "KY",
    "KZ",
    "LA",
    "LB",
    "LC",
    "LI",
    "LK",
    "LR",
    "LS",
    "LT",
    "LU",
    "LV",
    "LY",
    "MA",
    "MC",
    "MD",
    "ME",
    "MF",
    "MG",
    "MH",
    "MK",
    "ML",
    "MM",
    "MN",
    "MO",
    "MP",
    "MQ",
    "MR",
    "MS",
    "MT",
    "MU",
    "MV",
    "MW",
    "MX",
    "MY",
    "MZ",
    "NA",
    "NC",
    "NE",
    "NF",
    "NG",
    "NI",
    "NL",
    "NO",
    "NP",
    "NR",
    "NU",
    "NZ",
    "OM",
    "PA",
    "PE",
    "PF",
    "PG",
    "PH",
    "PK",
    "PL",
    "PM",
    "PN",
    "PR",
    "PS",
    "PT",
    "PW",
    "PY",
    "QA",
    "RE",
    "RO",
    "RS",
    "RU",
    "RW",
    "SA",
    "SB",
    "SC",
    "SD",
    "SE",
    "SG",
    "SH",
    "SI",
    "SJ",
    "SK",
    "SL",
    "SM",
    "SN",
    "SO",
    "SR",
    "SS",
    "ST",
    "SV",
    "SX",
    "SY",
    "SZ",
    "TC",
    "TD",
    "TF",
    "TG",
    "TH",
    "TJ",
    "TK",
    "TL",
    "TM",
    "TN",
    "TO",
    "TP",
    "TR",
    "TT",
    "TV",
    "TW",
    "TZ",
    "UA",
    "UG",
    "US",
    "UY",
    "UZ",
    "VA",
    "VC",
    "VE",
    "VG",
    "VI",
    "VN",
    "VU",
    "WF",
    "WS",
    "YE",
    "YT",
    "ZA",
    "ZM",
    "ZW",
)

# List of supported top-level domains that you can register with Amazon Route53
AWS_SUPPORTED_TLDS = (
    "ac",
    "academy",
    "accountants",
    "actor",
    "adult",
    "agency",
    "airforce",
    "apartments",
    "associates",
    "auction",
    "audio",
    "band",
    "bargains",
    "bet",
    "bike",
    "bingo",
    "biz",
    "black",
    "blue",
    "boutique",
    "builders",
    "business",
    "buzz",
    "cab",
    "cafe",
    "camera",
    "camp",
    "capital",
    "cards",
    "care",
    "careers",
    "cash",
    "casino",
    "catering",
    "cc",
    "center",
    "ceo",
    "chat",
    "cheap",
    "church",
    "city",
    "claims",
    "cleaning",
    "click",
    "clinic",
    "clothing",
    "cloud",
    "club",
    "coach",
    "codes",
    "coffee",
    "college",
    "com",
    "community",
    "company",
    "computer",
    "condos",
    "construction",
    "consulting",
    "contractors",
    "cool",
    "coupons",
    "credit",
    "creditcard",
    "cruises",
    "dance",
    "dating",
    "deals",
    "degree",
    "delivery",
    "democrat",
    "dental",
    "diamonds",
    "diet",
    "digital",
    "direct",
    "directory",
    "discount",
    "dog",
    "domains",
    "education",
    "email",
    "energy",
    "engineering",
    "enterprises",
    "equipment",
    "estate",
    "events",
    "exchange",
    "expert",
    "exposed",
    "express",
    "fail",
    "farm",
    "finance",
    "financial",
    "fish",
    "fitness",
    "flights",
    "florist",
    "flowers",
    "fm",
    "football",
    "forsale",
    "foundation",
    "fund",
    "furniture",
    "futbol",
    "fyi",
    "gallery",
    "games",
    "gift",
    "gifts",
    "gives",
    "glass",
    "global",
    "gmbh",
    "gold",
    "golf",
    "graphics",
    "gratis",
    "green",
    "gripe",
    "group",
    "guide",
    "guitars",
    "guru",
    "haus",
    "healthcare",
    "help",
    "hiv",
    "hockey",
    "holdings",
    "holiday",
    "host",
    "hosting",
    "house",
    "im",
    "immo",
    "immobilien",
    "industries",
    "info",
    "ink",
    "institute",
    "insure",
    "international",
    "investments",
    "io",
    "irish",
    "jewelry",
    "juegos",
    "kaufen",
    "kim",
    "kitchen",
    "kiwi",
    "land",
    "lease",
    "legal",
    "lgbt",
    "life",
    "lighting",
    "limited",
    "limo",
    "link",
    "live",
    "loan",
    "loans",
    "lol",
    "maison",
    "management",
    "marketing",
    "mba",
    "media",
    "memorial",
    "mobi",
    "moda",
    "money",
    "mortgage",
    "movie",
    "name",
    "net",
    "network",
    "news",
    "ninja",
    "onl",
    "online",
    "org",
    "partners",
    "parts",
    "photo",
    "photography",
    "photos",
    "pics",
    "pictures",
    "pink",
    "pizza",
    "place",
    "plumbing",
    "plus",
    "poker",
    "porn",
    "press",
    "pro",
    "productions",
    "properties",
    "property",
    "pub",
    "qpon",
    "recipes",
    "red",
    "reise",
    "reisen",
    "rentals",
    "repair",
    "report",
    "republican",
    "restaurant",
    "reviews",
    "rip",
    "rocks",
    "run",
    "sale",
    "sarl",
    "school",
    "schule",
    "services",
    "sex",
    "sexy",
    "shiksha",
    "shoes",
    "show",
    "singles",
    "site",
    "soccer",
    "social",
    "solar",
    "solutions",
    "space",
    "store",
    "studio",
    "style",
    "sucks",
    "supplies",
    "supply",
    "support",
    "surgery",
    "systems",
    "tattoo",
    "tax",
    "taxi",
    "team",
    "tech",
    "technology",
    "tennis",
    "theater",
    "tienda",
    "tips",
    "tires",
    "today",
    "tools",
    "tours",
    "town",
    "toys",
    "trade",
    "training",
    "tv",
    "university",
    "uno",
    "vacations",
    "vegas",
    "ventures",
    "vg",
    "viajes",
    "video",
    "villas",
    "vision",
    "voyage",
    "watch",
    "website",
    "wedding",
    "wiki",
    "wine",
    "works",
    "world",
    "wtf",
    "xyz",
    "zone",
)

VALID_DOMAIN_REGEX = re.compile(
    r"(?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.)+[a-z0-9][a-z0-9-]{0,61}[a-z0-9]"
)
PHONE_NUMBER_REGEX = re.compile(r"\+\d*\.\d+$")


class DomainFilterField(str, Enum):
    DOMAIN_NAME = "DomainName"
    EXPIRY = "Expiry"


class DomainSortOrder(str, Enum):
    ASCENDING = "ASC"
    DESCENDING = "DES"


class DomainFilterOperator(str, Enum):
    LE = "LE"
    GE = "GE"
    BEGINS_WITH = "BEGINS_WITH"


def is_valid_enum(value: Any, enum_cls: type[Enum]) -> bool:
    try:
        enum_cls(value)
        return True
    except ValueError:
        return False


class ValidationException(Exception):
    def __init__(self, errors: list[str]):
        super().__init__("\n\t".join(errors))
        self.errors = errors


class Route53DomainsOperation(BaseModel):
    def __init__(
        self,
        id_: str,
        domain_name: str,
        status: str,
        type_: str,
        submitted_date: datetime,
        last_updated_date: datetime,
        message: Optional[str] = None,
        status_flag: Optional[str] = None,
    ):
        self.id = id_
        self.domain_name = domain_name
        self.status = status
        self.type = type_
        self.submitted_date = submitted_date
        self.last_updated_date = last_updated_date
        self.message = message
        self.status_flag = status_flag

    @classmethod
    def validate(  # type: ignore[misc,no-untyped-def]
        cls,
        domain_name: str,
        status: str,
        type_: str,
        message: Optional[str] = None,
        status_flag: Optional[str] = None,
    ):
        id_ = str(mock_random.uuid4())
        submitted_date = datetime.now(timezone.utc)
        last_updated_date = datetime.now(timezone.utc)

        return cls(
            id_,
            domain_name,
            status,
            type_,
            submitted_date,
            last_updated_date,
            message,
            status_flag,
        )

    def to_json(self) -> dict[str, Any]:
        d = {
            "OperationId": self.id,
            "Status": self.status,
            "StatusFlag": self.status_flag,
            "DomainName": self.domain_name,
            "LastUpdatedDate": self.last_updated_date.timestamp(),
            "SubmittedDate": self.submitted_date.timestamp(),
            "Type": self.type,
        }

        if self.message:
            d["Message"] = self.message

        return d


class Route53DomainsContactDetail(BaseModel):
    def __init__(
        self,
        address_line_1: Optional[str] = None,
        address_line_2: Optional[str] = None,
        city: Optional[str] = None,
        contact_type: Optional[str] = None,
        country_code: Optional[str] = None,
        email: Optional[str] = None,
        extra_params: Optional[list[dict[str, Any]]] = None,
        fax: Optional[str] = None,
        first_name: Optional[str] = None,
        last_name: Optional[str] = None,
        organization_name: Optional[str] = None,
        phone_number: Optional[str] = None,
        state: Optional[str] = None,
        zip_code: Optional[str] = None,
    ):
        super().__init__()
        self.address_line_1 = address_line_1
        self.address_line_2 = address_line_2
        self.city = city
        self.contact_type = contact_type
        self.country_code = country_code
        self.email = email
        self.extra_params = extra_params
        self.fax = fax
        self.first_name = first_name
        self.last_name = last_name
        self.organization_name = organization_name
        self.phone_number = phone_number
        self.state = state
        self.zip_code = zip_code

    @classmethod
    def validate(  # type: ignore[misc, no-untyped-def]
        cls,
        address_line_1: Optional[str] = None,
        address_line_2: Optional[str] = None,
        city: Optional[str] = None,
        contact_type: Optional[str] = None,
        country_code: Optional[str] = None,
        email: Optional[str] = None,
        extra_params: Optional[list[dict[str, Any]]] = None,
        fax: Optional[str] = None,
        first_name: Optional[str] = None,
        last_name: Optional[str] = None,
        organization_name: Optional[str] = None,
        phone_number: Optional[str] = None,
        state: Optional[str] = None,
        zip_code: Optional[str] = None,
    ):
        input_errors: list[str] = []

        cls.__validate_str_len(address_line_1, "AddressLine1", 255, input_errors)
        cls.__validate_str_len(address_line_2, "AddressLine2", 255, input_errors)
        cls.__validate_str_len(city, "City", 255, input_errors)
        cls.__validate_str_len(email, "Email", 255, input_errors)
        cls.__validate_str_len(fax, "Fax", 255, input_errors)
        cls.__validate_str_len(first_name, "FirstName", 255, input_errors)
        cls.__validate_str_len(last_name, "LastName", 255, input_errors)
        cls.__validate_str_len(state, "State", 255, input_errors)
        cls.__validate_str_len(zip_code, "ZipCode", 255, input_errors)

        if contact_type:
            if contact_type not in DOMAIN_CONTACT_DETAIL_CONTACT_TYPES:
                input_errors.append(f"Invalid contact type {contact_type}")
            else:
                if contact_type != "PERSON" and not organization_name:
                    input_errors.append(
                        "Must supply OrganizationName when ContactType is not PERSON"
                    )

        if country_code and country_code not in DOMAIN_CONTACT_DETAIL_COUNTRY_CODES:
            input_errors.append(f"CountryCode {country_code} is invalid")

        if phone_number and not PHONE_NUMBER_REGEX.match(phone_number):
            input_errors.append("PhoneNumber is in an invalid format")

        if input_errors:
            raise ValidationException(input_errors)

        return cls(
            address_line_1,
            address_line_2,
            city,
            contact_type,
            country_code,
            email,
            extra_params,
            fax,
            first_name,
            last_name,
            organization_name,
            phone_number,
            state,
            zip_code,
        )

    @classmethod
    def validate_dict(cls, d: dict[str, Any]):  # type: ignore[misc, no-untyped-def]
        address_line_1 = d.get("AddressLine1")
        address_line_2 = d.get("AddressLine2")
        city = d.get("City")
        contact_type = d.get("ContactType")
        country_code = d.get("CountryCode")
        email = d.get("Email")
        extra_params = d.get("ExtraParams")
        fax = d.get("Fax")
        first_name = d.get("FirstName")
        last_name = d.get("LastName")
        organization_name = d.get("OrganizationName")
        phone_number = d.get("PhoneNumber")
        state = d.get("State")
        zip_code = d.get("ZipCode")
        return cls.validate(
            address_line_1=address_line_1,
            address_line_2=address_line_2,
            city=city,
            contact_type=contact_type,
            country_code=country_code,
            email=email,
            extra_params=extra_params,
            fax=fax,
            first_name=first_name,
            last_name=last_name,
            organization_name=organization_name,
            phone_number=phone_number,
            state=state,
            zip_code=zip_code,
        )

    @staticmethod
    def __validate_str_len(
        value: Optional[str], field_name: str, max_len: int, input_errors: list[str]
    ) -> None:
        if value and len(value) > max_len:
            input_errors.append(f"Length of {field_name} is more than {max_len}")

    def to_json(self) -> dict[str, Any]:
        d = {
            "FirstName": self.first_name,
            "LastName": self.last_name,
            "ContactType": self.contact_type,
            "OrganizationName": self.organization_name,
            "AddressLine1": self.address_line_1,
            "AddressLine2": self.address_line_2,
            "City": self.city,
            "State": self.state,
            "CountryCode": self.country_code,
            "ZipCode": self.zip_code,
            "PhoneNumber": self.phone_number,
            "Email": self.email,
            "Fax": self.fax,
            "ExtraParams": self.extra_params,
        }

        return {key: value for key, value in d.items() if value is not None}


class NameServer:
    def __init__(self, name: str, glue_ips: list[str]):
        self.name = name
        self.glue_ips = glue_ips

    @classmethod
    def validate(cls, name: str, glue_ips: Optional[list[str]] = None):  # type: ignore[misc,no-untyped-def]
        glue_ips = glue_ips or []
        input_errors: list[str] = []

        if not VALID_DOMAIN_REGEX.match(name):
            input_errors.append(f"{name} is not a valid host name")

        num_ipv4_addresses = 0
        num_ipv6_addresses = 0
        for ip in glue_ips:
            try:
                address = ip_address(ip)
                if isinstance(address, IPv4Address):
                    num_ipv4_addresses += 1
                elif isinstance(address, IPv6Address):
                    num_ipv6_addresses += 1
            except ValueError:
                input_errors.append(f"{ip} is not a valid IP address")

        if num_ipv4_addresses > 1:
            input_errors.append("GlueIps list must include only 1 IPv4 address")
        if num_ipv6_addresses > 1:
            input_errors.append("GlueIps list must include only 1 IPv6 address")

        if input_errors:
            raise ValidationException(input_errors)

        return cls(name, glue_ips)

    @classmethod
    def validate_dict(cls, data: dict[str, Any]):  # type: ignore[misc,no-untyped-def]
        name = data.get("Name")
        glue_ips = data.get("GlueIps")
        return cls.validate(name, glue_ips)  # type: ignore[arg-type]

    def to_json(self) -> dict[str, Any]:
        d: dict[str, Any] = {"Name": self.name}
        if self.glue_ips:
            d["GlueIps"] = self.glue_ips
        return d


class Route53Domain(BaseModel):
    def __init__(
        self,
        domain_name: str,
        nameservers: list[NameServer],
        auto_renew: bool,
        admin_contact: Route53DomainsContactDetail,
        registrant_contact: Route53DomainsContactDetail,
        tech_contact: Route53DomainsContactDetail,
        admin_privacy: bool,
        registrant_privacy: bool,
        tech_privacy: bool,
        registrar_name: str,
        whois_server: str,
        registrar_url: str,
        abuse_contact_email: str,
        abuse_contact_phone: str,
        registry_domain_id: str,
        creation_date: datetime,
        updated_date: datetime,
        expiration_date: datetime,
        reseller: str,
        status_list: list[str],
        dns_sec_keys: list[dict[str, Any]],
        extra_params: list[dict[str, Any]],
    ):
        self.domain_name = domain_name
        self.nameservers = nameservers
        self.auto_renew = auto_renew
        self.admin_contact = admin_contact
        self.registrant_contact = registrant_contact
        self.tech_contact = tech_contact
        self.admin_privacy = admin_privacy
        self.registrant_privacy = registrant_privacy
        self.tech_privacy = tech_privacy
        self.registrar_name = registrar_name
        self.whois_server = whois_server
        self.registrar_url = registrar_url
        self.abuse_contact_email = abuse_contact_email
        self.abuse_contact_phone = abuse_contact_phone
        self.registry_domain_id = registry_domain_id
        self.creation_date = creation_date
        self.updated_date = updated_date
        self.expiration_date = expiration_date
        self.reseller = reseller
        self.status_list = status_list
        self.dns_sec_keys = dns_sec_keys
        self.extra_params = extra_params

    @classmethod
    def validate(  # type: ignore[misc,no-untyped-def]
        cls,
        domain_name: str,
        admin_contact: Route53DomainsContactDetail,
        registrant_contact: Route53DomainsContactDetail,
        tech_contact: Route53DomainsContactDetail,
        nameservers: Optional[list[dict[str, Any]]] = None,
        auto_renew: bool = True,
        admin_privacy: bool = True,
        registrant_privacy: bool = True,
        tech_privacy: bool = True,
        registrar_name: Optional[str] = None,
        whois_server: Optional[str] = None,
        registrar_url: Optional[str] = None,
        abuse_contact_email: Optional[str] = None,
        abuse_contact_phone: Optional[str] = None,
        registry_domain_id: Optional[str] = None,
        expiration_date: Optional[datetime] = None,
        reseller: Optional[str] = None,
        dns_sec_keys: Optional[list[dict[str, Any]]] = None,
        extra_params: Optional[list[dict[str, Any]]] = None,
    ):
        input_errors: list[str] = []

        cls.validate_domain_name(domain_name, input_errors)

        nameservers = nameservers or []
        try:
            nameservers = [
                NameServer.validate_dict(nameserver) for nameserver in nameservers
            ] or [
                NameServer.validate(name="ns-2048.awscdn-64.net"),
                NameServer.validate(name="ns-2051.awscdn-67.net"),
                NameServer.validate(name="ns-2050.awscdn-66.net"),
                NameServer.validate(name="ns-2049.awscdn-65.net"),
            ]
        except ValidationException as e:
            input_errors += e.errors

        creation_date = datetime.now(timezone.utc)
        updated_date = datetime.now(timezone.utc)
        expiration_date = expiration_date or datetime.now(timezone.utc) + timedelta(
            days=365 * 10
        )
        registrar_name = registrar_name or "GANDI SAS"
        whois_server = whois_server or "whois.gandi.net"
        registrar_url = registrar_url or "http://www.gandi.net"
        abuse_contact_email = abuse_contact_email or "abuse@support.gandi.net"
        status_list = ["SUCCEEDED"]

        time_until_expiration = expiration_date - datetime.now(timezone.utc)
        if time_until_expiration < timedelta(
            days=365
        ) or time_until_expiration > timedelta(days=365 * 10):
            input_errors.append(
                "ExpirationDate must by between 1 and 10 years from now"
            )

        if input_errors:
            raise ValidationException(input_errors)

        return cls(
            domain_name=domain_name,
            nameservers=nameservers,  # type: ignore[arg-type]
            auto_renew=auto_renew,
            admin_contact=admin_contact,
            registrant_contact=registrant_contact,
            tech_contact=tech_contact,
            admin_privacy=admin_privacy,
            registrant_privacy=registrant_privacy,
            tech_privacy=tech_privacy,
            registrar_name=registrar_name,
            whois_server=whois_server,
            registrar_url=registrar_url,
            abuse_contact_email=abuse_contact_email,
            abuse_contact_phone=abuse_contact_phone,  # type: ignore[arg-type]
            registry_domain_id=registry_domain_id,  # type: ignore[arg-type]
            creation_date=creation_date,
            updated_date=updated_date,
            expiration_date=expiration_date,
            reseller=reseller,  # type: ignore[arg-type]
            status_list=status_list,
            dns_sec_keys=dns_sec_keys,  # type: ignore[arg-type]
            extra_params=extra_params,  # type: ignore[arg-type]
        )

    @staticmethod
    def validate_domain_name(domain_name: str, input_errors: list[str]) -> None:
        if not VALID_DOMAIN_REGEX.match(domain_name):
            input_errors.append("Invalid domain name")
            return

        tld = domain_name.split(".")[-1]
        if tld not in AWS_SUPPORTED_TLDS:
            raise UnsupportedTLDException(tld)

    def to_json(self) -> dict[str, Any]:
        return {
            "DomainName": self.domain_name,
            "Nameservers": [nameserver.to_json() for nameserver in self.nameservers],
            "AutoRenew": self.auto_renew,
            "AdminContact": self.admin_contact.to_json(),
            "RegistrantContact": self.registrant_contact.to_json(),
            "TechContact": self.tech_contact.to_json(),
            "AdminPrivacy": self.admin_privacy,
            "RegistrantPrivacy": self.registrant_privacy,
            "TechPrivacy": self.tech_privacy,
            "RegistrarName": self.registrar_name,
            "WhoIsServer": self.whois_server,
            "RegistrarUrl": self.registrar_url,
            "AbuseContactEmail": self.abuse_contact_email,
            "AbuseContactPhone": self.abuse_contact_phone,
            "RegistryDomainId": "",
            "CreationDate": self.creation_date.timestamp(),
            "UpdateDate": self.updated_date.timestamp(),
            "ExpirationDate": self.expiration_date.timestamp(),
            "Reseller": self.reseller,
            "DnsSec": "",
            "StatusList": self.status_list,
            "DnsSecKeys": self.dns_sec_keys,
            "BillingContact": self.admin_contact.to_json(),
        }


class DomainsFilter:
    def __init__(
        self, name: DomainFilterField, operator: DomainFilterOperator, values: list[str]
    ):
        self.name: DomainFilterField = name
        self.operator: DomainFilterOperator = operator
        self.values = values

    def filter(self, domain: Route53Domain) -> bool:
        if self.name == DomainFilterField.DOMAIN_NAME:
            return self.__filter_by_domain_name(domain)
        return self.__filter_by_expiry_date(domain)

    def __filter_by_domain_name(self, domain: Route53Domain) -> bool:
        return any(domain.domain_name.startswith(value) for value in self.values)

    def __filter_by_expiry_date(self, domain: Route53Domain) -> bool:
        return (
            any(
                value
                for value in self.values
                if domain.expiration_date
                >= datetime.fromtimestamp(float(value), tz=timezone.utc)
            )
            if self.operator == DomainFilterOperator.GE
            else any(
                value
                for value in self.values
                if domain.expiration_date
                <= datetime.fromtimestamp(float(value), tz=timezone.utc)
            )
        )

    @classmethod
    def validate(cls, name: str, operator: str, values: list[str]):  # type: ignore[misc, no-untyped-def]
        input_errors: list[str] = []

        if not is_valid_enum(name, DomainFilterField):
            input_errors.append(f"Cannot filter by field {name}")

        if not is_valid_enum(operator, DomainFilterOperator):
            input_errors.append(f"Invalid filter operator {operator}")

        if len(values) != 1:
            input_errors.append("Multiple filter values are not currently supported")

        if (
            name == DomainFilterField.DOMAIN_NAME
            and operator != DomainFilterOperator.BEGINS_WITH
        ):
            input_errors.append(
                f"Operator {operator} cannot be used with the DomainName filter"
            )

        if name == DomainFilterField.EXPIRY and operator not in (
            DomainFilterOperator.GE,
            DomainFilterOperator.LE,
        ):
            input_errors.append(
                f"Operator {operator} cannot be used with the Expiry filter"
            )

        if input_errors:
            raise ValidationException(input_errors)

        return cls(
            name=DomainFilterField(name),
            operator=DomainFilterOperator(operator),
            values=values,
        )

    @classmethod
    def validate_dict(cls, data: dict[str, Any]):  # type: ignore[misc,no-untyped-def]
        name = data.get("Name")
        operator = data.get("Operator")
        values = data.get("Values")
        return cls.validate(name=name, operator=operator, values=values)  # type: ignore[arg-type]


class DomainsSortCondition:
    def __init__(self, name: DomainFilterField, sort_order: DomainSortOrder):
        self.name: DomainFilterField = name
        self.sort_order: DomainSortOrder = sort_order

    @classmethod
    def validate(cls, name: str, sort_order: str):  # type: ignore[misc,no-untyped-def]
        input_errors: list[str] = []
        if not is_valid_enum(name, DomainFilterField):
            input_errors.append(f"Cannot sort by field {name}")

        if not is_valid_enum(sort_order, DomainSortOrder):
            input_errors.append(f"Invalid sort order {sort_order}")

        if input_errors:
            raise ValidationException(input_errors)

        return cls(name=DomainFilterField(name), sort_order=DomainSortOrder(sort_order))

    @classmethod
    def validate_dict(cls, data: dict[str, Any]):  # type: ignore[misc,no-untyped-def]
        name = data.get("Name")
        sort_order = data.get("SortOrder")
        return cls.validate(name=name, sort_order=sort_order)  # type: ignore[arg-type]