|
| 1 | +// Copyright 2025 The Nomulus Authors. All Rights Reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package google.registry.tools; |
| 16 | + |
| 17 | +import static com.google.common.base.Preconditions.checkArgument; |
| 18 | +import static com.google.common.collect.ImmutableList.toImmutableList; |
| 19 | +import static java.nio.charset.StandardCharsets.UTF_8; |
| 20 | + |
| 21 | +import com.beust.jcommander.Parameter; |
| 22 | +import com.beust.jcommander.Parameters; |
| 23 | +import com.google.common.base.Splitter; |
| 24 | +import com.google.common.collect.ImmutableList; |
| 25 | +import com.google.common.collect.ImmutableMap; |
| 26 | +import com.google.common.collect.Iterables; |
| 27 | +import com.google.common.io.Files; |
| 28 | +import com.google.common.net.MediaType; |
| 29 | +import com.google.gson.Gson; |
| 30 | +import google.registry.batch.BulkDomainTransferAction; |
| 31 | +import google.registry.model.registrar.Registrar; |
| 32 | +import google.registry.util.DomainNameUtils; |
| 33 | +import java.io.File; |
| 34 | +import java.io.IOException; |
| 35 | +import java.util.List; |
| 36 | + |
| 37 | +/** |
| 38 | + * A command to bulk-transfer any number of domains from one registrar to another. |
| 39 | + * |
| 40 | + * <p>This should be used as part of the BTAPPA (Bulk Transfer After a Partial Portfolio |
| 41 | + * Acquisition) process in order to transfer a (possibly large) list of domains from one registrar |
| 42 | + * to another, though it may be used in other situations as well. |
| 43 | + * |
| 44 | + * <p>For a true bulk transfer of domains, one should pass in a file with a list of domains (one per |
| 45 | + * line) but if we need to do an ad-hoc transfer of one domain we can do that as well. |
| 46 | + * |
| 47 | + * <p>For BTAPPA purposes, we expect "requestedByRegistrar" to be true; this may not be the case for |
| 48 | + * other purposes e.g. legal compliance transfers. |
| 49 | + */ |
| 50 | +@Parameters( |
| 51 | + separators = " =", |
| 52 | + commandDescription = "Transfer domain(s) in bulk with immediate effect.") |
| 53 | +public class BulkDomainTransferCommand extends ConfirmingCommand implements CommandWithConnection { |
| 54 | + |
| 55 | + // we don't need any configuration on the Gson because all we need is a list of strings |
| 56 | + private static final Gson GSON = new Gson(); |
| 57 | + private static final int DOMAIN_TRANSFER_BATCH_SIZE = 1000; |
| 58 | + |
| 59 | + @Parameter( |
| 60 | + names = {"--domains"}, |
| 61 | + description = |
| 62 | + "Comma-separated list of domains to transfer, otherwise use --domain_names_file to" |
| 63 | + + " specify a possibly-large list of domains") |
| 64 | + private List<String> domains; |
| 65 | + |
| 66 | + @Parameter( |
| 67 | + names = {"-d", "--domain_names_file"}, |
| 68 | + description = "A file with a list of newline-delimited domain names to create tokens for") |
| 69 | + private String domainNamesFile; |
| 70 | + |
| 71 | + @Parameter( |
| 72 | + names = {"-g", "--gaining_registrar_id"}, |
| 73 | + description = "The ID of the registrar to which domains should be transferred", |
| 74 | + required = true) |
| 75 | + private String gainingRegistrarId; |
| 76 | + |
| 77 | + @Parameter( |
| 78 | + names = {"-l", "--losing_registrar_id"}, |
| 79 | + description = "The ID of the registrar from which domains should be transferred", |
| 80 | + required = true) |
| 81 | + private String losingRegistrarId; |
| 82 | + |
| 83 | + @Parameter( |
| 84 | + names = {"--reason"}, |
| 85 | + description = "Reason to transfer the domains", |
| 86 | + required = true) |
| 87 | + private String reason; |
| 88 | + |
| 89 | + @Parameter( |
| 90 | + names = {"--registrar_request"}, |
| 91 | + description = "Whether the change was requested by a registrar.") |
| 92 | + private boolean requestedByRegistrar = false; |
| 93 | + |
| 94 | + @Parameter( |
| 95 | + names = {"--max_qps"}, |
| 96 | + description = |
| 97 | + "Maximum queries to run per second, otherwise the default (maxQps) will be used") |
| 98 | + private int maxQps; |
| 99 | + |
| 100 | + private ServiceConnection connection; |
| 101 | + |
| 102 | + @Override |
| 103 | + public void setConnection(ServiceConnection connection) { |
| 104 | + this.connection = connection; |
| 105 | + } |
| 106 | + |
| 107 | + @Override |
| 108 | + protected String prompt() throws Exception { |
| 109 | + checkArgument( |
| 110 | + domainNamesFile != null ^ (domains != null && !domains.isEmpty()), |
| 111 | + "Must specify exactly one input method, either --domains or --domain_names_file"); |
| 112 | + return String.format("Attempt to transfer %d domains?", getDomainList().size()); |
| 113 | + } |
| 114 | + |
| 115 | + @Override |
| 116 | + protected String execute() throws Exception { |
| 117 | + checkArgument( |
| 118 | + Registrar.loadByRegistrarIdCached(gainingRegistrarId).isPresent(), |
| 119 | + "Gaining registrar %s doesn't exist", |
| 120 | + gainingRegistrarId); |
| 121 | + checkArgument( |
| 122 | + Registrar.loadByRegistrarIdCached(losingRegistrarId).isPresent(), |
| 123 | + "Losing registrar %s doesn't exist", |
| 124 | + losingRegistrarId); |
| 125 | + |
| 126 | + ImmutableMap.Builder<String, Object> paramsBuilder = new ImmutableMap.Builder<>(); |
| 127 | + paramsBuilder.put("gainingRegistrarId", gainingRegistrarId); |
| 128 | + paramsBuilder.put("losingRegistrarId", losingRegistrarId); |
| 129 | + paramsBuilder.put("requestedByRegistrar", requestedByRegistrar); |
| 130 | + paramsBuilder.put("reason", reason); |
| 131 | + if (maxQps > 0) { |
| 132 | + paramsBuilder.put("maxQps", maxQps); |
| 133 | + } |
| 134 | + ImmutableMap<String, Object> params = paramsBuilder.build(); |
| 135 | + |
| 136 | + for (List<String> batch : Iterables.partition(getDomainList(), DOMAIN_TRANSFER_BATCH_SIZE)) { |
| 137 | + System.out.printf("Sending batch of %d domains\n", batch.size()); |
| 138 | + byte[] domainsList = GSON.toJson(batch).getBytes(UTF_8); |
| 139 | + System.out.println( |
| 140 | + connection.sendPostRequest( |
| 141 | + BulkDomainTransferAction.PATH, params, MediaType.PLAIN_TEXT_UTF_8, domainsList)); |
| 142 | + } |
| 143 | + return ""; |
| 144 | + } |
| 145 | + |
| 146 | + private ImmutableList<String> getDomainList() throws IOException { |
| 147 | + return domainNamesFile == null ? ImmutableList.copyOf(domains) : loadDomainsFromFile(); |
| 148 | + } |
| 149 | + |
| 150 | + private ImmutableList<String> loadDomainsFromFile() throws IOException { |
| 151 | + return Splitter.on('\n') |
| 152 | + .omitEmptyStrings() |
| 153 | + .trimResults() |
| 154 | + .splitToStream(Files.asCharSource(new File(domainNamesFile), UTF_8).read()) |
| 155 | + .map(DomainNameUtils::canonicalizeHostname) |
| 156 | + .collect(toImmutableList()); |
| 157 | + } |
| 158 | +} |
0 commit comments