diff --git a/gcloud/bigtable/cluster.py b/gcloud/bigtable/cluster.py index 019983885442..7826d8a50ef0 100644 --- a/gcloud/bigtable/cluster.py +++ b/gcloud/bigtable/cluster.py @@ -150,6 +150,21 @@ def from_pb(cls, cluster_pb, client): result._update_from_pb(cluster_pb) return result + def copy(self): + """Make a copy of this cluster. + + Copies the local data stored as simple types but does not copy the + current state of any operations with the Cloud Bigtable API. Also + copies the client attached to this instance. + + :rtype: :class:`.Cluster` + :returns: A copy of the current cluster. + """ + new_client = self._client.copy() + return self.__class__(self.zone, self.cluster_id, new_client, + display_name=self.display_name, + serve_nodes=self.serve_nodes) + @property def name(self): """Cluster name used in requests. diff --git a/gcloud/bigtable/test_cluster.py b/gcloud/bigtable/test_cluster.py index b8efe60e76ec..01b1f49fe344 100644 --- a/gcloud/bigtable/test_cluster.py +++ b/gcloud/bigtable/test_cluster.py @@ -53,6 +53,30 @@ def test_constructor_non_default(self): self.assertEqual(cluster.serve_nodes, serve_nodes) self.assertTrue(cluster._client is client) + def test_copy(self): + project = 'PROJECT' + zone = 'zone' + cluster_id = 'cluster-id' + display_name = 'display_name' + serve_nodes = 8 + + client = _Client(project) + cluster = self._makeOne(zone, cluster_id, client, + display_name=display_name, + serve_nodes=serve_nodes) + new_cluster = cluster.copy() + + # Make sure the client got copied to a new instance. + self.assertFalse(new_cluster._client is client) + self.assertEqual(new_cluster._client.__dict__, + client.__dict__) + + # Just replace the client on the new_cluster so we can + # check cluster equality. + new_cluster._client = client + self.assertFalse(cluster is new_cluster) + self.assertEqual(cluster, new_cluster) + def test_table_factory(self): from gcloud.bigtable.table import Table @@ -80,7 +104,7 @@ def test_from_pb_success(self): cluster_pb = data_pb2.Cluster( name=cluster_name, display_name=cluster_id, - serve_nodes=3, + serve_nodes=331, ) klass = self._getTargetClass() @@ -325,3 +349,7 @@ def __init__(self, project, timeout_seconds=None): self.project = project self.project_name = 'projects/' + self.project self.timeout_seconds = timeout_seconds + + def copy(self): + from copy import deepcopy + return deepcopy(self)