Skip to content

Commit c4ab6de

Browse files
committed
Merge pull request #1266 from dhermes/bigtable-cluster-copy
Implementing Bigtable Cluster.copy().
2 parents cc7a2ed + 796e122 commit c4ab6de

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

gcloud/bigtable/cluster.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,21 @@ def from_pb(cls, cluster_pb, client):
231231
result._update_from_pb(cluster_pb)
232232
return result
233233

234+
def copy(self):
235+
"""Make a copy of this cluster.
236+
237+
Copies the local data stored as simple types but does not copy the
238+
current state of any operations with the Cloud Bigtable API. Also
239+
copies the client attached to this instance.
240+
241+
:rtype: :class:`.Cluster`
242+
:returns: A copy of the current cluster.
243+
"""
244+
new_client = self._client.copy()
245+
return self.__class__(self.zone, self.cluster_id, new_client,
246+
display_name=self.display_name,
247+
serve_nodes=self.serve_nodes)
248+
234249
@property
235250
def name(self):
236251
"""Cluster name used in requests.

gcloud/bigtable/test_cluster.py

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,26 @@ def test_constructor_non_default(self):
5353
self.assertEqual(cluster.serve_nodes, serve_nodes)
5454
self.assertTrue(cluster._client is client)
5555

56+
def test_copy(self):
57+
project = 'PROJECT'
58+
zone = 'zone'
59+
cluster_id = 'cluster-id'
60+
display_name = 'display_name'
61+
serve_nodes = 8
62+
63+
client = _Client(project)
64+
cluster = self._makeOne(zone, cluster_id, client,
65+
display_name=display_name,
66+
serve_nodes=serve_nodes)
67+
new_cluster = cluster.copy()
68+
69+
# Make sure the client copy succeeded.
70+
self.assertFalse(new_cluster._client is client)
71+
self.assertEqual(new_cluster._client, client)
72+
# Make sure the client got copied to a new instance.
73+
self.assertFalse(cluster is new_cluster)
74+
self.assertEqual(cluster, new_cluster)
75+
5676
def test_table_factory(self):
5777
from gcloud.bigtable.table import Table
5878

@@ -80,7 +100,7 @@ def test_from_pb_success(self):
80100
cluster_pb = data_pb2.Cluster(
81101
name=cluster_name,
82102
display_name=cluster_id,
83-
serve_nodes=3,
103+
serve_nodes=331,
84104
)
85105

86106
klass = self._getTargetClass()
@@ -541,3 +561,12 @@ def __init__(self, project, timeout_seconds=None):
541561
self.project = project
542562
self.project_name = 'projects/' + self.project
543563
self.timeout_seconds = timeout_seconds
564+
565+
def copy(self):
566+
from copy import deepcopy
567+
return deepcopy(self)
568+
569+
def __eq__(self, other):
570+
return (other.project == self.project and
571+
other.project_name == self.project_name and
572+
other.timeout_seconds == self.timeout_seconds)

0 commit comments

Comments
 (0)