Skip to content

Commit

Permalink
Merge pull request #475 from stealthycoin/fix-stages
Browse files Browse the repository at this point in the history
Fix API Gateway stage not being set correctly
  • Loading branch information
stealthycoin committed Aug 14, 2017
2 parents 8313fd0 + ccce980 commit 60816e8
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 11 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ Next Release (TBD)
(`#450 <https://github.com/aws/chalice/issues/450>`__)
* Print useful error message when config.json is invalid
(`#458 <https://github.com/aws/chalice/pull/458>`__)
* Fix api gateway stage being set incorrectly in non-default chalice stage
(`#$70 <https://github.com/aws/chalice/issues/470>`__)


1.0.0
Expand Down
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -690,7 +690,7 @@ Tutorial: Customizing the HTTP Response

The return value from a chalice view function is serialized as JSON as the
response body returned back to the caller. This makes it easy to create
rest APIs that return JSON resonse bodies.
rest APIs that return JSON response bodies.

Chalice allows you to control this behavior by returning an instance of
a chalice specific ``Response`` class. This behavior allows you to:
Expand Down
2 changes: 1 addition & 1 deletion chalice/cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ def run_local_server(factory, port, env):
default=None,
help='Automatically generate IAM policy for app code.')
@click.option('--profile', help='Override profile at deploy time.')
@click.option('--api-gateway-stage',
@click.option('--api-gateway-stage', default=DEFAULT_APIGATEWAY_STAGE_NAME,
help='Name of the API gateway stage to deploy to.')
@click.option('--stage', default=DEFAULT_STAGE_NAME,
help=('Name of the Chalice stage to deploy to. '
Expand Down
6 changes: 4 additions & 2 deletions chalice/cli/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from chalice.package import create_app_packager
from chalice.package import AppPackager # noqa
from chalice.constants import DEFAULT_STAGE_NAME
from chalice.constants import DEFAULT_APIGATEWAY_STAGE_NAME
from chalice.logs import LogRetriever
from chalice import local
from chalice.utils import UI # noqa
Expand Down Expand Up @@ -87,8 +88,9 @@ def create_default_deployer(self, session, ui):
session=session, ui=ui)

def create_config_obj(self, chalice_stage_name=DEFAULT_STAGE_NAME,
autogen_policy=None, api_gateway_stage=None):
# type: (str, Optional[bool], Optional[str]) -> Config
autogen_policy=None,
api_gateway_stage=DEFAULT_APIGATEWAY_STAGE_NAME):
# type: (str, Optional[bool], str) -> Config
user_provided_params = {} # type: Dict[str, Any]
default_params = {'project_dir': self.project_dir,
'autogen_policy': True}
Expand Down
4 changes: 2 additions & 2 deletions chalice/deploy/deployer.py
Original file line number Diff line number Diff line change
Expand Up @@ -804,7 +804,7 @@ def _first_time_deploy(self, config, deployed_resources):
# for each rest API, but that would require injecting chalice stage
# information into the swagger generator.
rest_api_id = self._aws_client.import_rest_api(swagger_doc)
api_gateway_stage = config.api_gateway_stage or DEFAULT_STAGE_NAME
api_gateway_stage = config.api_gateway_stage
self._deploy_api_to_stage(rest_api_id, api_gateway_stage,
deployed_resources)
return rest_api_id, self._aws_client.region_name, api_gateway_stage
Expand All @@ -817,7 +817,7 @@ def _create_resources_for_api(self, config, rest_api_id,
LOGGER.debug("Generating swagger document for rest API.")
swagger_doc = generator.generate_swagger(config.chalice_app)
self._aws_client.update_api_from_swagger(rest_api_id, swagger_doc)
api_gateway_stage = config.api_gateway_stage or DEFAULT_STAGE_NAME
api_gateway_stage = config.api_gateway_stage
self._deploy_api_to_stage(
rest_api_id, api_gateway_stage,
deployed_resources)
Expand Down
15 changes: 15 additions & 0 deletions tests/functional/cli/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,21 @@ def test_can_deploy(runner, mock_cli_factory, mock_deployer):
assert data == deployed_values


def test_does_deploy_with_default_api_gateway_stage_name(
runner, mock_cli_factory, mock_deployer):
with runner.isolated_filesystem():
cli.create_new_project_skeleton('testproject')
os.chdir('testproject')
_run_cli_command(runner, cli.deploy, [], cli_factory=mock_cli_factory)
call = mock_cli_factory.create_config_obj.call_args
expected_call = mock.call(
api_gateway_stage='api',
autogen_policy=None,
chalice_stage_name='dev'
)
assert call == expected_call


def test_can_delete(runner, mock_cli_factory, mock_deployer):
deployed_values = {
'dev': {
Expand Down
5 changes: 5 additions & 0 deletions tests/functional/cli/test_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,11 @@ def test_can_create_config_obj_with_api_gateway_stage(clifactory):
assert config.api_gateway_stage == 'custom-stage'


def test_can_create_config_obj_with_default_api_gateway_stage(clifactory):
config = clifactory.create_config_obj()
assert config.api_gateway_stage == 'api'


def test_cant_load_config_obj_with_bad_project(clifactory):
clifactory.project_dir = 'nowhere-asdfasdfasdfas'
with pytest.raises(RuntimeError):
Expand Down
11 changes: 6 additions & 5 deletions tests/unit/deploy/test_deployer.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ def config_obj(sample_app):
config = Config.create(
chalice_app=sample_app,
stage='dev',
api_gateway_stage='api',
)
return config

Expand Down Expand Up @@ -128,7 +129,7 @@ def test_api_gateway_deployer_initial_deploy(config_obj, ui):
assert isinstance(first_arg, dict)
assert 'swagger' in first_arg

aws_client.deploy_rest_api.assert_called_with('rest-api-id', 'dev')
aws_client.deploy_rest_api.assert_called_with('rest-api-id', 'api')
aws_client.add_permission_for_apigateway_if_needed.assert_called_with(
'func-name', 'us-west-2', 'account-id', 'rest-api-id', mock.ANY
)
Expand All @@ -140,7 +141,7 @@ def test_api_gateway_deployer_redeploy_api(config_obj, ui):
# The rest_api_id does not exist which will trigger
# the initial import
deployed = DeployedResources(
None, None, None, 'existing-id', 'dev', None, None, {})
None, None, None, 'existing-id', 'api', None, None, {})
aws_client.rest_api_exists.return_value = True
lambda_arn = 'arn:aws:lambda:us-west-2:account-id:function:func-name'

Expand All @@ -153,7 +154,7 @@ def test_api_gateway_deployer_redeploy_api(config_obj, ui):
assert isinstance(second_arg, dict)
assert 'swagger' in second_arg

aws_client.deploy_rest_api.assert_called_with('existing-id', 'dev')
aws_client.deploy_rest_api.assert_called_with('existing-id', 'api')
aws_client.add_permission_for_apigateway_if_needed.assert_called_with(
'func-name', 'us-west-2', 'account-id', 'existing-id', mock.ANY
)
Expand All @@ -164,7 +165,7 @@ def test_api_gateway_deployer_delete(config_obj, ui):

rest_api_id = 'abcdef1234'
deployed = DeployedResources(
None, None, None, rest_api_id, 'dev', None, None, {})
None, None, None, rest_api_id, 'api', None, None, {})
aws_client.rest_api_exists.return_value = True

d = APIGatewayDeployer(aws_client, ui)
Expand All @@ -178,7 +179,7 @@ def test_api_gateway_deployer_delete_already_deleted(ui):
aws_client.delete_rest_api.side_effect = ResourceDoesNotExistError(
rest_api_id)
deployed = DeployedResources(
None, None, None, rest_api_id, 'dev', None, None, {})
None, None, None, rest_api_id, 'api', None, None, {})
aws_client.rest_api_exists.return_value = True
d = APIGatewayDeployer(aws_client, ui)
d.delete(deployed)
Expand Down

0 comments on commit 60816e8

Please sign in to comment.