Skip to content

Commit d23bb20

Browse files
author
Bonan Liu
committed
feat: allow restore backup to different instance
1 parent bcf2c4a commit d23bb20

File tree

2 files changed

+224
-92
lines changed

2 files changed

+224
-92
lines changed

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

+43-13
Original file line numberDiff line numberDiff line change
@@ -20,24 +20,47 @@
2020
import com.google.common.base.Objects;
2121
import com.google.common.base.Preconditions;
2222
import javax.annotation.Nonnull;
23+
import javax.annotation.Nullable;
2324

2425
/** Fluent wrapper for {@link com.google.bigtable.admin.v2.RestoreTableRequest} */
2526
public final class RestoreTableRequest {
2627
private final com.google.bigtable.admin.v2.RestoreTableRequest.Builder requestBuilder =
2728
com.google.bigtable.admin.v2.RestoreTableRequest.newBuilder();
28-
private final String backupId;
29-
private final String clusterId;
29+
private final String sourceBackupId;
30+
private final String sourceClusterId;
31+
private final String sourceInstanceId;
3032

31-
public static RestoreTableRequest of(String clusterId, String backupId) {
32-
RestoreTableRequest request = new RestoreTableRequest(clusterId, backupId);
33+
/**
34+
* Create a {@link RestoreTableRequest} object. It assumes the source backup locates in the same
35+
* instance as the destination table. To restore a table from a backup in another instance, use
36+
* {@link #of(String, String, String) of} method.
37+
*/
38+
public static RestoreTableRequest of(String sourceClusterId, String sourceBackupId) {
39+
RestoreTableRequest request = new RestoreTableRequest(null, sourceClusterId, sourceBackupId);
3340
return request;
3441
}
3542

36-
private RestoreTableRequest(String clusterId, String backupId) {
37-
Preconditions.checkNotNull(clusterId);
38-
Preconditions.checkNotNull(backupId);
39-
this.backupId = backupId;
40-
this.clusterId = clusterId;
43+
/**
44+
* Create a {@link RestoreTableRequest} object. It assumes the source backup locates in the same
45+
* instance as the destination table. To restore a table from a backup in another instance, use
46+
* {@link #of(String, String, String) of} method.
47+
*/
48+
public static RestoreTableRequest of(
49+
String sourceInstanceId, String sourceClusterId, String sourceBackupId) {
50+
RestoreTableRequest request =
51+
new RestoreTableRequest(sourceInstanceId, sourceClusterId, sourceBackupId);
52+
return request;
53+
}
54+
55+
private RestoreTableRequest(
56+
@Nullable String sourceInstanceId,
57+
@Nonnull String sourceClusterId,
58+
@Nonnull String sourceBackupId) {
59+
Preconditions.checkNotNull(sourceClusterId);
60+
Preconditions.checkNotNull(sourceBackupId);
61+
this.sourceBackupId = sourceBackupId;
62+
this.sourceInstanceId = sourceInstanceId;
63+
this.sourceClusterId = sourceClusterId;
4164
}
4265

4366
public RestoreTableRequest setTableId(String tableId) {
@@ -56,13 +79,15 @@ public boolean equals(Object o) {
5679
}
5780
RestoreTableRequest that = (RestoreTableRequest) o;
5881
return Objects.equal(requestBuilder.getTableId(), that.requestBuilder.getTableId())
59-
&& Objects.equal(clusterId, that.clusterId)
60-
&& Objects.equal(backupId, that.backupId);
82+
&& Objects.equal(sourceInstanceId, that.sourceInstanceId)
83+
&& Objects.equal(sourceClusterId, that.sourceClusterId)
84+
&& Objects.equal(sourceBackupId, that.sourceBackupId);
6185
}
6286

6387
@Override
6488
public int hashCode() {
65-
return Objects.hashCode(requestBuilder.getTableId(), clusterId, backupId);
89+
return Objects.hashCode(
90+
requestBuilder.getTableId(), sourceInstanceId, sourceClusterId, sourceBackupId);
6691
}
6792

6893
@InternalApi
@@ -73,7 +98,12 @@ public com.google.bigtable.admin.v2.RestoreTableRequest toProto(
7398

7499
return requestBuilder
75100
.setParent(NameUtil.formatInstanceName(projectId, instanceId))
76-
.setBackup(NameUtil.formatBackupName(projectId, instanceId, clusterId, backupId))
101+
.setBackup(
102+
NameUtil.formatBackupName(
103+
projectId,
104+
sourceInstanceId == null ? instanceId : sourceInstanceId,
105+
sourceClusterId,
106+
sourceBackupId))
77107
.build();
78108
}
79109
}

0 commit comments

Comments
 (0)