From e5c78414d1fdfafde99fb64bd25c6fde39dce649 Mon Sep 17 00:00:00 2001 From: adriancole Date: Sun, 24 Mar 2013 11:41:10 -0700 Subject: [PATCH] add ability to lookup traffic controller pool in ultradns by its dname --- .../ws/features/TrafficControllerPoolApi.java | 10 +++ .../TrafficControllerPoolAsyncApi.java | 11 ++++ .../ultradns/ws/xml/AttributeHandler.java | 63 +++++++++++++++++++ .../TrafficControllerPoolApiExpectTest.java | 24 ++++++- .../GetPoolForPoolHostNameResponseTest.java | 40 ++++++++++++ .../test/resources/get_tcpool_by_dname.xml | 1 + .../src/test/resources/tcpool_name.xml | 10 +++ 7 files changed, 156 insertions(+), 3 deletions(-) create mode 100644 providers/ultradns-ws/src/main/java/org/jclouds/ultradns/ws/xml/AttributeHandler.java create mode 100644 providers/ultradns-ws/src/test/java/org/jclouds/ultradns/ws/parse/GetPoolForPoolHostNameResponseTest.java create mode 100644 providers/ultradns-ws/src/test/resources/get_tcpool_by_dname.xml create mode 100644 providers/ultradns-ws/src/test/resources/tcpool_name.xml diff --git a/providers/ultradns-ws/src/main/java/org/jclouds/ultradns/ws/features/TrafficControllerPoolApi.java b/providers/ultradns-ws/src/main/java/org/jclouds/ultradns/ws/features/TrafficControllerPoolApi.java index 8e488de79c..da0c80f14e 100644 --- a/providers/ultradns-ws/src/main/java/org/jclouds/ultradns/ws/features/TrafficControllerPoolApi.java +++ b/providers/ultradns-ws/src/main/java/org/jclouds/ultradns/ws/features/TrafficControllerPoolApi.java @@ -56,6 +56,16 @@ public interface TrafficControllerPoolApi { */ String createPoolForHostname(String name, String hostname) throws ResourceAlreadyExistsException; + /** + * Retrieves the name of the specified pool by dname. + * + * @param dname + * {@see TrafficControllerPool#getDName()} ex. {@code jclouds.org.} + * @return null if not found + */ + @Nullable + String getNameByDName(String dname); + /** * removes a pool and all its records and probes * diff --git a/providers/ultradns-ws/src/main/java/org/jclouds/ultradns/ws/features/TrafficControllerPoolAsyncApi.java b/providers/ultradns-ws/src/main/java/org/jclouds/ultradns/ws/features/TrafficControllerPoolAsyncApi.java index ce3425f3a1..6127b3495c 100644 --- a/providers/ultradns-ws/src/main/java/org/jclouds/ultradns/ws/features/TrafficControllerPoolAsyncApi.java +++ b/providers/ultradns-ws/src/main/java/org/jclouds/ultradns/ws/features/TrafficControllerPoolAsyncApi.java @@ -35,6 +35,7 @@ import org.jclouds.ultradns.ws.domain.PoolRecordSpec; import org.jclouds.ultradns.ws.domain.TrafficControllerPool; import org.jclouds.ultradns.ws.domain.TrafficControllerPoolRecord; import org.jclouds.ultradns.ws.filters.SOAPWrapWithPasswordAuth; +import org.jclouds.ultradns.ws.xml.AttributeHandler; import org.jclouds.ultradns.ws.xml.PoolRecordSpecHandler; import org.jclouds.ultradns.ws.xml.TextHandler; import org.jclouds.ultradns.ws.xml.TrafficControllerPoolListHandler; @@ -82,6 +83,16 @@ public interface TrafficControllerPoolAsyncApi { ListenableFuture> listRecords(@PayloadParam("poolId") String poolId) throws ResourceNotFoundException; + /** + * @see TrafficControllerPoolApi#getByDName(String) + */ + @Named("getPoolForPoolHostName>") + @POST + @Payload("{hostName}") + @XMLResponseParser(AttributeHandler.PoolName.class) + @Fallback(NullOnNotFoundOr404.class) + ListenableFuture getNameByDName(@PayloadParam("hostName") String dname); + /** * @see TrafficControllerPoolApi#delete(String) */ diff --git a/providers/ultradns-ws/src/main/java/org/jclouds/ultradns/ws/xml/AttributeHandler.java b/providers/ultradns-ws/src/main/java/org/jclouds/ultradns/ws/xml/AttributeHandler.java new file mode 100644 index 0000000000..5dc9b32d86 --- /dev/null +++ b/providers/ultradns-ws/src/main/java/org/jclouds/ultradns/ws/xml/AttributeHandler.java @@ -0,0 +1,63 @@ +/** + * 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.ultradns.ws.xml; + +import static com.google.common.base.Preconditions.checkNotNull; +import static org.jclouds.util.SaxUtils.cleanseAttributes; + +import java.util.Map; + +import org.jclouds.http.functions.ParseSax; +import org.xml.sax.Attributes; + +/** + * @author Adrian Cole + */ +public abstract class AttributeHandler extends ParseSax.HandlerForGeneratedRequestWithResult { + + public static class PoolName extends AttributeHandler { + public PoolName() { + super("PoolName"); + } + } + + private String attributeName; + private String attribute = null; + + private AttributeHandler(String attributeName) { + this.attributeName = checkNotNull(attributeName, "attributeName"); + } + + @Override + public String getResult() { + try { + return checkNotNull(attribute, "%s not present in the response", attributeName); + } finally { + attribute = null; + } + } + + @Override + public void startElement(String uri, String localName, String qName, Attributes attrs) { + Map attributes = cleanseAttributes(attrs); + if (attribute == null && attributes.containsKey(attributeName)) { + attribute = attributes.get(attributeName); + } + } +} diff --git a/providers/ultradns-ws/src/test/java/org/jclouds/ultradns/ws/features/TrafficControllerPoolApiExpectTest.java b/providers/ultradns-ws/src/test/java/org/jclouds/ultradns/ws/features/TrafficControllerPoolApiExpectTest.java index 7936a2d80a..fc8833bbc8 100644 --- a/providers/ultradns-ws/src/test/java/org/jclouds/ultradns/ws/features/TrafficControllerPoolApiExpectTest.java +++ b/providers/ultradns-ws/src/test/java/org/jclouds/ultradns/ws/features/TrafficControllerPoolApiExpectTest.java @@ -89,6 +89,27 @@ public class TrafficControllerPoolApiExpectTest extends BaseUltraDNSWSApiExpectT new GetTCPoolRecordsResponseTest().expected().toString()); } + HttpRequest getNameByDName = HttpRequest.builder().method("POST") + .endpoint("https://ultra-api.ultradns.com:8443/UltraDNS_WS/v01") + .addHeader("Host", "ultra-api.ultradns.com:8443") + .payload(payloadFromResourceWithContentType("/get_tcpool_by_dname.xml", "application/xml")).build(); + + HttpResponse getNameByDNameResponse = HttpResponse.builder().statusCode(200) + .payload(payloadFromResourceWithContentType("/tcpool_name.xml", "application/xml")).build(); + + public void testGetNameByDNameWhenResponseIs2xx() { + UltraDNSWSApi success = requestSendsResponse(getNameByDName, getNameByDNameResponse); + assertEquals(success.getTrafficControllerPoolApiForZone("jclouds.org.").getNameByDName("www.foo.com."), "foo"); + } + + HttpResponse poolDoesntExist = HttpResponse.builder().message("Server Epoolor").statusCode(500) + .payload(payloadFromResource("/lbpool_doesnt_exist.xml")).build(); + + public void testGetNameByDNameWhenResponseNotFound() { + UltraDNSWSApi notFound = requestSendsResponse(getNameByDName, poolDoesntExist); + assertNull(notFound.getTrafficControllerPoolApiForZone("jclouds.org.").getNameByDName("www.foo.com.")); + } + HttpRequest delete = HttpRequest.builder().method("POST") .endpoint("https://ultra-api.ultradns.com:8443/UltraDNS_WS/v01") .addHeader("Host", "ultra-api.ultradns.com:8443") @@ -102,9 +123,6 @@ public class TrafficControllerPoolApiExpectTest extends BaseUltraDNSWSApiExpectT success.getTrafficControllerPoolApiForZone("jclouds.org.").delete("04053D8E57C7931F"); } - HttpResponse poolDoesntExist = HttpResponse.builder().message("Server Epoolor").statusCode(500) - .payload(payloadFromResource("/lbpool_doesnt_exist.xml")).build(); - public void testDeleteWhenResponseNotFound() { UltraDNSWSApi notFound = requestSendsResponse(delete, poolDoesntExist); notFound.getTrafficControllerPoolApiForZone("jclouds.org.").delete("04053D8E57C7931F"); diff --git a/providers/ultradns-ws/src/test/java/org/jclouds/ultradns/ws/parse/GetPoolForPoolHostNameResponseTest.java b/providers/ultradns-ws/src/test/java/org/jclouds/ultradns/ws/parse/GetPoolForPoolHostNameResponseTest.java new file mode 100644 index 0000000000..4103756400 --- /dev/null +++ b/providers/ultradns-ws/src/test/java/org/jclouds/ultradns/ws/parse/GetPoolForPoolHostNameResponseTest.java @@ -0,0 +1,40 @@ +/** + * 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.ultradns.ws.parse; + +import static org.testng.Assert.assertEquals; + +import java.io.InputStream; + +import org.jclouds.http.functions.BaseHandlerTest; +import org.jclouds.ultradns.ws.xml.AttributeHandler; +import org.testng.annotations.Test; + +/** + * @author Adrian Cole + */ +@Test(testName = "GetPoolForPoolHostNameResponseTest") +public class GetPoolForPoolHostNameResponseTest extends BaseHandlerTest { + + public void test() { + InputStream is = getClass().getResourceAsStream("/tcpool_name.xml"); + AttributeHandler.PoolName handler = injector.getInstance(AttributeHandler.PoolName.class); + assertEquals(factory.create(handler).parse(is), "foo"); + } +} diff --git a/providers/ultradns-ws/src/test/resources/get_tcpool_by_dname.xml b/providers/ultradns-ws/src/test/resources/get_tcpool_by_dname.xml new file mode 100644 index 0000000000..272b94f4bd --- /dev/null +++ b/providers/ultradns-ws/src/test/resources/get_tcpool_by_dname.xml @@ -0,0 +1 @@ +identitycredentialwww.foo.com. \ No newline at end of file diff --git a/providers/ultradns-ws/src/test/resources/tcpool_name.xml b/providers/ultradns-ws/src/test/resources/tcpool_name.xml new file mode 100644 index 0000000000..e7c3adbbb2 --- /dev/null +++ b/providers/ultradns-ws/src/test/resources/tcpool_name.xml @@ -0,0 +1,10 @@ + + + + + + + + + \ No newline at end of file