Modify DeleteAllocationTokensCommand to have the same input structure as UpdateATC

It's dangerous to have a blank prefix delete all tokens and this allows for some code unification.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=250493453
This commit is contained in:
gbrodman 2019-05-29 08:13:01 -07:00 committed by jianglai
parent 44ccd45439
commit 6a272bc8c6
5 changed files with 124 additions and 89 deletions

View file

@ -14,8 +14,6 @@
package google.registry.tools;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.collect.ImmutableList.toImmutableList;
import static com.google.common.collect.ImmutableMap.toImmutableMap;
import static com.google.common.collect.ImmutableSet.toImmutableSet;
import static com.google.common.collect.Iterables.partition;
@ -28,7 +26,6 @@ import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.ImmutableSortedMap;
import com.googlecode.objectify.Key;
import google.registry.model.domain.token.AllocationToken;
import google.registry.model.domain.token.AllocationToken.TokenStatus;
import google.registry.tools.params.TransitionListParameter.TokenStatusTransitions;
@ -42,49 +39,30 @@ import org.joda.time.DateTime;
@Parameters(
separators = " =",
commandDescription =
"Updates AllocationTokens with the given prefix, modifying some subset of their allowed"
+ " client IDs, allowed TLDs, discount fraction, or status transitions")
public final class UpdateAllocationTokensCommand extends ConfirmingCommand
implements CommandWithRemoteApi {
@Parameter(
names = {"-p", "--prefix"},
description =
"Update all allocation tokens with this prefix; otherwise use '--tokens' to specify"
+ " exact tokens(s) to update")
private String prefix;
@Parameter(
names = {"--tokens"},
description =
"Comma-separated list of tokens to update; otherwise use '--prefix' to update all tokens"
+ " with a given prefix")
private List<String> tokens;
@Parameter(
names = {"--dry_run"},
description = "Do not actually update the tokens; defaults to false")
private boolean dryRun;
"Updates AllocationTokens with the given prefix (or specified tokens), modifying some "
+ "subset of their allowed client IDs, allowed TLDs, discount fraction, or status "
+ "transitions")
final class UpdateAllocationTokensCommand extends UpdateOrDeleteAllocationTokensCommand {
@Parameter(
names = {"--allowed_client_ids"},
description =
"Comma-separated list of allowed client IDs. Use the empty string to clear the"
+ " existing list.")
"Comma-separated list of allowed client IDs. Use the empty string to clear the "
+ "existing list.")
private List<String> allowedClientIds;
@Parameter(
names = {"--allowed_tlds"},
description =
"Comma-separated list of allowed TLDs. Use the empty string to clear the"
+ " existing list.")
"Comma-separated list of allowed TLDs. Use the empty string to clear the "
+ "existing list.")
private List<String> allowedTlds;
@Parameter(
names = {"--discount_fraction"},
description =
"A discount off the base price for the first year between 0.0 and 1.0. Default is 0.0,"
+ " i.e. no discount.")
"A discount off the base price for the first year between 0.0 and 1.0. Default is 0.0, "
+ "i.e. no discount.")
private Double discountFraction;
@Parameter(
@ -92,8 +70,8 @@ public final class UpdateAllocationTokensCommand extends ConfirmingCommand
converter = TokenStatusTransitions.class,
validateWith = TokenStatusTransitions.class,
description =
"Comma-delimited list of token status transitions effective on specific dates, of the"
+ " form <time>=<status>[,<time>=<status>]* where each status represents the status.")
"Comma-delimited list of token status transitions effective on specific dates, of the "
+ "form <time>=<status>[,<time>=<status>]* where each status represents the status.")
private ImmutableSortedMap<DateTime, TokenStatus> tokenStatusTransitions;
private static final int BATCH_SIZE = 20;
@ -112,9 +90,8 @@ public final class UpdateAllocationTokensCommand extends ConfirmingCommand
allowedTlds = ImmutableList.of();
}
ImmutableList<Key<AllocationToken>> keysToUpdate = getKeysToUpdate();
tokensToSave =
ofy().load().keys(keysToUpdate).values().stream()
ofy().load().keys(getTokenKeys()).values().stream()
.collect(toImmutableMap(Function.identity(), this::updateToken))
.entrySet()
.stream()
@ -137,20 +114,6 @@ public final class UpdateAllocationTokensCommand extends ConfirmingCommand
return String.format("Updated %d tokens in total.", numUpdated);
}
private ImmutableList<Key<AllocationToken>> getKeysToUpdate() {
checkArgument(
tokens == null ^ prefix == null, "Must provide one of --tokens or --prefix, not both");
if (tokens != null) {
return tokens.stream()
.map(token -> Key.create(AllocationToken.class, token))
.collect(toImmutableList());
} else {
return ofy().load().type(AllocationToken.class).keys().list().stream()
.filter(key -> key.getName().startsWith(prefix))
.collect(toImmutableList());
}
}
private AllocationToken updateToken(AllocationToken original) {
AllocationToken.Builder builder = original.asBuilder();
Optional.ofNullable(allowedClientIds)