Support datastore restore in Nomulus tool

Two commands are being added:
- ImportDatastoreCommand starts an async import operation.
  User may choose to wait until import completes or quit
  immediately.
- GetOperationStatusCommand checks the status of an operation.
  It may be used to check the status of an operation started by
  ImportDatastoreCommand.

Both commands communicate with Datastore admin api directly, without
going through the Registry server.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=228400059
This commit is contained in:
weiminyu 2019-01-08 14:28:29 -08:00 committed by Ben McIlwain
parent 3078efdaac
commit 4e71421c81
13 changed files with 477 additions and 14 deletions

View file

@ -25,6 +25,7 @@ import com.google.api.client.json.GenericJson;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.util.Key;
import com.google.common.base.Strings;
import com.google.common.collect.ImmutableList;
import java.util.Collection;
import java.util.Optional;
@ -73,12 +74,32 @@ public class DatastoreAdmin extends AbstractGoogleJsonClient {
* </ul>
*
* @param outputUrlPrefix the full resource URL of the external storage location
* @param kinds the datastore 'kinds' to be exported
* @param kinds the datastore 'kinds' to be exported. If empty, all kinds will be exported
*/
public Export export(String outputUrlPrefix, Collection<String> kinds) {
return new Export(new ExportRequest(outputUrlPrefix, kinds));
}
/**
* Imports the entire backup specified by {@code backupUrl} back to Cloud Datastore.
*
* <p>A successful backup restores deleted entities and reverts updates to existing entities since
* the backup time. However, it does not affect newly added entities.
*/
public Import importBackup(String backupUrl) {
return new Import(new ImportRequest(backupUrl, ImmutableList.of()));
}
/**
* Imports the backup specified by {@code backupUrl} back to Cloud Datastore. Only entities whose
* types are included in {@code kinds} are imported.
*
* @see #importBackup(String)
*/
public Import importBackup(String backupUrl, Collection<String> kinds) {
return new Import(new ImportRequest(backupUrl, kinds));
}
/**
* Returns a {@link Get} request that retrieves the details of an export or import {@link
* Operation}.
@ -158,6 +179,20 @@ public class DatastoreAdmin extends AbstractGoogleJsonClient {
}
}
/** A request to restore an backup to a Cloud Datastore database. */
public class Import extends DatastoreAdminRequest<Operation> {
Import(ImportRequest importRequest) {
super(
DatastoreAdmin.this,
"POST",
"projects/{projectId}:import",
importRequest,
Operation.class);
set("projectId", projectId);
}
}
/** A request to retrieve details of an export or import operation. */
public class Get extends DatastoreAdminRequest<Operation> {
@ -216,8 +251,27 @@ public class DatastoreAdmin extends AbstractGoogleJsonClient {
ExportRequest(String outputUrlPrefix, Collection<String> kinds) {
checkNotNull(outputUrlPrefix, "outputUrlPrefix");
checkArgument(!kinds.isEmpty(), "kinds must not be empty");
this.outputUrlPrefix = outputUrlPrefix;
this.entityFilter = new EntityFilter(kinds);
}
}
/**
* Model object that describes the JSON content in an export request.
*
* <p>Please note that some properties defined in the API are excluded, e.g., {@code databaseId}
* (not supported by Cloud Datastore) and labels (not used by Domain Registry).
*/
@SuppressWarnings("unused")
static class ImportRequest extends GenericJson {
@Key private final String inputUrl;
@Key private final EntityFilter entityFilter;
ImportRequest(String inputUrl, Collection<String> kinds) {
checkNotNull(inputUrl, "outputUrlPrefix");
this.inputUrl = inputUrl;
this.entityFilter = new EntityFilter(kinds);
}
}
}

View file

@ -14,7 +14,6 @@
package google.registry.export.datastore;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import com.google.api.client.json.GenericJson;
@ -39,7 +38,6 @@ public class EntityFilter extends GenericJson {
EntityFilter(Collection<String> kinds) {
checkNotNull(kinds, "kinds");
checkArgument(!kinds.isEmpty(), "kinds must not be empty");
this.kinds = ImmutableList.copyOf(kinds);
}

View file

@ -15,10 +15,10 @@
package google.registry.export.datastore;
import static com.google.common.base.Preconditions.checkState;
import static com.google.common.base.Strings.isNullOrEmpty;
import com.google.api.client.json.GenericJson;
import com.google.api.client.util.Key;
import com.google.common.base.Strings;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import google.registry.export.datastore.DatastoreAdmin.Get;
@ -52,6 +52,14 @@ public class Operation extends GenericJson {
return name;
}
public boolean isExport() {
return !isNullOrEmpty(getExportFolderUrl());
}
public boolean isImport() {
return !isNullOrEmpty(getMetadata().getInputUrl());
}
public boolean isDone() {
return done;
}
@ -88,7 +96,9 @@ public class Operation extends GenericJson {
/**
* Returns the URL to the GCS folder that holds the exported data. This folder is created by
* Datastore and is under the {@code outputUrlPrefix} set to {@linkplain
* DatastoreAdmin#export(String, List) the export request}.
* DatastoreAdmin#export(String, java.util.Collection) the export request}.
*
* @throws IllegalStateException if this is not an export operation
*/
public String getExportFolderUrl() {
return getMetadata().getOutputUrlPrefix();
@ -98,6 +108,8 @@ public class Operation extends GenericJson {
* Returns the last segment of the {@linkplain #getExportFolderUrl() export folder URL} which can
* be used as unique identifier of this export operation. This is a better ID than the {@linkplain
* #getName() operation name}, which is opaque.
*
* @throws IllegalStateException if this is not an export operation
*/
public String getExportId() {
String exportFolderUrl = getExportFolderUrl();
@ -138,12 +150,12 @@ public class Operation extends GenericJson {
public CommonMetadata() {}
String getOperationType() {
checkState(!Strings.isNullOrEmpty(operationType), "operationType may not be null or empty");
checkState(!isNullOrEmpty(operationType), "operationType may not be null or empty");
return operationType;
}
String getState() {
checkState(!Strings.isNullOrEmpty(state), "state may not be null or empty");
checkState(!isNullOrEmpty(state), "state may not be null or empty");
return state;
}
@ -165,6 +177,7 @@ public class Operation extends GenericJson {
@Key private Progress progressEntities;
@Key private Progress progressBytes;
@Key private EntityFilter entityFilter;
@Key private String inputUrl;
@Key private String outputUrlPrefix;
public Metadata() {}
@ -186,9 +199,22 @@ public class Operation extends GenericJson {
return entityFilter;
}
public String getInputUrl() {
return checkUrls().inputUrl;
}
public String getOutputUrlPrefix() {
checkState(!Strings.isNullOrEmpty(outputUrlPrefix), "outputUrlPrefix");
return outputUrlPrefix;
return checkUrls().outputUrlPrefix;
}
Metadata checkUrls() {
checkState(
isNullOrEmpty(inputUrl) || isNullOrEmpty(outputUrlPrefix),
"inputUrl and outputUrlPrefix must not be both present");
checkState(
!isNullOrEmpty(inputUrl) || !isNullOrEmpty(outputUrlPrefix),
"inputUrl and outputUrlPrefix must not be both missing");
return this;
}
}