|
| 1 | +/* |
| 2 | + * Copyright 2022 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.google.cloud.bigtable.admin.v2.models; |
| 17 | + |
| 18 | +import com.google.api.core.InternalApi; |
| 19 | +import com.google.cloud.bigtable.admin.v2.internal.NameUtil; |
| 20 | +import com.google.common.base.Objects; |
| 21 | +import com.google.common.base.Preconditions; |
| 22 | +import com.google.protobuf.util.Timestamps; |
| 23 | +import javax.annotation.Nonnull; |
| 24 | +import org.threeten.bp.Instant; |
| 25 | + |
| 26 | +/** Build CopyBackupRequest for {@link com.google.bigtable.admin.v2.CopyBackupRequest}. */ |
| 27 | +public final class CopyBackupRequest { |
| 28 | + private final com.google.bigtable.admin.v2.CopyBackupRequest.Builder requestBuilder = |
| 29 | + com.google.bigtable.admin.v2.CopyBackupRequest.newBuilder(); |
| 30 | + private final String sourceBackupId; |
| 31 | + private final String sourceClusterId; |
| 32 | + private String sourceInstanceId; |
| 33 | + private String sourceProjectId; |
| 34 | + |
| 35 | + private String destClusterId; |
| 36 | + |
| 37 | + /** |
| 38 | + * Create a {@link CopyBackupRequest} object. It assumes the source backup is located in the same |
| 39 | + * instance and project as the destination backup, which is where the BigtableTableAdminClient is |
| 40 | + * created in. use setSourceInstance("[INSTANCE]") if the source backup is located in a different |
| 41 | + * instance. use setSourceInstance("[PROJECT]", "[INSTANCE]") if the source backup is located in a |
| 42 | + * different project. |
| 43 | + */ |
| 44 | + public static CopyBackupRequest of(String sourceClusterId, String sourceBackupId) { |
| 45 | + CopyBackupRequest request = new CopyBackupRequest(sourceClusterId, sourceBackupId); |
| 46 | + return request; |
| 47 | + } |
| 48 | + |
| 49 | + private CopyBackupRequest(@Nonnull String sourceClusterId, @Nonnull String sourceBackupId) { |
| 50 | + Preconditions.checkNotNull(sourceClusterId); |
| 51 | + Preconditions.checkNotNull(sourceBackupId); |
| 52 | + this.sourceClusterId = sourceClusterId; |
| 53 | + this.sourceBackupId = sourceBackupId; |
| 54 | + } |
| 55 | + |
| 56 | + public CopyBackupRequest setSourceInstance(String instanceId) { |
| 57 | + Preconditions.checkNotNull(instanceId); |
| 58 | + this.sourceInstanceId = instanceId; |
| 59 | + return this; |
| 60 | + } |
| 61 | + |
| 62 | + public CopyBackupRequest setSourceInstance(String projectId, String instanceId) { |
| 63 | + Preconditions.checkNotNull(projectId); |
| 64 | + Preconditions.checkNotNull(instanceId); |
| 65 | + this.sourceProjectId = projectId; |
| 66 | + this.sourceInstanceId = instanceId; |
| 67 | + return this; |
| 68 | + } |
| 69 | + |
| 70 | + public CopyBackupRequest setDestination(String clusterId, String backupId) { |
| 71 | + Preconditions.checkNotNull(backupId); |
| 72 | + Preconditions.checkNotNull(clusterId); |
| 73 | + requestBuilder.setBackupId(backupId); |
| 74 | + this.destClusterId = clusterId; |
| 75 | + return this; |
| 76 | + } |
| 77 | + |
| 78 | + public CopyBackupRequest setExpireTime(Instant expireTime) { |
| 79 | + Preconditions.checkNotNull(expireTime); |
| 80 | + requestBuilder.setExpireTime(Timestamps.fromMillis(expireTime.toEpochMilli())); |
| 81 | + return this; |
| 82 | + } |
| 83 | + |
| 84 | + @Override |
| 85 | + public boolean equals(Object o) { |
| 86 | + if (this == o) { |
| 87 | + return true; |
| 88 | + } |
| 89 | + if (o == null || getClass() != o.getClass()) { |
| 90 | + return false; |
| 91 | + } |
| 92 | + CopyBackupRequest that = (CopyBackupRequest) o; |
| 93 | + return Objects.equal(requestBuilder.getBackupId(), that.requestBuilder.getBackupId()) |
| 94 | + && Objects.equal(sourceBackupId, that.sourceBackupId) |
| 95 | + && Objects.equal(sourceClusterId, that.sourceClusterId) |
| 96 | + && Objects.equal(sourceInstanceId, that.sourceInstanceId) |
| 97 | + && Objects.equal(sourceProjectId, that.sourceProjectId); |
| 98 | + } |
| 99 | + |
| 100 | + @Override |
| 101 | + public int hashCode() { |
| 102 | + return Objects.hashCode( |
| 103 | + requestBuilder.getBackupId(), |
| 104 | + sourceBackupId, |
| 105 | + sourceClusterId, |
| 106 | + sourceInstanceId, |
| 107 | + sourceProjectId); |
| 108 | + } |
| 109 | + |
| 110 | + @InternalApi |
| 111 | + public com.google.bigtable.admin.v2.CopyBackupRequest toProto( |
| 112 | + @Nonnull String projectId, @Nonnull String instanceId) { |
| 113 | + Preconditions.checkNotNull(projectId); |
| 114 | + Preconditions.checkNotNull(instanceId); |
| 115 | + |
| 116 | + return requestBuilder |
| 117 | + .setParent(NameUtil.formatClusterName(projectId, instanceId, destClusterId)) |
| 118 | + .setSourceBackup( |
| 119 | + NameUtil.formatBackupName( |
| 120 | + sourceProjectId == null ? projectId : sourceProjectId, |
| 121 | + sourceInstanceId == null ? instanceId : sourceInstanceId, |
| 122 | + sourceClusterId, |
| 123 | + sourceBackupId)) |
| 124 | + .build(); |
| 125 | + } |
| 126 | +} |
0 commit comments