-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Storage: add lifecycle rules helpers to bucket #5877
Conversation
@tseaver take a look at the write up again. I added additional information on tests. This is a case that should fail for setstorageclass if storage class is missing from action. One follow up for me is a better error message. |
When I move {'lifecycle': {'rule': [{'action': {'type': 'Delete'},
'condition': {'age': 42}},
{'action': {'storageClass': 'COLDLINE',
'type': 'SetStorageClass'},
'condition': {'isLive': False,
'matchesStorageClass': 'NEARLINE'}}]},
'name': 'w-lifcycle-rules-1535767555303'} and the 400 error response like this: {'error': {'code': 400,
'errors': [{'domain': 'global',
'message': 'Invalid location constraint ""',
'reason': 'invalid'}],
'message': 'Invalid location constraint ""'}} |
I fixed the mistake in the document. Thanks for catching it. Expected request:
The difference is matchesStorageClass expects an array of storage classes. |
f9c4f0f
to
1edc592
Compare
@frankyn I've moved 'setStorageClass (Pdb) pp properties
{'lifecycle': {'rule': [{'action': {'type': 'Delete'},
'condition': {'age': 42}},
{'action': {'storageClass': 'COLDLINE',
'type': 'SetStorageClass'},
'condition': {'isLive': False,
'matchesStorageClass': ['NEARLINE']}}]},
'name': 'w-lifcycle-rules-1536091244549'} (pdb) pp response.json()
{'error': {'code': 400,
'errors': [{'domain': 'global',
'message': 'Invalid location constraint ""',
'reason': 'invalid'}],
'message': 'Invalid location constraint ""'}} |
@tseaver I was able to set the policy given (but needed to change |
The values I showed are the Python dict before serializing to JSON. The dumped JSON looks like: (Pdb) pp data
('{"lifecycle": {"rule": [{"action": {"type": "Delete"}, "condition": {"age": '
'42}}, {"action": {"type": "SetStorageClass", "storageClass": "COLDLINE"}, '
'"condition": {"isLive": false, "matchesStorageClass": ["NEARLINE"]}}]}, '
'"name": "w-lifcycle-rules-1536096086990"}') |
face palm, I was able to repro. This seems like a bug, but not yet sure. The part I missed was including the lifecycle policy on a bucket create operation. |
By including location for the bucket |
a534067
to
9959a42
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@tseaver, looking at the surface,
What are your thoughts on a surface similar to:
from google.cloud import storage
storage_client = storage.Client()
bucket = storage_client.bucket("bucket-name")
bucket.add_lifecycle_delete_rule(...conditions..)
# OR
# this method may be too long
bucket.add_lifecycle_set_storage_class(storage_class, ...conditions...)
In implementation, the methods can construct LifecycleRuleDelete
or LifecycleRuleSetStorageClass
and update bucket.lifecycle_rules
for the bucket instance.
I'm open to thoughts. Apologies for not providing this feedback sooner.
return instance | ||
|
||
|
||
class LifecycleRuleSetItemStorageClass(dict): |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
return self.get('numNewerVersions') | ||
|
||
|
||
class LifecycleRuleDeleteItem(dict): |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
1e04aa6
to
96adbf6
Compare
@frankyn I'd argue that adding a systest which tries to provoke a 400 is pointless for Python: we expect the underlying layers to convert / raise them automagically. Also, the back-end bug where creating a bucket w/ lifecycle rules but w/o an explicit location could be fixed, which would then break the systest. |
(w.r.t to tests) The reason to test failure was to test behavior of client, but I'm open to removing this expectation. It seems unnecessary at this point. (w.r.t to the surface) The surface LGTM just have a question. I'm curious about the lifecycle of
OR
|
@frankyn Both LifecycleRuleDelete and LifecycleRuleSetStorageClass are derived from Neither one defines an |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @tseaver, additional comments on documentation.
self.lifecycle_rules = [] | ||
|
||
def add_lifecycle_delete_rule(self, **kw): | ||
"""Add a "delete" rule to lifestyle rules configured for this bucket. |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
|
||
def add_lifecycle_set_storage_class_rule(self, storage_class, **kw): | ||
"""Add a "delete" rule to lifestyle rules configured for this bucket. | ||
|
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
return instance | ||
|
||
@property | ||
def age(self): |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
Maps out allowed values for conditons for bucket's 'lifecycle_rules', and handles converting as appropriate.
Pass it as a list in all tests.
- 'clear_lifecycle_rules' - 'add_lifcycle_rule_delete' - 'add_lifecycle_rule_set_storage_class' Update system test to exercise them.
d7674b1
to
ac51158
Compare
Merging after an "LGTM" from @frankyn in chat. |
Thanks Tres! |
@frankyn Note that the system test added in the last commit currently fails. It is sending the POST body as
and fails with a 400:
I'd like help tracking down what is causing that error from the back-end's perspective.