diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 407b4cb05..f14b7f7bc 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -9,6 +9,8 @@ Next Release (TBD) (`#450 `__) * Print useful error message when config.json is invalid (`#458 `__) +* Fix api gateway stage being set incorrectly in non-default chalice stage + (`#$70 `__) 1.0.0 diff --git a/README.rst b/README.rst index 4f018ce25..c625d2751 100644 --- a/README.rst +++ b/README.rst @@ -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: diff --git a/chalice/cli/__init__.py b/chalice/cli/__init__.py index 96b4de4e6..7e42c069d 100644 --- a/chalice/cli/__init__.py +++ b/chalice/cli/__init__.py @@ -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. ' diff --git a/chalice/cli/factory.py b/chalice/cli/factory.py index 932b8ba66..26cc1a12d 100644 --- a/chalice/cli/factory.py +++ b/chalice/cli/factory.py @@ -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 @@ -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} diff --git a/chalice/deploy/deployer.py b/chalice/deploy/deployer.py index 7cf74741e..29dd7d3b3 100644 --- a/chalice/deploy/deployer.py +++ b/chalice/deploy/deployer.py @@ -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 @@ -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) diff --git a/tests/functional/cli/test_cli.py b/tests/functional/cli/test_cli.py index cf23c2fc8..612b6abfd 100644 --- a/tests/functional/cli/test_cli.py +++ b/tests/functional/cli/test_cli.py @@ -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': { diff --git a/tests/functional/cli/test_factory.py b/tests/functional/cli/test_factory.py index b0ffb46cb..5dd11434f 100644 --- a/tests/functional/cli/test_factory.py +++ b/tests/functional/cli/test_factory.py @@ -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): diff --git a/tests/unit/deploy/test_deployer.py b/tests/unit/deploy/test_deployer.py index 94ad3e560..b5a5ad83a 100644 --- a/tests/unit/deploy/test_deployer.py +++ b/tests/unit/deploy/test_deployer.py @@ -98,6 +98,7 @@ def config_obj(sample_app): config = Config.create( chalice_app=sample_app, stage='dev', + api_gateway_stage='api', ) return config @@ -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 ) @@ -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' @@ -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 ) @@ -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) @@ -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)