13
13
ApiGatewayBasePathMappingV2 ,
14
14
ApiGatewayDeployment ,
15
15
ApiGatewayDomainName ,
16
+ ApiGatewayDomainNameAccessAssociation ,
16
17
ApiGatewayDomainNameV2 ,
17
18
ApiGatewayResponse ,
18
19
ApiGatewayRestApi ,
@@ -86,6 +87,7 @@ class ApiDomainResponseV2:
86
87
domain : Optional [ApiGatewayDomainNameV2 ]
87
88
apigw_basepath_mapping_list : Optional [List [ApiGatewayBasePathMappingV2 ]]
88
89
recordset_group : Any
90
+ domain_access_association : Any
89
91
90
92
91
93
class SharedApiUsagePlan :
@@ -218,6 +220,7 @@ def __init__( # noqa: PLR0913
218
220
api_key_source_type : Optional [Intrinsicable [str ]] = None ,
219
221
always_deploy : Optional [bool ] = False ,
220
222
feature_toggle : Optional [FeatureToggle ] = None ,
223
+ policy : Optional [Union [Dict [str , Any ], Intrinsicable [str ]]] = None ,
221
224
):
222
225
"""Constructs an API Generator class that generates API Gateway resources
223
226
@@ -275,6 +278,7 @@ def __init__( # noqa: PLR0913
275
278
self .api_key_source_type = api_key_source_type
276
279
self .always_deploy = always_deploy
277
280
self .feature_toggle = feature_toggle
281
+ self .policy = policy
278
282
279
283
def _construct_rest_api (self ) -> ApiGatewayRestApi :
280
284
"""Constructs and returns the ApiGateway RestApi.
@@ -328,6 +332,9 @@ def _construct_rest_api(self) -> ApiGatewayRestApi:
328
332
if self .api_key_source_type :
329
333
rest_api .ApiKeySourceType = self .api_key_source_type
330
334
335
+ if self .policy :
336
+ rest_api .Policy = self .policy
337
+
331
338
return rest_api
332
339
333
340
def _validate_properties (self ) -> None :
@@ -602,7 +609,7 @@ def _construct_api_domain_v2(
602
609
Constructs and returns the ApiGateway Domain V2 and BasepathMapping V2
603
610
"""
604
611
if self .domain is None :
605
- return ApiDomainResponseV2 (None , None , None )
612
+ return ApiDomainResponseV2 (None , None , None , None )
606
613
607
614
sam_expect (self .domain , self .logical_id , "Domain" ).to_be_a_map ()
608
615
domain_name : PassThrough = sam_expect (
@@ -657,6 +664,14 @@ def _construct_api_domain_v2(
657
664
basepath_mapping .BasePath = path if normalize_basepath else basepath
658
665
basepath_resource_list .extend ([basepath_mapping ])
659
666
667
+ # Create the DomainNameAccessAssociation
668
+ domain_access_association = self .domain .get ("AccessAssociation" )
669
+ domain_access_association_resource = None
670
+ if domain_access_association is not None :
671
+ domain_access_association_resource = self ._generate_domain_access_association (
672
+ domain_access_association , domain_name_arn , api_domain_name
673
+ )
674
+
660
675
# Create the Route53 RecordSetGroup resource
661
676
record_set_group = None
662
677
route53 = self .domain .get ("Route53" )
@@ -683,6 +698,7 @@ def _construct_api_domain_v2(
683
698
domain ,
684
699
basepath_resource_list ,
685
700
self ._construct_single_record_set_group (self .domain , domain_name , route53 ),
701
+ domain_access_association_resource ,
686
702
)
687
703
688
704
if not record_set_group :
@@ -691,7 +707,7 @@ def _construct_api_domain_v2(
691
707
692
708
record_set_group .RecordSets += self ._construct_record_sets_for_domain (self .domain , domain_name , route53 )
693
709
694
- return ApiDomainResponseV2 (domain , basepath_resource_list , record_set_group )
710
+ return ApiDomainResponseV2 (domain , basepath_resource_list , record_set_group , domain_access_association_resource )
695
711
696
712
def _get_basepaths (self ) -> Optional [List [str ]]:
697
713
if self .domain is None :
@@ -779,11 +795,14 @@ def _construct_alias_target(self, domain: Dict[str, Any], api_domain_name: str,
779
795
if domain .get ("EndpointConfiguration" ) == "REGIONAL" :
780
796
alias_target ["HostedZoneId" ] = fnGetAtt (api_domain_name , "RegionalHostedZoneId" )
781
797
alias_target ["DNSName" ] = fnGetAtt (api_domain_name , "RegionalDomainName" )
782
- else :
798
+ elif domain . get ( "EndpointConfiguration" ) == "EDGE" :
783
799
if route53 .get ("DistributionDomainName" ) is None :
784
800
route53 ["DistributionDomainName" ] = fnGetAtt (api_domain_name , "DistributionDomainName" )
785
801
alias_target ["HostedZoneId" ] = "Z2FDTNDATAQYW2"
786
802
alias_target ["DNSName" ] = route53 .get ("DistributionDomainName" )
803
+ else :
804
+ alias_target ["HostedZoneId" ] = route53 .get ("VpcEndpointHostedZoneId" )
805
+ alias_target ["DNSName" ] = route53 .get ("VpcEndpointDomainName" )
787
806
return alias_target
788
807
789
808
def _create_basepath_mapping (
@@ -833,12 +852,17 @@ def to_cloudformation(
833
852
domain : Union [Resource , None ]
834
853
basepath_mapping : Union [List [ApiGatewayBasePathMapping ], List [ApiGatewayBasePathMappingV2 ], None ]
835
854
rest_api = self ._construct_rest_api ()
855
+ is_private_domain = isinstance (self .domain , dict ) and self .domain .get ("EndpointConfiguration" ) == "PRIVATE"
836
856
api_domain_response = (
837
857
self ._construct_api_domain_v2 (rest_api , route53_record_set_groups )
838
- if isinstance ( self . domain , dict ) and self . domain . get ( "EndpointConfiguration" ) == "PRIVATE"
858
+ if is_private_domain
839
859
else self ._construct_api_domain (rest_api , route53_record_set_groups )
840
860
)
841
861
862
+ domain_access_association = None
863
+ if is_private_domain :
864
+ domain_access_association = cast (ApiDomainResponseV2 , api_domain_response ).domain_access_association
865
+
842
866
domain = api_domain_response .domain
843
867
basepath_mapping = api_domain_response .apigw_basepath_mapping_list
844
868
@@ -882,6 +906,9 @@ def to_cloudformation(
882
906
]
883
907
)
884
908
909
+ if domain_access_association is not None :
910
+ generated_resources .append (domain_access_association )
911
+
885
912
# Make a list of single resources
886
913
generated_resources_list : List [Resource ] = []
887
914
for resource in generated_resources :
@@ -1513,3 +1540,24 @@ def _set_endpoint_configuration(self, rest_api: ApiGatewayRestApi, value: Union[
1513
1540
else :
1514
1541
rest_api .EndpointConfiguration = {"Types" : [value ]}
1515
1542
rest_api .Parameters = {"endpointConfigurationTypes" : value }
1543
+
1544
+ def _generate_domain_access_association (
1545
+ self ,
1546
+ domain_access_association : Dict [str , Any ],
1547
+ domain_name_arn : Dict [str , str ],
1548
+ domain_logical_id : str ,
1549
+ ) -> ApiGatewayDomainNameAccessAssociation :
1550
+ """
1551
+ Generate domain access association resource
1552
+ """
1553
+ vpcEndpointId = domain_access_association .get ("VpcEndpointId" )
1554
+ logical_id = LogicalIdGenerator ("DomainNameAccessAssociation" , [vpcEndpointId , domain_logical_id ]).gen ()
1555
+
1556
+ domain_access_association_resource = ApiGatewayDomainNameAccessAssociation (
1557
+ logical_id , attributes = self .passthrough_resource_attributes
1558
+ )
1559
+ domain_access_association_resource .DomainNameArn = domain_name_arn
1560
+ domain_access_association_resource .AccessAssociationSourceType = "VPCE"
1561
+ domain_access_association_resource .AccessAssociationSource = vpcEndpointId
1562
+
1563
+ return domain_access_association_resource
0 commit comments