Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 17 additions & 11 deletions samtranslator/model/api/api_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -535,13 +535,13 @@ def _construct_api_domain( # noqa: PLR0912, PLR0915

self._set_optional_domain_properties(domain)

basepaths: Optional[List[str]]
basepaths: Optional[List[Any]]
basepath_value = self.domain.get("BasePath")
# Create BasepathMappings
if self.domain.get("BasePath") and isinstance(basepath_value, str):
if isinstance(basepath_value, str) or is_intrinsic(basepath_value):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thoughts on adding this same support on http_api_generator.py file?

basepaths = [basepath_value]
elif self.domain.get("BasePath") and isinstance(basepath_value, list):
basepaths = cast(Optional[List[Any]], basepath_value)
elif isinstance(basepath_value, list):
basepaths = basepath_value
else:
basepaths = None

Expand All @@ -554,13 +554,19 @@ def _construct_api_domain( # noqa: PLR0912, PLR0915
basepath_mapping = self._create_basepath_mapping(api_domain_name, rest_api, None, None)
basepath_resource_list.extend([basepath_mapping])
else:
sam_expect(basepaths, self.logical_id, "Domain.BasePath").to_be_a_list_of(ExpectedType.STRING)
if not is_intrinsic(basepaths[0]):
sam_expect(basepaths, self.logical_id, "Domain.BasePath").to_be_a_list_of(ExpectedType.STRING)
for basepath in basepaths:
# Remove possible leading and trailing '/' because a base path may only
# contain letters, numbers, and one of "$-_.+!*'()"
path = "".join(e for e in basepath if e.isalnum())
mapping_basepath = path if normalize_basepath else basepath
logical_id = "{}{}{}".format(self.logical_id, path, "BasePathMapping")
if is_intrinsic(basepath):
mapping_basepath = basepath
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So the parameter NormalizeBasePath is not used anymore if the BasePath is passed as an intrinsic?

logical_id = self.logical_id + "BasePathMapping"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what happens if there are different base paths all defined with intrinsics? Will they just have the same logical id? Should we add some sort of hash for the intrinsic values here, so we actually create different resources?

else:
# Remove possible leading and trailing '/' because a base path may only
# contain letters, numbers, and one of "$-_.+!*'()"
path = "".join(e for e in basepath if e.isalnum())
mapping_basepath = path if normalize_basepath else basepath
logical_id = "{}{}{}".format(self.logical_id, path, "BasePathMapping")

basepath_mapping = self._create_basepath_mapping(
api_domain_name, rest_api, logical_id, mapping_basepath
)
Expand Down Expand Up @@ -810,7 +816,7 @@ def _create_basepath_mapping(
api_domain_name: PassThrough,
rest_api: ApiGatewayRestApi,
logical_id: Optional[str],
basepath: Optional[str],
basepath: Optional[Any],
) -> ApiGatewayBasePathMapping:

basepath_mapping: ApiGatewayBasePathMapping
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ Resources:
DomainName: !Sub 'example-${AWS::Region}.com'
CertificateArn: !Ref MyDomainCert
EndpointConfiguration: !Ref EndpointConf
BasePath: [/get, /fetch]
BasePath: !Sub '${AWS::Region}-api'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you still keep the list, but do with intrinsics instead?

Something like

Suggested change
BasePath: !Sub '${AWS::Region}-api'
BasePath:
- !Sub '${AWS::Region}-get'
- !Sub '${AWS::Region}-fetch'

MutualTlsAuthentication:
TruststoreUri: !Ref MyMTLSUri
TruststoreVersion: !Ref MyMTLSVersion
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,33 +99,12 @@
},
"Type": "AWS::ApiGateway::RestApi"
},
"MyApiDeployment19c8cf5c63": {
"MyApiBasePathMapping": {
"Condition": "C1",
"Properties": {
"Description": "RestApi deployment id: 19c8cf5c63090f12c5a96f6f57162495bed446c7",
"RestApiId": {
"Ref": "MyApi"
}
},
"Type": "AWS::ApiGateway::Deployment"
},
"MyApiProdStage": {
"Condition": "C1",
"Properties": {
"DeploymentId": {
"Ref": "MyApiDeployment19c8cf5c63"
"BasePath": {
"Fn::Sub": "ap-southeast-1-api"
},
"RestApiId": {
"Ref": "MyApi"
},
"StageName": "Prod"
},
"Type": "AWS::ApiGateway::Stage"
},
"MyApifetchBasePathMapping": {
"Condition": "C1",
"Properties": {
"BasePath": "fetch",
"DomainName": {
"Ref": "ApiGatewayDomainNamec0ed6fae34"
},
Expand All @@ -138,21 +117,28 @@
},
"Type": "AWS::ApiGateway::BasePathMapping"
},
"MyApigetBasePathMapping": {
"MyApiDeployment651fe13005": {
"Condition": "C1",
"Properties": {
"BasePath": "get",
"DomainName": {
"Ref": "ApiGatewayDomainNamec0ed6fae34"
"Description": "RestApi deployment id: 651fe13005fac4c7c7f2fe93b77c7ef2c6c3e746",
"RestApiId": {
"Ref": "MyApi"
}
},
"Type": "AWS::ApiGateway::Deployment"
},
"MyApiProdStage": {
"Condition": "C1",
"Properties": {
"DeploymentId": {
"Ref": "MyApiDeployment651fe13005"
},
"RestApiId": {
"Ref": "MyApi"
},
"Stage": {
"Ref": "MyApiProdStage"
}
"StageName": "Prod"
},
"Type": "AWS::ApiGateway::BasePathMapping"
"Type": "AWS::ApiGateway::Stage"
},
"MyFunction": {
"Condition": "C1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,33 +107,12 @@
},
"Type": "AWS::ApiGateway::RestApi"
},
"MyApiDeployment4f2c19d290": {
"MyApiBasePathMapping": {
"Condition": "C1",
"Properties": {
"Description": "RestApi deployment id: 4f2c19d290875d88d8e30124f0953f1784e1b54d",
"RestApiId": {
"Ref": "MyApi"
}
},
"Type": "AWS::ApiGateway::Deployment"
},
"MyApiProdStage": {
"Condition": "C1",
"Properties": {
"DeploymentId": {
"Ref": "MyApiDeployment4f2c19d290"
"BasePath": {
"Fn::Sub": "cn-north-1-api"
},
"RestApiId": {
"Ref": "MyApi"
},
"StageName": "Prod"
},
"Type": "AWS::ApiGateway::Stage"
},
"MyApifetchBasePathMapping": {
"Condition": "C1",
"Properties": {
"BasePath": "fetch",
"DomainName": {
"Ref": "ApiGatewayDomainNamec0cd2d9dfc"
},
Expand All @@ -146,21 +125,28 @@
},
"Type": "AWS::ApiGateway::BasePathMapping"
},
"MyApigetBasePathMapping": {
"MyApiDeploymente1e3e9b849": {
"Condition": "C1",
"Properties": {
"BasePath": "get",
"DomainName": {
"Ref": "ApiGatewayDomainNamec0cd2d9dfc"
"Description": "RestApi deployment id: e1e3e9b849803f87115b2cb8fcdf5d144342aaa4",
"RestApiId": {
"Ref": "MyApi"
}
},
"Type": "AWS::ApiGateway::Deployment"
},
"MyApiProdStage": {
"Condition": "C1",
"Properties": {
"DeploymentId": {
"Ref": "MyApiDeploymente1e3e9b849"
},
"RestApiId": {
"Ref": "MyApi"
},
"Stage": {
"Ref": "MyApiProdStage"
}
"StageName": "Prod"
},
"Type": "AWS::ApiGateway::BasePathMapping"
"Type": "AWS::ApiGateway::Stage"
},
"MyFunction": {
"Condition": "C1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,33 +107,12 @@
},
"Type": "AWS::ApiGateway::RestApi"
},
"MyApiDeployment32e59613e2": {
"MyApiBasePathMapping": {
"Condition": "C1",
"Properties": {
"Description": "RestApi deployment id: 32e59613e2e02a1f1d264849167ea359f10342f0",
"RestApiId": {
"Ref": "MyApi"
}
},
"Type": "AWS::ApiGateway::Deployment"
},
"MyApiProdStage": {
"Condition": "C1",
"Properties": {
"DeploymentId": {
"Ref": "MyApiDeployment32e59613e2"
"BasePath": {
"Fn::Sub": "us-gov-west-1-api"
},
"RestApiId": {
"Ref": "MyApi"
},
"StageName": "Prod"
},
"Type": "AWS::ApiGateway::Stage"
},
"MyApifetchBasePathMapping": {
"Condition": "C1",
"Properties": {
"BasePath": "fetch",
"DomainName": {
"Ref": "ApiGatewayDomainName9c93aac102"
},
Expand All @@ -146,21 +125,28 @@
},
"Type": "AWS::ApiGateway::BasePathMapping"
},
"MyApigetBasePathMapping": {
"MyApiDeployment9d140ab9b8": {
"Condition": "C1",
"Properties": {
"BasePath": "get",
"DomainName": {
"Ref": "ApiGatewayDomainName9c93aac102"
"Description": "RestApi deployment id: 9d140ab9b8b8ee141d5cd2d4a90a374c37a10db7",
"RestApiId": {
"Ref": "MyApi"
}
},
"Type": "AWS::ApiGateway::Deployment"
},
"MyApiProdStage": {
"Condition": "C1",
"Properties": {
"DeploymentId": {
"Ref": "MyApiDeployment9d140ab9b8"
},
"RestApiId": {
"Ref": "MyApi"
},
"Stage": {
"Ref": "MyApiProdStage"
}
"StageName": "Prod"
},
"Type": "AWS::ApiGateway::BasePathMapping"
"Type": "AWS::ApiGateway::Stage"
},
"MyFunction": {
"Condition": "C1",
Expand Down