def _construct_rest_api(self):
        """Constructs and returns the ApiGateway RestApi.

        :returns: the RestApi to which this SAM Api corresponds
        :rtype: model.apigateway.ApiGatewayRestApi
        """
        rest_api = ApiGatewayRestApi(self.logical_id, depends_on=self.depends_on)
        rest_api.BinaryMediaTypes = self.binary_media

        if self.endpoint_configuration:
            self._set_endpoint_configuration(rest_api, self.endpoint_configuration)

        elif not RegionConfiguration.is_apigw_edge_configuration_supported():
            # Since this region does not support EDGE configuration, we explicitly set the endpoint type
            # to Regional which is the only supported config.
            self._set_endpoint_configuration(rest_api, "REGIONAL")

        if self.definition_uri and self.definition_body:
            raise InvalidResourceException(self.logical_id,
                                           "Specify either 'DefinitionUri' or 'DefinitionBody' property and not both")

        self._add_cors()
        self._add_auth()

        if self.definition_uri:
            rest_api.BodyS3Location = self._construct_body_s3_dict()
        elif self.definition_body:
            rest_api.Body = self.definition_body

        if self.name:
            rest_api.Name = self.name

        return rest_api
Example #2
0
    def _construct_rest_api(self):
        """Constructs and returns the ApiGateway RestApi.

        :returns: the RestApi to which this SAM Api corresponds
        :rtype: model.apigateway.ApiGatewayRestApi
        """
        rest_api = ApiGatewayRestApi(self.logical_id, depends_on=self.depends_on)
        rest_api.BinaryMediaTypes = self.binary_media

        if self.endpoint_configuration:
            self._set_endpoint_configuration(rest_api, self.endpoint_configuration)

        elif not RegionConfiguration.is_apigw_edge_configuration_supported():
            # Since this region does not support EDGE configuration, we explicitly set the endpoint type
            # to Regional which is the only supported config.
            self._set_endpoint_configuration(rest_api, "REGIONAL")


        if self.definition_uri and self.definition_body:
            raise InvalidResourceException(self.logical_id,
                                           "Specify either 'DefinitionUri' or 'DefinitionBody' property and not both")

        self._add_cors()

        if self.definition_uri:
            rest_api.BodyS3Location = self._construct_body_s3_dict()
        elif self.definition_body:
            rest_api.Body = self.definition_body

        if self.name:
            rest_api.Name = self.name

        return rest_api
    def test_when_apigw_edge_configuration_is_not_supported(self, partition):
        with patch(
                "samtranslator.translator.arn_generator.ArnGenerator.get_partition_name"
        ) as get_partition_name_patch:
            get_partition_name_patch.return_value = partition

            self.assertFalse(
                RegionConfiguration.is_apigw_edge_configuration_supported())
Example #4
0
    def _construct_rest_api(self):
        """Constructs and returns the ApiGateway RestApi.

        :returns: the RestApi to which this SAM Api corresponds
        :rtype: model.apigateway.ApiGatewayRestApi
        """
        rest_api = ApiGatewayRestApi(self.logical_id,
                                     depends_on=self.depends_on,
                                     attributes=self.resource_attributes)
        # NOTE: For backwards compatibility we need to retain BinaryMediaTypes on the CloudFormation Property
        # Removing this and only setting x-amazon-apigateway-binary-media-types results in other issues.
        rest_api.BinaryMediaTypes = self.binary_media
        rest_api.MinimumCompressionSize = self.minimum_compression_size

        if self.endpoint_configuration:
            self._set_endpoint_configuration(rest_api,
                                             self.endpoint_configuration)

        elif not RegionConfiguration.is_apigw_edge_configuration_supported():
            # Since this region does not support EDGE configuration, we explicitly set the endpoint type
            # to Regional which is the only supported config.
            self._set_endpoint_configuration(rest_api, "REGIONAL")

        if self.definition_uri and self.definition_body:
            raise InvalidResourceException(
                self.logical_id,
                "Specify either 'DefinitionUri' or 'DefinitionBody' property and not both."
            )

        if self.open_api_version:
            if not SwaggerEditor.safe_compare_regex_with_string(
                    SwaggerEditor.get_openapi_versions_supported_regex(),
                    self.open_api_version):
                raise InvalidResourceException(
                    self.logical_id,
                    "The OpenApiVersion value must be of the format '3.0.0'.")

        self._add_cors()
        self._add_auth()
        self._add_gateway_responses()
        self._add_binary_media_types()
        self._add_models()

        if self.definition_uri:
            rest_api.BodyS3Location = self._construct_body_s3_dict()
        elif self.definition_body:
            # # Post Process OpenApi Auth Settings
            self.definition_body = self._openapi_postprocess(
                self.definition_body)
            rest_api.Body = self.definition_body

        if self.name:
            rest_api.Name = self.name

        if self.description:
            rest_api.Description = self.description

        return rest_api
    def on_before_transform_template(self, template_dict):
        """
        Hook method that gets called before the SAM template is processed.
        The template has passed the validation and is guaranteed to contain a non-empty "Resources" section.

        This plugin needs to run as soon as possible to allow some time for templates to become available.
        This verifies that the user has access to all specified applications.

        :param dict template_dict: Dictionary of the SAM template
        :return: Nothing
        """
        template = SamTemplate(template_dict)
        intrinsic_resolvers = self._get_intrinsic_resolvers(
            template_dict.get("Mappings", {}))

        service_call = None
        if self._validate_only:
            service_call = self._handle_get_application_request
        else:
            service_call = self._handle_create_cfn_template_request
        for logical_id, app in template.iterate(
            {SamResourceType.Application.value}):
            if not self._can_process_application(app):
                # Handle these cases in the on_before_transform_resource event
                continue

            app_id = self._replace_value(app.properties[self.LOCATION_KEY],
                                         self.APPLICATION_ID_KEY,
                                         intrinsic_resolvers)

            semver = self._replace_value(app.properties[self.LOCATION_KEY],
                                         self.SEMANTIC_VERSION_KEY,
                                         intrinsic_resolvers)

            if isinstance(app_id, dict) or isinstance(semver, dict):
                key = (json.dumps(app_id), json.dumps(semver))
                self._applications[key] = False
                continue

            key = (app_id, semver)

            if key not in self._applications:
                try:
                    if not RegionConfiguration.is_sar_supported():
                        raise InvalidResourceException(
                            logical_id,
                            "Serverless Application Repository is not available in this region."
                        )
                    # Lazy initialization of the client- create it when it is needed
                    if not self._sar_client:
                        self._sar_client = boto3.client("serverlessrepo")
                    service_call(app_id, semver, key, logical_id)
                except InvalidResourceException as e:
                    # Catch all InvalidResourceExceptions, raise those in the before_resource_transform target.
                    self._applications[key] = e