use of com.google.spanner.admin.database.v1.Backup in project java-spanner by googleapis.
the class SpannerSample method createBackup.
// [END spanner_query_with_query_options]
// [START spanner_create_backup]
static void createBackup(DatabaseAdminClient dbAdminClient, DatabaseId databaseId, BackupId backupId, Timestamp versionTime) {
// Set expire time to 14 days from now.
Timestamp expireTime = Timestamp.ofTimeMicroseconds(TimeUnit.MICROSECONDS.convert(System.currentTimeMillis() + TimeUnit.DAYS.toMillis(14), TimeUnit.MILLISECONDS));
Backup backup = dbAdminClient.newBackupBuilder(backupId).setDatabase(databaseId).setExpireTime(expireTime).setVersionTime(versionTime).build();
// Initiate the request which returns an OperationFuture.
System.out.println("Creating backup [" + backup.getId() + "]...");
OperationFuture<Backup, CreateBackupMetadata> op = backup.create();
try {
// Wait for the backup operation to complete.
backup = op.get();
System.out.println("Created backup [" + backup.getId() + "]");
} catch (ExecutionException e) {
throw (SpannerException) e.getCause();
} catch (InterruptedException e) {
throw SpannerExceptionFactory.propagateInterrupt(e);
}
// Reload the metadata of the backup from the server.
backup = backup.reload();
System.out.println(String.format("Backup %s of size %d bytes was created at %s for version of database at %s", backup.getId().getName(), backup.getSize(), LocalDateTime.ofEpochSecond(backup.getProto().getCreateTime().getSeconds(), backup.getProto().getCreateTime().getNanos(), OffsetDateTime.now().getOffset()), LocalDateTime.ofEpochSecond(backup.getProto().getVersionTime().getSeconds(), backup.getProto().getVersionTime().getNanos(), OffsetDateTime.now().getOffset())));
}
use of com.google.spanner.admin.database.v1.Backup in project java-spanner by googleapis.
the class DatabaseAdminClient method deleteBackup.
// AUTO-GENERATED DOCUMENTATION AND METHOD.
/**
* Deletes a pending or completed [Backup][google.spanner.admin.database.v1.Backup].
*
* <p>Sample code:
*
* <pre>{@code
* try (DatabaseAdminClient databaseAdminClient = DatabaseAdminClient.create()) {
* String name = BackupName.of("[PROJECT]", "[INSTANCE]", "[BACKUP]").toString();
* databaseAdminClient.deleteBackup(name);
* }
* }</pre>
*
* @param name Required. Name of the backup to delete. Values are of the form
* `projects/<project>/instances/<instance>/backups/<backup>`.
* @throws com.google.api.gax.rpc.ApiException if the remote call fails
*/
public final void deleteBackup(String name) {
DeleteBackupRequest request = DeleteBackupRequest.newBuilder().setName(name).build();
deleteBackup(request);
}
use of com.google.spanner.admin.database.v1.Backup in project java-bigtable by googleapis.
the class BaseBigtableTableAdminClientTest method createBackupExceptionTest.
@Test
public void createBackupExceptionTest() throws Exception {
StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
mockBigtableTableAdmin.addException(exception);
try {
ClusterName parent = ClusterName.of("[PROJECT]", "[INSTANCE]", "[CLUSTER]");
String backupId = "backupId2121930365";
Backup backup = Backup.newBuilder().build();
client.createBackupAsync(parent, backupId, backup).get();
Assert.fail("No exception raised");
} catch (ExecutionException e) {
Assert.assertEquals(InvalidArgumentException.class, e.getCause().getClass());
InvalidArgumentException apiException = ((InvalidArgumentException) e.getCause());
Assert.assertEquals(StatusCode.Code.INVALID_ARGUMENT, apiException.getStatusCode().getCode());
}
}
use of com.google.spanner.admin.database.v1.Backup in project java-bigtable by googleapis.
the class BaseBigtableTableAdminClientTest method createBackupTest.
@Test
public void createBackupTest() throws Exception {
Backup expectedResponse = Backup.newBuilder().setName(BackupName.of("[PROJECT]", "[INSTANCE]", "[CLUSTER]", "[BACKUP]").toString()).setSourceTable("sourceTable-95372173").setExpireTime(Timestamp.newBuilder().build()).setStartTime(Timestamp.newBuilder().build()).setEndTime(Timestamp.newBuilder().build()).setSizeBytes(-1796325715).setEncryptionInfo(EncryptionInfo.newBuilder().build()).build();
Operation resultOperation = Operation.newBuilder().setName("createBackupTest").setDone(true).setResponse(Any.pack(expectedResponse)).build();
mockBigtableTableAdmin.addResponse(resultOperation);
ClusterName parent = ClusterName.of("[PROJECT]", "[INSTANCE]", "[CLUSTER]");
String backupId = "backupId2121930365";
Backup backup = Backup.newBuilder().build();
Backup actualResponse = client.createBackupAsync(parent, backupId, backup).get();
Assert.assertEquals(expectedResponse, actualResponse);
List<AbstractMessage> actualRequests = mockBigtableTableAdmin.getRequests();
Assert.assertEquals(1, actualRequests.size());
CreateBackupRequest actualRequest = ((CreateBackupRequest) actualRequests.get(0));
Assert.assertEquals(parent.toString(), actualRequest.getParent());
Assert.assertEquals(backupId, actualRequest.getBackupId());
Assert.assertEquals(backup, actualRequest.getBackup());
Assert.assertTrue(channelProvider.isHeaderSent(ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
}
use of com.google.spanner.admin.database.v1.Backup in project java-bigtable by googleapis.
the class BaseBigtableTableAdminClientTest method createBackupTest2.
@Test
public void createBackupTest2() throws Exception {
Backup expectedResponse = Backup.newBuilder().setName(BackupName.of("[PROJECT]", "[INSTANCE]", "[CLUSTER]", "[BACKUP]").toString()).setSourceTable("sourceTable-95372173").setExpireTime(Timestamp.newBuilder().build()).setStartTime(Timestamp.newBuilder().build()).setEndTime(Timestamp.newBuilder().build()).setSizeBytes(-1796325715).setEncryptionInfo(EncryptionInfo.newBuilder().build()).build();
Operation resultOperation = Operation.newBuilder().setName("createBackupTest").setDone(true).setResponse(Any.pack(expectedResponse)).build();
mockBigtableTableAdmin.addResponse(resultOperation);
String parent = "parent-995424086";
String backupId = "backupId2121930365";
Backup backup = Backup.newBuilder().build();
Backup actualResponse = client.createBackupAsync(parent, backupId, backup).get();
Assert.assertEquals(expectedResponse, actualResponse);
List<AbstractMessage> actualRequests = mockBigtableTableAdmin.getRequests();
Assert.assertEquals(1, actualRequests.size());
CreateBackupRequest actualRequest = ((CreateBackupRequest) actualRequests.get(0));
Assert.assertEquals(parent, actualRequest.getParent());
Assert.assertEquals(backupId, actualRequest.getBackupId());
Assert.assertEquals(backup, actualRequest.getBackup());
Assert.assertTrue(channelProvider.isHeaderSent(ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
}
Aggregations