google-nomulus/java/google/registry/tools/VerifyOteCommand.java
Michael Muller c458c05801 Rename Java packages to use the .google TLD
The dark lord Gosling designed the Java package naming system so that
ownership flows from the DNS system. Since we own the domain name
registry.google, it seems only appropriate that we should use
google.registry as our package name.
2016-05-13 20:04:42 -04:00

124 lines
4.9 KiB
Java

// Copyright 2016 The Domain Registry Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package google.registry.tools;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Predicates.notNull;
import static com.google.common.base.Verify.verifyNotNull;
import static google.registry.model.registrar.Registrar.loadByClientId;
import com.google.common.base.Function;
import com.google.common.base.Strings;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.beust.jcommander.Parameter;
import com.beust.jcommander.Parameters;
import google.registry.config.RegistryEnvironment;
import google.registry.model.registrar.Registrar;
import google.registry.tools.Command.GtechCommand;
import google.registry.tools.server.VerifyOteAction;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
/** Command to verify that a registrar has passed OT&E. */
@Parameters(
separators = " =",
commandDescription = "Verify passage of OT&E for specified (or all) registrars")
final class VerifyOteCommand implements ServerSideCommand, GtechCommand {
@Parameter(
description = "List of registrar names to check; must be the same names as the ones used "
+ "when creating the OT&E accounts")
private List<String> mainParameters = new ArrayList<>();
@Parameter(
names = "--check-all",
description = "Check the OT&E pass status of all active registrars")
private boolean checkAll;
@Parameter(
names = "--summarize",
description = "Only show a summary of information")
private boolean summarize;
private Connection connection;
@Override
public void setConnection(Connection connection) {
this.connection = connection;
}
@Override
public void run() throws IOException {
if (RegistryEnvironment.get() != RegistryEnvironment.SANDBOX) {
System.err.printf(
"WARNING: Running against %s environment. Are "
+ "you sure you didn\'t mean to run this against sandbox (e.g. \"-e SANDBOX\")?%n",
RegistryEnvironment.get());
}
checkArgument(
mainParameters.isEmpty() == checkAll,
"Must provide at least one registrar name, or supply --check-all with no names.");
for (String clientId : mainParameters) {
// OT&E registrars are created with clientIDs of registrarName-[1-4], but this command is
// passed registrarName. So check the existence of the first persisted registrar to see if
// the input is valid.
verifyNotNull(loadByClientId(clientId + "-1"), "Registrar %s does not exist.", clientId);
}
Collection<String> registrars =
mainParameters.isEmpty() ? getAllRegistrarNames() : mainParameters;
Map<String, Object> response = connection.sendJson(
VerifyOteAction.PATH,
ImmutableMap.of(
"summarize", Boolean.toString(summarize),
"registrars", new ArrayList<>(registrars)));
System.out.println(Strings.repeat("-", 80));
for (Entry<String, Object> registrar : response.entrySet()) {
System.out.printf(
summarize ? "%-20s - %s\n" : "\n=========== %s OT&E status ============\n%s\n",
registrar.getKey(),
registrar.getValue());
}
System.out.println(Strings.repeat("-", 80));
}
/**
* Returns the names of all active registrars. Finds registrar accounts with clientIds matching
* the format used for OT&E accounts (regname-1, regname-2, etc.) and returns just the common
* prefixes of those accounts (in this case, regname).
*/
private ImmutableSet<String> getAllRegistrarNames() {
return Registrar.loadAllActive()
.transform(new Function<Registrar, String>() {
@Override
public String apply(Registrar registrar) {
String name = registrar.getClientIdentifier();
// Look for names of the form "regname-1", "regname-2", etc. and strip the -# suffix.
String replacedName = name.replaceFirst("^(.*)-[1234]$", "$1");
// Check if any replacement happened, and thus whether the name matches the format.
// If it matches, provide the shortened name, and otherwise return null.
return name.equals(replacedName) ? null : replacedName;
}})
.filter(notNull())
.toSet();
}
}