-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Alexei Mochalov <[email protected]>
- Loading branch information
1 parent
f8a7c5f
commit 1e6c2f6
Showing
7 changed files
with
174 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import typing as T | ||
|
||
import boto3 | ||
|
||
|
||
class BuildClientBase(T.Protocol): | ||
def __call__(self, session: boto3.Session, client_kwargs: dict[str, T.Any], **kwargs): ... | ||
|
||
|
||
class BuildClientHook(T.Protocol): | ||
def __call__( | ||
self, build_client_base: BuildClientBase, session: boto3.Session, client_kwargs: dict[str, T.Any], **kwargs | ||
): ... | ||
|
||
|
||
_build_client_hook = None | ||
|
||
|
||
def get_build_s3_client_hook() -> T.Optional[BuildClientHook]: | ||
""" | ||
Return build S3 client hook. | ||
""" | ||
|
||
return _build_client_hook | ||
|
||
|
||
def set_build_s3_client_hook(hook: T.Optional[BuildClientHook]) -> T.Optional[BuildClientHook]: | ||
""" | ||
Set build S3 client hook. | ||
Example for overriding `ServerSideEncryption` parameter for certain S3 operations: | ||
```python | ||
def event_handler(params, **kwargs): | ||
# Be mindful with parameters you set here. | ||
# Specifically it's not recommended to override/delete already set parameters | ||
# because that can break quilt3 logic. | ||
params.setdefault("ServerSideEncryption", "AES256") | ||
def hook(build_client_base, session, client_kwargs, **kwargs): | ||
client = build_client_base(session, client_kwargs, **kwargs) | ||
# Docs for boto3 events system we use below: | ||
# https://boto3.amazonaws.com/v1/documentation/api/latest/guide/events.html | ||
for op in ( | ||
"CreateMultipartUpload", | ||
"CopyObject", | ||
"PutObject", | ||
): | ||
client.meta.events.register(f"before-parameter-build.s3.{op}", event_handler) | ||
return client | ||
``` | ||
Args: | ||
hook: Build client hook. | ||
Returns: | ||
Old build client hook. | ||
""" | ||
global _build_client_hook | ||
old_hook = _build_client_hook | ||
_build_client_hook = hook | ||
return old_hook |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
from botocore.stub import Stubber | ||
|
||
from quilt3 import data_transfer, hooks, util | ||
|
||
|
||
def test_build_client_hooks(): | ||
try: | ||
assert hooks.get_build_s3_client_hook() is None | ||
|
||
stubber = None | ||
|
||
def event_handler(params, **kwargs): | ||
params.setdefault("ServerSideEncryption", "AES256") | ||
|
||
def hook(build_client_base, session, client_kwargs): | ||
client = build_client_base(session, client_kwargs) | ||
# use register_first and * to ensure that our hook runs before the stubber's one | ||
client.meta.events.register_first("before-parameter-build.*.*", event_handler) | ||
|
||
nonlocal stubber | ||
stubber = Stubber(client) | ||
stubber.add_response( | ||
"put_object", | ||
{}, | ||
{ | ||
"Bucket": "bucket", | ||
"Key": "key", | ||
"Body": b"data", | ||
"ServerSideEncryption": "AES256", | ||
}, | ||
) | ||
stubber.activate() | ||
|
||
return client | ||
|
||
assert hooks.set_build_s3_client_hook(hook) is None | ||
assert hooks.get_build_s3_client_hook() is hook | ||
|
||
data_transfer.put_bytes(b"data", util.PhysicalKey("bucket", "key", None)) | ||
|
||
assert stubber is not None | ||
stubber.assert_no_pending_responses() | ||
finally: | ||
hooks.set_build_s3_client_hook(None) | ||
assert hooks.get_build_s3_client_hook() is None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
|
||
# get\_build\_s3\_client\_hook() -> Optional[quilt3.hooks.BuildClientHook] {#get\_build\_s3\_client\_hook} | ||
|
||
Return build S3 client hook. | ||
|
||
|
||
# set\_build\_s3\_client\_hook(hook: Optional[quilt3.hooks.BuildClientHook]) -> Optional[quilt3.hooks.BuildClientHook] {#set\_build\_s3\_client\_hook} | ||
|
||
Set build S3 client hook. | ||
|
||
Example for overriding `ServerSideEncryption` parameter for certain S3 operations: | ||
|
||
```python | ||
def event_handler(params, **kwargs): | ||
# Be mindful with parameters you set here. | ||
# Specifically it's not recommended to override/delete already set parameters | ||
# because that can break quilt3 logic. | ||
params.setdefault("ServerSideEncryption", "AES256") | ||
|
||
def hook(build_client_base, session, client_kwargs, **kwargs): | ||
client = build_client_base(session, client_kwargs, **kwargs) | ||
# Docs for boto3 events system we use below: | ||
# https://boto3.amazonaws.com/v1/documentation/api/latest/guide/events.html | ||
for op in ( | ||
"CreateMultipartUpload", | ||
"CopyObject", | ||
"PutObject", | ||
): | ||
client.meta.events.register(f"before-parameter-build.s3.{op}", event_handler) | ||
return client | ||
``` | ||
|
||
__Arguments__ | ||
|
||
* __hook__: Build client hook. | ||
|
||
__Returns__ | ||
|
||
Old build client hook. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters