Skip to content

Commit

Permalink
Rebase off new deployer
Browse files Browse the repository at this point in the history
Also added support for CloudFormation.
stealthycoin committed Jun 7, 2019
1 parent d6d5d9f commit 820d119
Showing 10 changed files with 106 additions and 11 deletions.
13 changes: 8 additions & 5 deletions chalice/deploy/deployer.py
Original file line number Diff line number Diff line change
@@ -103,18 +103,16 @@
from chalice.awsclient import ResourceDoesNotExistError
from chalice.awsclient import AWSClientError
from chalice.awsclient import TypedAWSClient
from chalice.constants import MAX_LAMBDA_DEPLOYMENT_SIZE,
from chalice.constants import MAX_LAMBDA_DEPLOYMENT_SIZE
from chalice.constants import VPC_ATTACH_POLICY
from chalice.constants import DEFAULT_LAMBDA_TIMEOUT
from chalice.constants import DEFAULT_LAMBDA_MEMORY_SIZE,
from chalice.constants import DEFAULT_LAMBDA_MEMORY_SIZE
from chalice.constants import LAMBDA_TRUST_POLICY
from chalice.constants import SQS_EVENT_SOURCE_POLICY
from chalice.constants import DEFAULT_STAGE_NAME
from chalice.constants import MIN_COMPRESSION_SIZE
from chalice.constants import MAX_COMPRESSION_SIZE
from chalice.deploy import models
from chalice.deploy.executor import Executor
from chalice.deploy.packager import PipRunner,
from chalice.deploy.packager import PipRunner
from chalice.deploy.packager import SubprocessPip
from chalice.deploy.packager import DependencyBuilder as PipDependencyBuilder
from chalice.deploy.packager import LambdaDeploymentPackager
@@ -448,6 +446,10 @@ def _create_rest_api_model(self,
# it's just <app>-<stage>.
function_name = '%s-%s' % (config.app_name, config.chalice_stage)
lambda_function.function_name = function_name
if config.minimum_compression_size is None:
minimum_compression = ''
else:
minimum_compression = str(config.minimum_compression_size)
authorizers = []
for auth in config.chalice_app.builtin_auth_handlers:
auth_lambda = self._create_lambda_model(
@@ -458,6 +460,7 @@ def _create_rest_api_model(self,
return models.RestAPI(
resource_name='rest_api',
swagger_doc=models.Placeholder.BUILD_STAGE,
minimum_compression=minimum_compression,
api_gateway_stage=config.api_gateway_stage,
lambda_function=lambda_function,
authorizers=authorizers,
1 change: 1 addition & 0 deletions chalice/deploy/models.py
Original file line number Diff line number Diff line change
@@ -180,6 +180,7 @@ def dependencies(self):
class RestAPI(ManagedModel):
resource_type = 'rest_api'
swagger_doc = attrib() # type: DV[Dict[str, Any]]
minimum_compression = attrib() # type: Optional[int]
api_gateway_stage = attrib() # type: str
lambda_function = attrib() # type: LambdaFunction
authorizers = attrib(default=Factory(list)) # type: List[LambdaFunction]
11 changes: 11 additions & 0 deletions chalice/deploy/planner.py
Original file line number Diff line number Diff line change
@@ -759,6 +759,17 @@ def _plan_restapi(self, resource):
# at the end of deploying a rest API that apply to both
# the update and create case.
shared_plan_epilogue = [
models.APICall(
method_name='update_rest_api',
params={
'rest_api_id': Variable('rest_api_id'),
'patch_operations': [{
'op': 'replace',
'path': '/minimumCompressionSize',
'value': resource.minimum_compression,
}],
},
),
models.APICall(
method_name='add_permission_for_apigateway',
params={'function_name': function_name,
4 changes: 3 additions & 1 deletion chalice/deploy/validate.py
Original file line number Diff line number Diff line change
@@ -6,6 +6,8 @@
from chalice import app # noqa
from chalice.config import Config # noqa
from chalice.constants import EXPERIMENTAL_ERROR_MSG
from chalice.constants import MIN_COMPRESSION_SIZE
from chalice.constants import MAX_COMPRESSION_SIZE


class ExperimentalFeatureError(Exception):
@@ -154,7 +156,7 @@ def validate_minimum_compression_size(config):
raise ValueError("'minimum_compression_size' must be an int.")
elif config.minimum_compression_size < MIN_COMPRESSION_SIZE \
or config.minimum_compression_size > MAX_COMPRESSION_SIZE:
raise ValueError("'minimum_compression_size' must be equal to or"
raise ValueError("'minimum_compression_size' must be equal to or "
"greater than %s and less than or equal to %s."
% (MIN_COMPRESSION_SIZE, MAX_COMPRESSION_SIZE))

5 changes: 5 additions & 0 deletions chalice/package.py
Original file line number Diff line number Diff line change
@@ -170,6 +170,11 @@ def _generate_restapi(self, resource, template):
'DefinitionBody': resource.swagger_doc,
}
}
if resource.minimum_compression:
properties = resources['RestAPI']['Properties']
properties['MinimumCompressionSize'] = \
int(resource.minimum_compression)

handler_cfn_name = to_cfn_resource_name(
resource.lambda_function.resource_name)
api_handler = template['Resources'].pop(handler_cfn_name)
1 change: 1 addition & 0 deletions tests/unit/deploy/test_deployer.py
Original file line number Diff line number Diff line change
@@ -1061,6 +1061,7 @@ def test_can_generate_swagger_builder(self):
rest_api = models.RestAPI(
resource_name='foo',
swagger_doc=models.Placeholder.BUILD_STAGE,
minimum_compression='',
api_gateway_stage='api',
lambda_function=None,
)
2 changes: 2 additions & 0 deletions tests/unit/deploy/test_models.py
Original file line number Diff line number Diff line change
@@ -39,6 +39,7 @@ def test_can_default_to_no_auths_in_rest_api(lambda_function):
rest_api = models.RestAPI(
resource_name='rest_api',
swagger_doc={'swagger': '2.0'},
minimum_compression='',
api_gateway_stage='api',
lambda_function=lambda_function,
)
@@ -51,6 +52,7 @@ def test_can_add_authorizers_to_dependencies(lambda_function):
rest_api = models.RestAPI(
resource_name='rest_api',
swagger_doc={'swagger': '2.0'},
minimum_compression='',
api_gateway_stage='api',
lambda_function=lambda_function,
authorizers=[auth1, auth2],
25 changes: 25 additions & 0 deletions tests/unit/deploy/test_planner.py
Original file line number Diff line number Diff line change
@@ -516,6 +516,7 @@ def test_can_plan_rest_api(self):
rest_api = models.RestAPI(
resource_name='rest_api',
swagger_doc={'swagger': '2.0'},
minimum_compression='100',
api_gateway_stage='api',
lambda_function=function,
)
@@ -536,6 +537,17 @@ def test_can_plan_rest_api(self):
models.APICall(method_name='deploy_rest_api',
params={'rest_api_id': Variable('rest_api_id'),
'api_gateway_stage': 'api'}),
models.APICall(
method_name='update_rest_api',
params={
'rest_api_id': Variable('rest_api_id'),
'patch_operations': [{
'op': 'replace',
'path': '/minimumCompressionSize',
'value': '100',
}],
},
),
models.APICall(
method_name='add_permission_for_apigateway',
params={
@@ -569,6 +581,7 @@ def test_can_update_rest_api(self):
rest_api = models.RestAPI(
resource_name='rest_api',
swagger_doc={'swagger': '2.0'},
minimum_compression='',
api_gateway_stage='api',
lambda_function=function,
)
@@ -605,6 +618,17 @@ def test_can_update_rest_api(self):
'account_id': Variable('account_id'),
'rest_api_id': Variable('rest_api_id')},
),
models.APICall(
method_name='update_rest_api',
params={
'rest_api_id': Variable('rest_api_id'),
'patch_operations': [{
'op': 'replace',
'path': '/minimumCompressionSize',
'value': '',
}],
},
),
models.APICall(
method_name='add_permission_for_apigateway',
params={'rest_api_id': Variable("rest_api_id"),
@@ -983,6 +1007,7 @@ def create_rest_api_model(self):
rest_api = models.RestAPI(
resource_name='rest_api',
swagger_doc={'swagger': '2.0'},
minimum_compression='',
api_gateway_stage='api',
lambda_function=None,
)
38 changes: 34 additions & 4 deletions tests/unit/deploy/test_validate.py
Original file line number Diff line number Diff line change
@@ -4,10 +4,15 @@
from chalice.app import Chalice
from chalice.config import Config
from chalice import CORSConfig
from chalice.deploy.validate import validate_configuration, validate_routes, \
validate_python_version, validate_route_content_types, \
validate_unique_function_names, validate_feature_flags, \
ExperimentalFeatureError
from chalice.constants import MIN_COMPRESSION_SIZE
from chalice.constants import MAX_COMPRESSION_SIZE
from chalice.deploy.validate import validate_configuration
from chalice.deploy.validate import validate_routes
from chalice.deploy.validate import validate_python_version
from chalice.deploy.validate import validate_route_content_types
from chalice.deploy.validate import validate_unique_function_names
from chalice.deploy.validate import validate_feature_flags
from chalice.deploy.validate import ExperimentalFeatureError


def test_trailing_slash_routes_result_in_error():
@@ -242,3 +247,28 @@ def test_can_validate_feature_flags(sample_app):
except ExperimentalFeatureError:
raise AssertionError("App was not suppose to raise an error when "
"opting in to features via a feature flag.")


def test_validation_error_if_minimum_compression_size_not_int(sample_app):
config = Config.create(chalice_app=sample_app,
minimum_compression_size='not int')
with pytest.raises(ValueError):
validate_configuration(config)


def test_validation_error_if_minimum_compression_size_invalid_int(sample_app):
config = Config.create(chalice_app=sample_app,
minimum_compression_size=MIN_COMPRESSION_SIZE-1)
with pytest.raises(ValueError):
validate_configuration(config)

config = Config.create(chalice_app=sample_app,
minimum_compression_size=MAX_COMPRESSION_SIZE+1)
with pytest.raises(ValueError):
validate_configuration(config)


def test_valid_minimum_compression_size(sample_app):
config = Config.create(chalice_app=sample_app,
minimum_compression_size=1)
assert validate_configuration(config) is None
17 changes: 16 additions & 1 deletion tests/unit/test_package.py
Original file line number Diff line number Diff line change
@@ -257,10 +257,23 @@ def test_can_generate_scheduled_event(self):
},
}

def test_can_generate_rest_api_without_compression(
self, sample_app_with_auth):
config = Config.create(chalice_app=sample_app_with_auth,
project_dir='.',
api_gateway_stage='api',
)
template = self.generate_template(config, 'dev')
resources = template['Resources']
assert 'MinimumCompressionSize' not in \
resources['RestAPI']['Properties']

def test_can_generate_rest_api(self, sample_app_with_auth):
config = Config.create(chalice_app=sample_app_with_auth,
project_dir='.',
api_gateway_stage='api')
api_gateway_stage='api',
minimum_compression_size=100,
)
template = self.generate_template(config, 'dev')
resources = template['Resources']
# Lambda function should be created.
@@ -279,6 +292,8 @@ def test_can_generate_rest_api(self, sample_app_with_auth):
{'RestAPIId': {'Ref': 'RestAPI'}}]}},
}
assert resources['RestAPI']['Type'] == 'AWS::Serverless::Api'
assert resources['RestAPI']['Properties']['MinimumCompressionSize'] \
== 100
# We should also create the auth lambda function.
assert resources['Myauth']['Type'] == 'AWS::Serverless::Function'
# Along with permission to invoke from API Gateway.

0 comments on commit 820d119

Please sign in to comment.