Skip to content

Commit ea97822

Browse files
committed
Merge pull request #1272 from dhermes/bigtable-cluster-undelete
Implemented Bigtable Cluster.undelete().
2 parents b4013b3 + 07ba7ba commit ea97822

File tree

2 files changed

+108
-0
lines changed

2 files changed

+108
-0
lines changed

gcloud/bigtable/cluster.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,11 @@
3939
_ADMIN_TYPE_URL_BASE = _TYPE_URL_BASE + 'admin.cluster.v1.'
4040
_CLUSTER_CREATE_METADATA = _ADMIN_TYPE_URL_BASE + 'CreateClusterMetadata'
4141
_UPDATE_CREATE_METADATA = _ADMIN_TYPE_URL_BASE + 'UpdateClusterMetadata'
42+
_UNDELETE_CREATE_METADATA = _ADMIN_TYPE_URL_BASE + 'UndeleteClusterMetadata'
4243
_TYPE_URL_MAP = {
4344
_CLUSTER_CREATE_METADATA: messages_pb2.CreateClusterMetadata,
4445
_UPDATE_CREATE_METADATA: messages_pb2.UpdateClusterMetadata,
46+
_UNDELETE_CREATE_METADATA: messages_pb2.UndeleteClusterMetadata,
4547
}
4648

4749

@@ -447,6 +449,39 @@ def delete(self):
447449
self._client._cluster_stub.DeleteCluster(
448450
request_pb, self._client.timeout_seconds)
449451

452+
def undelete(self):
453+
"""Undelete this cluster.
454+
455+
Cancels the scheduled deletion of an cluster and begins preparing it to
456+
resume serving. The returned operation will also be embedded as the
457+
cluster's ``current_operation``.
458+
459+
Immediately upon completion of this request:
460+
461+
* The cluster's ``delete_time`` field will be unset, protecting it from
462+
automatic deletion.
463+
464+
Until completion of the returned operation:
465+
466+
* The operation cannot be cancelled.
467+
468+
Upon completion of the returned operation:
469+
470+
* Billing for the cluster's resources will resume.
471+
* All tables within the cluster will be available.
472+
473+
:rtype: :class:`Operation`
474+
:returns: The long-running operation corresponding to the
475+
undelete operation.
476+
"""
477+
request_pb = messages_pb2.UndeleteClusterRequest(name=self.name)
478+
# We expect a `._generated.operations_pb2.Operation`
479+
operation_pb2 = self._client._cluster_stub.UndeleteCluster(
480+
request_pb, self._client.timeout_seconds)
481+
482+
op_id, op_begin = _process_operation(operation_pb2)
483+
return Operation('undelete', op_id, op_begin)
484+
450485
def list_tables(self):
451486
"""List the tables in this cluster.
452487

gcloud/bigtable/test_cluster.py

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,57 @@ def test_delete(self):
511511
{},
512512
)])
513513

514+
def test_undelete(self):
515+
from gcloud._testing import _Monkey
516+
from gcloud.bigtable._generated import (
517+
bigtable_cluster_service_messages_pb2 as messages_pb2)
518+
from gcloud.bigtable._generated import operations_pb2
519+
from gcloud.bigtable._testing import _FakeStub
520+
from gcloud.bigtable import cluster as MUT
521+
522+
project = 'PROJECT'
523+
zone = 'zone'
524+
cluster_id = 'cluster-id'
525+
timeout_seconds = 78
526+
527+
client = _Client(project, timeout_seconds=timeout_seconds)
528+
cluster = self._makeOne(zone, cluster_id, client)
529+
530+
# Create request_pb
531+
cluster_name = ('projects/' + project + '/zones/' + zone +
532+
'/clusters/' + cluster_id)
533+
request_pb = messages_pb2.UndeleteClusterRequest(name=cluster_name)
534+
535+
# Create response_pb
536+
response_pb = operations_pb2.Operation()
537+
538+
# Patch the stub used by the API method.
539+
client._cluster_stub = stub = _FakeStub(response_pb)
540+
541+
# Create expected_result.
542+
op_id = 5678
543+
op_begin = object()
544+
expected_result = MUT.Operation('undelete', op_id, op_begin)
545+
546+
# Create the mocks.
547+
process_operation_called = []
548+
549+
def mock_process_operation(operation_pb):
550+
process_operation_called.append(operation_pb)
551+
return op_id, op_begin
552+
553+
# Perform the method and check the result.
554+
with _Monkey(MUT, _process_operation=mock_process_operation):
555+
result = cluster.undelete()
556+
557+
self.assertEqual(result, expected_result)
558+
self.assertEqual(stub.method_calls, [(
559+
'UndeleteCluster',
560+
(request_pb, timeout_seconds),
561+
{},
562+
)])
563+
self.assertEqual(process_operation_called, [response_pb])
564+
514565
def _list_tables_helper(self, table_id, table_name=None):
515566
from gcloud.bigtable._generated import (
516567
bigtable_table_data_pb2 as table_data_pb2)
@@ -738,6 +789,28 @@ def test_with_update_cluster_metadata(self):
738789
result = self._callFUT(any_val)
739790
self.assertEqual(result, metadata)
740791

792+
def test_with_undelete_cluster_metadata(self):
793+
from gcloud.bigtable._generated import any_pb2
794+
from gcloud.bigtable._generated import (
795+
bigtable_cluster_data_pb2 as data_pb2)
796+
from gcloud.bigtable._generated import (
797+
bigtable_cluster_service_messages_pb2 as messages_pb2)
798+
from gcloud.bigtable._generated.timestamp_pb2 import Timestamp
799+
800+
type_url = ('type.googleapis.com/' +
801+
messages_pb2._UNDELETECLUSTERMETADATA.full_name)
802+
metadata = messages_pb2.UndeleteClusterMetadata(
803+
request_time=Timestamp(seconds=1, nanos=1234),
804+
finish_time=Timestamp(seconds=10, nanos=891011),
805+
)
806+
807+
any_val = any_pb2.Any(
808+
type_url=type_url,
809+
value=metadata.SerializeToString(),
810+
)
811+
result = self._callFUT(any_val)
812+
self.assertEqual(result, metadata)
813+
741814
def test_unknown_type_url(self):
742815
from gcloud._testing import _Monkey
743816
from gcloud.bigtable._generated import any_pb2

0 commit comments

Comments
 (0)