Skip to content
Permalink
Browse files
Merge pull request #33 from rackerlabs/rax-dns
Examples for Rackspace Cloud DNS
  • Loading branch information
abayer committed Apr 29, 2013
2 parents 956e5d1 + a8d0e9d commit ed6642c0049cd0123551356c85323a8c3d0215fc
Show file tree
Hide file tree
Showing 12 changed files with 1,202 additions and 1 deletion.
@@ -6,7 +6,7 @@ Example code that uses jclouds to perform common tasks on the Rackspace Cloud. T
1. Username and API key for the Rackspace Cloud - See the [Getting Started guide](http://www.jclouds.org/documentation/quickstart/rackspace/).
1. Java Development Kit (JDK) version 6 or later - [Download](http://www.oracle.com/technetwork/java/javase/downloads/index.html).
1. Apache Ant - [Download](http://ant.apache.org/bindownload.cgi).
1. Git - [Download](http://git-scm.com/downloads) or you can just [download this repository as a zip or tar.gz](https://github.com/jclouds/jclouds-examples/downloads).
1. Git - [Download](http://git-scm.com/downloads).

## Environment
To setup an environment to compile and run the examples use these commands:
@@ -53,6 +53,13 @@ The [cloudloadbalancers package](https://github.com/jclouds/jclouds-examples/tre
* [CreateLoadBalancerWithNewServers.java](https://github.com/jclouds/jclouds-examples/blob/master/rackspace/src/main/java/org/jclouds/examples/rackspace/cloudloadbalancers/CreateLoadBalancerWithNewServers.java) - An end to end example of creating a load balancer and adding new servers (nodes) to it.
* Other examples of creating, manipulating, listing, and deleting load balancers and nodes.

The [clouddns package](https://github.com/jclouds/jclouds-examples/tree/master/rackspace/src/main/java/org/jclouds/examples/rackspace/clouddns) demonstrates how to accomplish common tasks for working with DNS in the cloud.

* [CreateDomains.java](https://github.com/jclouds/jclouds-examples/blob/master/rackspace/src/main/java/org/jclouds/examples/rackspace/clouddns/CreateDomains.java) - An example of creating domains with subdomains and records.
* [CreateRecords.java](https://github.com/jclouds/jclouds-examples/blob/master/rackspace/src/main/java/org/jclouds/examples/rackspace/clouddns/CreateRecords.java) - An example of creating records and adding them to a domain.
* [CRUDReverseDNSRecords.java](https://github.com/jclouds/jclouds-examples/blob/master/rackspace/src/main/java/org/jclouds/examples/rackspace/clouddns/CRUDReverseDNSRecords.java) - An example of creating a Cloud Server and a reverse DNS record (PTR) to go along with it.
* Other examples of creating, manipulating, listing, and deleting domains and records.


## Command Line
To run these examples from the command line use these commands:
@@ -27,6 +27,15 @@
import org.jclouds.examples.rackspace.cloudblockstorage.ListVolumeAttachments;
import org.jclouds.examples.rackspace.cloudblockstorage.ListVolumeTypes;
import org.jclouds.examples.rackspace.cloudblockstorage.ListVolumes;
import org.jclouds.examples.rackspace.clouddns.CRUDReverseDNSRecords;
import org.jclouds.examples.rackspace.clouddns.CreateDomains;
import org.jclouds.examples.rackspace.clouddns.CreateRecords;
import org.jclouds.examples.rackspace.clouddns.DeleteDomains;
import org.jclouds.examples.rackspace.clouddns.DeleteRecords;
import org.jclouds.examples.rackspace.clouddns.ListDomains;
import org.jclouds.examples.rackspace.clouddns.ListRecords;
import org.jclouds.examples.rackspace.clouddns.UpdateDomains;
import org.jclouds.examples.rackspace.clouddns.UpdateRecords;
import org.jclouds.examples.rackspace.cloudfiles.CloudFilesPublish;
import org.jclouds.examples.rackspace.cloudfiles.CreateContainer;
import org.jclouds.examples.rackspace.cloudfiles.DeleteObjectsAndContainer;
@@ -103,5 +112,16 @@ private void smokeTest(String[] args) {
CreateLoadBalancerWithNewServers.main(args);
DeleteServer.main(args);
DeleteLoadBalancers.main(args);

CreateDomains.main(args);
ListDomains.main(args);
UpdateDomains.main(args);
CreateRecords.main(args);
ListRecords.main(args);
UpdateRecords.main(args);
DeleteRecords.main(args);
CRUDReverseDNSRecords.main(args);
DeleteServer.main(args);
DeleteDomains.main(args);
}
}
@@ -0,0 +1,148 @@
/**
* Licensed to jclouds, Inc. (jclouds) under one or more
* contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. jclouds licenses this file
* to you 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 org.jclouds.examples.rackspace.clouddns;

import static com.google.common.io.Closeables.closeQuietly;
import static org.jclouds.examples.rackspace.clouddns.Constants.CLOUD_SERVERS;
import static org.jclouds.examples.rackspace.clouddns.Constants.NAME;
import static org.jclouds.rackspace.clouddns.v1.predicates.JobPredicates.awaitComplete;

import java.io.Closeable;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeoutException;

import org.jclouds.ContextBuilder;
import org.jclouds.compute.domain.NodeMetadata;
import org.jclouds.examples.rackspace.cloudservers.CloudServersPublish;
import org.jclouds.rackspace.clouddns.v1.CloudDNSApi;
import org.jclouds.rackspace.clouddns.v1.domain.Record;
import org.jclouds.rackspace.clouddns.v1.domain.RecordDetail;

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Lists;

/**
* This example creates reverse DNS records on an existing domain for a Cloud Server.
*
* @author Everett Toews
*/
public class CRUDReverseDNSRecords implements Closeable {
private static NodeMetadata node;
private static RecordDetail recordDetail;

private CloudDNSApi dnsApi;

/**
* To get a username and API key see http://www.jclouds.org/documentation/quickstart/rackspace/
*
* The first argument (args[0]) must be your username
* The second argument (args[1]) must be your API key
*/
public static void main(String[] args) {
CRUDReverseDNSRecords crudReverseDNSRecords = new CRUDReverseDNSRecords();

try {
List<String> argsList = Lists.newArrayList(args);
argsList.add("1"); // the number of Cloud Servers to start
node = CloudServersPublish.getPublishedCloudServers(argsList).iterator().next();

crudReverseDNSRecords.init(args);
crudReverseDNSRecords.createReverseDNSRecords();
crudReverseDNSRecords.listReverseDNSRecords();
crudReverseDNSRecords.updateReverseDNSRecords();
crudReverseDNSRecords.deleteAllReverseDNSRecords();
}
catch (Exception e) {
e.printStackTrace();
}
finally {
crudReverseDNSRecords.close();
}
}

private void init(String[] args) {
// The provider configures jclouds To use the Rackspace Cloud (US)
// To use the Rackspace Cloud (UK) set the provider to "rackspace-clouddns-uk"
String provider = "rackspace-clouddns-us";

String username = args[0];
String apiKey = args[1];

dnsApi = ContextBuilder.newBuilder(provider)
.credentials(username, apiKey)
.buildApi(CloudDNSApi.class);
}

private void createReverseDNSRecords() throws TimeoutException {
System.out.println("Create Reverse DNS Records");

Record createPTRRecordIPv4 = Record.builder()
.name(NAME)
.type("PTR")
.data(node.getPublicAddresses().iterator().next())
.ttl(11235)
.build();

Set<Record> records = ImmutableSet.<Record> of(createPTRRecordIPv4);

Iterable<RecordDetail> recordDetails = awaitComplete(dnsApi, dnsApi.getReverseDNSApiForService(CLOUD_SERVERS).create(node.getUri(), records));

recordDetail = recordDetails.iterator().next();
System.out.println(" " + recordDetail);
}

private void listReverseDNSRecords() {
System.out.println("List Reverse DNS Records");

Iterable<RecordDetail> recordDetails = dnsApi.getReverseDNSApiForService(CLOUD_SERVERS).list(node.getUri()).concat();

for (RecordDetail recordDetail: recordDetails) {
System.out.println(" " + recordDetail);
}
}

private void updateReverseDNSRecords() throws TimeoutException {
System.out.println("Update Reverse DNS Records");

Record updatePTRRecord = recordDetail.getRecord().toBuilder().comment("Hello Cloud DNS").build();
Map<String, Record> idsToRecords = ImmutableMap.<String, Record> of(recordDetail.getId(), updatePTRRecord);

awaitComplete(dnsApi, dnsApi.getReverseDNSApiForService(CLOUD_SERVERS).update(node.getUri(), idsToRecords));

System.out.println(" " + dnsApi.getReverseDNSApiForService(CLOUD_SERVERS).get(node.getUri(), recordDetail.getId()));
}

private void deleteAllReverseDNSRecords() throws TimeoutException {
System.out.println("Delete Reverse DNS Records");

awaitComplete(dnsApi, dnsApi.getReverseDNSApiForService(CLOUD_SERVERS).deleteAll(node.getUri()));

System.out.println(" Deleted all reverse DNS records");
}

/**
* Always close your service when you're done with it.
*/
public void close() {
closeQuietly(dnsApi);
}
}
@@ -0,0 +1,61 @@
/**
* Licensed to jclouds, Inc. (jclouds) under one or more
* contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. jclouds licenses this file
* to you 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 org.jclouds.examples.rackspace.clouddns;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.jclouds.rackspace.clouddns.v1.domain.Domain;

import com.google.common.base.Function;
import com.google.common.base.Predicate;


/**
* Constants used by the Rackspace Examples.
*
* @author Everett Toews
*/
public interface Constants {
public static final String NAME = "jclouds-example.com";
public static final String ALT_NAME = "alt-jclouds-example.com";
public static final String CLOUD_SERVERS = "cloudServersOpenStack";

// subdomains contain two or more '.'
public static final Pattern SUBDOMAIN_PATTERN = Pattern.compile(".*\\..*\\.");

/**
* Determine if a domain is a top level domain (i.e. not a subdomain).
*/
public static final Predicate<Domain> IS_DOMAIN = new Predicate<Domain>() {
public boolean apply(Domain domain) {
Matcher matcher = SUBDOMAIN_PATTERN.matcher(domain.getName());
return !matcher.find();
}
};

/**
* Take a Domain and return its id.
*/
public static final Function<Domain, Integer> GET_DOMAIN_ID = new Function<Domain, Integer>() {
public Integer apply(Domain domain) {
return domain.getId();
}
};
}

0 comments on commit ed6642c

Please sign in to comment.