Refactor CreateDomainCommand and add UpdateDomainCommand

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=160282921
This commit is contained in:
bbilbo 2017-06-27 09:12:41 -07:00 committed by Ben McIlwain
parent 30d5d05fdf
commit bbdf9bfc38
18 changed files with 944 additions and 80 deletions

View file

@ -34,6 +34,7 @@ java_library(
]),
visibility = [":allowed-tools"],
deps = [
"//java/com/google/common/flogger",
"//java/google/registry/backup",
"//java/google/registry/bigquery",
"//java/google/registry/config",

View file

@ -16,66 +16,24 @@ package google.registry.tools;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Strings.isNullOrEmpty;
import static google.registry.util.CollectionUtils.findDuplicates;
import static google.registry.util.PreconditionsUtils.checkArgumentNotNull;
import com.beust.jcommander.Parameter;
import com.beust.jcommander.Parameters;
import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableSet;
import com.google.template.soy.data.SoyMapData;
import google.registry.tools.soy.DomainCreateSoyInfo;
import google.registry.util.StringGenerator;
import java.util.List;
import java.util.Set;
import javax.inject.Inject;
/** A command to create a new domain via EPP. */
@Parameters(separators = " =", commandDescription = "Create a new domain via EPP.")
final class CreateDomainCommand extends MutatingEppToolCommand {
@Parameter(
names = {"-c", "--client"},
description = "Client identifier of the registrar to execute the command as",
required = true)
String clientId;
@Parameter(description = "Names of the domains", required = true)
private List<String> mainParameters;
final class CreateDomainCommand extends CreateOrUpdateDomainCommand {
@Parameter(
names = "--period",
description = "Initial registration period, in years.")
private Integer period;
@Parameter(
names = {"-n", "--nameservers"},
description = "Comma-separated list of nameservers, up to 13."
)
private List<String> ns;
@Parameter(
names = {"-r", "--registrant"},
description = "Domain registrant.",
required = true)
private String registrant;
@Parameter(
names = {"-a", "--admin"},
description = "Admin contact.",
required = true)
private String admin;
@Parameter(
names = {"-t", "--tech"},
description = "Technical contact.",
required = true)
private String tech;
@Parameter(
names = {"-p", "--password"},
description = "Password. Optional, randomly generated if not provided.")
private String password;
@Inject
StringGenerator passwordGenerator;
@ -83,13 +41,12 @@ final class CreateDomainCommand extends MutatingEppToolCommand {
@Override
protected void initMutatingEppToolCommand() {
checkArgumentNotNull(registrant, "Registrant must be specified");
checkArgument(!admins.isEmpty(), "At least one admin must be specified");
checkArgument(!techs.isEmpty(), "At least one tech must be specified");
if (isNullOrEmpty(password)) {
password = passwordGenerator.createString(PASSWORD_LENGTH);
}
checkArgument(ns == null || ns.size() <= 13, "There can be at most 13 nameservers.");
String duplicates = Joiner.on(", ").join(findDuplicates(mainParameters));
checkArgument(duplicates.isEmpty(), "Duplicate arguments found: '%s'", duplicates);
Set<String> domains = ImmutableSet.copyOf(mainParameters);
for (String domain : domains) {
setSoyTemplate(DomainCreateSoyInfo.getInstance(), DomainCreateSoyInfo.DOMAINCREATE);
@ -98,10 +55,10 @@ final class CreateDomainCommand extends MutatingEppToolCommand {
new SoyMapData(
"domain", domain,
"period", period == null ? null : period.toString(),
"ns", ns,
"nameservers", nameservers,
"registrant", registrant,
"admin", admin,
"tech", tech,
"admins", admins,
"techs", techs,
"password", password));
}
}

View file

@ -0,0 +1,82 @@
// Copyright 2017 The Nomulus 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 google.registry.util.CollectionUtils.findDuplicates;
import com.beust.jcommander.Parameter;
import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableSet;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
/** Shared base class for commands to create or update a Domain via EPP. */
abstract class CreateOrUpdateDomainCommand extends MutatingEppToolCommand {
@Parameter(
names = {"-c", "--client"},
description = "Client identifier of the registrar to execute the command as",
required = true
)
String clientId;
@Parameter(description = "Names of the domains", required = true)
private List<String> mainParameters;
@Parameter(
names = {"-n", "--nameservers"},
description = "Comma-separated list of nameservers, up to 13."
)
List<String> nameservers = new ArrayList<>();
@Parameter(
names = {"-r", "--registrant"},
description = "Domain registrant."
)
String registrant;
@Parameter(
names = {"-a", "--admins"},
description = "Comma-separated list of admin contacts."
)
List<String> admins = new ArrayList<>();
@Parameter(
names = {"-t", "--techs"},
description = "Comma-separated list of technical contacts."
)
List<String> techs = new ArrayList<>();
@Parameter(
names = {"-p", "--password"},
description = "Password."
)
String password;
Set<String> domains;
@Override
protected void initEppToolCommand() throws Exception {
checkArgument(nameservers.size() <= 13, "There can be at most 13 nameservers.");
String duplicates = Joiner.on(", ").join(findDuplicates(mainParameters));
checkArgument(duplicates.isEmpty(), "Duplicate arguments found: '%s'", duplicates);
domains = ImmutableSet.copyOf(mainParameters);
initMutatingEppToolCommand();
}
}

View file

@ -113,6 +113,7 @@ public final class RegistryTool {
.put("update_application_status", UpdateApplicationStatusCommand.class)
.put("update_claims_notice", UpdateClaimsNoticeCommand.class)
.put("update_cursors", UpdateCursorsCommand.class)
.put("update_domain", UpdateDomainCommand.class)
.put("update_kms_keyring", UpdateKmsKeyringCommand.class)
.put("update_premium_list", UpdatePremiumListCommand.class)
.put("update_registrar", UpdateRegistrarCommand.class)

View file

@ -94,6 +94,9 @@ interface RegistryToolComponent {
void inject(SendEscrowReportToIcannCommand command);
void inject(SetupOteCommand command);
void inject(UpdateCursorsCommand command);
void inject(UpdateDomainCommand command);
void inject(UpdateKmsKeyringCommand command);
void inject(UpdateTldCommand command);
void inject(ValidateEscrowDepositCommand command);

View file

@ -0,0 +1,235 @@
// Copyright 2017 The Nomulus 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 google.registry.model.EppResourceUtils.loadByForeignKey;
import static google.registry.model.ofy.ObjectifyService.ofy;
import static org.joda.time.DateTimeZone.UTC;
import com.beust.jcommander.Parameter;
import com.beust.jcommander.Parameters;
import com.google.common.base.Function;
import com.google.common.base.Predicate;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.ImmutableSortedSet;
import com.google.common.collect.Sets;
import com.google.common.flogger.GoogleLogger;
import com.google.template.soy.data.SoyMapData;
import google.registry.model.domain.DesignatedContact;
import google.registry.model.domain.DomainResource;
import google.registry.model.eppcommon.StatusValue;
import google.registry.tools.soy.DomainUpdateSoyInfo;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.joda.time.DateTime;
/** A command to update a new domain via EPP. */
@Parameters(separators = " =", commandDescription = "Update a new domain via EPP.")
final class UpdateDomainCommand extends CreateOrUpdateDomainCommand {
private static final GoogleLogger logger = GoogleLogger.forEnclosingClass();
@Parameter(names = "--statuses", description = "Comma-separated list of statuses to set.")
private List<String> statuses = new ArrayList<>();
@Parameter(
names = "--add_nameservers",
description =
"Comma-separated list of nameservers to add, up to 13. "
+ "Cannot be set if --nameservers is set."
)
private List<String> addNameservers = new ArrayList<>();
@Parameter(
names = "--add_admins",
description = "Admins to add. Cannot be set if --admins is set."
)
private List<String> addAdmins = new ArrayList<>();
@Parameter(names = "--add_techs", description = "Techs to add. Cannot be set if --techs is set.")
private List<String> addTechs = new ArrayList<>();
@Parameter(
names = "--add_statuses",
description = "Statuses to add. Cannot be set if --statuses is set."
)
private List<String> addStatuses = new ArrayList<>();
@Parameter(
names = "--remove_nameservers",
description =
"Comma-separated list of nameservers to remove, up to 13. "
+ "Cannot be set if --nameservers is set."
)
private List<String> removeNameservers = new ArrayList<>();
@Parameter(
names = "--remove_admins",
description = "Admins to remove. Cannot be set if --admins is set."
)
private List<String> removeAdmins = new ArrayList<>();
@Parameter(
names = "--remove_techs",
description = "Techs to remove. Cannot be set if --techs is set."
)
private List<String> removeTechs = new ArrayList<>();
@Parameter(
names = "--remove_statuses",
description = "Statuses to remove. Cannot be set if --statuses is set."
)
private List<String> removeStatuses = new ArrayList<>();
@Override
protected void initMutatingEppToolCommand() {
if (!nameservers.isEmpty()) {
checkArgument(
addNameservers.isEmpty() && removeNameservers.isEmpty(),
"If you provide the nameservers flag, "
+ "you cannot use the add_nameservers and remove_nameservers flags.");
} else {
checkArgument(addNameservers.size() <= 13, "You can add at most 13 nameservers.");
}
if (!admins.isEmpty()) {
checkArgument(
addAdmins.isEmpty() && removeAdmins.isEmpty(),
"If you provide the admins flag, you cannot use the add_admins and remove_admins flags.");
}
if (!techs.isEmpty()) {
checkArgument(
addTechs.isEmpty() && removeTechs.isEmpty(),
"If you provide the techs flag, you cannot use the add_techs and remove_techs flags.");
}
if (!statuses.isEmpty()) {
checkArgument(
addStatuses.isEmpty() && removeStatuses.isEmpty(),
"If you provide the statuses flag, "
+ "you cannot use the add_statuses and remove_statuses flags.");
}
for (String domain : domains) {
if (!nameservers.isEmpty() || !admins.isEmpty() || !techs.isEmpty() || !statuses.isEmpty()) {
DateTime now = DateTime.now(UTC);
DomainResource domainResource = loadByForeignKey(DomainResource.class, domain, now);
checkArgument(domainResource != null, "Domain '%s' does not exist", domain);
if (!nameservers.isEmpty()) {
ImmutableSortedSet<String> existingNameservers =
domainResource.loadNameserverFullyQualifiedHostNames();
populateAddRemoveLists(
ImmutableSet.copyOf(nameservers),
existingNameservers,
addNameservers,
removeNameservers);
checkArgument(
existingNameservers.size() + addNameservers.size() - removeNameservers.size() <= 13,
"The resulting nameservers count for domain %s would be more than 13",
domain);
}
if (!admins.isEmpty() || !techs.isEmpty()) {
ImmutableSet<String> existingAdmins =
getContactsOfType(domainResource, DesignatedContact.Type.ADMIN);
ImmutableSet<String> existingTechs =
getContactsOfType(domainResource, DesignatedContact.Type.TECH);
if (!admins.isEmpty()) {
populateAddRemoveLists(
ImmutableSet.copyOf(admins), existingAdmins, addAdmins, removeAdmins);
}
if (!techs.isEmpty()) {
populateAddRemoveLists(
ImmutableSet.copyOf(techs), existingTechs, addTechs, removeTechs);
}
}
if (!statuses.isEmpty()) {
Set<String> currentStatusValues = new HashSet<>();
for (StatusValue statusValue : domainResource.getStatusValues()) {
currentStatusValues.add(statusValue.getXmlName());
}
populateAddRemoveLists(
ImmutableSet.copyOf(statuses), currentStatusValues, addStatuses, removeStatuses);
}
}
boolean add =
!addNameservers.isEmpty()
|| !addAdmins.isEmpty()
|| !addTechs.isEmpty()
|| !addStatuses.isEmpty();
boolean remove =
!removeNameservers.isEmpty()
|| !removeAdmins.isEmpty()
|| !removeTechs.isEmpty()
|| !removeStatuses.isEmpty();
boolean change = registrant != null || password != null;
if (!add && !remove && !change) {
logger.atInfo().log("No changes need to be made to domain %s", domain);
continue;
}
setSoyTemplate(DomainUpdateSoyInfo.getInstance(), DomainUpdateSoyInfo.DOMAINUPDATE);
addSoyRecord(
clientId,
new SoyMapData(
"domain", domain,
"add", add,
"addNameservers", addNameservers,
"addAdmins", addAdmins,
"addTechs", addTechs,
"addStatuses", addStatuses,
"remove", remove,
"removeNameservers", removeNameservers,
"removeAdmins", removeAdmins,
"removeTechs", removeTechs,
"removeStatuses", removeStatuses,
"change", change,
"registrant", registrant,
"password", password));
}
}
protected void populateAddRemoveLists(
Set<String> targetSet, Set<String> oldSet, List<String> addList, List<String> removeList) {
addList.addAll(Sets.difference(targetSet, oldSet));
removeList.addAll(Sets.difference(oldSet, targetSet));
}
ImmutableSet<String> getContactsOfType(
DomainResource domainResource, final DesignatedContact.Type contactType) {
return FluentIterable.from(domainResource.getContacts())
.filter(
new Predicate<DesignatedContact>() {
@Override
public boolean apply(DesignatedContact contact) {
return contact.getType().equals(contactType);
}
})
.transform(
new Function<DesignatedContact, String>() {
@Override
public String apply(DesignatedContact contact) {
return ofy().load().key(contact.getContactKey()).now().getContactId();
}
})
.toSet();
}
}

View file

@ -19,10 +19,10 @@
{template .domaincreate}
{@param domain: string}
{@param? period: string}
{@param? ns: list<string>}
{@param nameservers: list<string>}
{@param registrant: string}
{@param admin: string}
{@param tech: string}
{@param admins: list<string>}
{@param techs: list<string>}
{@param password: string}
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
@ -34,16 +34,20 @@
{if $period}
<domain:period unit="y">{$period}</domain:period>
{/if}
{if $ns}
{if length($nameservers) > 0}
<domain:ns>
{foreach $s in $ns}
{foreach $s in $nameservers}
<domain:hostObj>{$s}</domain:hostObj>
{/foreach}
</domain:ns>
{/if}
<domain:registrant>{$registrant}</domain:registrant>
<domain:contact type="admin">{$admin}</domain:contact>
<domain:contact type="tech">{$tech}</domain:contact>
{foreach $admin in $admins}
<domain:contact type="admin">{$admin}</domain:contact>
{/foreach}
{foreach $tech in $techs}
<domain:contact type="tech">{$tech}</domain:contact>
{/foreach}
<domain:authInfo>
<domain:pw>{$password}</domain:pw>
</domain:authInfo>

View file

@ -0,0 +1,98 @@
// Copyright 2017 The Nomulus 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.
{namespace domain.registry.tools autoescape="strict"}
/**
* Update domain
*/
{template .domainupdate}
{@param domain: string}
{@param add: bool}
{@param addNameservers: list<string>}
{@param addAdmins: list<string>}
{@param addTechs: list<string>}
{@param addStatuses: list<string>}
{@param remove: bool}
{@param removeNameservers: list<string>}
{@param removeAdmins: list<string>}
{@param removeTechs: list<string>}
{@param removeStatuses: list<string>}
{@param change: bool}
{@param? registrant: string}
{@param? password: string}
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<epp xmlns="urn:ietf:params:xml:ns:epp-1.0">
<command>
<update>
<domain:update xmlns:domain="urn:ietf:params:xml:ns:domain-1.0">
<domain:name>{$domain}</domain:name>
{if $add}
<domain:add>
{if length($addNameservers) > 0}
<domain:ns>
{foreach $s in $addNameservers}
<domain:hostObj>{$s}</domain:hostObj>
{/foreach}
</domain:ns>
{/if}
{foreach $admin in $addAdmins}
<domain:contact type="admin">{$admin}</domain:contact>
{/foreach}
{foreach $tech in $addTechs}
<domain:contact type="tech">{$tech}</domain:contact>
{/foreach}
{foreach $status in $addStatuses}
<domain:status s="{$status}"/>
{/foreach}
</domain:add>
{/if}
{if $remove}
<domain:rem>
{if length($removeNameservers) > 0}
<domain:ns>
{foreach $s in $removeNameservers}
<domain:hostObj>{$s}</domain:hostObj>
{/foreach}
</domain:ns>
{/if}
{foreach $admin in $removeAdmins}
<domain:contact type="admin">{$admin}</domain:contact>
{/foreach}
{foreach $tech in $removeTechs}
<domain:contact type="tech">{$tech}</domain:contact>
{/foreach}
{foreach $status in $removeStatuses}
<domain:status s="{$status}"/>
{/foreach}
</domain:rem>
{/if}
{if $change}
<domain:chg>
{if $registrant}
<domain:registrant>{$registrant}</domain:registrant>
{/if}
{if $password}
<domain:authInfo>
<domain:pw>{$password}</domain:pw>
</domain:authInfo>
{/if}
</domain:chg>
{/if}
</domain:update>
</update>
<clTRID>RegistryTool</clTRID>
</command>
</epp>
{/template}