Add the ability to dump the history of a single resource

This would have saved me 2+ hours yesterday of mucking around
in datastore and manually copying out the xml bytes so that
I could base64decode them.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=139472542
This commit is contained in:
cgoldfeder 2016-11-17 09:54:00 -08:00 committed by Ben McIlwain
parent 419a04bc26
commit 5bac24186f
3 changed files with 57 additions and 25 deletions

View file

@ -16,10 +16,39 @@ package google.registry.tools;
import com.google.common.base.Ascii; import com.google.common.base.Ascii;
import com.google.common.base.Strings; import com.google.common.base.Strings;
import com.google.common.collect.ImmutableMap;
import com.googlecode.objectify.Key;
import google.registry.model.EppResource;
import google.registry.model.contact.ContactResource;
import google.registry.model.domain.DomainApplication;
import google.registry.model.domain.DomainResource;
import google.registry.model.host.HostResource;
import google.registry.model.index.ForeignKeyIndex;
import org.joda.time.DateTime;
/** Container class for static utility methods. */ /** Container class for static utility methods. */
class CommandUtilities { class CommandUtilities {
/** A useful parameter enum for commands that operate on {@link EppResource} objects. */
public enum ResourceType {
CONTACT,
HOST,
DOMAIN,
APPLICATION;
public Key<? extends EppResource> getKey(String uniqueId, DateTime now) {
return this == APPLICATION
? Key.create(DomainApplication.class, uniqueId)
: ForeignKeyIndex.loadAndGetKey(
ImmutableMap.of(
CONTACT, ContactResource.class,
HOST, HostResource.class,
DOMAIN, DomainResource.class).get(this),
uniqueId,
now);
}
}
static String addHeader(String header, String body) { static String addHeader(String header, String body) {
return String.format("%s:\n%s\n%s", header, Strings.repeat("-", header.length() + 1), body); return String.format("%s:\n%s\n%s", header, Strings.repeat("-", header.length() + 1), body);
} }

View file

@ -14,14 +14,19 @@
package google.registry.tools; package google.registry.tools;
import static com.google.common.base.Preconditions.checkArgument;
import static google.registry.model.ofy.ObjectifyService.ofy; import static google.registry.model.ofy.ObjectifyService.ofy;
import static google.registry.util.DateTimeUtils.END_OF_TIME; import static google.registry.util.DateTimeUtils.END_OF_TIME;
import static google.registry.util.DateTimeUtils.START_OF_TIME; import static google.registry.util.DateTimeUtils.START_OF_TIME;
import static org.joda.time.DateTimeZone.UTC;
import com.beust.jcommander.Parameter; import com.beust.jcommander.Parameter;
import com.beust.jcommander.Parameters; import com.beust.jcommander.Parameters;
import com.googlecode.objectify.Key;
import google.registry.model.EppResource;
import google.registry.model.reporting.HistoryEntry; import google.registry.model.reporting.HistoryEntry;
import google.registry.tools.Command.RemoteApiCommand; import google.registry.tools.Command.RemoteApiCommand;
import google.registry.tools.CommandUtilities.ResourceType;
import google.registry.xml.XmlTransformer; import google.registry.xml.XmlTransformer;
import org.joda.time.DateTime; import org.joda.time.DateTime;
@ -40,9 +45,29 @@ final class GetHistoryEntriesCommand implements RemoteApiCommand {
description = "Only show history entries that occurred at or before this time") description = "Only show history entries that occurred at or before this time")
private DateTime before = END_OF_TIME; private DateTime before = END_OF_TIME;
@Parameter(
names = "--type",
description = "Resource type.")
private ResourceType type;
@Parameter(
names = "--id",
description = "Foreign key of the resource, or application ID of the domain application.")
private String uniqueId;
@Override @Override
public void run() { public void run() {
for (HistoryEntry entry : ofy().load().type(HistoryEntry.class) Key<? extends EppResource> parentKey = null;
if (type != null || uniqueId != null) {
checkArgument(
type != null && uniqueId != null,
"If either of 'type' or 'id' are set then both must be");
parentKey = type.getKey(uniqueId, DateTime.now(UTC));
}
for (HistoryEntry entry :
(parentKey == null
? ofy().load().type(HistoryEntry.class)
: ofy().load().type(HistoryEntry.class).ancestor(parentKey))
.order("modificationTime") .order("modificationTime")
.filter("modificationTime >=", after) .filter("modificationTime >=", after)
.filter("modificationTime <=", before)) { .filter("modificationTime <=", before)) {

View file

@ -14,7 +14,6 @@
package google.registry.tools; package google.registry.tools;
import static google.registry.model.index.ForeignKeyIndex.loadAndGetKey;
import static google.registry.model.ofy.ObjectifyService.ofy; import static google.registry.model.ofy.ObjectifyService.ofy;
import static google.registry.util.PreconditionsUtils.checkArgumentNotNull; import static google.registry.util.PreconditionsUtils.checkArgumentNotNull;
import static org.joda.time.DateTimeZone.UTC; import static org.joda.time.DateTimeZone.UTC;
@ -23,10 +22,7 @@ import com.beust.jcommander.Parameter;
import com.beust.jcommander.Parameters; import com.beust.jcommander.Parameters;
import com.googlecode.objectify.Key; import com.googlecode.objectify.Key;
import google.registry.model.EppResource; import google.registry.model.EppResource;
import google.registry.model.contact.ContactResource; import google.registry.tools.CommandUtilities.ResourceType;
import google.registry.model.domain.DomainApplication;
import google.registry.model.domain.DomainResource;
import google.registry.model.host.HostResource;
import org.joda.time.DateTime; import org.joda.time.DateTime;
/** /**
@ -40,8 +36,6 @@ import org.joda.time.DateTime;
commandDescription = "Load and resave EPP resources by foreign key") commandDescription = "Load and resave EPP resources by foreign key")
public final class ResaveEppResourceCommand extends MutatingCommand { public final class ResaveEppResourceCommand extends MutatingCommand {
private enum ResourceType { CONTACT, HOST, DOMAIN, APPLICATION }
@Parameter( @Parameter(
names = "--type", names = "--type",
description = "Resource type.") description = "Resource type.")
@ -55,27 +49,11 @@ public final class ResaveEppResourceCommand extends MutatingCommand {
@Override @Override
protected void init() throws Exception { protected void init() throws Exception {
Key<? extends EppResource> resourceKey = checkArgumentNotNull( Key<? extends EppResource> resourceKey = checkArgumentNotNull(
getResourceKey(type, uniqueId, DateTime.now(UTC)), type.getKey(uniqueId, DateTime.now(UTC)),
"Could not find active resource of type %s: %s", type, uniqueId); "Could not find active resource of type %s: %s", type, uniqueId);
// Load the resource directly to bypass running cloneProjectedAtTime() automatically, which can // Load the resource directly to bypass running cloneProjectedAtTime() automatically, which can
// cause stageEntityChange() to fail due to implicit projection changes. // cause stageEntityChange() to fail due to implicit projection changes.
EppResource resource = ofy().load().key(resourceKey).now(); EppResource resource = ofy().load().key(resourceKey).now();
stageEntityChange(resource, resource); stageEntityChange(resource, resource);
} }
private Key<? extends EppResource> getResourceKey(
ResourceType type, String uniqueId, DateTime now) {
switch (type) {
case CONTACT:
return loadAndGetKey(ContactResource.class, uniqueId, now);
case HOST:
return loadAndGetKey(HostResource.class, uniqueId, now);
case DOMAIN:
return loadAndGetKey(DomainResource.class, uniqueId, now);
case APPLICATION:
return Key.create(DomainApplication.class, uniqueId);
default:
throw new IllegalStateException("Unknown type: " + type);
}
}
} }