Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API Gateway Property Updates and Improved Validation Coverage #749

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
94 changes: 86 additions & 8 deletions troposphere/apigateway.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
from . import AWSHelperFn, AWSObject, AWSProperty
from .validators import positive_integer
from .validators import (
positive_integer, defer, floatingpoint
)
import json

HTTP = 'HTTP'
AWS = 'AWS'
MOCK = 'MOCK'
HTTP_PROXY = 'HTTP_PROXY'
AWS_PROXY = 'AWS_PROXY'


def validate_authorizer_ttl(ttl_value):
""" Validate authorizer ttl timeout
Expand Down Expand Up @@ -47,7 +55,7 @@ class Authorizer(AWSObject):
props = {
"AuthorizerCredentials": (basestring, False),
"AuthorizerResultTtlInSeconds": (validate_authorizer_ttl, False),
"AuthorizerUri": (basestring, True),
"AuthorizerUri": (defer, False),
"IdentitySource": (basestring, True),
"IdentityValidationExpression": (basestring, False),
"Name": (basestring, True),
Expand All @@ -56,6 +64,23 @@ class Authorizer(AWSObject):
"Type": (basestring, True)
}

def validate(self):
if 'Type' in self.properties:

type_property = self.properties.get('Type', None)

if 'TOKEN' in type_property:
if 'AuthorizerUri' in self.properties:

authorizer_uri = self.properties.get('AuthorizerUri', None)

if not isinstance(authorizer_uri, basestring):
raise ValueError('AuthorizerUri value must'
' be a string')
else:
raise ValueError('AuthorizerUri is required when'
' Type is set to TOKEN')


class BasePathMapping(AWSObject):
resource_type = "AWS::ApiGateway::BasePathMapping"
Expand Down Expand Up @@ -88,7 +113,7 @@ class MethodSetting(AWSProperty):
"MetricsEnabled": (bool, False),
"ResourcePath": (basestring, True),
"ThrottlingBurstLimit": (positive_integer, False),
"ThrottlingRateLimit": (positive_integer, False)
"ThrottlingRateLimit": (floatingpoint, False)
}


Expand All @@ -108,7 +133,7 @@ class StageDescription(AWSProperty):
"MetricsEnabled": (bool, False),
"StageName": (basestring, False),
"ThrottlingBurstLimit": (positive_integer, False),
"ThrottlingRateLimit": (positive_integer, False),
"ThrottlingRateLimit": (floatingpoint, False),
"Variables": (dict, False)
}

Expand Down Expand Up @@ -149,6 +174,28 @@ class Integration(AWSProperty):
"Uri": (basestring, False)
}

def validate(self):
if 'Type' in self.properties:

valid_values = [
HTTP,
AWS,
MOCK,
HTTP_PROXY,
AWS_PROXY,
]

type_property = self.properties.get('Type', None)

if type_property not in valid_values:
raise ValueError('Only HTTP, AWS, MOCK, HTTP_PROXY,'
' and AWS_PROXY are valid values')

if 'MOCK' not in type_property:
if 'IntegrationHttpMethod' not in self.properties:
raise ValueError('IntegrationHttpMethod must be set when'
' Type is not defined as MOCK')


class MethodResponse(AWSProperty):

Expand All @@ -165,7 +212,7 @@ class Method(AWSObject):
props = {
"ApiKeyRequired": (bool, False),
"AuthorizationType": (basestring, True),
"AuthorizerId": (basestring, False),
"AuthorizerId": (defer, False),
"HttpMethod": (basestring, True),
"Integration": (Integration, False),
"MethodResponses": ([MethodResponse], False),
Expand All @@ -175,6 +222,19 @@ class Method(AWSObject):
"RestApiId": (basestring, True)
}

def validate(self):
if 'AuthorizerId' in self.properties:
if 'AuthorizationType' in self.properties:

auth_type = self.properties.get('AuthorizationType', None)

if 'CUSTOM' not in auth_type:
raise ValueError('AuthorizationType must be set to'
'CUSTOM when AuthorizerId is defined')
else:
raise ValueError('AuthorizationType must be defined'
' when AuthorizerId is defined')


class Model(AWSObject):
resource_type = "AWS::ApiGateway::Model"
Expand Down Expand Up @@ -226,15 +286,33 @@ class RestApi(AWSObject):
resource_type = "AWS::ApiGateway::RestApi"

props = {
"Body": (dict, False),
"Body": ((basestring, dict), False),
"BodyS3Location": (S3Location, False),
"CloneFrom": (basestring, False),
"Description": (basestring, False),
"FailOnWarnings": (basestring, False),
"FailOnWarnings": (bool, False),
"Name": (basestring, False),
"Parameters": ([basestring], False)
}

def validate(self):
if 'Body' in self.properties:
body = self.properties.get('Body')
if isinstance(body, basestring):
# Verify it is a valid json string
json.loads(body)
elif isinstance(body, dict):
# Convert the dict to a basestring
self.properties['Schema'] = json.dumps(body)
elif isinstance(body, AWSHelperFn):
pass
else:
raise ValueError("Body must be a str or dict")

if 'Body' not in self.properties:
if 'Name' not in self.properties:
raise ValueError('Name must be defined when Body is undefined')


class Stage(AWSObject):
resource_type = "AWS::ApiGateway::Stage"
Expand Down Expand Up @@ -270,7 +348,7 @@ class QuotaSettings(AWSProperty):
class ThrottleSettings(AWSProperty):
props = {
"BurstLimit": (positive_integer, False),
"RateLimit": (positive_integer, False),
"RateLimit": (floatingpoint, False),
}


Expand Down