Skip to content

Commit

Permalink
feat: support OLM Prefix/Suffix (#773)
Browse files Browse the repository at this point in the history
* feat: support OLM Prefix/Suffix

* update tests
  • Loading branch information
cojenco committed Jun 7, 2022
1 parent 7b6895c commit 187cf50
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 6 deletions.
34 changes: 30 additions & 4 deletions google/cloud/storage/bucket.py
Expand Up @@ -163,11 +163,19 @@ class LifecycleRuleConditions(dict):
rule action to versioned items with at least one newer
version.
:type matches_prefix: list(str)
:param matches_prefix: (Optional) Apply rule action to items which
any prefix matches the beginning of the item name.
:type matches_storage_class: list(str), one or more of
:attr:`Bucket.STORAGE_CLASSES`.
:param matches_storage_class: (Optional) Apply rule action to items which
:param matches_storage_class: (Optional) Apply rule action to items
whose storage class matches this value.
:type matches_suffix: list(str)
:param matches_suffix: (Optional) Apply rule action to items which
any suffix matches the end of the item name.
:type number_of_newer_versions: int
:param number_of_newer_versions: (Optional) Apply rule action to versioned
items having N newer versions.
Expand Down Expand Up @@ -211,6 +219,8 @@ def __init__(
custom_time_before=None,
days_since_noncurrent_time=None,
noncurrent_time_before=None,
matches_prefix=None,
matches_suffix=None,
_factory=False,
):
conditions = {}
Expand All @@ -236,15 +246,21 @@ def __init__(
if custom_time_before is not None:
conditions["customTimeBefore"] = custom_time_before.isoformat()

if not _factory and not conditions:
raise ValueError("Supply at least one condition")

if days_since_noncurrent_time is not None:
conditions["daysSinceNoncurrentTime"] = days_since_noncurrent_time

if noncurrent_time_before is not None:
conditions["noncurrentTimeBefore"] = noncurrent_time_before.isoformat()

if matches_prefix is not None:
conditions["matchesPrefix"] = matches_prefix

if matches_suffix is not None:
conditions["matchesSuffix"] = matches_suffix

if not _factory and not conditions:
raise ValueError("Supply at least one condition")

super(LifecycleRuleConditions, self).__init__(conditions)

@classmethod
Expand Down Expand Up @@ -278,11 +294,21 @@ def is_live(self):
"""Conditon's 'is_live' value."""
return self.get("isLive")

@property
def matches_prefix(self):
"""Conditon's 'matches_prefix' value."""
return self.get("matchesPrefix")

@property
def matches_storage_class(self):
"""Conditon's 'matches_storage_class' value."""
return self.get("matchesStorageClass")

@property
def matches_suffix(self):
"""Conditon's 'matches_suffix' value."""
return self.get("matchesSuffix")

@property
def number_of_newer_versions(self):
"""Conditon's 'number_of_newer_versions' value."""
Expand Down
18 changes: 16 additions & 2 deletions tests/system/test_bucket.py
Expand Up @@ -47,6 +47,8 @@ def test_bucket_lifecycle_rules(storage_client, buckets_to_delete):
bucket_name = _helpers.unique_name("w-lifcycle-rules")
custom_time_before = datetime.date(2018, 8, 1)
noncurrent_before = datetime.date(2018, 8, 1)
matches_prefix = ["storage-sys-test", "gcs-sys-test"]
matches_suffix = ["suffix-test"]

with pytest.raises(exceptions.NotFound):
storage_client.get_bucket(bucket_name)
Expand All @@ -59,6 +61,8 @@ def test_bucket_lifecycle_rules(storage_client, buckets_to_delete):
custom_time_before=custom_time_before,
days_since_noncurrent_time=2,
noncurrent_time_before=noncurrent_before,
matches_prefix=matches_prefix,
matches_suffix=matches_suffix,
)
bucket.add_lifecycle_set_storage_class_rule(
constants.COLDLINE_STORAGE_CLASS,
Expand All @@ -77,6 +81,8 @@ def test_bucket_lifecycle_rules(storage_client, buckets_to_delete):
custom_time_before=custom_time_before,
days_since_noncurrent_time=2,
noncurrent_time_before=noncurrent_before,
matches_prefix=matches_prefix,
matches_suffix=matches_suffix,
),
LifecycleRuleSetStorageClass(
constants.COLDLINE_STORAGE_CLASS,
Expand All @@ -95,9 +101,17 @@ def test_bucket_lifecycle_rules(storage_client, buckets_to_delete):
assert list(bucket.lifecycle_rules) == expected_rules

# Test modifying lifecycle rules
expected_rules[0] = LifecycleRuleDelete(age=30)
expected_rules[0] = LifecycleRuleDelete(
age=30,
matches_prefix=["new-prefix"],
matches_suffix=["new-suffix"],
)
rules = list(bucket.lifecycle_rules)
rules[0]["condition"] = {"age": 30}
rules[0]["condition"] = {
"age": 30,
"matchesPrefix": ["new-prefix"],
"matchesSuffix": ["new-suffix"],
}
bucket.lifecycle_rules = rules
bucket.patch()

Expand Down
22 changes: 22 additions & 0 deletions tests/unit/test_bucket.py
Expand Up @@ -230,6 +230,28 @@ def test_ctor_w_noncurrent_time_before(self):
self.assertEqual(conditions.number_of_newer_versions, 3)
self.assertEqual(conditions.noncurrent_time_before, noncurrent_before)

def test_ctor_w_matches_prefix(self):
conditions = self._make_one(matches_prefix=["test-prefix"])
expected = {"matchesPrefix": ["test-prefix"]}
self.assertEqual(dict(conditions), expected)
self.assertIsNone(conditions.age)
self.assertIsNone(conditions.created_before)
self.assertIsNone(conditions.is_live)
self.assertIsNone(conditions.matches_storage_class)
self.assertIsNone(conditions.matches_suffix)
self.assertEqual(conditions.matches_prefix, ["test-prefix"])

def test_ctor_w_matches_suffix(self):
conditions = self._make_one(matches_suffix=["test-suffix"])
expected = {"matchesSuffix": ["test-suffix"]}
self.assertEqual(dict(conditions), expected)
self.assertIsNone(conditions.age)
self.assertIsNone(conditions.created_before)
self.assertIsNone(conditions.is_live)
self.assertIsNone(conditions.matches_storage_class)
self.assertIsNone(conditions.matches_prefix)
self.assertEqual(conditions.matches_suffix, ["test-suffix"])

def test_from_api_repr(self):
import datetime

Expand Down

0 comments on commit 187cf50

Please sign in to comment.