Skip to content

Commit 7e9c61d

Browse files
committed
Update bigtable.column_family to use V2 protos.
Note that {Create,Update,Delete}ColumnFamily messages all collapse to ModifyColumnFamilies.
1 parent 3c533e0 commit 7e9c61d

File tree

2 files changed

+85
-70
lines changed

2 files changed

+85
-70
lines changed

gcloud/bigtable/column_family.py

Lines changed: 45 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@
2020
from google.protobuf import duration_pb2
2121

2222
from gcloud._helpers import _total_seconds
23-
from gcloud.bigtable._generated import (
24-
bigtable_table_data_pb2 as data_v1_pb2)
25-
from gcloud.bigtable._generated import (
26-
bigtable_table_service_messages_pb2 as messages_v1_pb2)
23+
from gcloud.bigtable._generated_v2 import (
24+
table_pb2 as table_v2_pb2)
25+
from gcloud.bigtable._generated_v2 import (
26+
bigtable_table_admin_pb2 as table_admin_v2_pb2)
2727

2828

2929
def _timedelta_to_duration_pb(timedelta_val):
@@ -111,10 +111,10 @@ def __eq__(self, other):
111111
def to_pb(self):
112112
"""Converts the garbage collection rule to a protobuf.
113113
114-
:rtype: :class:`.data_v1_pb2.GcRule`
114+
:rtype: :class:`.table_v2_pb2.GcRule`
115115
:returns: The converted current object.
116116
"""
117-
return data_v1_pb2.GcRule(max_num_versions=self.max_num_versions)
117+
return table_v2_pb2.GcRule(max_num_versions=self.max_num_versions)
118118

119119

120120
class MaxAgeGCRule(GarbageCollectionRule):
@@ -135,11 +135,11 @@ def __eq__(self, other):
135135
def to_pb(self):
136136
"""Converts the garbage collection rule to a protobuf.
137137
138-
:rtype: :class:`.data_v1_pb2.GcRule`
138+
:rtype: :class:`.table_v2_pb2.GcRule`
139139
:returns: The converted current object.
140140
"""
141141
max_age = _timedelta_to_duration_pb(self.max_age)
142-
return data_v1_pb2.GcRule(max_age=max_age)
142+
return table_v2_pb2.GcRule(max_age=max_age)
143143

144144

145145
class GCRuleUnion(GarbageCollectionRule):
@@ -160,12 +160,12 @@ def __eq__(self, other):
160160
def to_pb(self):
161161
"""Converts the union into a single GC rule as a protobuf.
162162
163-
:rtype: :class:`.data_v1_pb2.GcRule`
163+
:rtype: :class:`.table_v2_pb2.GcRule`
164164
:returns: The converted current object.
165165
"""
166-
union = data_v1_pb2.GcRule.Union(
166+
union = table_v2_pb2.GcRule.Union(
167167
rules=[rule.to_pb() for rule in self.rules])
168-
return data_v1_pb2.GcRule(union=union)
168+
return table_v2_pb2.GcRule(union=union)
169169

170170

171171
class GCRuleIntersection(GarbageCollectionRule):
@@ -186,12 +186,12 @@ def __eq__(self, other):
186186
def to_pb(self):
187187
"""Converts the intersection into a single GC rule as a protobuf.
188188
189-
:rtype: :class:`.data_v1_pb2.GcRule`
189+
:rtype: :class:`.table_v2_pb2.GcRule`
190190
:returns: The converted current object.
191191
"""
192-
intersection = data_v1_pb2.GcRule.Intersection(
192+
intersection = table_v2_pb2.GcRule.Intersection(
193193
rules=[rule.to_pb() for rule in self.rules])
194-
return data_v1_pb2.GcRule(intersection=intersection)
194+
return table_v2_pb2.GcRule(intersection=intersection)
195195

196196

197197
class ColumnFamily(object):
@@ -251,21 +251,22 @@ def __ne__(self, other):
251251
def create(self):
252252
"""Create this column family."""
253253
if self.gc_rule is None:
254-
column_family = data_v1_pb2.ColumnFamily()
254+
column_family = table_v2_pb2.ColumnFamily()
255255
else:
256-
column_family = data_v1_pb2.ColumnFamily(
256+
column_family = table_v2_pb2.ColumnFamily(
257257
gc_rule=self.gc_rule.to_pb())
258-
request_pb = messages_v1_pb2.CreateColumnFamilyRequest(
259-
name=self._table.name,
260-
column_family_id=self.column_family_id,
261-
column_family=column_family,
258+
request_pb = table_admin_v2_pb2.ModifyColumnFamiliesRequest(
259+
name=self._table.name)
260+
request_pb.modifications.add(
261+
id=self.column_family_id,
262+
create=column_family,
262263
)
263264
client = self._table._cluster._client
264-
# We expect a `.data_v1_pb2.ColumnFamily`. We ignore it since the only
265+
# We expect a `.table_v2_pb2.ColumnFamily`. We ignore it since the only
265266
# data it contains are the GC rule and the column family ID already
266267
# stored on this instance.
267-
client._table_stub.CreateColumnFamily(request_pb,
268-
client.timeout_seconds)
268+
client._table_stub.ModifyColumnFamilies(request_pb,
269+
client.timeout_seconds)
269270

270271
def update(self):
271272
"""Update this column family.
@@ -275,30 +276,40 @@ def update(self):
275276
Only the GC rule can be updated. By changing the column family ID,
276277
you will simply be referring to a different column family.
277278
"""
278-
request_kwargs = {'name': self.name}
279-
if self.gc_rule is not None:
280-
request_kwargs['gc_rule'] = self.gc_rule.to_pb()
281-
request_pb = data_v1_pb2.ColumnFamily(**request_kwargs)
279+
if self.gc_rule is None:
280+
column_family = table_v2_pb2.ColumnFamily()
281+
else:
282+
column_family = table_v2_pb2.ColumnFamily(
283+
gc_rule=self.gc_rule.to_pb())
284+
request_pb = table_admin_v2_pb2.ModifyColumnFamiliesRequest(
285+
name=self._table.name)
286+
request_pb.modifications.add(
287+
id=self.column_family_id,
288+
update=column_family)
282289
client = self._table._cluster._client
283-
# We expect a `.data_v1_pb2.ColumnFamily`. We ignore it since the only
290+
# We expect a `.table_v2_pb2.ColumnFamily`. We ignore it since the only
284291
# data it contains are the GC rule and the column family ID already
285292
# stored on this instance.
286-
client._table_stub.UpdateColumnFamily(request_pb,
287-
client.timeout_seconds)
293+
client._table_stub.ModifyColumnFamilies(request_pb,
294+
client.timeout_seconds)
288295

289296
def delete(self):
290297
"""Delete this column family."""
291-
request_pb = messages_v1_pb2.DeleteColumnFamilyRequest(name=self.name)
298+
request_pb = table_admin_v2_pb2.ModifyColumnFamiliesRequest(
299+
name=self._table.name)
300+
request_pb.modifications.add(
301+
id=self.column_family_id,
302+
drop=True)
292303
client = self._table._cluster._client
293304
# We expect a `google.protobuf.empty_pb2.Empty`
294-
client._table_stub.DeleteColumnFamily(request_pb,
295-
client.timeout_seconds)
305+
client._table_stub.ModifyColumnFamilies(request_pb,
306+
client.timeout_seconds)
296307

297308

298309
def _gc_rule_from_pb(gc_rule_pb):
299310
"""Convert a protobuf GC rule to a native object.
300311
301-
:type gc_rule_pb: :class:`.data_v1_pb2.GcRule`
312+
:type gc_rule_pb: :class:`.table_v2_pb2.GcRule`
302313
:param gc_rule_pb: The GC rule to convert.
303314
304315
:rtype: :class:`GarbageCollectionRule` or :data:`NoneType <types.NoneType>`

gcloud/bigtable/test_column_family.py

Lines changed: 40 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -389,8 +389,8 @@ def test___ne__(self):
389389
self.assertNotEqual(column_family1, column_family2)
390390

391391
def _create_test_helper(self, gc_rule=None):
392-
from gcloud.bigtable._generated import (
393-
bigtable_table_service_messages_pb2 as messages_v1_pb2)
392+
from gcloud.bigtable._generated_v2 import (
393+
bigtable_table_admin_pb2 as table_admin_v2_pb2)
394394
from gcloud.bigtable._testing import _FakeStub
395395

396396
project_id = 'project-id'
@@ -411,12 +411,12 @@ def _create_test_helper(self, gc_rule=None):
411411
if gc_rule is None:
412412
column_family_pb = _ColumnFamilyPB()
413413
else:
414-
column_family_pb = _ColumnFamilyPB(
415-
gc_rule=gc_rule.to_pb())
416-
request_pb = messages_v1_pb2.CreateColumnFamilyRequest(
417-
name=table_name,
418-
column_family_id=column_family_id,
419-
column_family=column_family_pb,
414+
column_family_pb = _ColumnFamilyPB(gc_rule=gc_rule.to_pb())
415+
request_pb = table_admin_v2_pb2.ModifyColumnFamiliesRequest(
416+
name=table_name)
417+
request_pb.modifications.add(
418+
id=column_family_id,
419+
create=column_family_pb,
420420
)
421421

422422
# Create response_pb
@@ -434,7 +434,7 @@ def _create_test_helper(self, gc_rule=None):
434434
self.assertEqual(stub.results, ())
435435
self.assertEqual(result, expected_result)
436436
self.assertEqual(stub.method_calls, [(
437-
'CreateColumnFamily',
437+
'ModifyColumnFamilies',
438438
(request_pb, timeout_seconds),
439439
{},
440440
)])
@@ -449,6 +449,8 @@ def test_create_with_gc_rule(self):
449449

450450
def _update_test_helper(self, gc_rule=None):
451451
from gcloud.bigtable._testing import _FakeStub
452+
from gcloud.bigtable._generated_v2 import (
453+
bigtable_table_admin_pb2 as table_admin_v2_pb2)
452454

453455
project_id = 'project-id'
454456
zone = 'zone'
@@ -458,8 +460,6 @@ def _update_test_helper(self, gc_rule=None):
458460
timeout_seconds = 28
459461
table_name = ('projects/' + project_id + '/zones/' + zone +
460462
'/clusters/' + cluster_id + '/tables/' + table_id)
461-
column_family_name = (
462-
table_name + '/columnFamilies/' + column_family_id)
463463

464464
client = _Client(timeout_seconds=timeout_seconds)
465465
table = _Table(table_name, client=client)
@@ -468,12 +468,15 @@ def _update_test_helper(self, gc_rule=None):
468468

469469
# Create request_pb
470470
if gc_rule is None:
471-
request_pb = _ColumnFamilyPB(name=column_family_name)
471+
column_family_pb = _ColumnFamilyPB()
472472
else:
473-
request_pb = _ColumnFamilyPB(
474-
name=column_family_name,
475-
gc_rule=gc_rule.to_pb(),
476-
)
473+
column_family_pb = _ColumnFamilyPB(gc_rule=gc_rule.to_pb())
474+
request_pb = table_admin_v2_pb2.ModifyColumnFamiliesRequest(
475+
name=table_name)
476+
request_pb.modifications.add(
477+
id=column_family_id,
478+
update=column_family_pb,
479+
)
477480

478481
# Create response_pb
479482
response_pb = _ColumnFamilyPB()
@@ -490,7 +493,7 @@ def _update_test_helper(self, gc_rule=None):
490493
self.assertEqual(stub.results, ())
491494
self.assertEqual(result, expected_result)
492495
self.assertEqual(stub.method_calls, [(
493-
'UpdateColumnFamily',
496+
'ModifyColumnFamilies',
494497
(request_pb, timeout_seconds),
495498
{},
496499
)])
@@ -505,8 +508,8 @@ def test_update_with_gc_rule(self):
505508

506509
def test_delete(self):
507510
from google.protobuf import empty_pb2
508-
from gcloud.bigtable._generated import (
509-
bigtable_table_service_messages_pb2 as messages_v1_pb2)
511+
from gcloud.bigtable._generated_v2 import (
512+
bigtable_table_admin_pb2 as table_admin_v2_pb2)
510513
from gcloud.bigtable._testing import _FakeStub
511514

512515
project_id = 'project-id'
@@ -517,16 +520,17 @@ def test_delete(self):
517520
timeout_seconds = 7
518521
table_name = ('projects/' + project_id + '/zones/' + zone +
519522
'/clusters/' + cluster_id + '/tables/' + table_id)
520-
column_family_name = (
521-
table_name + '/columnFamilies/' + column_family_id)
522523

523524
client = _Client(timeout_seconds=timeout_seconds)
524525
table = _Table(table_name, client=client)
525526
column_family = self._makeOne(column_family_id, table)
526527

527528
# Create request_pb
528-
request_pb = messages_v1_pb2.DeleteColumnFamilyRequest(
529-
name=column_family_name)
529+
request_pb = table_admin_v2_pb2.ModifyColumnFamiliesRequest(
530+
name=table_name)
531+
request_pb.modifications.add(
532+
id=column_family_id,
533+
drop=True)
530534

531535
# Create response_pb
532536
response_pb = empty_pb2.Empty()
@@ -543,7 +547,7 @@ def test_delete(self):
543547
self.assertEqual(stub.results, ())
544548
self.assertEqual(result, expected_result)
545549
self.assertEqual(stub.method_calls, [(
546-
'DeleteColumnFamily',
550+
'ModifyColumnFamilies',
547551
(request_pb, timeout_seconds),
548552
{},
549553
)])
@@ -623,27 +627,27 @@ def WhichOneof(cls, name):
623627

624628

625629
def _GcRulePB(*args, **kw):
626-
from gcloud.bigtable._generated import (
627-
bigtable_table_data_pb2 as data_v1_pb2)
628-
return data_v1_pb2.GcRule(*args, **kw)
630+
from gcloud.bigtable._generated_v2 import (
631+
table_pb2 as table_v2_pb2)
632+
return table_v2_pb2.GcRule(*args, **kw)
629633

630634

631635
def _GcRuleIntersectionPB(*args, **kw):
632-
from gcloud.bigtable._generated import (
633-
bigtable_table_data_pb2 as data_v1_pb2)
634-
return data_v1_pb2.GcRule.Intersection(*args, **kw)
636+
from gcloud.bigtable._generated_v2 import (
637+
table_pb2 as table_v2_pb2)
638+
return table_v2_pb2.GcRule.Intersection(*args, **kw)
635639

636640

637641
def _GcRuleUnionPB(*args, **kw):
638-
from gcloud.bigtable._generated import (
639-
bigtable_table_data_pb2 as data_v1_pb2)
640-
return data_v1_pb2.GcRule.Union(*args, **kw)
642+
from gcloud.bigtable._generated_v2 import (
643+
table_pb2 as table_v2_pb2)
644+
return table_v2_pb2.GcRule.Union(*args, **kw)
641645

642646

643647
def _ColumnFamilyPB(*args, **kw):
644-
from gcloud.bigtable._generated import (
645-
bigtable_table_data_pb2 as data_v1_pb2)
646-
return data_v1_pb2.ColumnFamily(*args, **kw)
648+
from gcloud.bigtable._generated_v2 import (
649+
table_pb2 as table_v2_pb2)
650+
return table_v2_pb2.ColumnFamily(*args, **kw)
647651

648652

649653
class _Cluster(object):

0 commit comments

Comments
 (0)