From 796e1229d82faceb447ce90ce1439508f1f9eda8 Mon Sep 17 00:00:00 2001 From: Danny Hermes Date: Fri, 4 Dec 2015 15:38:43 -0800 Subject: [PATCH] Implementing Bigtable Cluster.copy(). --- gcloud/bigtable/cluster.py | 15 +++++++++++++++ gcloud/bigtable/test_cluster.py | 31 ++++++++++++++++++++++++++++++- 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/gcloud/bigtable/cluster.py b/gcloud/bigtable/cluster.py index 3141fde93110..47211d9a5017 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 fee12a7aa517..48895f34530a 100644 --- a/gcloud/bigtable/test_cluster.py +++ b/gcloud/bigtable/test_cluster.py @@ -53,6 +53,26 @@ 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 copy succeeded. + self.assertFalse(new_cluster._client is client) + self.assertEqual(new_cluster._client, client) + # Make sure the client got copied to a new instance. + self.assertFalse(cluster is new_cluster) + self.assertEqual(cluster, new_cluster) + def test_table_factory(self): from gcloud.bigtable.table import Table @@ -80,7 +100,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() @@ -363,3 +383,12 @@ 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) + + def __eq__(self, other): + return (other.project == self.project and + other.project_name == self.project_name and + other.timeout_seconds == self.timeout_seconds)