|
102 | 102 | "md5Hash", |
103 | 103 | "metadata", |
104 | 104 | "name", |
| 105 | + "retention", |
105 | 106 | "storageClass", |
106 | 107 | ) |
107 | 108 | _READ_LESS_THAN_SIZE = ( |
@@ -1700,6 +1701,7 @@ def _get_writable_metadata(self): |
1700 | 1701 | * ``md5Hash`` |
1701 | 1702 | * ``metadata`` |
1702 | 1703 | * ``name`` |
| 1704 | + * ``retention`` |
1703 | 1705 | * ``storageClass`` |
1704 | 1706 |
|
1705 | 1707 | For now, we don't support ``acl``, access control lists should be |
@@ -4667,6 +4669,16 @@ def custom_time(self, value): |
4667 | 4669 |
|
4668 | 4670 | self._patch_property("customTime", value) |
4669 | 4671 |
|
| 4672 | + @property |
| 4673 | + def retention(self): |
| 4674 | + """Retrieve the retention configuration for this object. |
| 4675 | +
|
| 4676 | + :rtype: :class:`Retention` |
| 4677 | + :returns: an instance for managing the object's retention configuration. |
| 4678 | + """ |
| 4679 | + info = self._properties.get("retention", {}) |
| 4680 | + return Retention.from_api_repr(info, self) |
| 4681 | + |
4670 | 4682 |
|
4671 | 4683 | def _get_host_name(connection): |
4672 | 4684 | """Returns the host name from the given connection. |
@@ -4797,3 +4809,126 @@ def _add_query_parameters(base_url, name_value_pairs): |
4797 | 4809 | query = parse_qsl(query) |
4798 | 4810 | query.extend(name_value_pairs) |
4799 | 4811 | return urlunsplit((scheme, netloc, path, urlencode(query), frag)) |
| 4812 | + |
| 4813 | + |
| 4814 | +class Retention(dict): |
| 4815 | + """Map an object's retention configuration. |
| 4816 | +
|
| 4817 | + :type blob: :class:`Blob` |
| 4818 | + :params blob: blob for which this retention configuration applies to. |
| 4819 | +
|
| 4820 | + :type mode: str or ``NoneType`` |
| 4821 | + :params mode: |
| 4822 | + (Optional) The mode of the retention configuration, which can be either Unlocked or Locked. |
| 4823 | + See: https://cloud.google.com/storage/docs/object-lock |
| 4824 | +
|
| 4825 | + :type retain_until_time: :class:`datetime.datetime` or ``NoneType`` |
| 4826 | + :params retain_until_time: |
| 4827 | + (Optional) The earliest time that the object can be deleted or replaced, which is the |
| 4828 | + retention configuration set for this object. |
| 4829 | +
|
| 4830 | + :type retention_expiration_time: :class:`datetime.datetime` or ``NoneType`` |
| 4831 | + :params retention_expiration_time: |
| 4832 | + (Optional) The earliest time that the object can be deleted, which depends on any |
| 4833 | + retention configuration set for the object and any retention policy set for the bucket |
| 4834 | + that contains the object. This value should normally only be set by the back-end API. |
| 4835 | + """ |
| 4836 | + |
| 4837 | + def __init__( |
| 4838 | + self, |
| 4839 | + blob, |
| 4840 | + mode=None, |
| 4841 | + retain_until_time=None, |
| 4842 | + retention_expiration_time=None, |
| 4843 | + ): |
| 4844 | + data = {"mode": mode} |
| 4845 | + if retain_until_time is not None: |
| 4846 | + retain_until_time = _datetime_to_rfc3339(retain_until_time) |
| 4847 | + data["retainUntilTime"] = retain_until_time |
| 4848 | + |
| 4849 | + if retention_expiration_time is not None: |
| 4850 | + retention_expiration_time = _datetime_to_rfc3339(retention_expiration_time) |
| 4851 | + data["retentionExpirationTime"] = retention_expiration_time |
| 4852 | + |
| 4853 | + super(Retention, self).__init__(data) |
| 4854 | + self._blob = blob |
| 4855 | + |
| 4856 | + @classmethod |
| 4857 | + def from_api_repr(cls, resource, blob): |
| 4858 | + """Factory: construct instance from resource. |
| 4859 | +
|
| 4860 | + :type blob: :class:`Blob` |
| 4861 | + :params blob: Blob for which this retention configuration applies to. |
| 4862 | +
|
| 4863 | + :type resource: dict |
| 4864 | + :param resource: mapping as returned from API call. |
| 4865 | +
|
| 4866 | + :rtype: :class:`Retention` |
| 4867 | + :returns: Retention configuration created from resource. |
| 4868 | + """ |
| 4869 | + instance = cls(blob) |
| 4870 | + instance.update(resource) |
| 4871 | + return instance |
| 4872 | + |
| 4873 | + @property |
| 4874 | + def blob(self): |
| 4875 | + """Blob for which this retention configuration applies to. |
| 4876 | +
|
| 4877 | + :rtype: :class:`Blob` |
| 4878 | + :returns: the instance's blob. |
| 4879 | + """ |
| 4880 | + return self._blob |
| 4881 | + |
| 4882 | + @property |
| 4883 | + def mode(self): |
| 4884 | + """The mode of the retention configuration. Options are 'Unlocked' or 'Locked'. |
| 4885 | +
|
| 4886 | + :rtype: string |
| 4887 | + :returns: The mode of the retention configuration, which can be either set to 'Unlocked' or 'Locked'. |
| 4888 | + """ |
| 4889 | + return self.get("mode") |
| 4890 | + |
| 4891 | + @mode.setter |
| 4892 | + def mode(self, value): |
| 4893 | + self["mode"] = value |
| 4894 | + self.blob._patch_property("retention", self) |
| 4895 | + |
| 4896 | + @property |
| 4897 | + def retain_until_time(self): |
| 4898 | + """The earliest time that the object can be deleted or replaced, which is the |
| 4899 | + retention configuration set for this object. |
| 4900 | +
|
| 4901 | + :rtype: :class:`datetime.datetime` or ``NoneType`` |
| 4902 | + :returns: Datetime object parsed from RFC3339 valid timestamp, or |
| 4903 | + ``None`` if the blob's resource has not been loaded from |
| 4904 | + the server (see :meth:`reload`). |
| 4905 | + """ |
| 4906 | + value = self.get("retainUntilTime") |
| 4907 | + if value is not None: |
| 4908 | + return _rfc3339_nanos_to_datetime(value) |
| 4909 | + |
| 4910 | + @retain_until_time.setter |
| 4911 | + def retain_until_time(self, value): |
| 4912 | + """Set the retain_until_time for the object retention configuration. |
| 4913 | +
|
| 4914 | + :type value: :class:`datetime.datetime` |
| 4915 | + :param value: The earliest time that the object can be deleted or replaced. |
| 4916 | + """ |
| 4917 | + if value is not None: |
| 4918 | + value = _datetime_to_rfc3339(value) |
| 4919 | + self["retainUntilTime"] = value |
| 4920 | + self.blob._patch_property("retention", self) |
| 4921 | + |
| 4922 | + @property |
| 4923 | + def retention_expiration_time(self): |
| 4924 | + """The earliest time that the object can be deleted, which depends on any |
| 4925 | + retention configuration set for the object and any retention policy set for |
| 4926 | + the bucket that contains the object. |
| 4927 | +
|
| 4928 | + :rtype: :class:`datetime.datetime` or ``NoneType`` |
| 4929 | + :returns: |
| 4930 | + (readonly) The earliest time that the object can be deleted. |
| 4931 | + """ |
| 4932 | + retention_expiration_time = self.get("retentionExpirationTime") |
| 4933 | + if retention_expiration_time is not None: |
| 4934 | + return _rfc3339_nanos_to_datetime(retention_expiration_time) |
0 commit comments