/
BackupInfo.java
294 lines (256 loc) · 9.02 KB
/
BackupInfo.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/*
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.cloud.spanner;
import com.google.api.client.util.Preconditions;
import com.google.cloud.Timestamp;
import com.google.cloud.spanner.encryption.BackupEncryptionConfig;
import com.google.cloud.spanner.encryption.EncryptionInfo;
import com.google.spanner.admin.database.v1.Database;
import java.util.Objects;
import javax.annotation.Nullable;
/** Represents a Cloud Spanner database backup. */
public class BackupInfo {
public abstract static class Builder {
abstract Builder setState(State state);
abstract Builder setSize(long size);
/**
* Returned when retrieving a backup.
*
* <p>The encryption information for the backup. If the encryption key protecting this resource
* is customer managed, then kms_key_version will be filled.
*/
abstract Builder setEncryptionInfo(EncryptionInfo encryptionInfo);
abstract Builder setProto(com.google.spanner.admin.database.v1.Backup proto);
/**
* Optional for creating a new backup.
*
* <p>The encryption configuration to be used for the backup. The possible configurations are
* {@link com.google.cloud.spanner.encryption.CustomerManagedEncryption}, {@link
* com.google.cloud.spanner.encryption.GoogleDefaultEncryption} and {@link
* com.google.cloud.spanner.encryption.UseDatabaseEncryption}.
*
* <p>If no encryption config is given the backup will be created with the same encryption as
* set by the database ({@link com.google.cloud.spanner.encryption.UseDatabaseEncryption}).
*/
public abstract Builder setEncryptionConfig(BackupEncryptionConfig encryptionConfig);
/**
* Required for creating a new backup.
*
* <p>Sets the expiration time of the backup. The expiration time of the backup, with
* microseconds granularity that must be at least 6 hours and at most 366 days from the time the
* request is received. Once the expireTime has passed, Cloud Spanner will delete the backup and
* free the resources used by the backup.
*/
public abstract Builder setExpireTime(Timestamp expireTime);
/**
* Optional for creating a new backup.
*
* <p>Specifies the timestamp to have an externally consistent copy of the database. If no
* version time is specified, it will be automatically set to the backup create time.
*
* <p>The version time can be as far in the past as specified by the database earliest version
* time (see {@link Database#getEarliestVersionTime()}).
*/
public abstract Builder setVersionTime(Timestamp versionTime);
/**
* Required for creating a new backup.
*
* <p>Sets the source database to use for creating the backup.
*/
public abstract Builder setDatabase(DatabaseId database);
/** Builds the backup from this builder. */
public abstract Backup build();
}
abstract static class BuilderImpl extends Builder {
protected final BackupId id;
private State state = State.UNSPECIFIED;
private Timestamp expireTime;
private Timestamp versionTime;
private DatabaseId database;
private long size;
private BackupEncryptionConfig encryptionConfig;
private EncryptionInfo encryptionInfo;
private com.google.spanner.admin.database.v1.Backup proto;
BuilderImpl(BackupId id) {
this.id = Preconditions.checkNotNull(id);
}
BuilderImpl(BackupInfo other) {
this.id = other.id;
this.state = other.state;
this.expireTime = other.expireTime;
this.versionTime = other.versionTime;
this.database = other.database;
this.size = other.size;
this.encryptionConfig = other.encryptionConfig;
this.encryptionInfo = other.encryptionInfo;
this.proto = other.proto;
}
@Override
Builder setState(State state) {
this.state = Preconditions.checkNotNull(state);
return this;
}
@Override
public Builder setExpireTime(Timestamp expireTime) {
this.expireTime = Preconditions.checkNotNull(expireTime);
return this;
}
@Override
public Builder setVersionTime(Timestamp versionTime) {
this.versionTime = versionTime;
return this;
}
@Override
public Builder setDatabase(DatabaseId database) {
Preconditions.checkArgument(
database.getInstanceId().equals(id.getInstanceId()),
"The instance of the source database must be equal to the instance of the backup.");
this.database = Preconditions.checkNotNull(database);
return this;
}
@Override
public Builder setEncryptionConfig(BackupEncryptionConfig encryptionConfig) {
this.encryptionConfig = encryptionConfig;
return this;
}
@Override
Builder setSize(long size) {
this.size = size;
return this;
}
@Override
Builder setEncryptionInfo(EncryptionInfo encryptionInfo) {
this.encryptionInfo = encryptionInfo;
return this;
}
@Override
Builder setProto(@Nullable com.google.spanner.admin.database.v1.Backup proto) {
this.proto = proto;
return this;
}
}
/** State of the backup. */
public enum State {
// Not specified.
UNSPECIFIED,
// The backup is still being created and is not ready to use.
CREATING,
// The backup is fully created and ready to use.
READY,
}
private final BackupId id;
private final State state;
private final Timestamp expireTime;
private final Timestamp versionTime;
private final DatabaseId database;
private final long size;
private final BackupEncryptionConfig encryptionConfig;
private final EncryptionInfo encryptionInfo;
private final com.google.spanner.admin.database.v1.Backup proto;
BackupInfo(BuilderImpl builder) {
this.id = builder.id;
this.state = builder.state;
this.size = builder.size;
this.encryptionConfig = builder.encryptionConfig;
this.encryptionInfo = builder.encryptionInfo;
this.expireTime = builder.expireTime;
this.versionTime = builder.versionTime;
this.database = builder.database;
this.proto = builder.proto;
}
/** Returns the backup id. */
public BackupId getId() {
return id;
}
/** Returns the id of the instance that the backup belongs to. */
public InstanceId getInstanceId() {
return id.getInstanceId();
}
/** Returns the state of the backup. */
public State getState() {
return state;
}
/** Returns the size of the backup in bytes. */
public long getSize() {
return size;
}
/**
* Returns the {@link BackupEncryptionConfig} to encrypt the backup during its creation. Returns
* <code>null</code> if no customer-managed encryption key should be used.
*/
public BackupEncryptionConfig getEncryptionConfig() {
return encryptionConfig;
}
/**
* Returns the {@link EncryptionInfo} of the backup if the backup is encrypted, or <code>null
* </code> if this backup is not encrypted.
*/
public EncryptionInfo getEncryptionInfo() {
return encryptionInfo;
}
/** Returns the expire time of the backup. */
public Timestamp getExpireTime() {
return expireTime;
}
/** Returns the version time of the backup. */
public Timestamp getVersionTime() {
return versionTime;
}
/** Returns the id of the database that was used to create the backup. */
public DatabaseId getDatabase() {
return database;
}
/** Returns the raw proto instance that was used to construct this {@link Backup}. */
public @Nullable com.google.spanner.admin.database.v1.Backup getProto() {
return proto;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
BackupInfo that = (BackupInfo) o;
return id.equals(that.id)
&& state == that.state
&& size == that.size
&& Objects.equals(encryptionConfig, that.encryptionConfig)
&& Objects.equals(encryptionInfo, that.encryptionInfo)
&& Objects.equals(expireTime, that.expireTime)
&& Objects.equals(versionTime, that.versionTime)
&& Objects.equals(database, that.database);
}
@Override
public int hashCode() {
return Objects.hash(
id, state, size, encryptionConfig, encryptionInfo, expireTime, versionTime, database);
}
@Override
public String toString() {
return String.format(
"Backup[%s, %s, %d, %s, %s, %s, %s, %s]",
id.getName(),
state,
size,
encryptionConfig,
encryptionInfo,
expireTime,
versionTime,
database);
}
}