Skip to content

Commit 5deba50

Browse files
feat: add Dataset.storage_billing_model setter, use client.update_dataset(ds, fields=["storage_billing_model"]) to update (#1643)
Adding Storage Billing Model property. See: https://cloud.google.com/bigquery/docs/updating-datasets#update_storage_billing_models --------- Co-authored-by: Tim Swast <[email protected]>
1 parent 7248f1f commit 5deba50

File tree

2 files changed

+57
-1
lines changed

2 files changed

+57
-1
lines changed

google/cloud/bigquery/dataset.py

+33
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,7 @@ class Dataset(object):
527527
"default_table_expiration_ms": "defaultTableExpirationMs",
528528
"friendly_name": "friendlyName",
529529
"default_encryption_configuration": "defaultEncryptionConfiguration",
530+
"storage_billing_model": "storageBillingModel",
530531
}
531532

532533
def __init__(self, dataset_ref) -> None:
@@ -763,6 +764,38 @@ def default_encryption_configuration(self, value):
763764
api_repr = value.to_api_repr()
764765
self._properties["defaultEncryptionConfiguration"] = api_repr
765766

767+
@property
768+
def storage_billing_model(self):
769+
"""Union[str, None]: StorageBillingModel of the dataset as set by the user
770+
(defaults to :data:`None`).
771+
772+
Set the value to one of ``'LOGICAL'`` or ``'PHYSICAL'``. This change
773+
takes 24 hours to take effect and you must wait 14 days before you can
774+
change the storage billing model again.
775+
776+
See `storage billing model
777+
<https://cloud.google.com/bigquery/docs/reference/rest/v2/datasets#Dataset.FIELDS.storage_billing_model>`_
778+
in REST API docs and `updating the storage billing model
779+
<https://cloud.google.com/bigquery/docs/updating-datasets#update_storage_billing_models>`_
780+
guide.
781+
782+
Raises:
783+
ValueError: for invalid value types.
784+
"""
785+
return self._properties.get("storageBillingModel")
786+
787+
@storage_billing_model.setter
788+
def storage_billing_model(self, value):
789+
if not isinstance(value, str) and value is not None:
790+
raise ValueError(
791+
"storage_billing_model must be a string (e.g. 'LOGICAL', 'PHYSICAL'), or None. "
792+
f"Got {repr(value)}."
793+
)
794+
if value:
795+
self._properties["storageBillingModel"] = value
796+
if value is None:
797+
self._properties["storageBillingModel"] = "LOGICAL"
798+
766799
@classmethod
767800
def from_string(cls, full_dataset_id: str) -> "Dataset":
768801
"""Construct a dataset from fully-qualified dataset ID.

tests/unit/test_dataset.py

+24-1
Original file line numberDiff line numberDiff line change
@@ -667,6 +667,7 @@ def _make_resource(self):
667667
"location": "US",
668668
"selfLink": self.RESOURCE_URL,
669669
"defaultTableExpirationMs": 3600,
670+
"storageBillingModel": "LOGICAL",
670671
"access": [
671672
{"role": "OWNER", "userByEmail": USER_EMAIL},
672673
{"role": "OWNER", "groupByEmail": GROUP_EMAIL},
@@ -736,7 +737,12 @@ def _verify_resource_properties(self, dataset, resource):
736737
)
737738
else:
738739
self.assertIsNone(dataset.default_encryption_configuration)
739-
740+
if "storageBillingModel" in resource:
741+
self.assertEqual(
742+
dataset.storage_billing_model, resource.get("storageBillingModel")
743+
)
744+
else:
745+
self.assertIsNone(dataset.storage_billing_model)
740746
if "access" in resource:
741747
self._verify_access_entry(dataset.access_entries, resource)
742748
else:
@@ -941,6 +947,23 @@ def test_default_encryption_configuration_setter(self):
941947
dataset.default_encryption_configuration = None
942948
self.assertIsNone(dataset.default_encryption_configuration)
943949

950+
def test_storage_billing_model_setter(self):
951+
dataset = self._make_one(self.DS_REF)
952+
dataset.storage_billing_model = "PHYSICAL"
953+
self.assertEqual(dataset.storage_billing_model, "PHYSICAL")
954+
955+
def test_storage_billing_model_setter_with_none(self):
956+
dataset = self._make_one(self.DS_REF)
957+
dataset.storage_billing_model = None
958+
self.assertEqual(dataset.storage_billing_model, "LOGICAL")
959+
960+
def test_storage_billing_model_setter_with_invalid_type(self):
961+
dataset = self._make_one(self.DS_REF)
962+
with self.assertRaises(ValueError) as raises:
963+
dataset.storage_billing_model = object()
964+
965+
self.assertIn("storage_billing_model", str(raises.exception))
966+
944967
def test_from_string(self):
945968
cls = self._get_target_class()
946969
got = cls.from_string("string-project.string_dataset")

0 commit comments

Comments
 (0)