Skip to content

Commit

Permalink
Add AWS::Serverless::HttpApi (#1941)
Browse files Browse the repository at this point in the history
  • Loading branch information
Markitox authored Aug 22, 2021
1 parent 028e251 commit 65813fa
Show file tree
Hide file tree
Showing 2 changed files with 168 additions and 2 deletions.
80 changes: 79 additions & 1 deletion tests/test_serverless.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
S3Event,
S3Location,
SimpleTable,
HttpApi,
HttpApiAuth,
OAuth2Authorizer,
HttpApiDomainConfiguration,
)


Expand Down Expand Up @@ -186,7 +190,7 @@ def test_api_auth_resource_policy(self):
t.add_resource(serverless_api)
t.to_json()

def test_api_with_endpoint_configuation(self):
def test_api_with_endpoint_configuration(self):
serverless_api = Api(
title="SomeApi",
StageName="testStageName",
Expand Down Expand Up @@ -217,6 +221,80 @@ def test_api_with_domain(self):
t.add_resource(serverless_api)
t.to_json()

def test_http_api_definition_uri_defined(self):
serverless_http_api = HttpApi(
"SomeHttpApi",
StageName="testHttp",
DefinitionUri="s3://bucket/swagger.yml",
)
t = Template()
t.add_resource(serverless_http_api)
t.to_json()

def test_http_api_both_definition_uri_and_body_defined(self):
serverless_http_api = HttpApi(
"SomeHttpApi",
StageName="testHttp",
DefinitionUri="s3://bucket/swagger.yml",
DefinitionBody=self.swagger,
)
t = Template()
t.add_resource(serverless_http_api)
with self.assertRaises(ValueError):
t.to_json()

def test_http_api_definition_body(self):
serverless_http_api = HttpApi(
"SomeHttpApi",
StageName="testHttp",
DefinitionBody=self.swagger,
)
t = Template()
t.add_resource(serverless_http_api)
t.to_json()

def test_http_api_no_definition(self):
serverless_api = HttpApi(
"SomeHttpApi",
StageName="testHttp",
)
t = Template()
t.add_resource(serverless_api)
t.to_json()

def test_http_api_authorization_scopes(self):
serverless_api = HttpApi(
title="SomeHttpApi",
Auth=HttpApiAuth(
Authorizers=OAuth2Authorizer(AuthorizationScopes=["scope1", "scope2"])
),
StageName="testHttpStageName",
)
t = Template()
t.add_resource(serverless_api)
t.to_json()

def test_http_api_with_domain(self):
certificate = Parameter("certificate", Type="String")
serverless_http_api = HttpApi(
"SomeHttpApi",
StageName="testHttp",
Domain=HttpApiDomainConfiguration(
BasePath=["/"],
CertificateArn=Ref(certificate),
DomainName=Sub("subdomain.${Zone}", Zone=ImportValue("MyZone")),
EndpointConfiguration="REGIONAL",
Route53=Route53(
HostedZoneId=ImportValue("MyZone"),
IpV6=True,
),
),
)
t = Template()
t.add_parameter(certificate)
t.add_resource(serverless_http_api)
t.to_json()

def test_simple_table(self):
serverless_table = SimpleTable("SomeTable")
t = Template()
Expand Down
90 changes: 89 additions & 1 deletion troposphere/serverless.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

from . import AWSHelperFn, AWSObject, AWSProperty
from .apigateway import AccessLogSetting, CanarySetting, MethodSetting
from .apigatewayv2 import AccessLogSettings, RouteSettings
from .awslambda import (
DestinationConfig,
Environment,
Expand All @@ -25,6 +26,7 @@
integer_range,
mutually_exclusive,
positive_integer,
boolean,
)

try:
Expand Down Expand Up @@ -335,6 +337,10 @@ def validate(self):
)


class ApiDefinition(AWSProperty):
props = {"Bucket": (str, True), "Key": (str, True), "Version": (str, False)}


class Api(AWSObject):
resource_type = "AWS::Serverless::Api"

Expand All @@ -347,7 +353,7 @@ class Api(AWSObject):
"CanarySetting": (CanarySetting, False),
"Cors": ((str, Cors), False),
"DefinitionBody": (dict, False),
"DefinitionUri": (str, False),
"DefinitionUri": ((str, ApiDefinition), False),
"Domain": (Domain, False),
"EndpointConfiguration": (EndpointConfiguration, False),
"MethodSettings": ([MethodSetting], False),
Expand All @@ -367,6 +373,88 @@ def validate(self):
mutually_exclusive(self.__class__.__name__, self.properties, conds)


class OAuth2Authorizer(AWSProperty):
props = {
"AuthorizationScopes": (list, False),
"IdentitySource": (str, False),
"JwtConfiguration": (dict, False),
}


class LambdaAuthorizationIdentity(AWSProperty):
props = {
"Context": (list, False),
"Headers": (list, False),
"QueryStrings": (list, False),
"ReauthorizeEvery": (integer, False),
"StageVariables": (list, False),
}


class LambdaAuthorizer(AWSProperty):
props = {
"AuthorizerPayloadFormatVersion": (str, True),
"EnableSimpleResponses": (boolean, False),
"FunctionArn": (str, True),
"FunctionInvokeRole": (str, False),
"Identity": (LambdaAuthorizationIdentity, False),
}


class HttpApiAuth(AWSProperty):
props = {
"Authorizers": ((OAuth2Authorizer, LambdaAuthorizer), False),
"DefaultAuthorizer": (str, False),
}


class HttpApiCorsConfiguration(AWSProperty):
props = {
"AllowCredentials": (boolean, False),
"AllowHeaders": (list, False),
"AllowMethods": (list, False),
"AllowOrigins": (list, False),
"ExposeHeaders": (list, False),
"MaxAge": (integer, False),
}


class HttpApiDefinition(ApiDefinition):
pass


class HttpApiDomainConfiguration(Domain):
pass


class HttpApi(AWSObject):
resource_type = "AWS::Serverless::HttpApi"

props = {
"AccessLogSettings": (AccessLogSettings, False),
"Auth": (HttpApiAuth, False),
"CorsConfiguration": ((str, HttpApiCorsConfiguration), False),
"DefaultRouteSettings": (RouteSettings, False),
"DefinitionBody": (dict, False),
"DefinitionUri": ((str, HttpApiDefinition), False),
"Description": (str, False),
"DisableExecuteApiEndpoint": (boolean, False),
"Domain": (HttpApiDomainConfiguration, False),
"FailOnWarnings": (boolean, False),
"RouteSettings": (dict, False),
"StageName": (str, False),
"StageVariables": (dict, False),
"Tags": (dict, False),
}

def validate(self):
conds = [
"DefinitionBody",
"DefinitionUri",
]
mutually_exclusive(self.__class__.__name__, self.properties, conds)


class PrimaryKey(AWSProperty):
props = {"Name": (str, False), "Type": (primary_key_type_validator, False)}

Expand Down

0 comments on commit 65813fa

Please sign in to comment.