Skip to content

Commit 687b6df

Browse files
authored
feat: add support for table deletion protection (#2430)
* add support for table deletion protection * fear: add support for table deletion protection * stily fix * stily fix * styling fix * rename isProtected to isDeletionProtected
1 parent c98410b commit 687b6df

File tree

7 files changed

+107
-6
lines changed

7 files changed

+107
-6
lines changed

google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/CreateTableRequest.java

+6
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,12 @@ public CreateTableRequest addChangeStreamRetention(Duration retention) {
123123
return this;
124124
}
125125

126+
/** Configures if the table is deletion protected. */
127+
public CreateTableRequest setDeletionProtection(boolean deletionProtection) {
128+
requestBuilder.getTableBuilder().setDeletionProtection(deletionProtection);
129+
return this;
130+
}
131+
126132
@Override
127133
public boolean equals(Object o) {
128134
if (this == o) {

google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/Table.java

+19-4
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ public com.google.bigtable.admin.v2.Table.ClusterState.ReplicationState toProto(
105105
private final List<ColumnFamily> columnFamilies;
106106

107107
private final Duration changeStreamRetention;
108+
private final boolean deletionProtection;
108109

109110
@InternalApi
110111
public static Table fromProto(@Nonnull com.google.bigtable.admin.v2.Table proto) {
@@ -135,19 +136,22 @@ public static Table fromProto(@Nonnull com.google.bigtable.admin.v2.Table proto)
135136
TableName.parse(proto.getName()),
136137
replicationStates.build(),
137138
columnFamilies.build(),
138-
changeStreamConfig);
139+
changeStreamConfig,
140+
proto.getDeletionProtection());
139141
}
140142

141143
private Table(
142144
TableName tableName,
143145
Map<String, ReplicationState> replicationStatesByClusterId,
144146
List<ColumnFamily> columnFamilies,
145-
Duration changeStreamRetention) {
147+
Duration changeStreamRetention,
148+
boolean deletionProtection) {
146149
this.instanceId = tableName.getInstance();
147150
this.id = tableName.getTable();
148151
this.replicationStatesByClusterId = replicationStatesByClusterId;
149152
this.columnFamilies = columnFamilies;
150153
this.changeStreamRetention = changeStreamRetention;
154+
this.deletionProtection = deletionProtection;
151155
}
152156

153157
/** Gets the table's id. */
@@ -172,6 +176,11 @@ public Duration getChangeStreamRetention() {
172176
return changeStreamRetention;
173177
}
174178

179+
/** Returns whether this table is deletion protected. */
180+
public boolean isDeletionProtected() {
181+
return deletionProtection;
182+
}
183+
175184
@Override
176185
public boolean equals(Object o) {
177186
if (this == o) {
@@ -185,12 +194,18 @@ public boolean equals(Object o) {
185194
&& Objects.equal(instanceId, table.instanceId)
186195
&& Objects.equal(replicationStatesByClusterId, table.replicationStatesByClusterId)
187196
&& Objects.equal(columnFamilies, table.columnFamilies)
188-
&& Objects.equal(changeStreamRetention, table.changeStreamRetention);
197+
&& Objects.equal(changeStreamRetention, table.changeStreamRetention)
198+
&& Objects.equal(deletionProtection, table.deletionProtection);
189199
}
190200

191201
@Override
192202
public int hashCode() {
193203
return Objects.hashCode(
194-
id, instanceId, replicationStatesByClusterId, columnFamilies, changeStreamRetention);
204+
id,
205+
instanceId,
206+
replicationStatesByClusterId,
207+
columnFamilies,
208+
changeStreamRetention,
209+
deletionProtection);
195210
}
196211
}

google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/UpdateTableRequest.java

+7
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,13 @@ public UpdateTableRequest disableChangeStreamRetention() {
7474
return addChangeStreamRetention(Duration.ZERO);
7575
}
7676

77+
/** Changes the deletion protection of an existing table. */
78+
public UpdateTableRequest setDeletionProtection(boolean deletionProtection) {
79+
requestBuilder.getTableBuilder().setDeletionProtection(deletionProtection);
80+
requestBuilder.getUpdateMaskBuilder().addPaths("deletion_protection");
81+
return this;
82+
}
83+
7784
@InternalApi
7885
public com.google.bigtable.admin.v2.UpdateTableRequest toProto(
7986
String projectId, String instanceId) {

google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/BigtableTableAdminClientTests.java

+37
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,43 @@ public void testCreateTable() {
299299
assertThat(result).isEqualTo(Table.fromProto(expectedResponse));
300300
}
301301

302+
@Test
303+
public void testCreateTableWithDeletionProtectionSet() {
304+
// Setup
305+
Mockito.when(mockStub.createTableCallable()).thenReturn(mockCreateTableCallable);
306+
307+
com.google.bigtable.admin.v2.CreateTableRequest expectedRequest =
308+
com.google.bigtable.admin.v2.CreateTableRequest.newBuilder()
309+
.setParent(INSTANCE_NAME)
310+
.setTableId(TABLE_ID)
311+
.setTable(
312+
com.google.bigtable.admin.v2.Table.newBuilder()
313+
.setDeletionProtection(true)
314+
.putColumnFamilies(
315+
"cf1",
316+
ColumnFamily.newBuilder()
317+
.setGcRule(GcRule.getDefaultInstance())
318+
.setValueType(TypeProtos.intSumType())
319+
.build()))
320+
.build();
321+
322+
com.google.bigtable.admin.v2.Table expectedResponse =
323+
com.google.bigtable.admin.v2.Table.newBuilder().setName(TABLE_NAME).build();
324+
325+
Mockito.when(mockCreateTableCallable.futureCall(expectedRequest))
326+
.thenReturn(ApiFutures.immediateFuture(expectedResponse));
327+
328+
// Execute
329+
Table result =
330+
adminClient.createTable(
331+
CreateTableRequest.of(TABLE_ID)
332+
.addFamily("cf1", Type.int64Sum())
333+
.setDeletionProtection(true));
334+
335+
// Verify
336+
assertThat(result).isEqualTo(Table.fromProto(expectedResponse));
337+
}
338+
302339
@Test
303340
public void testUpdateTable() {
304341
// Setup

google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/models/CreateTableRequestTest.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ public void testToProto() {
4848
.addFamily("another-family", GCRULES.maxAge(100, TimeUnit.HOURS))
4949
.addSplit(splitKey)
5050
.addSplit(secondSplitKey)
51-
.addChangeStreamRetention(Duration.ofHours(24));
51+
.addChangeStreamRetention(Duration.ofHours(24))
52+
.setDeletionProtection(true);
5253

5354
com.google.bigtable.admin.v2.CreateTableRequest requestProto =
5455
com.google.bigtable.admin.v2.CreateTableRequest.newBuilder()
@@ -70,7 +71,8 @@ public void testToProto() {
7071
ChangeStreamConfig.newBuilder()
7172
.setRetentionPeriod(
7273
com.google.protobuf.Duration.newBuilder().setSeconds(86400))
73-
.build()))
74+
.build())
75+
.setDeletionProtection(true))
7476
.setParent(NameUtil.formatInstanceName(PROJECT_ID, INSTANCE_ID))
7577
.addInitialSplits(
7678
com.google.bigtable.admin.v2.CreateTableRequest.Split.newBuilder().setKey(splitKey))

google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/models/TableTest.java

+2
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ public void testFromProto() {
6767
.setSeconds(1)
6868
.setNanos(99)))
6969
.build())
70+
.setDeletionProtection(true)
7071
.build();
7172

7273
Table result = Table.fromProto(proto);
@@ -78,6 +79,7 @@ public void testFromProto() {
7879
"cluster1", Table.ReplicationState.READY,
7980
"cluster2", Table.ReplicationState.INITIALIZING);
8081
assertThat(result.getColumnFamilies()).hasSize(3);
82+
assertThat(result.isDeletionProtected()).isTrue();
8183

8284
for (Entry<String, ColumnFamily> entry : proto.getColumnFamiliesMap().entrySet()) {
8385
assertThat(result.getColumnFamilies())

google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/models/UpdateTableRequestTest.java

+32
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,36 @@ public void testNoChangeChangeStreamToProto() {
8181
.build();
8282
assertThat(request.toProto(PROJECT_ID, INSTANCE_ID)).isEqualTo(requestProto);
8383
}
84+
85+
@Test
86+
public void testEnableDeletionProtection() {
87+
UpdateTableRequest request = UpdateTableRequest.of(TABLE_ID).setDeletionProtection(true);
88+
89+
com.google.bigtable.admin.v2.UpdateTableRequest requestProto =
90+
com.google.bigtable.admin.v2.UpdateTableRequest.newBuilder()
91+
.setTable(
92+
Table.newBuilder()
93+
.setName(NameUtil.formatTableName(PROJECT_ID, INSTANCE_ID, TABLE_ID))
94+
.setDeletionProtection(true))
95+
.setUpdateMask(FieldMask.newBuilder().addPaths("deletion_protection").build())
96+
.build();
97+
98+
assertThat(request.toProto(PROJECT_ID, INSTANCE_ID)).isEqualTo(requestProto);
99+
}
100+
101+
@Test
102+
public void testDisableDeletionProtection() {
103+
UpdateTableRequest request = UpdateTableRequest.of(TABLE_ID).setDeletionProtection(false);
104+
105+
com.google.bigtable.admin.v2.UpdateTableRequest requestProto =
106+
com.google.bigtable.admin.v2.UpdateTableRequest.newBuilder()
107+
.setTable(
108+
Table.newBuilder()
109+
.setName(NameUtil.formatTableName(PROJECT_ID, INSTANCE_ID, TABLE_ID))
110+
.setDeletionProtection(false))
111+
.setUpdateMask(FieldMask.newBuilder().addPaths("deletion_protection").build())
112+
.build();
113+
114+
assertThat(request.toProto(PROJECT_ID, INSTANCE_ID)).isEqualTo(requestProto);
115+
}
84116
}

0 commit comments

Comments
 (0)