diff --git a/troposphere/apigateway.py b/troposphere/apigateway.py index 875fc0d45..cb936f898 100644 --- a/troposphere/apigateway.py +++ b/troposphere/apigateway.py @@ -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 @@ -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), @@ -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" @@ -88,7 +113,7 @@ class MethodSetting(AWSProperty): "MetricsEnabled": (bool, False), "ResourcePath": (basestring, True), "ThrottlingBurstLimit": (positive_integer, False), - "ThrottlingRateLimit": (positive_integer, False) + "ThrottlingRateLimit": (floatingpoint, False) } @@ -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) } @@ -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): @@ -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), @@ -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" @@ -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" @@ -270,7 +348,7 @@ class QuotaSettings(AWSProperty): class ThrottleSettings(AWSProperty): props = { "BurstLimit": (positive_integer, False), - "RateLimit": (positive_integer, False), + "RateLimit": (floatingpoint, False), }