From af99f64b888ee172d2518118cf6b6728d1486514 Mon Sep 17 00:00:00 2001 From: guyben Date: Mon, 16 Oct 2017 13:32:02 -0700 Subject: [PATCH] Convert CreateCdnsTld into a ConfirmingCommand This will result is a prompt like Creating TLD with: projectId= domain-registry-alpha description= some description dnsName= mytld. name= mytld. nameServerSet= cloud-dns-registry-test Perform this command? (y/N): before actually performing the command, and adds a --force flag to bypass the prompt. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=172369944 --- java/google/registry/tools/CreateCdnsTld.java | 30 +++++++++++++++---- .../registry/tools/CreateCdnsTldTest.java | 4 +-- 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/java/google/registry/tools/CreateCdnsTld.java b/java/google/registry/tools/CreateCdnsTld.java index c495ab67a..a7f85d818 100644 --- a/java/google/registry/tools/CreateCdnsTld.java +++ b/java/google/registry/tools/CreateCdnsTld.java @@ -28,11 +28,12 @@ import google.registry.config.RegistryConfig.Config; import java.io.IOException; import java.security.GeneralSecurityException; import java.util.Arrays; +import java.util.stream.Collectors; import javax.annotation.Nullable; import javax.inject.Inject; @Parameters(separators = " =", commandDescription = "Create a Managed Zone for a TLD in Cloud DNS.") -class CreateCdnsTld implements Command { +class CreateCdnsTld extends ConfirmingCommand { @Parameter(names = "--description", description = "Description of the new TLD.") String description; @@ -55,21 +56,38 @@ class CreateCdnsTld implements Command { @Config("projectId") String projectId; + private static final String KEY_VALUE_FORMAT = " %s = %s"; + + private ManagedZone requestBody; + @Override - public void run() throws IOException, GeneralSecurityException { - ManagedZone requestBody = new ManagedZone(); + protected void init() throws IOException, GeneralSecurityException { + requestBody = new ManagedZone(); requestBody.setDescription(description); // TODO(b/67413698): allow parameterizing the nameserver set once it's safe to do so. requestBody.setNameServerSet("cloud-dns-registry-test"); requestBody.setDnsName(dnsName); requestBody.setName((name != null) ? name : dnsName); + } + @Override + protected String prompt() { + return String.format( + "Creating TLD with:\n%s\n%s", + String.format(KEY_VALUE_FORMAT, "projectId", projectId), + requestBody + .entrySet() + .stream() + .map(entry -> String.format(KEY_VALUE_FORMAT, entry.getKey(), entry.getValue())) + .collect(Collectors.joining("\n"))); + } + + @Override + public String execute() throws IOException, GeneralSecurityException { Dns dnsService = createDnsService(); Dns.ManagedZones.Create request = dnsService.managedZones().create(projectId, requestBody); - ManagedZone response = request.execute(); - - System.err.println("Created managed zone: " + response); + return String.format("Created managed zone: %s", response); } @VisibleForTesting diff --git a/javatests/google/registry/tools/CreateCdnsTldTest.java b/javatests/google/registry/tools/CreateCdnsTldTest.java index f7d6bfa32..ba57dd436 100644 --- a/javatests/google/registry/tools/CreateCdnsTldTest.java +++ b/javatests/google/registry/tools/CreateCdnsTldTest.java @@ -58,7 +58,7 @@ public class CreateCdnsTldTest extends CommandTestCase { @Test public void testBasicFunctionality() throws Exception { - runCommand("--dns_name=tld.", "--name=tld", "--description=test run"); + runCommand("--dns_name=tld.", "--name=tld", "--description=test run", "--force"); verify(request).execute(); assertThat(projectId.getValue()).isEqualTo("test-project"); ManagedZone zone = requestBody.getValue(); @@ -69,7 +69,7 @@ public class CreateCdnsTldTest extends CommandTestCase { @Test public void testNameDefault() throws Exception { - runCommand("--dns_name=tld.", "--description=test run"); + runCommand("--dns_name=tld.", "--description=test run", "--force"); ManagedZone zone = requestBody.getValue(); assertThat(zone.getNameServerSet()).isEqualTo("cloud-dns-registry-test"); assertThat(zone.getDnsName()).isEqualTo("tld.");