Skip to content

Commit

Permalink
fix indentation for s3 storage getter and setter.
Browse files Browse the repository at this point in the history
Fixed storage_class change eventbridge notification test case accordingly
  • Loading branch information
MikiPWata committed Sep 11, 2024
1 parent f654058 commit 7364754
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 28 deletions.
34 changes: 17 additions & 17 deletions moto/s3/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,25 +231,25 @@ def set_metadata(self, metadata: Any, replace: bool = False) -> None:
self._metadata.update(metadata)


@property
def storage_class(self) -> Optional[str]:
return self._storage_class
@property
def storage_class_status(self) -> Optional[str]:
return self._storage_class


@storage_class.setter
def storage_class(self, storage: Optional[str]) -> None:
if storage is not None and storage not in STORAGE_CLASS:
raise InvalidStorageClass(storage=storage)
if self._storage_class != storage:
s3_backend = s3_backends[self.account_id][self.partition]
bucket = s3_backend.get_bucket(self.bucket_name) # type: ignore
notifications.send_event(
self.account_id,
notifications.S3NotificationEvent.LIFECYCLE_TRANSITION_EVENT,
bucket,
key=self,
)
self._storage_class = storage
@storage_class_status.setter
def storage_class_status(self, storage: Optional[str]) -> None:
if storage is not None and storage not in STORAGE_CLASS:
raise InvalidStorageClass(storage=storage)
if self._storage_class != storage:
s3_backend = s3_backends[self.account_id][self.partition]
bucket = s3_backend.get_bucket(self.bucket_name) # type: ignore
notifications.send_event(
self.account_id,
notifications.S3NotificationEvent.LIFECYCLE_TRANSITION_EVENT,
bucket,
key=self,
)
self._storage_class = storage

def set_expiry(self, expiry: Optional[datetime.datetime]) -> None:
self._expiry = expiry
Expand Down
27 changes: 16 additions & 11 deletions tests/test_s3/test_s3_eventbridge_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,19 +348,24 @@ def test_storage_class_change_notifications():
bucket_name = resource_names["bucket_name"]
s3_client = boto3.client("s3", region_name=REGION_NAME)

bucket = s3_client.Bucket(bucket_name)
key = bucket.put_object(Bucket=bucket_name, Key="keyname", Body="bodyofnewobject")
s3_client.put_object(Bucket=bucket_name, Key="keyname", Body="bodyofnewobject")

# Change the storage class
key.storage_class = "GLACIER"

events = _get_send_events()
event_names = [json.loads(e["message"])["detail"]["reason"] for e in events]
assert event_names == ["ObjectCreated", "StorageClassChanged"]
new_class = "GLACIER"
copy_source = {
"Bucket": bucket_name,
"Key": "keyname"
}

# Sanity check - changing the storage class to the same value does not trigger the event
key.storage_class = "GLACIER"
s3_client.copy_obejct(copy_source, bucket_name, "keyname_copy", ExtraArgs={'StorageClass': new_class})

events = _get_send_events()
event_names = [json.loads(e["message"])["detail"]["reason"] for e in events]
assert event_names == ["ObjectCreated", "StorageClassChanged"]
assert len(events) == 3
event_message = json.loads(events[2]["message"])
print(event_message)
assert event_message["detail-type"] == "Object Created"
assert event_message["source"] == "aws.s3"
assert event_message["account"] == ACCOUNT_ID
assert event_message["region"] == REGION_NAME
assert event_message["detail"]["bucket"]["name"] == bucket_name
assert event_message["detail"]["reason"] == "ObjectCreated"

0 comments on commit 7364754

Please sign in to comment.