Add some updates to UpdateReservedListCommand to facilitate internal config presubmits and syncing (#2292)

* Add some updates to UpdateReservedListCommand to facilitate internal config presubmits and syncing

Added a dry-run tag for presubmit tests

Added early exit behavior when there are no new changes to the list

Added a new --build_environment tag to be used to indicate command runs from build tools. This tag was also added to UpdatePremiumListCommand. Once this new tag is deployed, and break glass behavior is added, these commands will be modified to prevent runs on the command line in the production environment unless the --build_environment or --break_glass flag is used.

* Fix capitalization

* Added in commented out production environment check for buildEnv flag
This commit is contained in:
sarahcaseybot 2024-01-23 17:32:33 -05:00 committed by GitHub
parent ea96ed300f
commit 818944317f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 125 additions and 1 deletions

View file

@ -35,11 +35,24 @@ class UpdatePremiumListCommand extends CreateOrUpdatePremiumListCommand {
description = "Does not execute the entity mutation")
boolean dryRun;
// indicates if there is a new change made by this command
@Parameter(
names = {"--build_environment"},
description =
"DO NOT USE THIS FLAG ON THE COMMAND LINE! This flag indicates the command is being run"
+ " by the build environment tools. This flag should never be used by a human user"
+ " from the command line.")
boolean buildEnv;
// Indicates if there is a new change made by this command
private boolean newChange = false;
@Override
protected String prompt() throws Exception {
// TODO(sarahbot): uncomment once go/r3pr/2292 is deployed
// checkArgument(
// !RegistryToolEnvironment.get().equals(RegistryToolEnvironment.PRODUCTION) || buildEnv,
// "The --build_environment flag must be used when running update_premium_list in
// production");
name = Strings.isNullOrEmpty(name) ? convertFilePathToName(inputFile) : name;
PremiumList existingList =
PremiumListDao.getLatestRevision(name)

View file

@ -18,6 +18,7 @@ import static google.registry.util.DiffUtils.prettyPrintEntityDeepDiff;
import static google.registry.util.ListNamingUtils.convertFilePathToName;
import static java.nio.charset.StandardCharsets.UTF_8;
import com.beust.jcommander.Parameter;
import com.beust.jcommander.Parameters;
import com.google.common.base.Strings;
import google.registry.model.tld.label.ReservedList;
@ -28,8 +29,29 @@ import java.util.List;
@Parameters(separators = " =", commandDescription = "Update a ReservedList.")
final class UpdateReservedListCommand extends CreateOrUpdateReservedListCommand {
@Parameter(
names = {"-d", "--dry_run"},
description = "Does not execute the entity mutation")
boolean dryRun;
@Parameter(
names = {"--build_environment"},
description =
"DO NOT USE THIS FLAG ON THE COMMAND LINE! This flag indicates the command is being run"
+ " by the build environment tools. This flag should never be used by a human user"
+ " from the command line.")
boolean buildEnv;
// indicates if there is a new change made by this command
private boolean newChange = true;
@Override
protected String prompt() throws Exception {
// TODO(sarahbot): uncomment once go/r3pr/2292 is deployed
// checkArgument(
// !RegistryToolEnvironment.get().equals(RegistryToolEnvironment.PRODUCTION) || buildEnv,
// "The --build_environment flag must be used when running update_reserved_list in"
// + " production");
name = Strings.isNullOrEmpty(name) ? convertFilePathToName(input) : name;
ReservedList existingReservedList =
ReservedList.get(name)
@ -54,6 +76,7 @@ final class UpdateReservedListCommand extends CreateOrUpdateReservedListCommand
.getReservedListEntries()
.equals(reservedList.getReservedListEntries());
if (!shouldPublishChanged && !reservedListEntriesChanged) {
newChange = false;
return "No entity changes to apply.";
}
String result = String.format("Update reserved list for %s?\n", name);
@ -70,4 +93,9 @@ final class UpdateReservedListCommand extends CreateOrUpdateReservedListCommand
}
return result;
}
@Override
protected boolean dontRunCommand() {
return dryRun || !newChange;
}
}

View file

@ -205,4 +205,40 @@ class UpdatePremiumListCommandTest<C extends UpdatePremiumListCommand>
.comparingElementsUsing(immutableObjectCorrespondence("revisionId"))
.containsExactly(PremiumEntry.create(0L, new BigDecimal("9090.00"), "doge"));
}
// TODO(sarahbot): uncomment once go/r3pr/2292 is deployed
// @Test
// void testFailure_runCommandOnProduction_noFlag() throws Exception {
// File tmpFile = tmpDir.resolve(String.format("%s.txt", TLD_TEST)).toFile();
// String newPremiumListData = "eth,USD 9999";
// Files.asCharSink(tmpFile, UTF_8).write(newPremiumListData);
// IllegalArgumentException thrown =
// assertThrows(
// IllegalArgumentException.class,
// () ->
// runCommandInEnvironment(
// RegistryToolEnvironment.PRODUCTION,
// "--name=" + TLD_TEST,
// "--input=" + Paths.get(tmpFile.getPath())));
// assertThat(thrown.getMessage())
// .isEqualTo(
// "The --build_environment flag must be used when running update_premium_list in"
// + " production");
// }
//
// @Test
// void testSuccess_runCommandOnProduction_buildEnvFlag() throws Exception {
// File tmpFile = tmpDir.resolve(String.format("%s.txt", TLD_TEST)).toFile();
// String newPremiumListData = "eth,USD 9999";
// Files.asCharSink(tmpFile, UTF_8).write(newPremiumListData);
// runCommandInEnvironment(
// RegistryToolEnvironment.PRODUCTION,
// "--name=" + TLD_TEST,
// "--input=" + Paths.get(tmpFile.getPath()),
// "--build_environment",
// "-f");
// assertThat(PremiumListDao.loadAllPremiumEntries(TLD_TEST))
// .comparingElementsUsing(immutableObjectCorrespondence("revisionId"))
// .containsExactly(PremiumEntry.create(0L, new BigDecimal("9999.00"), "eth"));
// }
}

View file

@ -142,4 +142,51 @@ class UpdateReservedListCommandTest
assertThat(command.prompt()).contains("baddies: null -> baddies,FULLY_BLOCKED");
assertThat(command.prompt()).contains("ford: null -> ford,FULLY_BLOCKED # random comment");
}
@Test
void testSuccess_dryRun() throws Exception {
runCommandForced("--input=" + reservedTermsPath, "--dry_run");
assertThat(command.prompt()).contains("Update reserved list for xn--q9jyb4c_common-reserved?");
assertThat(ReservedList.get("xn--q9jyb4c_common-reserved")).isPresent();
ReservedList reservedList = ReservedList.get("xn--q9jyb4c_common-reserved").get();
assertThat(reservedList.getReservedListEntries()).hasSize(1);
assertThat(reservedList.getReservationInList("helicopter")).hasValue(FULLY_BLOCKED);
}
// TODO(sarahbot): uncomment once go/r3pr/2292 is deployed
// @Test
// void testFailure_runCommandOnProduction_noFlag() throws Exception {
// IllegalArgumentException thrown =
// assertThrows(
// IllegalArgumentException.class,
// () ->
// runCommandInEnvironment(
// RegistryToolEnvironment.PRODUCTION,
// "--name=xn--q9jyb4c_common-reserved",
// "--input=" + reservedTermsPath));
// assertThat(thrown.getMessage())
// .isEqualTo(
// "The --build_environment flag must be used when running update_reserved_list in"
// + " production");
// }
//
// @Test
// void testSuccess_runCommandOnProduction_buildEnvFlag() throws Exception {
// runCommandInEnvironment(
// RegistryToolEnvironment.PRODUCTION,
// "--name=xn--q9jyb4c_common-reserved",
// "--input=" + reservedTermsPath,
// "--build_environment",
// "-f");
// assertThat(ReservedList.get("xn--q9jyb4c_common-reserved")).isPresent();
// ReservedList reservedList = ReservedList.get("xn--q9jyb4c_common-reserved").get();
// assertThat(reservedList.getReservedListEntries()).hasSize(2);
// assertThat(reservedList.getReservationInList("baddies")).hasValue(FULLY_BLOCKED);
// assertThat(reservedList.getReservationInList("ford")).hasValue(FULLY_BLOCKED);
// assertThat(reservedList.getReservationInList("helicopter")).isEmpty();
// assertInStdout("Update reserved list for xn--q9jyb4c_common-reserved?");
// assertInStdout("helicopter: helicopter,FULLY_BLOCKED -> null");
// assertInStdout("baddies: null -> baddies,FULLY_BLOCKED");
// assertInStdout("ford: null -> ford,FULLY_BLOCKED # random comment");
// }
}