mirror of
https://github.com/google/nomulus.git
synced 2025-05-29 17:00:11 +02:00
Add an unrenew_domain command to nomulus tool
This is used to reduce the expiration time of domain(s) by some number of years (if enough length remains in the registration term to do so). This does not back out the previously saved BillingEvent entities as they may have already been sent out and invoiced, so any related refunds must be handled out of band. In addition to reducing the registration expiration time on the domain itself, this command writes out a new history entry, one-time poll message informing the registrar of this change, auto-renew billing event and poll message, and updates/ends the old auto-renew billing event and poll message. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=224999285
This commit is contained in:
parent
9c706e79fd
commit
f58211402a
21 changed files with 801 additions and 15 deletions
|
@ -520,7 +520,7 @@ public class DomainFlowUtils {
|
|||
* Fills in a builder with the data needed for an autorenew billing event for this domain. This
|
||||
* does not copy over the id of the current autorenew billing event.
|
||||
*/
|
||||
static BillingEvent.Recurring.Builder newAutorenewBillingEvent(DomainResource domain) {
|
||||
public static BillingEvent.Recurring.Builder newAutorenewBillingEvent(DomainResource domain) {
|
||||
return new BillingEvent.Recurring.Builder()
|
||||
.setReason(Reason.RENEW)
|
||||
.setFlags(ImmutableSet.of(Flag.AUTO_RENEW))
|
||||
|
@ -533,7 +533,7 @@ public class DomainFlowUtils {
|
|||
* Fills in a builder with the data needed for an autorenew poll message for this domain. This
|
||||
* does not copy over the id of the current autorenew poll message.
|
||||
*/
|
||||
static PollMessage.Autorenew.Builder newAutorenewPollMessage(DomainResource domain) {
|
||||
public static PollMessage.Autorenew.Builder newAutorenewPollMessage(DomainResource domain) {
|
||||
return new PollMessage.Autorenew.Builder()
|
||||
.setTargetId(domain.getFullyQualifiedDomainName())
|
||||
.setClientId(domain.getCurrentSponsorClientId())
|
||||
|
@ -542,12 +542,14 @@ public class DomainFlowUtils {
|
|||
}
|
||||
|
||||
/**
|
||||
* Re-saves the current autorenew billing event and poll message with a new end time. This may end
|
||||
* up deleting the poll message (if closing the message interval) or recreating it (if opening the
|
||||
* message interval).
|
||||
* Re-saves the current autorenew billing event and poll message with a new end time.
|
||||
*
|
||||
* <p>This may end up deleting the poll message (if closing the message interval) or recreating it
|
||||
* (if opening the message interval). This may cause an autorenew billing event to have an end
|
||||
* time earlier than its event time (i.e. if it's being ended before it was ever triggered).
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
static void updateAutorenewRecurrenceEndTime(DomainResource domain, DateTime newEndTime) {
|
||||
public static void updateAutorenewRecurrenceEndTime(DomainResource domain, DateTime newEndTime) {
|
||||
Optional<PollMessage.Autorenew> autorenewPollMessage =
|
||||
Optional.ofNullable(ofy().load().key(domain.getAutorenewPollMessage()).now());
|
||||
|
||||
|
|
|
@ -226,7 +226,7 @@ public final class DomainRenewFlow implements TransactionalFlow {
|
|||
.setType(HistoryEntry.Type.DOMAIN_RENEW)
|
||||
.setPeriod(period)
|
||||
.setModificationTime(now)
|
||||
.setParent(Key.create(existingDomain))
|
||||
.setParent(existingDomain)
|
||||
.setDomainTransactionRecords(
|
||||
ImmutableSet.of(
|
||||
DomainTransactionRecord.create(
|
||||
|
|
|
@ -64,6 +64,7 @@ public final class GtechTool {
|
|||
"setup_ote",
|
||||
"uniform_rapid_suspension",
|
||||
"unlock_domain",
|
||||
"unrenew_domain",
|
||||
"update_domain",
|
||||
"update_registrar",
|
||||
"update_sandbox_tld",
|
||||
|
|
|
@ -108,6 +108,7 @@ public final class RegistryTool {
|
|||
.put("setup_ote", SetupOteCommand.class)
|
||||
.put("uniform_rapid_suspension", UniformRapidSuspensionCommand.class)
|
||||
.put("unlock_domain", UnlockDomainCommand.class)
|
||||
.put("unrenew_domain", UnrenewDomainCommand.class)
|
||||
.put("update_application_status", UpdateApplicationStatusCommand.class)
|
||||
.put("update_claims_notice", UpdateClaimsNoticeCommand.class)
|
||||
.put("update_cursors", UpdateCursorsCommand.class)
|
||||
|
|
|
@ -101,6 +101,7 @@ interface RegistryToolComponent {
|
|||
void inject(SetNumInstancesCommand command);
|
||||
void inject(SetupOteCommand command);
|
||||
void inject(UnlockDomainCommand command);
|
||||
void inject(UnrenewDomainCommand command);
|
||||
void inject(UpdateCursorsCommand command);
|
||||
void inject(UpdateDomainCommand command);
|
||||
void inject(UpdateKmsKeyringCommand command);
|
||||
|
|
|
@ -37,7 +37,7 @@ import org.joda.time.format.DateTimeFormatter;
|
|||
final class RenewDomainCommand extends MutatingEppToolCommand {
|
||||
|
||||
@Parameter(
|
||||
names = "--period",
|
||||
names = {"-p", "--period"},
|
||||
description = "Number of years to renew the registration for (defaults to 1).")
|
||||
private int period = 1;
|
||||
|
||||
|
|
225
java/google/registry/tools/UnrenewDomainCommand.java
Normal file
225
java/google/registry/tools/UnrenewDomainCommand.java
Normal file
|
@ -0,0 +1,225 @@
|
|||
// Copyright 2018 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 com.google.common.base.Preconditions.checkState;
|
||||
import static google.registry.flows.domain.DomainFlowUtils.newAutorenewBillingEvent;
|
||||
import static google.registry.flows.domain.DomainFlowUtils.newAutorenewPollMessage;
|
||||
import static google.registry.flows.domain.DomainFlowUtils.updateAutorenewRecurrenceEndTime;
|
||||
import static google.registry.model.EppResourceUtils.loadByForeignKey;
|
||||
import static google.registry.model.ofy.ObjectifyService.ofy;
|
||||
import static google.registry.util.DateTimeUtils.isBeforeOrAt;
|
||||
import static google.registry.util.DateTimeUtils.leapSafeSubtractYears;
|
||||
|
||||
import com.beust.jcommander.Parameter;
|
||||
import com.beust.jcommander.Parameters;
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import com.google.common.collect.ImmutableMultimap;
|
||||
import com.google.common.collect.ImmutableSet;
|
||||
import com.google.common.collect.Sets;
|
||||
import com.googlecode.objectify.Key;
|
||||
import google.registry.model.billing.BillingEvent;
|
||||
import google.registry.model.domain.DomainResource;
|
||||
import google.registry.model.domain.Period;
|
||||
import google.registry.model.domain.Period.Unit;
|
||||
import google.registry.model.eppcommon.StatusValue;
|
||||
import google.registry.model.index.ForeignKeyIndex.ForeignKeyDomainIndex;
|
||||
import google.registry.model.poll.PollMessage;
|
||||
import google.registry.model.reporting.HistoryEntry;
|
||||
import google.registry.model.reporting.HistoryEntry.Type;
|
||||
import google.registry.util.Clock;
|
||||
import google.registry.util.NonFinalForTesting;
|
||||
import java.util.List;
|
||||
import javax.inject.Inject;
|
||||
import org.joda.time.DateTime;
|
||||
|
||||
/**
|
||||
* Command to unrenew a domain.
|
||||
*
|
||||
* <p>This removes years off a domain's registration period. Note that the expiration time cannot be
|
||||
* set to prior than the present. Reversal of the charges for these years (if desired) must happen
|
||||
* out of band, as they may already have been billed out and thus cannot and won't be reversed in
|
||||
* Datastore.
|
||||
*/
|
||||
@Parameters(separators = " =", commandDescription = "Unrenew a domain.")
|
||||
@NonFinalForTesting
|
||||
class UnrenewDomainCommand extends ConfirmingCommand implements CommandWithRemoteApi {
|
||||
|
||||
@Parameter(
|
||||
names = {"-p", "--period"},
|
||||
description = "Number of years to unrenew the registration for (defaults to 1).")
|
||||
int period = 1;
|
||||
|
||||
@Parameter(description = "Names of the domains to unrenew.", required = true)
|
||||
List<String> mainParameters;
|
||||
|
||||
@Inject Clock clock;
|
||||
|
||||
private static final ImmutableSet<StatusValue> DISALLOWED_STATUSES =
|
||||
ImmutableSet.of(
|
||||
StatusValue.PENDING_TRANSFER,
|
||||
StatusValue.SERVER_RENEW_PROHIBITED,
|
||||
StatusValue.SERVER_UPDATE_PROHIBITED);
|
||||
|
||||
@Override
|
||||
protected void init() {
|
||||
checkArgument(period >= 1 && period <= 9, "Period must be in the range 1-9");
|
||||
DateTime now = clock.nowUtc();
|
||||
ImmutableSet.Builder<String> domainsNonexistentBuilder = new ImmutableSet.Builder<>();
|
||||
ImmutableSet.Builder<String> domainsDeletingBuilder = new ImmutableSet.Builder<>();
|
||||
ImmutableMultimap.Builder<String, StatusValue> domainsWithDisallowedStatusesBuilder =
|
||||
new ImmutableMultimap.Builder<>();
|
||||
ImmutableMap.Builder<String, DateTime> domainsExpiringTooSoonBuilder =
|
||||
new ImmutableMap.Builder<>();
|
||||
|
||||
for (String domainName : mainParameters) {
|
||||
if (ofy().load().type(ForeignKeyDomainIndex.class).id(domainName).now() == null) {
|
||||
domainsNonexistentBuilder.add(domainName);
|
||||
continue;
|
||||
}
|
||||
DomainResource domain = loadByForeignKey(DomainResource.class, domainName, now);
|
||||
if (domain == null || domain.getStatusValues().contains(StatusValue.PENDING_DELETE)) {
|
||||
domainsDeletingBuilder.add(domainName);
|
||||
continue;
|
||||
}
|
||||
domainsWithDisallowedStatusesBuilder.putAll(
|
||||
domainName, Sets.intersection(domain.getStatusValues(), DISALLOWED_STATUSES));
|
||||
if (isBeforeOrAt(
|
||||
leapSafeSubtractYears(domain.getRegistrationExpirationTime(), period), now)) {
|
||||
domainsExpiringTooSoonBuilder.put(domainName, domain.getRegistrationExpirationTime());
|
||||
}
|
||||
}
|
||||
|
||||
ImmutableSet<String> domainsNonexistent = domainsNonexistentBuilder.build();
|
||||
ImmutableSet<String> domainsDeleting = domainsDeletingBuilder.build();
|
||||
ImmutableMultimap<String, StatusValue> domainsWithDisallowedStatuses =
|
||||
domainsWithDisallowedStatusesBuilder.build();
|
||||
ImmutableMap<String, DateTime> domainsExpiringTooSoon = domainsExpiringTooSoonBuilder.build();
|
||||
|
||||
boolean foundInvalidDomains =
|
||||
!(domainsNonexistent.isEmpty()
|
||||
&& domainsDeleting.isEmpty()
|
||||
&& domainsWithDisallowedStatuses.isEmpty()
|
||||
&& domainsExpiringTooSoon.isEmpty());
|
||||
if (foundInvalidDomains) {
|
||||
System.err.print("Found domains that cannot be unrenewed for the following reasons:\n\n");
|
||||
}
|
||||
if (!domainsNonexistent.isEmpty()) {
|
||||
System.err.printf("Domains that don't exist: %s\n\n", domainsNonexistent);
|
||||
}
|
||||
if (!domainsDeleting.isEmpty()) {
|
||||
System.err.printf("Domains that are deleted or pending delete: %s\n\n", domainsDeleting);
|
||||
}
|
||||
if (!domainsWithDisallowedStatuses.isEmpty()) {
|
||||
System.err.printf("Domains with disallowed statuses: %s\n\n", domainsWithDisallowedStatuses);
|
||||
}
|
||||
if (!domainsExpiringTooSoon.isEmpty()) {
|
||||
System.err.printf("Domains expiring too soon: %s\n\n", domainsExpiringTooSoon);
|
||||
}
|
||||
checkArgument(!foundInvalidDomains, "Aborting because some domains cannot be unrewed");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String prompt() {
|
||||
return String.format("Unrenew these domain(s) for %d years?", period);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String execute() {
|
||||
for (String domainName : mainParameters) {
|
||||
ofy().transact(() -> unrenewDomain(domainName));
|
||||
System.out.printf("Unrenewed %s\n", domainName);
|
||||
}
|
||||
return "Successfully unrenewed all domains.";
|
||||
}
|
||||
|
||||
private void unrenewDomain(String domainName) {
|
||||
ofy().assertInTransaction();
|
||||
DateTime now = ofy().getTransactionTime();
|
||||
DomainResource domain = loadByForeignKey(DomainResource.class, domainName, now);
|
||||
// Transactional sanity checks on the off chance that something changed between init() running
|
||||
// and here.
|
||||
checkState(
|
||||
domain != null && !domain.getStatusValues().contains(StatusValue.PENDING_DELETE),
|
||||
"Domain %s was deleted or is pending deletion",
|
||||
domainName);
|
||||
checkState(
|
||||
Sets.intersection(domain.getStatusValues(), DISALLOWED_STATUSES).isEmpty(),
|
||||
"Domain %s has prohibited status values",
|
||||
domainName);
|
||||
checkState(
|
||||
leapSafeSubtractYears(domain.getRegistrationExpirationTime(), period).isAfter(now),
|
||||
"Domain %s expires too soon",
|
||||
domainName);
|
||||
|
||||
DateTime newExpirationTime =
|
||||
leapSafeSubtractYears(domain.getRegistrationExpirationTime(), period);
|
||||
HistoryEntry historyEntry =
|
||||
new HistoryEntry.Builder()
|
||||
.setParent(domain)
|
||||
.setModificationTime(now)
|
||||
.setBySuperuser(true)
|
||||
.setType(Type.SYNTHETIC)
|
||||
.setClientId(domain.getCurrentSponsorClientId())
|
||||
.setReason("Domain unrenewal")
|
||||
.setPeriod(Period.create(period, Unit.YEARS))
|
||||
.setRequestedByRegistrar(false)
|
||||
.build();
|
||||
PollMessage oneTimePollMessage =
|
||||
new PollMessage.OneTime.Builder()
|
||||
.setClientId(domain.getCurrentSponsorClientId())
|
||||
.setMsg(
|
||||
String.format(
|
||||
"Domain %s was unrenewed by %d years; now expires at %s.",
|
||||
domainName, period, newExpirationTime))
|
||||
.setParent(historyEntry)
|
||||
.setEventTime(now)
|
||||
.build();
|
||||
// Create a new autorenew billing event and poll message starting at the new expiration time.
|
||||
BillingEvent.Recurring newAutorenewEvent =
|
||||
newAutorenewBillingEvent(domain)
|
||||
.setEventTime(newExpirationTime)
|
||||
.setParent(historyEntry)
|
||||
.build();
|
||||
PollMessage.Autorenew newAutorenewPollMessage =
|
||||
newAutorenewPollMessage(domain)
|
||||
.setEventTime(newExpirationTime)
|
||||
.setParent(historyEntry)
|
||||
.build();
|
||||
// End the old autorenew billing event and poll message now.
|
||||
updateAutorenewRecurrenceEndTime(domain, now);
|
||||
DomainResource newDomain =
|
||||
domain
|
||||
.asBuilder()
|
||||
.setRegistrationExpirationTime(newExpirationTime)
|
||||
.setLastEppUpdateTime(now)
|
||||
.setLastEppUpdateClientId(domain.getCurrentSponsorClientId())
|
||||
.setAutorenewBillingEvent(Key.create(newAutorenewEvent))
|
||||
.setAutorenewPollMessage(Key.create(newAutorenewPollMessage))
|
||||
.build();
|
||||
// In order to do it'll need to write out a new HistoryEntry (likely of type SYNTHETIC), a new
|
||||
// autorenew billing event and poll message, and a new one time poll message at the present time
|
||||
// informing the registrar of this out-of-band change.
|
||||
ofy()
|
||||
.save()
|
||||
.entities(
|
||||
newDomain,
|
||||
historyEntry,
|
||||
oneTimePollMessage,
|
||||
newAutorenewEvent,
|
||||
newAutorenewPollMessage);
|
||||
}
|
||||
}
|
|
@ -77,4 +77,13 @@ public class DateTimeUtils {
|
|||
checkArgument(years >= 0);
|
||||
return years == 0 ? now : now.plusYears(1).plusYears(years - 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Subtracts years from a date, in the {@code Duration} sense of semantic years. Use this instead
|
||||
* of {@link DateTime#minusYears} to ensure that we never end up on February 29.
|
||||
*/
|
||||
public static DateTime leapSafeSubtractYears(DateTime now, int years) {
|
||||
checkArgument(years >= 0);
|
||||
return years == 0 ? now : now.minusYears(1).minusYears(years - 1);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue