Replace loadByUniqueId() with methods that don't overload unique id

It is replaced by loadByForeignKey(), which does the same thing that
loadByUniqueId() did for contacts, hosts, and domains, and also
loadDomainApplication(), which loads domain application by ROID. This eliminates
the ugly mode-switching of attemping to load by other foreign key or ROID.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=133980156
This commit is contained in:
mcilwain 2016-09-22 11:45:58 -07:00 committed by Ben McIlwain
parent 025a4ae012
commit 21a98b899c
57 changed files with 367 additions and 340 deletions

View file

@ -18,7 +18,7 @@ import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.collect.Sets.difference;
import static google.registry.mapreduce.MapreduceRunner.PARAM_DRY_RUN;
import static google.registry.mapreduce.inputs.EppResourceInputs.createChildEntityInput;
import static google.registry.model.EppResourceUtils.loadByUniqueId;
import static google.registry.model.EppResourceUtils.loadByForeignKey;
import static google.registry.model.common.Cursor.CursorType.RECURRING_BILLING;
import static google.registry.model.ofy.ObjectifyService.ofy;
import static google.registry.pricing.PricingEngineProxy.getDomainRenewCost;
@ -158,7 +158,7 @@ public class ExpandRecurringBillingEventsAction implements Runnable {
Iterable<OneTime> oneTimesForDomain = ofy().load()
.type(OneTime.class)
.ancestor(loadByUniqueId(
.ancestor(loadByForeignKey(
DomainResource.class, recurring.getTargetId(), executeTime));
// Determine the billing times that already have OneTime events persisted.

View file

@ -14,7 +14,7 @@
package google.registry.dns;
import static google.registry.model.EppResourceUtils.loadByUniqueId;
import static google.registry.model.EppResourceUtils.loadByForeignKey;
import google.registry.dns.DnsConstants.TargetType;
import google.registry.model.EppResource;
@ -58,7 +58,7 @@ public final class RefreshDnsAction implements Runnable {
throw new BadRequestException("Unsupported type: " + type);
}
EppResource eppResource = loadByUniqueId(clazz, domainOrHostName, clock.nowUtc());
EppResource eppResource = loadByForeignKey(clazz, domainOrHostName, clock.nowUtc());
if (eppResource == null) {
throw new NotFoundException(
String.format("%s %s not found", type, domainOrHostName));

View file

@ -15,7 +15,7 @@
package google.registry.dns.writer.clouddns;
import static com.google.common.base.Preconditions.checkArgument;
import static google.registry.model.EppResourceUtils.loadByUniqueId;
import static google.registry.model.EppResourceUtils.loadByForeignKey;
import com.google.api.client.googleapis.json.GoogleJsonError.ErrorInfo;
import com.google.api.client.googleapis.json.GoogleJsonResponseException;
@ -110,7 +110,7 @@ class CloudDnsWriter implements DnsWriter {
// Load the target domain. Note that it can be null if this domain was just deleted.
Optional<DomainResource> domainResource =
Optional.fromNullable(loadByUniqueId(DomainResource.class, domainName, clock.nowUtc()));
Optional.fromNullable(loadByForeignKey(DomainResource.class, domainName, clock.nowUtc()));
// Return early if no DNS records should be published.
// desiredRecordsBuilder is populated with an empty set to indicate that all existing records
@ -180,7 +180,7 @@ class CloudDnsWriter implements DnsWriter {
// desiredRecords is populated with an empty set to indicate that all existing records
// should be deleted.
Optional<HostResource> host =
Optional.fromNullable(loadByUniqueId(HostResource.class, hostName, clock.nowUtc()));
Optional.fromNullable(loadByForeignKey(HostResource.class, hostName, clock.nowUtc()));
// Return early if the host is deleted.
if (!host.isPresent()) {

View file

@ -17,7 +17,7 @@ package google.registry.dns.writer.dnsupdate;
import static com.google.common.base.Verify.verify;
import static com.google.common.collect.Sets.intersection;
import static com.google.common.collect.Sets.union;
import static google.registry.model.EppResourceUtils.loadByUniqueId;
import static google.registry.model.EppResourceUtils.loadByForeignKey;
import com.google.common.base.Joiner;
import com.google.common.base.Optional;
@ -111,7 +111,7 @@ public class DnsUpdateWriter implements DnsWriter {
* this domain refresh request
*/
private void publishDomain(String domainName, String requestingHostName) {
DomainResource domain = loadByUniqueId(DomainResource.class, domainName, clock.nowUtc());
DomainResource domain = loadByForeignKey(DomainResource.class, domainName, clock.nowUtc());
try {
Update update = new Update(toAbsoluteName(findTldFromName(domainName)));
update.delete(toAbsoluteName(domainName), Type.ANY);
@ -202,7 +202,7 @@ public class DnsUpdateWriter implements DnsWriter {
for (String hostName :
intersection(
domain.loadNameserverFullyQualifiedHostNames(), domain.getSubordinateHosts())) {
HostResource host = loadByUniqueId(HostResource.class, hostName, clock.nowUtc());
HostResource host = loadByForeignKey(HostResource.class, hostName, clock.nowUtc());
update.add(makeAddressSet(host));
update.add(makeV6AddressSet(host));
}

View file

@ -15,7 +15,7 @@
package google.registry.flows;
import static com.google.common.base.Preconditions.checkState;
import static google.registry.model.EppResourceUtils.loadByUniqueId;
import static google.registry.model.EppResourceUtils.loadByForeignKey;
import static google.registry.model.EppResourceUtils.queryDomainsUsingResource;
import static google.registry.model.domain.DomainResource.extendRegistrationWithCap;
import static google.registry.model.ofy.ObjectifyService.ofy;
@ -287,7 +287,7 @@ public class ResourceFlowUtils {
public static <R extends EppResource> R loadResourceForQuery(
Class<R> clazz, String targetId, DateTime now) throws ResourceToQueryDoesNotExistException {
R resource = loadByUniqueId(clazz, targetId, now);
R resource = loadByForeignKey(clazz, targetId, now);
if (resource == null) {
throw new ResourceToQueryDoesNotExistException(clazz, targetId);
}
@ -296,7 +296,7 @@ public class ResourceFlowUtils {
public static <R extends EppResource> R loadResourceToMutate(
Class<R> clazz, String targetId, DateTime now) throws ResourceToMutateDoesNotExistException {
R resource = loadByUniqueId(clazz, targetId, now);
R resource = loadByForeignKey(clazz, targetId, now);
if (resource == null) {
throw new ResourceToMutateDoesNotExistException(clazz, targetId);
}
@ -305,7 +305,7 @@ public class ResourceFlowUtils {
public static <R extends EppResource> void verifyResourceDoesNotExist(
Class<R> clazz, String targetId, DateTime now) throws EppException {
if (loadByUniqueId(clazz, targetId, now) != null) {
if (loadByForeignKey(clazz, targetId, now) != null) {
throw new ResourceAlreadyExistsException(targetId);
}
}

View file

@ -14,13 +14,15 @@
package google.registry.flows;
import static google.registry.model.EppResourceUtils.loadByUniqueId;
import static google.registry.model.EppResourceUtils.loadByForeignKey;
import static google.registry.model.EppResourceUtils.loadDomainApplication;
import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Sets;
import google.registry.flows.EppException.StatusProhibitsOperationException;
import google.registry.model.EppResource;
import google.registry.model.domain.DomainApplication;
import google.registry.model.domain.launch.ApplicationIdTargetExtension;
import google.registry.model.eppcommon.StatusValue;
import google.registry.model.eppinput.ResourceCommand.SingleResourceCommand;
@ -38,6 +40,7 @@ public abstract class SingleResourceFlow<R extends EppResource, C extends Single
protected R existingResource;
protected String targetId;
@SuppressWarnings("unchecked")
@Override
protected final void initResourceFlow() throws EppException {
targetId = getTargetId();
@ -48,9 +51,14 @@ public abstract class SingleResourceFlow<R extends EppResource, C extends Single
// Some flows such as DomainApplicationInfoFlow have the id marked as optional in the schema.
// We require it by policy in the relevant flow, but here we need to make sure not to NPE when
// initializing the (obviously nonexistent) existing resource.
existingResource = (targetId == null || !tryToLoadExisting())
? null
: loadByUniqueId(resourceClass, targetId, now);
if (targetId != null && tryToLoadExisting()) {
// This ugliness goes away once domain application flows are flattened.
if (resourceClass == DomainApplication.class) {
existingResource = (R) loadDomainApplication(targetId, now);
} else {
existingResource = loadByForeignKey(resourceClass, targetId, now);
}
}
if (existingResource != null) {
Set<StatusValue> problems = Sets.intersection(
existingResource.getStatusValues(), getDisallowedStatuses());

View file

@ -32,7 +32,7 @@ import static google.registry.flows.domain.DomainFlowUtils.verifyPremiumNameIsNo
import static google.registry.flows.domain.DomainFlowUtils.verifySignedMarks;
import static google.registry.flows.domain.DomainFlowUtils.verifyUnitIsYears;
import static google.registry.model.EppResourceUtils.createDomainRoid;
import static google.registry.model.EppResourceUtils.loadByUniqueId;
import static google.registry.model.EppResourceUtils.loadByForeignKey;
import static google.registry.model.domain.fee.Fee.FEE_CREATE_COMMAND_EXTENSIONS_IN_PREFERENCE_ORDER;
import static google.registry.model.ofy.ObjectifyService.ofy;
import static google.registry.model.registry.Registries.findTldForName;
@ -164,7 +164,7 @@ public abstract class BaseDomainCreateFlow<R extends DomainBase, B extends Build
@Override
public DomainResource run() {
// This is cacheable because we are outside of a transaction.
return loadByUniqueId(DomainResource.class, targetId, now);
return loadByForeignKey(DomainResource.class, targetId, now);
}});
// If the domain exists already and isn't in the ADD grace period then there is no way it will
// be suddenly deleted and therefore the create must fail.

View file

@ -16,7 +16,7 @@ package google.registry.flows.domain;
import static com.google.common.base.Preconditions.checkNotNull;
import static google.registry.flows.domain.DomainFlowUtils.getReservationType;
import static google.registry.model.EppResourceUtils.loadByUniqueId;
import static google.registry.model.EppResourceUtils.loadDomainApplication;
import static google.registry.model.ofy.ObjectifyService.ofy;
import static google.registry.pricing.PricingEngineProxy.getDomainCreateCost;
import static google.registry.util.CollectionUtils.isNullOrEmpty;
@ -75,7 +75,7 @@ public class DomainAllocateFlow extends DomainCreateOrAllocateFlow {
throw new OnlySuperuserCanAllocateException();
}
String applicationRoid = allocateCreate.getApplicationRoid();
application = loadByUniqueId(DomainApplication.class, applicationRoid, now);
application = loadDomainApplication(applicationRoid, now);
if (application == null) {
throw new MissingApplicationException(applicationRoid);
}

View file

@ -15,11 +15,11 @@
package google.registry.flows.domain;
import static google.registry.flows.EppXmlTransformer.unmarshal;
import static google.registry.flows.ResourceFlowUtils.loadResourceForQuery;
import static google.registry.flows.ResourceFlowUtils.verifyOptionalAuthInfoForResource;
import static google.registry.flows.ResourceFlowUtils.verifyResourceOwnership;
import static google.registry.flows.domain.DomainFlowUtils.addSecDnsExtensionIfPresent;
import static google.registry.flows.domain.DomainFlowUtils.verifyApplicationDomainMatchesTargetId;
import static google.registry.model.EppResourceUtils.loadDomainApplication;
import static google.registry.model.eppoutput.Result.Code.SUCCESS;
import com.google.common.base.Optional;
@ -31,6 +31,7 @@ import google.registry.flows.FlowModule.ApplicationId;
import google.registry.flows.FlowModule.ClientId;
import google.registry.flows.FlowModule.TargetId;
import google.registry.flows.LoggedInFlow;
import google.registry.flows.exceptions.ResourceToQueryDoesNotExistException;
import google.registry.model.domain.DomainApplication;
import google.registry.model.domain.DomainCommand.Info;
import google.registry.model.domain.launch.LaunchInfoExtension;
@ -75,8 +76,10 @@ public final class DomainApplicationInfoFlow extends LoggedInFlow {
if (applicationId.isEmpty()) {
throw new MissingApplicationIdException();
}
DomainApplication application =
loadResourceForQuery(DomainApplication.class, applicationId, now);
DomainApplication application = loadDomainApplication(applicationId, now);
if (application == null) {
throw new ResourceToQueryDoesNotExistException(DomainApplication.class, applicationId);
}
verifyApplicationDomainMatchesTargetId(application, targetId);
verifyOptionalAuthInfoForResource(authInfo, application);
LaunchInfoExtension launchInfo = eppInput.getSingleExtension(LaunchInfoExtension.class);

View file

@ -15,7 +15,7 @@
package google.registry.flows.domain;
import static com.google.common.base.Preconditions.checkArgument;
import static google.registry.model.EppResourceUtils.loadByUniqueId;
import static google.registry.model.EppResourceUtils.loadByForeignKey;
import static google.registry.model.ofy.ObjectifyService.ofy;
import static google.registry.pricing.PricingEngineProxy.getPricesForDomainName;
import static google.registry.util.CollectionUtils.nullToEmpty;
@ -181,7 +181,7 @@ public final class TldSpecificLogicProxy {
RegistryExtraFlowLogicProxy.newInstanceForTld(registry.getTldStr());
if (extraFlowLogic.isPresent()) {
// TODO: Consider changing the method definition to have the domain passed in to begin with.
DomainResource domain = loadByUniqueId(DomainResource.class, domainName, date);
DomainResource domain = loadByForeignKey(DomainResource.class, domainName, date);
if (domain == null) {
throw new ResourceToMutateDoesNotExistException(DomainResource.class, domainName);
}
@ -247,7 +247,7 @@ public final class TldSpecificLogicProxy {
RegistryExtraFlowLogicProxy.newInstanceForTld(registry.getTldStr());
if (extraFlowLogic.isPresent()) {
// TODO: Consider changing the method definition to have the domain passed in to begin with.
DomainResource domain = loadByUniqueId(DomainResource.class, domainName, date);
DomainResource domain = loadByForeignKey(DomainResource.class, domainName, date);
if (domain == null) {
throw new ResourceToMutateDoesNotExistException(DomainResource.class, domainName);
}

View file

@ -15,7 +15,7 @@
package google.registry.flows.host;
import static google.registry.model.EppResourceUtils.isActive;
import static google.registry.model.EppResourceUtils.loadByUniqueId;
import static google.registry.model.EppResourceUtils.loadByForeignKey;
import static google.registry.model.registry.Registries.findTldForName;
import com.google.common.base.Joiner;
@ -84,7 +84,7 @@ public class HostFlowUtils {
// This is a subordinate host
String domainName = Joiner.on('.').join(Iterables.skip(
hostName.parts(), hostName.parts().size() - (tld.get().parts().size() + 1)));
DomainResource superordinateDomain = loadByUniqueId(DomainResource.class, domainName, now);
DomainResource superordinateDomain = loadByForeignKey(DomainResource.class, domainName, now);
if (superordinateDomain == null || !isActive(superordinateDomain, now)) {
throw new SuperordinateDomainDoesNotExistException(domainName);
}

View file

@ -31,6 +31,7 @@ import google.registry.config.RegistryEnvironment;
import google.registry.model.EppResource.Builder;
import google.registry.model.EppResource.ForeignKeyedEppResource;
import google.registry.model.contact.ContactResource;
import google.registry.model.domain.DomainApplication;
import google.registry.model.domain.DomainBase;
import google.registry.model.eppcommon.StatusValue;
import google.registry.model.host.HostResource;
@ -84,40 +85,29 @@ public final class EppResourceUtils {
*
* <p>Loading an {@link EppResource} by itself is not sufficient to know its current state since
* it may have various expirable conditions and status values that might implicitly change its
* state as time progresses even if it has not been updated in the datastore. Rather, the
* resource must be combined with a timestamp to view its current state. We use a global last
* updated timestamp on the entire entity group (which is essentially free since all writes to
* the entity group must be serialized anyways) to guarantee monotonically increasing write
* times, so forwarding our projected time to the greater of "now", and this update timestamp
* guarantees that we're not projecting into the past.
* state as time progresses even if it has not been updated in the datastore. Rather, the resource
* must be combined with a timestamp to view its current state. We use a global last updated
* timestamp on the entire entity group (which is essentially free since all writes to the entity
* group must be serialized anyways) to guarantee monotonically increasing write times, so
* forwarding our projected time to the greater of "now", and this update timestamp guarantees
* that we're not projecting into the past.
*
* @param clazz the resource type to load
* @param foreignKey id to match
* @param now the current logical time to project resources at
*/
public static <T extends EppResource> T loadByUniqueId(
@Nullable
public static <T extends EppResource> T loadByForeignKey(
Class<T> clazz, String foreignKey, DateTime now) {
// For regular foreign-keyed resources, get the key by loading the FKI; for domain applications,
// we can construct the key directly, since the provided foreignKey is just the repoId.
Key<T> resourceKey = ForeignKeyedEppResource.class.isAssignableFrom(clazz)
? loadAndGetKey(clazz, foreignKey, now)
: Key.create(null, clazz, foreignKey);
checkArgument(
ForeignKeyedEppResource.class.isAssignableFrom(clazz),
"loadByForeignKey may only be called for foreign keyed EPP resources");
Key<T> resourceKey = loadAndGetKey(clazz, foreignKey, now);
if (resourceKey == null) {
return null;
}
T resource = ofy().load().key(resourceKey).now();
if (resource == null
// You'd think this couldn't happen, but it can. For polymorphic entities, a Key is of
// necessity a reference to the base type (since datastore doesn't have polymorphism and
// Objectify is faking it). In the non-foreign-key code path above where we directly create
// a Key, there is no way to know whether the Key points to an instance of the desired
// subclass without loading it. Due to type erasure, it gets stuffed into "resource" without
// causing a ClassCastException even if it's the wrong type until you actually try to use it
// as the wrong type, at which point it blows up somewhere else in the code. Concretely,
// this means that without this line bad things would happen if you tried to load a
// DomainApplication using the id of a DomainResource (but not vice versa).
|| !clazz.isInstance(resource)
|| isAtOrAfter(now, resource.getDeletionTime())) {
if (resource == null || isAtOrAfter(now, resource.getDeletionTime())) {
return null;
}
// When setting status values based on a time, choose the greater of "now" and the resource's
@ -127,8 +117,23 @@ public final class EppResourceUtils {
// fail when it tries to save anything via Ofy, since "now" is needed to be > the last update
// time for writes.
return cloneProjectedAtTime(
resource,
latestOf(now, resource.getUpdateAutoTimestamp().getTimestamp()));
resource, latestOf(now, resource.getUpdateAutoTimestamp().getTimestamp()));
}
/**
* Returns the domain application with the given application id if it exists, or null if it does
* not or is soft-deleted as of the given time.
*/
@Nullable
public static DomainApplication loadDomainApplication(String applicationId, DateTime now) {
DomainApplication application =
ofy().load().key(Key.create(DomainApplication.class, applicationId)).now();
if (application == null || isAtOrAfter(now, application.getDeletionTime())) {
return null;
}
// Applications don't have any speculative changes that become effective later, so no need to
// clone forward in time.
return application;
}
/**

View file

@ -14,7 +14,7 @@
package google.registry.rdap;
import static google.registry.model.EppResourceUtils.loadByUniqueId;
import static google.registry.model.EppResourceUtils.loadByForeignKey;
import static google.registry.request.Action.Method.GET;
import static google.registry.request.Action.Method.HEAD;
@ -55,8 +55,7 @@ public class RdapDomainAction extends RdapActionBase {
pathSearchString = canonicalizeName(pathSearchString);
validateDomainName(pathSearchString);
// The query string is not used; the RDAP syntax is /rdap/domain/mydomain.com.
DomainResource domainResource =
loadByUniqueId(DomainResource.class, pathSearchString, now);
DomainResource domainResource = loadByForeignKey(DomainResource.class, pathSearchString, now);
if (domainResource == null) {
throw new NotFoundException(pathSearchString + " not found");
}

View file

@ -14,7 +14,7 @@
package google.registry.rdap;
import static google.registry.model.EppResourceUtils.loadByUniqueId;
import static google.registry.model.EppResourceUtils.loadByForeignKey;
import static google.registry.model.index.ForeignKeyIndex.loadAndGetKey;
import static google.registry.model.ofy.ObjectifyService.ofy;
import static google.registry.request.Action.Method.GET;
@ -136,7 +136,7 @@ public class RdapDomainSearchAction extends RdapActionBase {
// Handle queries without a wildcard -- just load by foreign key.
if (!partialStringQuery.getHasWildcard()) {
DomainResource domainResource =
loadByUniqueId(DomainResource.class, partialStringQuery.getInitialString(), now);
loadByForeignKey(DomainResource.class, partialStringQuery.getInitialString(), now);
if (domainResource == null) {
return ImmutableList.of();
}
@ -222,7 +222,7 @@ public class RdapDomainSearchAction extends RdapActionBase {
// differently. We use the suffix to look up the domain, then loop through the subordinate hosts
// looking for matches.
} else {
DomainResource domainResource = loadByUniqueId(
DomainResource domainResource = loadByForeignKey(
DomainResource.class, partialStringQuery.getSuffix(), now);
if (domainResource == null) {
throw new NotFoundException("No domain found for specified nameserver suffix");

View file

@ -14,7 +14,7 @@
package google.registry.rdap;
import static google.registry.model.EppResourceUtils.loadByUniqueId;
import static google.registry.model.EppResourceUtils.loadByForeignKey;
import static google.registry.request.Action.Method.GET;
import static google.registry.request.Action.Method.HEAD;
@ -55,8 +55,7 @@ public class RdapNameserverAction extends RdapActionBase {
pathSearchString = canonicalizeName(pathSearchString);
// The RDAP syntax is /rdap/nameserver/ns1.mydomain.com.
validateDomainName(pathSearchString);
HostResource hostResource =
loadByUniqueId(HostResource.class, pathSearchString, now);
HostResource hostResource = loadByForeignKey(HostResource.class, pathSearchString, now);
if (hostResource == null) {
throw new NotFoundException(pathSearchString + " not found");
}

View file

@ -14,7 +14,7 @@
package google.registry.rdap;
import static google.registry.model.EppResourceUtils.loadByUniqueId;
import static google.registry.model.EppResourceUtils.loadByForeignKey;
import static google.registry.model.ofy.ObjectifyService.ofy;
import static google.registry.request.Action.Method.GET;
import static google.registry.request.Action.Method.HEAD;
@ -118,7 +118,7 @@ public class RdapNameserverSearchAction extends RdapActionBase {
// Handle queries without a wildcard -- just load by foreign key.
if (!partialStringQuery.getHasWildcard()) {
HostResource hostResource =
loadByUniqueId(HostResource.class, partialStringQuery.getInitialString(), now);
loadByForeignKey(HostResource.class, partialStringQuery.getInitialString(), now);
if (hostResource == null) {
throw new NotFoundException("No nameservers found");
}
@ -141,7 +141,7 @@ public class RdapNameserverSearchAction extends RdapActionBase {
// looking for matches.
} else {
DomainResource domainResource =
loadByUniqueId(DomainResource.class, partialStringQuery.getSuffix(), now);
loadByForeignKey(DomainResource.class, partialStringQuery.getSuffix(), now);
if (domainResource == null) {
throw new NotFoundException("No domain found for specified nameserver suffix");
}
@ -150,7 +150,7 @@ public class RdapNameserverSearchAction extends RdapActionBase {
// We can't just check that the host name starts with the initial query string, because then
// the query ns.exam*.example.com would match against nameserver ns.example.com.
if (partialStringQuery.matches(fqhn)) {
HostResource hostResource = loadByUniqueId(HostResource.class, fqhn, now);
HostResource hostResource = loadByForeignKey(HostResource.class, fqhn, now);
if (hostResource != null) {
hostListBuilder.add(hostResource);
}

View file

@ -15,13 +15,15 @@
package google.registry.tools;
import static com.google.common.base.Preconditions.checkArgument;
import static google.registry.model.EppResourceUtils.loadByUniqueId;
import static google.registry.model.EppResourceUtils.loadByForeignKey;
import static google.registry.model.EppResourceUtils.loadDomainApplication;
import static org.joda.time.DateTimeZone.UTC;
import com.beust.jcommander.Parameter;
import com.beust.jcommander.Parameters;
import com.googlecode.objectify.Key;
import google.registry.model.EppResource;
import google.registry.model.domain.DomainApplication;
import google.registry.tools.Command.RemoteApiCommand;
import google.registry.util.TypeUtils.TypeInstantiator;
import org.joda.time.DateTime;
@ -32,8 +34,7 @@ import org.joda.time.DateTime;
* @param <R> {@link EppResource} subclass.
*/
@Parameters(separators = " =")
abstract class GetEppResourceCommand<R extends EppResource>
implements RemoteApiCommand {
abstract class GetEppResourceCommand<R extends EppResource> implements RemoteApiCommand {
private final DateTime now = DateTime.now(UTC);
@ -52,14 +53,17 @@ abstract class GetEppResourceCommand<R extends EppResource>
/** Resolve any parameters into ids for loadResource. */
abstract void processParameters();
/**
/**
* Load a resource by ID and output. Append the websafe key to the output for use in e.g.
* manual mapreduce calls.
*/
void printResource(String uniqueId) {
R resource = loadByUniqueId(clazz, uniqueId, readTimestamp);
EppResource resource =
(clazz == DomainApplication.class)
? loadDomainApplication(uniqueId, readTimestamp)
: loadByForeignKey(clazz, uniqueId, readTimestamp);
System.out.println(resource != null
? String.format("%s\n\nWebsafe key: %s",
? String.format("%s\n\nWebsafe key: %s",
expand ? resource.toHydratedString() : resource,
Key.create(resource).getString())
: String.format(

View file

@ -18,7 +18,7 @@ import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Strings.nullToEmpty;
import static com.google.common.collect.Sets.difference;
import static google.registry.model.EppResourceUtils.checkResourcesExist;
import static google.registry.model.EppResourceUtils.loadByUniqueId;
import static google.registry.model.EppResourceUtils.loadByForeignKey;
import static google.registry.model.ofy.ObjectifyService.ofy;
import static org.joda.time.DateTimeZone.UTC;
@ -120,7 +120,7 @@ final class UniformRapidSuspensionCommand extends MutatingEppToolCommand impleme
} catch (ClassCastException | ParseException e) {
throw new IllegalArgumentException("Invalid --dsdata JSON", e);
}
DomainResource domain = loadByUniqueId(DomainResource.class, domainName, now);
DomainResource domain = loadByForeignKey(DomainResource.class, domainName, now);
checkArgument(domain != null, "Domain '%s' does not exist", domainName);
Set<String> missingHosts =
difference(newHostsSet, checkResourcesExist(HostResource.class, newHosts, now));

View file

@ -16,7 +16,7 @@ package google.registry.tools;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkState;
import static google.registry.model.EppResourceUtils.loadByUniqueId;
import static google.registry.model.EppResourceUtils.loadDomainApplication;
import static google.registry.model.domain.launch.ApplicationStatus.ALLOCATED;
import static google.registry.model.ofy.ObjectifyService.ofy;
import static google.registry.util.PreconditionsUtils.checkArgumentNotNull;
@ -89,8 +89,7 @@ final class UpdateApplicationStatusCommand extends MutatingCommand {
DateTime now = ofy().getTransactionTime();
// Load the domain application.
DomainApplication domainApplication =
loadByUniqueId(DomainApplication.class, applicationId, now);
DomainApplication domainApplication = loadDomainApplication(applicationId, now);
checkArgumentNotNull(domainApplication, "Domain application does not exist");
// It's not an error if the application already has the intended status. We want the method

View file

@ -15,7 +15,7 @@
package google.registry.tools;
import static com.google.common.base.Preconditions.checkArgument;
import static google.registry.model.EppResourceUtils.loadByUniqueId;
import static google.registry.model.EppResourceUtils.loadDomainApplication;
import static google.registry.model.ofy.ObjectifyService.ofy;
import com.beust.jcommander.Parameter;
@ -84,8 +84,7 @@ final class UpdateClaimsNoticeCommand implements RemoteApiCommand {
DateTime now = ofy().getTransactionTime();
// Load the domain application.
DomainApplication domainApplication =
loadByUniqueId(DomainApplication.class, applicationId, now);
DomainApplication domainApplication = loadDomainApplication(applicationId, now);
checkArgument(domainApplication != null, "Domain application does not exist");
// Make sure this isn't a sunrise application.

View file

@ -16,7 +16,7 @@ package google.registry.tools;
import static com.google.common.base.Preconditions.checkArgument;
import static google.registry.flows.domain.DomainFlowUtils.verifyEncodedSignedMark;
import static google.registry.model.EppResourceUtils.loadByUniqueId;
import static google.registry.model.EppResourceUtils.loadDomainApplication;
import static google.registry.model.ofy.ObjectifyService.ofy;
import static google.registry.tmch.TmchData.readEncodedSignedMark;
import static java.nio.charset.StandardCharsets.US_ASCII;
@ -81,8 +81,7 @@ final class UpdateSmdCommand implements RemoteApiCommand {
DateTime now = ofy().getTransactionTime();
// Load the domain application.
DomainApplication domainApplication =
loadByUniqueId(DomainApplication.class, applicationId, now);
DomainApplication domainApplication = loadDomainApplication(applicationId, now);
checkArgument(domainApplication != null, "Domain application does not exist");
// Make sure this is a sunrise application.

View file

@ -15,7 +15,8 @@
package google.registry.tools.javascrap;
import static com.google.common.collect.Maps.uniqueIndex;
import static google.registry.model.EppResourceUtils.loadByUniqueId;
import static google.registry.model.EppResourceUtils.loadByForeignKey;
import static google.registry.model.EppResourceUtils.loadDomainApplication;
import static google.registry.model.ofy.ObjectifyService.ofy;
import static org.joda.time.DateTimeZone.UTC;
@ -32,7 +33,7 @@ import java.util.Arrays;
import java.util.Map;
import org.joda.time.DateTime;
/** Load and resave an object in the probers, to trigger @OnSave changes. */
/** A command to load and resave an entity, which triggers @OnSave changes. */
@Parameters(
separators = " =",
commandDescription = "Load and resave an object, to trigger @OnSave changes")
@ -45,9 +46,9 @@ public final class LoadAndResaveCommand extends MutatingCommand {
protected String type;
@Parameter(
names = "--key",
description = "Foreign key of the resource. ")
protected String foreignKey;
names = "--id",
description = "Foreign key of the resource, or application ID of the domain application.")
protected String uniqueId;
private static final Map<String, Class<? extends EppResource>> CLASSES_BY_NAME =
uniqueIndex(
@ -64,12 +65,15 @@ public final class LoadAndResaveCommand extends MutatingCommand {
@Override
protected void init() throws Exception {
Class<? extends EppResource> clazz = CLASSES_BY_NAME.get(type);
EppResource existing =
(clazz == DomainApplication.class)
? loadDomainApplication(uniqueId, DateTime.now(UTC))
: loadByForeignKey(clazz, uniqueId, DateTime.now(UTC));
// Find the resource by foreign key, and then reload it directly, bypassing loadByUniqueId().
// We need to do a reload because otherwise stageEntityChange() can fail due to the implicit
// changes done when forwarding the resource to "now" in cloneProjectedAtTime().
EppResource resource = ofy().load().entity(
loadByUniqueId(CLASSES_BY_NAME.get(type), foreignKey, DateTime.now(UTC))).now();
EppResource resource = ofy().load().entity(existing).now();
stageEntityChange(resource, resource);
}
}

View file

@ -15,7 +15,7 @@
package google.registry.whois;
import static com.google.common.base.Preconditions.checkNotNull;
import static google.registry.model.EppResourceUtils.loadByUniqueId;
import static google.registry.model.EppResourceUtils.loadByForeignKey;
import static google.registry.model.registry.Registries.findTldForName;
import static google.registry.model.registry.Registries.getTlds;
import static javax.servlet.http.HttpServletResponse.SC_NOT_FOUND;
@ -52,7 +52,7 @@ abstract class DomainOrHostLookupCommand<T extends EppResource> implements Whois
}
// Google Policy: Do not return records under TLDs for which we're not authoritative.
if (tld.isPresent() && getTlds().contains(tld.get().toString())) {
T domainOrHost = loadByUniqueId(
T domainOrHost = loadByForeignKey(
new TypeInstantiator<T>(getClass()){}.getExactType(),
domainOrHostName.toString(),
now);