示例#1
0
 def _create_rest_api_model(self,
                            config,        # type: Config
                            deployment,    # type: models.DeploymentPackage
                            chalice_app,   # type: app.Chalice
                            stage_name,    # type: str
                            ):
     # type: (...) -> models.RestAPI
     # Need to mess with the function name for back-compat.
     lambda_function = self._create_lambda_model(
         config=config, deployment=deployment, name='api_handler',
         handler_name='app.app', stage_name=stage_name
     )
     # For backwards compatibility with the old deployer, the
     # lambda function for the API handler doesn't have the
     # resource_name appended to its complete function_name,
     # it's just <app>-<stage>.
     function_name = '%s-%s' % (config.app_name, config.chalice_stage)
     lambda_function.function_name = function_name
     authorizers = []
     for auth in config.chalice_app.builtin_auth_handlers:
         auth_lambda = self._create_lambda_model(
             config=config, deployment=deployment, name=auth.name,
             handler_name=auth.handler_string, stage_name=stage_name,
         )
         authorizers.append(auth_lambda)
     return models.RestAPI(
         resource_name='rest_api',
         swagger_doc=models.Placeholder.BUILD_STAGE,
         api_gateway_stage=config.api_gateway_stage,
         lambda_function=lambda_function,
         authorizers=authorizers,
     )
示例#2
0
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'},
        api_gateway_stage='api',
        lambda_function=lambda_function,
    )
    assert rest_api.dependencies() == [lambda_function]
示例#3
0
 def create_rest_api_model(self):
     rest_api = models.RestAPI(
         resource_name='rest_api',
         swagger_doc={'swagger': '2.0'},
         api_gateway_stage='api',
         lambda_function=None,
     )
     return rest_api
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',
                              endpoint_type='EDGE',
                              lambda_function=lambda_function,
                              xray=False)
    assert rest_api.dependencies() == [lambda_function]
示例#5
0
def test_can_add_authorizers_to_dependencies(lambda_function):
    auth1 = evolve(lambda_function, resource_name='auth1')
    auth2 = evolve(lambda_function, resource_name='auth2')
    rest_api = models.RestAPI(
        resource_name='rest_api',
        swagger_doc={'swagger': '2.0'},
        api_gateway_stage='api',
        lambda_function=lambda_function,
        authorizers=[auth1, auth2],
    )
    assert rest_api.dependencies() == [lambda_function, auth1, auth2]
示例#6
0
    def _create_rest_api_model(
            self,
            config,  # type: Config
            deployment,  # type: models.DeploymentPackage
            stage_name,  # type: str
    ):
        # type: (...) -> models.RestAPI
        # Need to mess with the function name for back-compat.
        lambda_function = self._create_lambda_model(config=config,
                                                    deployment=deployment,
                                                    name='api_handler',
                                                    handler_name='app.app',
                                                    stage_name=stage_name)
        # For backwards compatibility with the old deployer, the
        # lambda function for the API handler doesn't have the
        # resource_name appended to its complete function_name,
        # 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(
                config=config,
                deployment=deployment,
                name=auth.name,
                handler_name=auth.handler_string,
                stage_name=stage_name,
            )
            authorizers.append(auth_lambda)

        policy = None
        policy_path = config.api_gateway_policy_file
        if (config.api_gateway_endpoint_type == 'PRIVATE' and not policy_path):
            policy = models.IAMPolicy(
                document=self._get_default_private_api_policy(config))
        elif policy_path:
            policy = models.FileBasedIAMPolicy(
                document=models.Placeholder.BUILD_STAGE,
                filename=os.path.join(config.project_dir, '.chalice',
                                      policy_path))

        return models.RestAPI(resource_name='rest_api',
                              swagger_doc=models.Placeholder.BUILD_STAGE,
                              endpoint_type=config.api_gateway_endpoint_type,
                              minimum_compression=minimum_compression,
                              api_gateway_stage=config.api_gateway_stage,
                              lambda_function=lambda_function,
                              authorizers=authorizers,
                              policy=policy)
示例#7
0
 def test_can_plan_rest_api(self):
     function = create_function_resource('function_name')
     rest_api = models.RestAPI(
         resource_name='rest_api',
         swagger_doc={'swagger': '2.0'},
         api_gateway_stage='api',
         lambda_function=function,
     )
     plan = self.determine_plan(rest_api)
     self.assert_loads_needed_variables(plan)
     assert plan[4:] == [
         models.APICall(
             method_name='import_rest_api',
             params={'swagger_document': {
                 'swagger': '2.0'
             }},
             output_var='rest_api_id',
         ),
         models.RecordResourceVariable(
             resource_type='rest_api',
             resource_name='rest_api',
             name='rest_api_id',
             variable_name='rest_api_id',
         ),
         models.APICall(method_name='deploy_rest_api',
                        params={
                            'rest_api_id': Variable('rest_api_id'),
                            'api_gateway_stage': 'api'
                        }),
         models.APICall(
             method_name='add_permission_for_apigateway_if_needed',
             params={
                 'function_name': 'appname-dev-function_name',
                 'region_name': Variable('region_name'),
                 'account_id': Variable('account_id'),
                 'rest_api_id': Variable('rest_api_id'),
             }),
         models.StoreValue(
             name='rest_api_url',
             value=StringFormat(
                 'https://{rest_api_id}.execute-api.{region_name}'
                 '.amazonaws.com/api/',
                 ['rest_api_id', 'region_name'],
             ),
         ),
         models.RecordResourceVariable(resource_type='rest_api',
                                       resource_name='rest_api',
                                       name='rest_api_url',
                                       variable_name='rest_api_url'),
     ]
     assert list(
         self.last_plan.messages.values()) == ['Creating Rest API\n']
def test_can_add_authorizers_to_dependencies(lambda_function):
    auth1 = evolve(lambda_function, resource_name='auth1')
    auth2 = evolve(lambda_function, resource_name='auth2')
    rest_api = models.RestAPI(
        resource_name='rest_api',
        swagger_doc={'swagger': '2.0'},
        minimum_compression='',
        api_gateway_stage='api',
        endpoint_type='EDGE',
        lambda_function=lambda_function,
        xray=False,
        authorizers=[auth1, auth2],
    )
    assert rest_api.dependencies() == [lambda_function, auth1, auth2]
示例#9
0
    def test_can_generate_swagger_builder(self):
        generator = mock.Mock(spec=SwaggerGenerator)
        generator.generate_swagger.return_value = {'swagger': '2.0'}

        rest_api = models.RestAPI(
            resource_name='foo',
            swagger_doc=models.Placeholder.BUILD_STAGE,
            minimum_compression='',
            endpoint_type='EDGE',
            api_gateway_stage='api',
            lambda_function=None,
        )
        app = Chalice(app_name='foo')
        config = Config.create(chalice_app=app)
        p = SwaggerBuilder(generator)
        p.handle(config, rest_api)
        assert rest_api.swagger_doc == {'swagger': '2.0'}
        generator.generate_swagger.assert_called_with(app, rest_api)
示例#10
0
 def test_can_update_rest_api(self):
     function = create_function_resource('function_name')
     rest_api = models.RestAPI(
         resource_name='rest_api',
         swagger_doc={'swagger': '2.0'},
         api_gateway_stage='api',
         lambda_function=function,
     )
     self.remote_state.declare_resource_exists(rest_api)
     self.remote_state.deployed_values['rest_api'] = {
         'rest_api_id': 'my_rest_api_id',
     }
     plan = self.determine_plan(rest_api)
     self.assert_loads_needed_variables(plan)
     assert plan[4:] == [
         models.StoreValue(name='rest_api_id', value='my_rest_api_id'),
         models.RecordResourceVariable(
             resource_type='rest_api',
             resource_name='rest_api',
             name='rest_api_id',
             variable_name='rest_api_id',
         ),
         models.APICall(
             method_name='update_api_from_swagger',
             params={
                 'rest_api_id': Variable('rest_api_id'),
                 'swagger_document': {
                     'swagger': '2.0'
                 },
             },
         ),
         models.APICall(
             method_name='deploy_rest_api',
             params={
                 'rest_api_id': Variable('rest_api_id'),
                 'api_gateway_stage': 'api'
             },
         ),
         models.APICall(
             method_name='add_permission_for_apigateway_if_needed',
             params={
                 'function_name': 'appname-dev-function_name',
                 'region_name': Variable('region_name'),
                 'account_id': Variable('account_id'),
                 'rest_api_id': Variable('rest_api_id')
             },
         ),
         models.APICall(
             method_name='add_permission_for_apigateway_if_needed',
             params={
                 'rest_api_id': Variable("rest_api_id"),
                 'region_name': Variable("region_name"),
                 'account_id': Variable("account_id"),
                 'function_name': 'appname-dev-function_name'
             },
             output_var=None),
         models.StoreValue(
             name='rest_api_url',
             value=StringFormat(
                 'https://{rest_api_id}.execute-api.{region_name}'
                 '.amazonaws.com/api/',
                 ['rest_api_id', 'region_name'],
             ),
         ),
         models.RecordResourceVariable(resource_type='rest_api',
                                       resource_name='rest_api',
                                       name='rest_api_url',
                                       variable_name='rest_api_url'),
     ]