Refactor GetEppResourceCommand hierarchy

This refactors the GetEppResourceCommand hierarchy a bit so that instead of
using the type param on the class to do implicit loading (which doesn't
work that well any more for domain applications anyway), we just do the
loading in each child class and rely on the parent class only for printing
and setting common flags.

I did this to make it possible for loadByForeignKey() to have strong typing
(in a future CL), but I think this changes stands on its own merits for
making the logic here more straightforward and actually somewhat shorter.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=134115072
This commit is contained in:
nickfelt 2016-09-23 14:05:22 -07:00 committed by Ben McIlwain
parent 9dde013066
commit c3723bfa2f
8 changed files with 50 additions and 67 deletions

View file

@ -14,16 +14,16 @@
package google.registry.tools; package google.registry.tools;
import static google.registry.model.EppResourceUtils.loadDomainApplication;
import com.beust.jcommander.Parameter; import com.beust.jcommander.Parameter;
import com.beust.jcommander.Parameters; import com.beust.jcommander.Parameters;
import google.registry.model.domain.DomainApplication;
import google.registry.tools.Command.GtechCommand; import google.registry.tools.Command.GtechCommand;
import java.util.List; import java.util.List;
/** Command to show a domain application. */ /** Command to show a domain application. */
@Parameters(separators = " =", commandDescription = "Show domain application record(s)") @Parameters(separators = " =", commandDescription = "Show domain application resource(s)")
final class GetApplicationCommand extends GetEppResourceCommand<DomainApplication> final class GetApplicationCommand extends GetEppResourceCommand implements GtechCommand {
implements GtechCommand {
@Parameter( @Parameter(
description = "Domain application id(s)", description = "Domain application id(s)",
@ -31,9 +31,10 @@ final class GetApplicationCommand extends GetEppResourceCommand<DomainApplicatio
private List<String> mainParameters; private List<String> mainParameters;
@Override @Override
public void processParameters() { public void runAndPrint() {
for (String applicationId : mainParameters) { for (String applicationId : mainParameters) {
printResource(applicationId); printResource(
"Application", applicationId, loadDomainApplication(applicationId, readTimestamp));
} }
} }
} }

View file

@ -17,6 +17,7 @@ package google.registry.tools;
import static google.registry.model.index.DomainApplicationIndex.loadActiveApplicationsByDomainName; import static google.registry.model.index.DomainApplicationIndex.loadActiveApplicationsByDomainName;
import static google.registry.model.registry.Registries.assertTldExists; import static google.registry.model.registry.Registries.assertTldExists;
import static google.registry.model.registry.Registries.findTldForNameOrThrow; import static google.registry.model.registry.Registries.findTldForNameOrThrow;
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;
@ -26,12 +27,12 @@ import google.registry.model.domain.DomainApplication;
import google.registry.tools.Command.GtechCommand; import google.registry.tools.Command.GtechCommand;
import google.registry.tools.Command.RemoteApiCommand; import google.registry.tools.Command.RemoteApiCommand;
import java.util.List; import java.util.List;
import org.joda.time.DateTime;
/** Command to generate a list of all applications for a given domain name(s). */ /** Command to generate a list of all applications for a given domain name(s). */
@Parameters(separators = " =", @Parameters(separators = " =",
commandDescription = "Generate list of application IDs and sponsors for given domain name(s)") commandDescription = "Generate list of application IDs and sponsors for given domain name(s)")
final class GetApplicationIdsCommand extends GetEppResourceCommand<DomainApplication> final class GetApplicationIdsCommand implements RemoteApiCommand, GtechCommand {
implements RemoteApiCommand, GtechCommand {
@Parameter( @Parameter(
description = "Fully qualified domain name(s)", description = "Fully qualified domain name(s)",
@ -39,10 +40,7 @@ final class GetApplicationIdsCommand extends GetEppResourceCommand<DomainApplica
private List<String> mainParameters; private List<String> mainParameters;
@Override @Override
void processParameters() { public void run() {
// Note that this function explicitly performs its own resource load and print, ignoring
// GetEppResourceCommand.printResource(), because we do NOT want to display the entire
// application to gTech users -- just the ID# and sponsor.
for (String domainName : mainParameters) { for (String domainName : mainParameters) {
InternetDomainName tld = findTldForNameOrThrow(InternetDomainName.from(domainName)); InternetDomainName tld = findTldForNameOrThrow(InternetDomainName.from(domainName));
assertTldExists(tld.toString()); assertTldExists(tld.toString());
@ -55,7 +53,7 @@ final class GetApplicationIdsCommand extends GetEppResourceCommand<DomainApplica
// example2.tld: // example2.tld:
// No applications exist for 'example2.tld'. // No applications exist for 'example2.tld'.
ImmutableList<DomainApplication> applications = ImmutableList.copyOf( ImmutableList<DomainApplication> applications = ImmutableList.copyOf(
loadActiveApplicationsByDomainName(domainName, readTimestamp)); loadActiveApplicationsByDomainName(domainName, DateTime.now(UTC)));
if (applications.isEmpty()) { if (applications.isEmpty()) {
System.out.printf(" No applications exist for \'%s\'.%n", domainName); System.out.printf(" No applications exist for \'%s\'.%n", domainName);
} else { } else {

View file

@ -14,6 +14,8 @@
package google.registry.tools; package google.registry.tools;
import static google.registry.model.EppResourceUtils.loadByForeignKey;
import com.beust.jcommander.Parameter; import com.beust.jcommander.Parameter;
import com.beust.jcommander.Parameters; import com.beust.jcommander.Parameters;
import google.registry.model.contact.ContactResource; import google.registry.model.contact.ContactResource;
@ -21,9 +23,8 @@ import google.registry.tools.Command.GtechCommand;
import java.util.List; import java.util.List;
/** Command to show one or more contacts. */ /** Command to show one or more contacts. */
@Parameters(separators = " =", commandDescription = "Show contact record(s)") @Parameters(separators = " =", commandDescription = "Show contact resource(s)")
final class GetContactCommand extends GetEppResourceCommand<ContactResource> final class GetContactCommand extends GetEppResourceCommand implements GtechCommand {
implements GtechCommand {
@Parameter( @Parameter(
description = "Contact id(s)", description = "Contact id(s)",
@ -31,9 +32,10 @@ final class GetContactCommand extends GetEppResourceCommand<ContactResource>
private List<String> mainParameters; private List<String> mainParameters;
@Override @Override
public void processParameters() { public void runAndPrint() {
for (String contactNameString : mainParameters) { for (String contactId : mainParameters) {
printResource(contactNameString); printResource(
"Contact", contactId, loadByForeignKey(ContactResource.class, contactId, readTimestamp));
} }
} }
} }

View file

@ -14,6 +14,8 @@
package google.registry.tools; package google.registry.tools;
import static google.registry.model.EppResourceUtils.loadByForeignKey;
import com.beust.jcommander.Parameter; import com.beust.jcommander.Parameter;
import com.beust.jcommander.Parameters; import com.beust.jcommander.Parameters;
import google.registry.model.domain.DomainResource; import google.registry.model.domain.DomainResource;
@ -21,9 +23,8 @@ import google.registry.tools.Command.GtechCommand;
import java.util.List; import java.util.List;
/** Command to show a domain resource. */ /** Command to show a domain resource. */
@Parameters(separators = " =", commandDescription = "Show domain record(s)") @Parameters(separators = " =", commandDescription = "Show domain resource(s)")
final class GetDomainCommand extends GetEppResourceCommand<DomainResource> final class GetDomainCommand extends GetEppResourceCommand implements GtechCommand {
implements GtechCommand {
@Parameter( @Parameter(
description = "Fully qualified domain name(s)", description = "Fully qualified domain name(s)",
@ -31,9 +32,10 @@ final class GetDomainCommand extends GetEppResourceCommand<DomainResource>
private List<String> mainParameters; private List<String> mainParameters;
@Override @Override
public void processParameters() { public void runAndPrint() {
for (String domainName : mainParameters) { for (String domainName : mainParameters) {
printResource(domainName); printResource(
"Domain", domainName, loadByForeignKey(DomainResource.class, domainName, readTimestamp));
} }
} }
} }

View file

@ -15,31 +15,22 @@
package google.registry.tools; package google.registry.tools;
import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Preconditions.checkArgument;
import static google.registry.model.EppResourceUtils.loadByForeignKey;
import static google.registry.model.EppResourceUtils.loadDomainApplication;
import static org.joda.time.DateTimeZone.UTC; 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 com.googlecode.objectify.Key;
import google.registry.model.EppResource; import google.registry.model.EppResource;
import google.registry.model.domain.DomainApplication;
import google.registry.tools.Command.RemoteApiCommand; import google.registry.tools.Command.RemoteApiCommand;
import google.registry.util.TypeUtils.TypeInstantiator; import javax.annotation.Nullable;
import org.joda.time.DateTime; import org.joda.time.DateTime;
/** /** Abstract command to print one or more resources to stdout. */
* Abstract command to show a resource.
*
* @param <R> {@link EppResource} subclass.
*/
@Parameters(separators = " =") @Parameters(separators = " =")
abstract class GetEppResourceCommand<R extends EppResource> implements RemoteApiCommand { abstract class GetEppResourceCommand implements RemoteApiCommand {
private final DateTime now = DateTime.now(UTC); private final DateTime now = DateTime.now(UTC);
private Class<R> clazz = new TypeInstantiator<R>(getClass()){}.getExactType();
@Parameter( @Parameter(
names = "--read_timestamp", names = "--read_timestamp",
description = "Timestamp to use when reading. May not be in the past.") description = "Timestamp to use when reading. May not be in the past.")
@ -50,31 +41,26 @@ abstract class GetEppResourceCommand<R extends EppResource> implements RemoteApi
description = "Fully expand the requested resource. NOTE: Output may be lengthy.") description = "Fully expand the requested resource. NOTE: Output may be lengthy.")
boolean expand; boolean expand;
/** Resolve any parameters into ids for loadResource. */ /** Runs the command's own logic that calls {@link #printResource}. */
abstract void processParameters(); abstract void runAndPrint();
/** /**
* Load a resource by ID and output. Append the websafe key to the output for use in e.g. * Prints a possibly-null resource to stdout, using resourceType and uniqueId to construct a
* manual mapreduce calls. * nice error message if the resource was null (i.e. doesn't exist).
*
* <p>The websafe key is appended to the output for use in e.g. manual mapreduce calls.
*/ */
void printResource(String uniqueId) { void printResource(String resourceType, String uniqueId, @Nullable EppResource resource) {
EppResource resource =
(clazz == DomainApplication.class)
? loadDomainApplication(uniqueId, readTimestamp)
: loadByForeignKey(clazz, uniqueId, readTimestamp);
System.out.println(resource != null System.out.println(resource != null
? String.format("%s\n\nWebsafe key: %s", ? String.format("%s\n\nWebsafe key: %s",
expand ? resource.toHydratedString() : resource, expand ? resource.toHydratedString() : resource,
Key.create(resource).getString()) Key.create(resource).getString())
: String.format( : String.format("%s '%s' does not exist or is deleted\n", resourceType, uniqueId));
"%s '%s' does not exist or is deleted\n",
clazz.getSimpleName().replace("Resource", ""),
uniqueId));
} }
@Override @Override
public void run() { public void run() {
checkArgument(!readTimestamp.isBefore(now), "--read_timestamp may not be in the past"); checkArgument(!readTimestamp.isBefore(now), "--read_timestamp may not be in the past");
processParameters(); runAndPrint();
} }
} }

View file

@ -14,6 +14,8 @@
package google.registry.tools; package google.registry.tools;
import static google.registry.model.EppResourceUtils.loadByForeignKey;
import com.beust.jcommander.Parameter; import com.beust.jcommander.Parameter;
import com.beust.jcommander.Parameters; import com.beust.jcommander.Parameters;
import google.registry.model.host.HostResource; import google.registry.model.host.HostResource;
@ -21,9 +23,8 @@ import google.registry.tools.Command.GtechCommand;
import java.util.List; import java.util.List;
/** Command to show one or more host resources. */ /** Command to show one or more host resources. */
@Parameters(separators = " =", commandDescription = "Show host record(s)") @Parameters(separators = " =", commandDescription = "Show host resource(s)")
final class GetHostCommand extends GetEppResourceCommand<HostResource> final class GetHostCommand extends GetEppResourceCommand implements GtechCommand {
implements GtechCommand {
@Parameter( @Parameter(
description = "Fully qualified host name(s)", description = "Fully qualified host name(s)",
@ -31,9 +32,10 @@ final class GetHostCommand extends GetEppResourceCommand<HostResource>
private List<String> mainParameters; private List<String> mainParameters;
@Override @Override
public void processParameters() { public void runAndPrint() {
for (String hostName : mainParameters) { for (String hostName : mainParameters) {
printResource(hostName); printResource(
"Host", hostName, loadByForeignKey(HostResource.class, hostName, readTimestamp));
} }
} }
} }

View file

@ -70,20 +70,20 @@ public class GetApplicationCommandTest extends CommandTestCase<GetApplicationCom
persistResource( persistResource(
newDomainApplication("example.tld").asBuilder().setDeletionTime(now.plusDays(1)).build()); newDomainApplication("example.tld").asBuilder().setDeletionTime(now.plusDays(1)).build());
runCommand("--read_timestamp=" + now.plusMonths(1), "2-TLD"); runCommand("--read_timestamp=" + now.plusMonths(1), "2-TLD");
assertInStdout("DomainApplication '2-TLD' does not exist or is deleted"); assertInStdout("Application '2-TLD' does not exist or is deleted");
} }
@Test @Test
public void testSuccess_deletedApplication() throws Exception { public void testSuccess_deletedApplication() throws Exception {
persistDeletedDomainApplication("example.tld", now); persistDeletedDomainApplication("example.tld", now);
runCommand("2-TLD"); runCommand("2-TLD");
assertInStdout("DomainApplication '2-TLD' does not exist or is deleted"); assertInStdout("Application '2-TLD' does not exist or is deleted");
} }
@Test @Test
public void testSuccess_applicationDoesNotExist() throws Exception { public void testSuccess_applicationDoesNotExist() throws Exception {
runCommand("42-TLD"); runCommand("42-TLD");
assertInStdout("DomainApplication '42-TLD' does not exist or is deleted"); assertInStdout("Application '42-TLD' does not exist or is deleted");
} }
@Test @Test
@ -99,6 +99,6 @@ public class GetApplicationCommandTest extends CommandTestCase<GetApplicationCom
runCommand("2-TLD", "4-TLD", "55-TLD"); runCommand("2-TLD", "4-TLD", "55-TLD");
assertInStdout("fullyQualifiedDomainName=example.tld"); assertInStdout("fullyQualifiedDomainName=example.tld");
assertInStdout("fullyQualifiedDomainName=example2.tld"); assertInStdout("fullyQualifiedDomainName=example2.tld");
assertInStdout("DomainApplication '55-TLD' does not exist or is deleted"); assertInStdout("Application '55-TLD' does not exist or is deleted");
} }
} }

View file

@ -64,14 +64,6 @@ public class GetApplicationIdsCommandTest extends CommandTestCase<GetApplication
runCommand("example.foo"); runCommand("example.foo");
} }
@Test
public void testSuccess_afterDeletion() throws Exception {
persistResource(
newDomainApplication("example.tld").asBuilder().setDeletionTime(now.plusDays(1)).build());
runCommand("example.tld", "--read_timestamp=" + now.plusMonths(1));
assertInStdout("No applications exist for 'example.tld'.");
}
@Test @Test
public void testSuccess_deletedApplication() throws Exception { public void testSuccess_deletedApplication() throws Exception {
persistResource( persistResource(