Ejemplo n.º 1
0
    def update_base_path_mapping(
        self,
        context: RequestContext,
        domain_name: String,
        base_path: String,
        patch_operations: ListOfPatchOperation = None,
    ) -> BasePathMapping:
        region_details = APIGatewayRegion.get()

        mappings_list = region_details.base_path_mappings.get(domain_name) or []

        mapping = ([m for m in mappings_list if m["basePath"] == base_path] or [None])[0]
        if mapping is None:
            raise NotFoundException(
                f"Not found: mapping for domain name {domain_name}, "
                f"base path {base_path} in list {mappings_list}"
            )

        patch_operations = ensure_list(patch_operations)
        for operation in patch_operations:
            if operation["path"] == "/restapiId":
                operation["path"] = "/restApiId"
        result = apply_json_patch_safe(mapping, patch_operations)

        for i in range(len(mappings_list)):
            if mappings_list[i]["basePath"] == base_path:
                mappings_list[i] = result

        result = to_base_mapping_response_json(domain_name, base_path, result)
        return BasePathMapping(**result)
Ejemplo n.º 2
0
def delete_all_s3_objects(buckets):
    s3_client = aws_stack.connect_to_service("s3")
    buckets = ensure_list(buckets)
    for bucket in buckets:
        keys = all_s3_object_keys(bucket)
        deletes = [{"Key": key} for key in keys]
        if deletes:
            s3_client.delete_objects(Bucket=bucket,
                                     Delete={"Objects": deletes})
Ejemplo n.º 3
0
def normalize_authorizer(data):
    is_list = isinstance(data, list)
    entries = ensure_list(data)
    for i in range(len(entries)):
        entry = deepcopy(entries[i])
        # terraform sends this as a string in patch, so convert to int
        entry["authorizerResultTtlInSeconds"] = int(entry.get("authorizerResultTtlInSeconds", 300))
        entries[i] = entry
    return entries if is_list else entries[0]
Ejemplo n.º 4
0
def map_all_s3_objects(to_json: bool = True,
                       buckets: List[str] = None) -> Dict[str, Any]:
    s3_client = aws_stack.connect_to_resource("s3")
    result = {}
    buckets = ensure_list(buckets)
    buckets = [s3_client.Bucket(b)
               for b in buckets] if buckets else s3_client.buckets.all()
    for bucket in buckets:
        for key in bucket.objects.all():
            value = download_s3_object(s3_client, key.bucket_name, key.key)
            try:
                if to_json:
                    value = json.loads(value)
                key = "%s%s%s" % (
                    key.bucket_name,
                    "" if key.key.startswith("/") else "/",
                    key.key,
                )
                result[key] = value
            except Exception:
                # skip non-JSON or binary objects
                pass
    return result
Ejemplo n.º 5
0
    def _patch_api_gateway_entity(self, entity: Dict) -> Optional[Tuple[int, Dict, str]]:
        not_supported_attributes = ["/id", "/region_name", "/create_date"]

        patch_operations = self._get_param("patchOperations")

        model_attributes = list(entity.keys())
        for operation in patch_operations:
            if operation["path"].strip("/") in REST_API_ATTRIBUTES:
                operation["path"] = camelcase_to_underscores(operation["path"])
            path_start = operation["path"].strip("/").split("/")[0]
            path_start_usc = camelcase_to_underscores(path_start)
            if path_start not in model_attributes and path_start_usc in model_attributes:
                operation["path"] = operation["path"].replace(path_start, path_start_usc)
            if operation["path"] in not_supported_attributes:
                msg = f'Invalid patch path {operation["path"]}'
                return 400, {}, msg

        apply_json_patch_safe(entity, patch_operations, in_place=True)
        # apply some type fixes - TODO refactor/generalize
        if "disable_execute_api_endpoint" in entity:
            entity["disableExecuteApiEndpoint"] = bool(entity.pop("disable_execute_api_endpoint"))
        if "binary_media_types" in entity:
            entity["binaryMediaTypes"] = ensure_list(entity.pop("binary_media_types"))