Skip to content

Commit

Permalink
Fix API Gateway stage not being set correctly
Browse files Browse the repository at this point in the history
The API Gateway stage is not set correctly since it was renamed from dev
to api. This change enforces the 'api' default all the way through the
config layer and CLI layer rather than making the end code that consumes
the configuration manually enforce a default everywhere the api gateway
stage name is used.

fixes aws#470
  • Loading branch information
stealthycoin committed Aug 14, 2017
1 parent fe0c83a commit 27a2a11
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 10 deletions.
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
6 changes: 2 additions & 4 deletions chalice/deploy/deployer.py
Original file line number Diff line number Diff line change
Expand Up @@ -805,8 +805,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_APIGATEWAY_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 @@ -819,8 +818,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_APIGATEWAY_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
7 changes: 4 additions & 3 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 @@ -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 @@ -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 27a2a11

Please sign in to comment.