google-nomulus/java/google/registry/tools/CreateCdnsTld.java
mmuller 44df5da771 Add "create_cdns_tld" command to nomulus
Automated g4 rollback of changelist 171011304.  Please see history below, the original CL was []  Rolling back and re-mailing
so I can get a readability review on it.

*** Reason for rollback ***

Re-opening for readability review.

*** Original change description ***

Automated g4 rollback of changelist 170906329.

*** Reason for rollback ***

Forgot to send this for readability review.

*** Original change description ***

Add "create_cdns_tld" command to nomulus

Add a command to allow us to create the managed zone for a new TLD in cloud
dns.

Note: this implementation is problematic, it's currently doing its own thing
as far as credentials and http transport, making it unusable with the []
channel and the credentials generated by login.  Unfortunately, fixing the plumbing to make it work right is difficult, and we have an immediate need for
this functionality.

***

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=171161371
2017-10-10 12:09:41 -04:00

104 lines
4 KiB
Java

// Copyright 2017 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 com.beust.jcommander.Parameter;
import com.beust.jcommander.Parameters;
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.services.dns.Dns;
import com.google.api.services.dns.model.ManagedZone;
import com.google.common.annotations.VisibleForTesting;
import google.registry.config.RegistryConfig.Config;
import java.io.IOException;
import java.security.GeneralSecurityException;
import java.util.Arrays;
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 {
@Parameter(names = "--description", description = "Description of the new TLD.")
String description;
@Parameter(
names = "--dns_name",
description = "DNS name of the new tld, including trailing period, e.g.: search.",
required = true
)
String dnsName;
@Nullable
@Parameter(
names = "--name",
description = "Managed zone name. If not specified, dns_name is used."
)
String name;
@Inject
@Config("projectId")
String projectId;
@Override
public void run() throws IOException, GeneralSecurityException {
ManagedZone 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);
Dns dnsService = createDnsService();
Dns.ManagedZones.Create request = dnsService.managedZones().create(projectId, requestBody);
ManagedZone response = request.execute();
System.err.println("Created managed zone: " + response);
}
@VisibleForTesting
Dns createDnsService() throws IOException, GeneralSecurityException {
// TODO(b/67367533): We should be obtaining the Dns instance from CloudDnsWriter module. But
// to do this cleanly we need to refactor everything down to the credential object. Having
// done that, this method will go away and this class will become final.
HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
GoogleCredential credential = GoogleCredential.getApplicationDefault();
if (credential.createScopedRequired()) {
credential =
credential.createScoped(
Arrays.asList(
"https://www.googleapis.com/auth/cloud-platform",
"https://www.googleapis.com/auth/cloud-platform.read-only",
"https://www.googleapis.com/auth/ndev.clouddns.readonly",
"https://www.googleapis.com/auth/ndev.clouddns.readwrite"));
}
Dns.Builder builder =
new Dns.Builder(httpTransport, jsonFactory, credential).setApplicationName(projectId);
if (RegistryToolEnvironment.get() != RegistryToolEnvironment.PRODUCTION) {
builder
.setRootUrl("https://staging-www.sandbox.googleapis.com")
.setServicePath("dns/v2beta1_staging/projects/");
}
return builder.build();
}
}