From e3f51bd0305bc4d5f3ecc97517ec2ea74818698c Mon Sep 17 00:00:00 2001 From: adriancole Date: Sat, 23 Mar 2013 17:16:27 -0700 Subject: [PATCH 1/3] refactored out common id parsing logic in ultradns --- .../ws/features/ResourceRecordAsyncApi.java | 4 +- .../ws/features/RoundRobinPoolAsyncApi.java | 11 ++-- .../ultradns/ws/features/TaskAsyncApi.java | 4 +- .../xml/{GuidHandler.java => IDHandler.java} | 42 +++++++++---- .../ultradns/ws/xml/RRPoolIDHandler.java | 61 ------------------- .../ws/parse/RunTestResponseTest.java | 4 +- 6 files changed, 42 insertions(+), 84 deletions(-) rename providers/ultradns-ws/src/main/java/org/jclouds/ultradns/ws/xml/{GuidHandler.java => IDHandler.java} (65%) delete mode 100644 providers/ultradns-ws/src/main/java/org/jclouds/ultradns/ws/xml/RRPoolIDHandler.java diff --git a/providers/ultradns-ws/src/main/java/org/jclouds/ultradns/ws/features/ResourceRecordAsyncApi.java b/providers/ultradns-ws/src/main/java/org/jclouds/ultradns/ws/features/ResourceRecordAsyncApi.java index b2bcd9536c..2fee1d8524 100644 --- a/providers/ultradns-ws/src/main/java/org/jclouds/ultradns/ws/features/ResourceRecordAsyncApi.java +++ b/providers/ultradns-ws/src/main/java/org/jclouds/ultradns/ws/features/ResourceRecordAsyncApi.java @@ -35,7 +35,7 @@ import org.jclouds.ultradns.ws.binders.ZoneAndResourceRecordToXML; import org.jclouds.ultradns.ws.domain.ResourceRecord; import org.jclouds.ultradns.ws.domain.ResourceRecordMetadata; import org.jclouds.ultradns.ws.filters.SOAPWrapWithPasswordAuth; -import org.jclouds.ultradns.ws.xml.GuidHandler; +import org.jclouds.ultradns.ws.xml.IDHandler; import org.jclouds.ultradns.ws.xml.ResourceRecordListHandler; import com.google.common.collect.FluentIterable; @@ -56,7 +56,7 @@ public interface ResourceRecordAsyncApi { */ @Named("createResourceRecord") @POST - @XMLResponseParser(GuidHandler.class) + @XMLResponseParser(IDHandler.Guid.class) @MapBinder(ZoneAndResourceRecordToXML.class) ListenableFuture create(@PayloadParam("resourceRecord") ResourceRecord toCreate) throws ResourceAlreadyExistsException; diff --git a/providers/ultradns-ws/src/main/java/org/jclouds/ultradns/ws/features/RoundRobinPoolAsyncApi.java b/providers/ultradns-ws/src/main/java/org/jclouds/ultradns/ws/features/RoundRobinPoolAsyncApi.java index 9e8c9d39c7..e7087f525d 100644 --- a/providers/ultradns-ws/src/main/java/org/jclouds/ultradns/ws/features/RoundRobinPoolAsyncApi.java +++ b/providers/ultradns-ws/src/main/java/org/jclouds/ultradns/ws/features/RoundRobinPoolAsyncApi.java @@ -33,8 +33,7 @@ import org.jclouds.ultradns.ws.UltraDNSWSExceptions.ResourceAlreadyExistsExcepti import org.jclouds.ultradns.ws.domain.ResourceRecord; import org.jclouds.ultradns.ws.domain.RoundRobinPool; import org.jclouds.ultradns.ws.filters.SOAPWrapWithPasswordAuth; -import org.jclouds.ultradns.ws.xml.GuidHandler; -import org.jclouds.ultradns.ws.xml.RRPoolIDHandler; +import org.jclouds.ultradns.ws.xml.IDHandler; import org.jclouds.ultradns.ws.xml.ResourceRecordListHandler; import org.jclouds.ultradns.ws.xml.RoundRobinPoolListHandler; @@ -75,7 +74,7 @@ public interface RoundRobinPoolAsyncApi { */ @Named("addRRLBPool") @POST - @XMLResponseParser(RRPoolIDHandler.class) + @XMLResponseParser(IDHandler.RRPool.class) @Payload("{zoneName}{hostName}{description}1") ListenableFuture createAPoolForHostname(@PayloadParam("description") String name, @PayloadParam("hostName") String hostname) throws ResourceAlreadyExistsException; @@ -85,7 +84,7 @@ public interface RoundRobinPoolAsyncApi { */ @Named("addRecordToRRPool") @POST - @XMLResponseParser(GuidHandler.class) + @XMLResponseParser(IDHandler.Guid.class) @Payload("") ListenableFuture addARecordWithAddressAndTTL(@PayloadParam("lbPoolID") String lbPoolID, @PayloadParam("address") String ipv4Address, @PayloadParam("ttl") int ttl) @@ -115,7 +114,7 @@ public interface RoundRobinPoolAsyncApi { */ @Named("addRRLBPool") @POST - @XMLResponseParser(RRPoolIDHandler.class) + @XMLResponseParser(IDHandler.RRPool.class) @Payload("{zoneName}{hostName}{description}28") ListenableFuture createAAAAPoolForHostname(@PayloadParam("description") String name, @PayloadParam("hostName") String hostname) throws ResourceAlreadyExistsException; @@ -125,7 +124,7 @@ public interface RoundRobinPoolAsyncApi { */ @Named("addRecordToRRPool") @POST - @XMLResponseParser(GuidHandler.class) + @XMLResponseParser(IDHandler.Guid.class) @Payload("") ListenableFuture addAAAARecordWithAddressAndTTL(@PayloadParam("lbPoolID") String lbPoolID, @PayloadParam("address") String ipv6Address, @PayloadParam("ttl") int ttl) diff --git a/providers/ultradns-ws/src/main/java/org/jclouds/ultradns/ws/features/TaskAsyncApi.java b/providers/ultradns-ws/src/main/java/org/jclouds/ultradns/ws/features/TaskAsyncApi.java index f1a748ee52..654c9ef1be 100644 --- a/providers/ultradns-ws/src/main/java/org/jclouds/ultradns/ws/features/TaskAsyncApi.java +++ b/providers/ultradns-ws/src/main/java/org/jclouds/ultradns/ws/features/TaskAsyncApi.java @@ -31,7 +31,7 @@ import org.jclouds.rest.annotations.VirtualHost; import org.jclouds.rest.annotations.XMLResponseParser; import org.jclouds.ultradns.ws.domain.Task; import org.jclouds.ultradns.ws.filters.SOAPWrapWithPasswordAuth; -import org.jclouds.ultradns.ws.xml.GuidHandler; +import org.jclouds.ultradns.ws.xml.IDHandler; import org.jclouds.ultradns.ws.xml.TaskHandler; import org.jclouds.ultradns.ws.xml.TaskListHandler; @@ -52,7 +52,7 @@ public interface TaskAsyncApi { */ @Named("runTest") @POST - @XMLResponseParser(GuidHandler.class) + @XMLResponseParser(IDHandler.Guid.class) @Payload("{value}") ListenableFuture runTest(@PayloadParam("value") String value); diff --git a/providers/ultradns-ws/src/main/java/org/jclouds/ultradns/ws/xml/GuidHandler.java b/providers/ultradns-ws/src/main/java/org/jclouds/ultradns/ws/xml/IDHandler.java similarity index 65% rename from providers/ultradns-ws/src/main/java/org/jclouds/ultradns/ws/xml/GuidHandler.java rename to providers/ultradns-ws/src/main/java/org/jclouds/ultradns/ws/xml/IDHandler.java index 7de3d7e953..2c1cd6ebf2 100644 --- a/providers/ultradns-ws/src/main/java/org/jclouds/ultradns/ws/xml/GuidHandler.java +++ b/providers/ultradns-ws/src/main/java/org/jclouds/ultradns/ws/xml/IDHandler.java @@ -23,33 +23,53 @@ import static org.jclouds.util.SaxUtils.currentOrNull; import static org.jclouds.util.SaxUtils.equalsOrSuffix; import org.jclouds.http.functions.ParseSax; -import org.xml.sax.Attributes; /** * * @author Adrian Cole */ -public class GuidHandler extends ParseSax.HandlerForGeneratedRequestWithResult { +public abstract class IDHandler extends ParseSax.HandlerForGeneratedRequestWithResult { + + public static class Guid extends IDHandler { + public Guid() { + super("guid"); + } + } + + public static class RRPool extends IDHandler { + public RRPool() { + super("RRPoolID"); + } + } + + public static class TCPool extends IDHandler { + public TCPool() { + super("TCPoolID"); + } + } + + private String idElement; + private StringBuilder currentText = new StringBuilder(); - private String guid = null; + private String id = null; + + private IDHandler(String idElement) { + this.idElement = checkNotNull(idElement, "idElement"); + } @Override public String getResult() { try { - return checkNotNull(guid, "guid not present in the response"); + return checkNotNull(id, "%s not present in the response", idElement); } finally { - guid = null; + id = null; } } - @Override - public void startElement(String url, String name, String qName, Attributes attributes) { - } - @Override public void endElement(String uri, String name, String qName) { - if (equalsOrSuffix(qName, "guid")) { - guid = currentOrNull(currentText); + if (equalsOrSuffix(qName, idElement)) { + id = currentOrNull(currentText); } currentText = new StringBuilder(); } diff --git a/providers/ultradns-ws/src/main/java/org/jclouds/ultradns/ws/xml/RRPoolIDHandler.java b/providers/ultradns-ws/src/main/java/org/jclouds/ultradns/ws/xml/RRPoolIDHandler.java deleted file mode 100644 index b57f1632c1..0000000000 --- a/providers/ultradns-ws/src/main/java/org/jclouds/ultradns/ws/xml/RRPoolIDHandler.java +++ /dev/null @@ -1,61 +0,0 @@ -/** - * 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.currentOrNull; -import static org.jclouds.util.SaxUtils.equalsOrSuffix; - -import org.jclouds.http.functions.ParseSax; -import org.xml.sax.Attributes; - -/** - * - * @author Adrian Cole - */ -public class RRPoolIDHandler extends ParseSax.HandlerForGeneratedRequestWithResult { - private StringBuilder currentText = new StringBuilder(); - private String rrPoolID = null; - - @Override - public String getResult() { - try { - return checkNotNull(rrPoolID, "RRPoolId not present in the response"); - } finally { - rrPoolID = null; - } - } - - @Override - public void startElement(String url, String name, String qName, Attributes attributes) { - } - - @Override - public void endElement(String uri, String name, String qName) { - if (equalsOrSuffix(qName, "RRPoolID")) { - rrPoolID = currentOrNull(currentText); - } - currentText = new StringBuilder(); - } - - @Override - public void characters(char ch[], int start, int length) { - currentText.append(ch, start, length); - } -} diff --git a/providers/ultradns-ws/src/test/java/org/jclouds/ultradns/ws/parse/RunTestResponseTest.java b/providers/ultradns-ws/src/test/java/org/jclouds/ultradns/ws/parse/RunTestResponseTest.java index 4b33527bf6..f818089f62 100644 --- a/providers/ultradns-ws/src/test/java/org/jclouds/ultradns/ws/parse/RunTestResponseTest.java +++ b/providers/ultradns-ws/src/test/java/org/jclouds/ultradns/ws/parse/RunTestResponseTest.java @@ -23,7 +23,7 @@ import static org.testng.Assert.assertEquals; import java.io.InputStream; import org.jclouds.http.functions.BaseHandlerTest; -import org.jclouds.ultradns.ws.xml.GuidHandler; +import org.jclouds.ultradns.ws.xml.IDHandler; import org.testng.annotations.Test; /** @@ -35,7 +35,7 @@ public class RunTestResponseTest extends BaseHandlerTest { public void test() { InputStream is = getClass().getResourceAsStream("/taskid.xml"); - GuidHandler handler = injector.getInstance(GuidHandler.class); + IDHandler.Guid handler = injector.getInstance(IDHandler.Guid.class); assertEquals(factory.create(handler).parse(is), "8d7a1725-4f4a-4b70-affa-f01dcce1526e"); } } From a329feee7c0208a9765e108ea9d7dd6ca66ed08e Mon Sep 17 00:00:00 2001 From: adriancole Date: Sat, 23 Mar 2013 17:17:38 -0700 Subject: [PATCH 2/3] create and delete traffic controller pools in ultradns --- .../ws/features/TrafficControllerPoolApi.java | 23 +++++++ .../TrafficControllerPoolAsyncApi.java | 24 +++++++- .../TrafficControllerPoolApiExpectTest.java | 42 +++++++++++++ .../TrafficControllerPoolApiLiveTest.java | 60 +++++++++++++++++++ .../src/test/resources/create_tcpool.xml | 1 + .../src/test/resources/tcpool_created.xml | 5 ++ 6 files changed, 154 insertions(+), 1 deletion(-) create mode 100644 providers/ultradns-ws/src/test/resources/create_tcpool.xml create mode 100644 providers/ultradns-ws/src/test/resources/tcpool_created.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 b4d3e95e32..dfb6789f48 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 @@ -19,6 +19,8 @@ package org.jclouds.ultradns.ws.features; import org.jclouds.rest.ResourceNotFoundException; +import org.jclouds.ultradns.ws.UltraDNSWSExceptions.ResourceAlreadyExistsException; +import org.jclouds.ultradns.ws.domain.RoundRobinPool; import org.jclouds.ultradns.ws.domain.TrafficControllerPool; import org.jclouds.ultradns.ws.domain.TrafficControllerPoolRecord; @@ -29,6 +31,19 @@ import com.google.common.collect.FluentIterable; * @author Adrian Cole */ public interface TrafficControllerPoolApi { + /** + * creates a traffic controller pool. + * + * @param name + * {@link TrafficControllerPool#getName() name} of the TC pool + * @param hostname + * {@link TrafficControllerPool#getDName() dname} of the TC pool + * {ex. www.jclouds.org.} + * @return the {@code guid} of the new record + * @throws ResourceAlreadyExistsException + * if a pool already exists with the same attrs + */ + String createPoolForHostname(String name, String hostname) throws ResourceAlreadyExistsException; /** * Returns all traffic controller pools in the zone. @@ -45,4 +60,12 @@ public interface TrafficControllerPoolApi { * if the pool doesn't exist */ FluentIterable listRecords(String poolId) throws ResourceNotFoundException; + + /** + * removes a pool and all its records and probes + * + * @param id + * the {@link RoundRobinPool#getId() id} + */ + void delete(String id); } 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 44e1d26224..422b7fde3d 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 @@ -21,15 +21,19 @@ package org.jclouds.ultradns.ws.features; import javax.inject.Named; import javax.ws.rs.POST; +import org.jclouds.Fallbacks.VoidOnNotFoundOr404; import org.jclouds.rest.ResourceNotFoundException; +import org.jclouds.rest.annotations.Fallback; import org.jclouds.rest.annotations.Payload; import org.jclouds.rest.annotations.PayloadParam; import org.jclouds.rest.annotations.RequestFilters; import org.jclouds.rest.annotations.VirtualHost; import org.jclouds.rest.annotations.XMLResponseParser; +import org.jclouds.ultradns.ws.UltraDNSWSExceptions.ResourceAlreadyExistsException; 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.IDHandler; import org.jclouds.ultradns.ws.xml.TrafficControllerPoolListHandler; import org.jclouds.ultradns.ws.xml.TrafficControllerPoolRecordListHandler; @@ -46,6 +50,16 @@ import com.google.common.util.concurrent.ListenableFuture; @VirtualHost public interface TrafficControllerPoolAsyncApi { + /** + * @see RoundRobinPoolApi#createPoolForHostname + */ + @Named("addTCLBPool") + @POST + @XMLResponseParser(IDHandler.TCPool.class) + @Payload("{zoneName}{hostName}{description}1EnabledEnabled0") + ListenableFuture createPoolForHostname(@PayloadParam("description") String name, + @PayloadParam("hostName") String hostname) throws ResourceAlreadyExistsException; + /** * @see TrafficControllerPoolApi#list() */ @@ -54,7 +68,7 @@ public interface TrafficControllerPoolAsyncApi { @XMLResponseParser(TrafficControllerPoolListHandler.class) @Payload("{zoneName}TC") ListenableFuture> list() throws ResourceNotFoundException; - + /** * @see TrafficControllerPoolApi#listRecords(String) */ @@ -65,4 +79,12 @@ public interface TrafficControllerPoolAsyncApi { ListenableFuture> listRecords(@PayloadParam("poolId") String poolId) throws ResourceNotFoundException; + /** + * @see TrafficControllerPoolApi#delete(String) + */ + @Named("deleteLBPool") + @POST + @Payload("{lbPoolID}Yes") + @Fallback(VoidOnNotFoundOr404.class) + ListenableFuture delete(@PayloadParam("lbPoolID") String id); } 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 69d894495a..4bf762ff24 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 @@ -23,6 +23,7 @@ import static org.testng.Assert.assertEquals; import org.jclouds.http.HttpRequest; import org.jclouds.http.HttpResponse; import org.jclouds.ultradns.ws.UltraDNSWSApi; +import org.jclouds.ultradns.ws.UltraDNSWSExceptions.ResourceAlreadyExistsException; import org.jclouds.ultradns.ws.internal.BaseUltraDNSWSApiExpectTest; import org.jclouds.ultradns.ws.parse.GetTCLoadBalancingPoolsByZoneResponseTest; import org.jclouds.ultradns.ws.parse.GetTCPoolRecordsResponseTest; @@ -33,6 +34,27 @@ import org.testng.annotations.Test; */ @Test(groups = "unit", testName = "TrafficControllerPoolApiExpectTest") public class TrafficControllerPoolApiExpectTest extends BaseUltraDNSWSApiExpectTest { + HttpRequest create = HttpRequest.builder().method("POST") + .endpoint("https://ultra-api.ultradns.com:8443/UltraDNS_WS/v01") + .addHeader("Host", "ultra-api.ultradns.com:8443") + .payload(payloadFromResourceWithContentType("/create_tcpool.xml", "application/xml")).build(); + + HttpResponse createResponse = HttpResponse.builder().statusCode(200) + .payload(payloadFromResourceWithContentType("/tcpool_created.xml", "application/xml")).build(); + + public void testCreateWhenResponseIs2xx() { + UltraDNSWSApi success = requestSendsResponse(create, createResponse); + assertEquals(success.getTrafficControllerPoolApiForZone("jclouds.org.").createPoolForHostname("www.jclouds.org.", "foo"), "060339AA0417567A"); + } + + HttpResponse alreadyCreated = HttpResponse.builder().statusCode(500) + .payload(payloadFromResourceWithContentType("/lbpool_already_exists.xml", "application/xml")).build(); + + @Test(expectedExceptions = ResourceAlreadyExistsException.class, expectedExceptionsMessageRegExp = "Pool already created for this host name : www.rrpool.adrianc.rrpool.ultradnstest.jclouds.org.") + public void testCreateWhenResponseError2912() { + UltraDNSWSApi already = requestSendsResponse(create, alreadyCreated); + already.getTrafficControllerPoolApiForZone("jclouds.org.").createPoolForHostname("www.jclouds.org.", "foo"); + } HttpRequest list = HttpRequest.builder().method("POST") .endpoint("https://ultra-api.ultradns.com:8443/UltraDNS_WS/v01") @@ -65,4 +87,24 @@ public class TrafficControllerPoolApiExpectTest extends BaseUltraDNSWSApiExpectT new GetTCPoolRecordsResponseTest().expected().toString()); } + HttpRequest delete = HttpRequest.builder().method("POST") + .endpoint("https://ultra-api.ultradns.com:8443/UltraDNS_WS/v01") + .addHeader("Host", "ultra-api.ultradns.com:8443") + .payload(payloadFromResourceWithContentType("/delete_lbpool.xml", "application/xml")).build(); + + HttpResponse deleteResponse = HttpResponse.builder().statusCode(404) + .payload(payloadFromResourceWithContentType("/lbpool_deleted.xml", "application/xml")).build(); + + public void testDeleteWhenResponseIs2xx() { + UltraDNSWSApi success = requestSendsResponse(delete, deleteResponse); + success.getTrafficControllerPoolApiForZone("jclouds.org.").delete("04053D8E57C7931F"); + } + + HttpResponse poolDoesntExist = HttpResponse.builder().message("Server Epoolor").statusCode(500) + .payload(payloadFromResource("/lbpool_doesnt_exist.xml")).build(); + + public void testDeleteWhenResponseRRNotFound() { + UltraDNSWSApi notFound = requestSendsResponse(delete, poolDoesntExist); + notFound.getTrafficControllerPoolApiForZone("jclouds.org.").delete("04053D8E57C7931F"); + } } diff --git a/providers/ultradns-ws/src/test/java/org/jclouds/ultradns/ws/features/TrafficControllerPoolApiLiveTest.java b/providers/ultradns-ws/src/test/java/org/jclouds/ultradns/ws/features/TrafficControllerPoolApiLiveTest.java index 8833b53e23..1759c40689 100644 --- a/providers/ultradns-ws/src/test/java/org/jclouds/ultradns/ws/features/TrafficControllerPoolApiLiveTest.java +++ b/providers/ultradns-ws/src/test/java/org/jclouds/ultradns/ws/features/TrafficControllerPoolApiLiveTest.java @@ -19,31 +19,43 @@ package org.jclouds.ultradns.ws.features; import static com.google.common.base.Preconditions.checkNotNull; +import static java.util.logging.Logger.getAnonymousLogger; +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertFalse; import static org.testng.Assert.assertTrue; +import static org.testng.Assert.fail; import org.jclouds.rest.ResourceNotFoundException; +import org.jclouds.ultradns.ws.UltraDNSWSExceptions.ResourceAlreadyExistsException; import org.jclouds.ultradns.ws.domain.Account; import org.jclouds.ultradns.ws.domain.TrafficControllerPool; import org.jclouds.ultradns.ws.domain.TrafficControllerPoolRecord; import org.jclouds.ultradns.ws.domain.TrafficControllerPoolRecord.Status; import org.jclouds.ultradns.ws.domain.Zone; import org.jclouds.ultradns.ws.internal.BaseUltraDNSWSApiLiveTest; +import org.testng.annotations.AfterClass; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; +import com.google.common.base.Optional; +import com.google.common.base.Predicate; + /** * @author Adrian Cole */ @Test(groups = "live", singleThreaded = true, testName = "TrafficControllerPoolApiLiveTest") public class TrafficControllerPoolApiLiveTest extends BaseUltraDNSWSApiLiveTest { + private String zoneName = System.getProperty("user.name").replace('.', '-') + ".tcpool.ultradnstest.jclouds.org."; private Account account; @Override @BeforeClass(groups = { "integration", "live" }) public void setupContext() { super.setupContext(); + context.getApi().getZoneApi().delete(zoneName); account = context.getApi().getCurrentAccount(); + context.getApi().getZoneApi().createInAccount(zoneName, account.getId()); } private void checkTCPool(TrafficControllerPool pool) { @@ -90,7 +102,55 @@ public class TrafficControllerPoolApiLiveTest extends BaseUltraDNSWSApiLiveTest api("AAAAAAAAAAAAAAAA").list(); } + @Test + public void testDeleteWhenNotFound() { + api(zoneName).delete("06063D9C54C5AE09"); + } + + String hostname = "www.tcpool." + zoneName; + String poolId; + + @Test + public void testCreatePool() { + poolId = api(zoneName).createPoolForHostname("pool", hostname); + getAnonymousLogger().info("created tc pool: " + poolId); + try { + api(zoneName).createPoolForHostname("pool", hostname); + fail(); + } catch (ResourceAlreadyExistsException e) { + + } + Optional pool = getPoolById(poolId); + assertTrue(pool.isPresent()); + assertEquals(pool.get().getName(), "pool"); + assertEquals(pool.get().getDName(), hostname); + checkTCPool(pool.get()); + } + + @Test(dependsOnMethods = "testCreatePool") + public void testDeletePool() { + api(zoneName).delete(poolId); + assertFalse(getPoolById(poolId).isPresent()); + } + + protected Optional getPoolById(final String poolId) { + return api(zoneName).list().firstMatch(new Predicate() { + public boolean apply(TrafficControllerPool in) { + return in.getId().equals(poolId); + } + }); + } + private TrafficControllerPoolApi api(String zoneName) { return context.getApi().getTrafficControllerPoolApiForZone(zoneName); } + + @Override + @AfterClass(groups = { "integration", "live" }) + protected void tearDownContext() { + if (poolId != null) + api(zoneName).delete(poolId); + context.getApi().getZoneApi().delete(zoneName); + super.tearDownContext(); + } } diff --git a/providers/ultradns-ws/src/test/resources/create_tcpool.xml b/providers/ultradns-ws/src/test/resources/create_tcpool.xml new file mode 100644 index 0000000000..48fe1bdcc6 --- /dev/null +++ b/providers/ultradns-ws/src/test/resources/create_tcpool.xml @@ -0,0 +1 @@ +identitycredentialjclouds.org.foowww.jclouds.org.1EnabledEnabled0 \ No newline at end of file diff --git a/providers/ultradns-ws/src/test/resources/tcpool_created.xml b/providers/ultradns-ws/src/test/resources/tcpool_created.xml new file mode 100644 index 0000000000..e052d2a0a2 --- /dev/null +++ b/providers/ultradns-ws/src/test/resources/tcpool_created.xml @@ -0,0 +1,5 @@ + + + 060339AA0417567A + + \ No newline at end of file From eb1d4710ec64b3f8850b12b93f76619bd8b1d9dc Mon Sep 17 00:00:00 2001 From: adriancole Date: Sat, 23 Mar 2013 18:49:43 -0700 Subject: [PATCH 3/3] create and delete traffic controller records in ultradns --- .../ws/features/TrafficControllerPoolApi.java | 44 ++++++++--- .../TrafficControllerPoolAsyncApi.java | 20 +++++ .../jclouds/ultradns/ws/xml/IDHandler.java | 6 ++ .../TrafficControllerPoolApiExpectTest.java | 22 ++++++ .../TrafficControllerPoolApiLiveTest.java | 74 +++++++++++++++++++ .../src/test/resources/create_tcrecord.xml | 1 + .../resources/tcrecord_already_exists.xml | 1 + .../src/test/resources/tcrecord_created.xml | 1 + 8 files changed, 160 insertions(+), 9 deletions(-) create mode 100644 providers/ultradns-ws/src/test/resources/create_tcrecord.xml create mode 100644 providers/ultradns-ws/src/test/resources/tcrecord_already_exists.xml create mode 100644 providers/ultradns-ws/src/test/resources/tcrecord_created.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 dfb6789f48..006cecb5b0 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 @@ -20,7 +20,7 @@ package org.jclouds.ultradns.ws.features; import org.jclouds.rest.ResourceNotFoundException; import org.jclouds.ultradns.ws.UltraDNSWSExceptions.ResourceAlreadyExistsException; -import org.jclouds.ultradns.ws.domain.RoundRobinPool; +import org.jclouds.ultradns.ws.domain.ResourceRecord; import org.jclouds.ultradns.ws.domain.TrafficControllerPool; import org.jclouds.ultradns.ws.domain.TrafficControllerPoolRecord; @@ -31,6 +31,15 @@ import com.google.common.collect.FluentIterable; * @author Adrian Cole */ public interface TrafficControllerPoolApi { + + /** + * Returns all traffic controller pools in the zone. + * + * @throws ResourceNotFoundException + * if the zone doesn't exist + */ + FluentIterable list() throws ResourceNotFoundException; + /** * creates a traffic controller pool. * @@ -46,12 +55,12 @@ public interface TrafficControllerPoolApi { String createPoolForHostname(String name, String hostname) throws ResourceAlreadyExistsException; /** - * Returns all traffic controller pools in the zone. + * removes a pool and all its records and probes * - * @throws ResourceNotFoundException - * if the zone doesn't exist + * @param id + * the {@link TrafficControllerPool#getId() id} */ - FluentIterable list() throws ResourceNotFoundException; + void delete(String id); /** * Returns all records in the traffic controller pool. @@ -62,10 +71,27 @@ public interface TrafficControllerPoolApi { FluentIterable listRecords(String poolId) throws ResourceNotFoundException; /** - * removes a pool and all its records and probes + * adds a new record to the pool * - * @param id - * the {@link RoundRobinPool#getId() id} + * @param pointsTo + * the ipv4 address or hostname + * @param lbPoolID + * the pool to add the record to. + * @param ttl + * the {@link ResourceRecord#getTTL ttl} of the record + * @return the {@link TrafficControllerPoolRecord#getId() id} of the new + * record + * @throws ResourceAlreadyExistsException + * if a record already exists with the same attrs */ - void delete(String id); + String addRecordToPoolWithTTL(String pointsTo, String lbPoolID, int ttl) throws ResourceAlreadyExistsException; + + /** + * deletes a specific pooled resource record + * + * @param poolRecordID + * {@see TrafficControllerPoolRecord#getId()} + */ + void deleteRecord(String poolRecordID); + } 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 422b7fde3d..c719f899ef 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 @@ -87,4 +87,24 @@ public interface TrafficControllerPoolAsyncApi { @Payload("{lbPoolID}Yes") @Fallback(VoidOnNotFoundOr404.class) ListenableFuture delete(@PayloadParam("lbPoolID") String id); + + /** + * @see TrafficControllerPoolApi#addRecordToPoolWithTTL + */ + @Named("addPoolRecord") + @POST + @XMLResponseParser(IDHandler.PoolRecord.class) + @Payload("{poolID}{pointsTo}{ttl}") + ListenableFuture addRecordToPoolWithTTL(@PayloadParam("pointsTo") String pointsTo, + @PayloadParam("poolID") String lbPoolID, @PayloadParam("ttl") int ttl) throws ResourceAlreadyExistsException; + + /** + * @see TrafficControllerPoolApi#deleteRecord(String) + */ + @Named("deletePoolRecord") + @POST + @Payload("{poolRecordID}") + @Fallback(VoidOnNotFoundOr404.class) + ListenableFuture deleteRecord(@PayloadParam("poolRecordID") String poolRecordID); + } diff --git a/providers/ultradns-ws/src/main/java/org/jclouds/ultradns/ws/xml/IDHandler.java b/providers/ultradns-ws/src/main/java/org/jclouds/ultradns/ws/xml/IDHandler.java index 2c1cd6ebf2..541bb07baf 100644 --- a/providers/ultradns-ws/src/main/java/org/jclouds/ultradns/ws/xml/IDHandler.java +++ b/providers/ultradns-ws/src/main/java/org/jclouds/ultradns/ws/xml/IDHandler.java @@ -48,6 +48,12 @@ public abstract class IDHandler extends ParseSax.HandlerForGeneratedRequestWithR } } + public static class PoolRecord extends IDHandler { + public PoolRecord() { + super("poolRecordID"); + } + } + private String idElement; private StringBuilder currentText = new StringBuilder(); 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 4bf762ff24..e2288d3cd6 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 @@ -107,4 +107,26 @@ public class TrafficControllerPoolApiExpectTest extends BaseUltraDNSWSApiExpectT UltraDNSWSApi notFound = requestSendsResponse(delete, poolDoesntExist); notFound.getTrafficControllerPoolApiForZone("jclouds.org.").delete("04053D8E57C7931F"); } + + HttpRequest createRecord = HttpRequest.builder().method("POST") + .endpoint("https://ultra-api.ultradns.com:8443/UltraDNS_WS/v01") + .addHeader("Host", "ultra-api.ultradns.com:8443") + .payload(payloadFromResourceWithContentType("/create_tcrecord.xml", "application/xml")).build(); + + HttpResponse createRecordResponse = HttpResponse.builder().statusCode(200) + .payload(payloadFromResourceWithContentType("/tcrecord_created.xml", "application/xml")).build(); + + public void testCreateRecordWhenResponseIs2xx() { + UltraDNSWSApi success = requestSendsResponse(createRecord, createRecordResponse); + assertEquals(success.getTrafficControllerPoolApiForZone("jclouds.org.").addRecordToPoolWithTTL("1.2.3.4", "04053D8E57C7931F", 300), "06063DAC54F8D3D9"); + } + + HttpResponse recordAlreadyCreated = HttpResponse.builder().statusCode(500) + .payload(payloadFromResourceWithContentType("/tcrecord_already_exists.xml", "application/xml")).build(); + + @Test(expectedExceptions = ResourceAlreadyExistsException.class, expectedExceptionsMessageRegExp = "Resource Record of type 1 with these attributes already exists in the system.") + public void testCreateWhenResponseError1802() { + UltraDNSWSApi already = requestSendsResponse(createRecord, recordAlreadyCreated); + already.getTrafficControllerPoolApiForZone("jclouds.org.").addRecordToPoolWithTTL("1.2.3.4", "04053D8E57C7931F", 300); + } } diff --git a/providers/ultradns-ws/src/test/java/org/jclouds/ultradns/ws/features/TrafficControllerPoolApiLiveTest.java b/providers/ultradns-ws/src/test/java/org/jclouds/ultradns/ws/features/TrafficControllerPoolApiLiveTest.java index 1759c40689..103cb2a699 100644 --- a/providers/ultradns-ws/src/test/java/org/jclouds/ultradns/ws/features/TrafficControllerPoolApiLiveTest.java +++ b/providers/ultradns-ws/src/test/java/org/jclouds/ultradns/ws/features/TrafficControllerPoolApiLiveTest.java @@ -35,6 +35,7 @@ import org.jclouds.ultradns.ws.domain.Zone; import org.jclouds.ultradns.ws.internal.BaseUltraDNSWSApiLiveTest; import org.testng.annotations.AfterClass; import org.testng.annotations.BeforeClass; +import org.testng.annotations.DataProvider; import org.testng.annotations.Test; import com.google.common.base.Optional; @@ -127,7 +128,80 @@ public class TrafficControllerPoolApiLiveTest extends BaseUltraDNSWSApiLiveTest checkTCPool(pool.get()); } + @DataProvider(name = "records") + public Object[][] createRecords() { + Object[][] records = new Object[2][2]; + records[0][0] = "1.2.3.4"; + records[0][1] = "A"; + records[1][0] = "5.6.7.8"; + records[1][1] = "A"; + return records; + } + + @Test(dependsOnMethods = "testCreatePool", dataProvider = "records") + public void addRecordsToPool(final String pointsTo, final String type) { + final String record = api(zoneName).addRecordToPoolWithTTL(pointsTo, poolId, 30); + + getAnonymousLogger().info("created " + type + " record: " + record); + + assertTrue(api(zoneName).listRecords(poolId).anyMatch(new Predicate() { + public boolean apply(TrafficControllerPoolRecord in) { + return record.equals(in.getId()) && pointsTo.equals(in.getPointsTo()) && type.equals(in.getType()); + } + })); + } + + String cname1; + String cname2; + @Test(dependsOnMethods = "testCreatePool") + public void addCNAMERecordsToPool() { + cname1 = api(zoneName).addRecordToPoolWithTTL("www.foo.com.", poolId, 30); + + getAnonymousLogger().info("created CNAME record: " + cname1); + + assertTrue(api(zoneName).listRecords(poolId).anyMatch(new Predicate() { + public boolean apply(TrafficControllerPoolRecord in) { + return cname1.equals(in.getId()) && "www.foo.com.".equals(in.getPointsTo()) && "CNAME".equals(in.getType()); + } + })); + + try { + api(zoneName).addRecordToPoolWithTTL("www.foo.com.", poolId, 30); + fail(); + } catch (ResourceAlreadyExistsException e) { + + } + + cname2 = api(zoneName).addRecordToPoolWithTTL("www.bar.com.", poolId, 30); + + getAnonymousLogger().info("created CNAME record: " + cname2); + + assertTrue(api(zoneName).listRecords(poolId).anyMatch(new Predicate() { + public boolean apply(TrafficControllerPoolRecord in) { + return cname2.equals(in.getId()) && "www.bar.com.".equals(in.getPointsTo()) && "CNAME".equals(in.getType()); + } + })); + + } + + @Test(dependsOnMethods = "addCNAMERecordsToPool") + public void testDeleteRecord() { + api(zoneName).deleteRecord(cname1); + assertTrue(api(zoneName).listRecords(poolId).anyMatch(new Predicate() { + public boolean apply(TrafficControllerPoolRecord in) { + return cname2.equals(in.getId()); + } + })); + + assertFalse(api(zoneName).listRecords(poolId).anyMatch(new Predicate() { + public boolean apply(TrafficControllerPoolRecord in) { + return cname1.equals(in.getId()); + } + })); + } + + @Test(dependsOnMethods = "testDeleteRecord") public void testDeletePool() { api(zoneName).delete(poolId); assertFalse(getPoolById(poolId).isPresent()); diff --git a/providers/ultradns-ws/src/test/resources/create_tcrecord.xml b/providers/ultradns-ws/src/test/resources/create_tcrecord.xml new file mode 100644 index 0000000000..c7e3bb6666 --- /dev/null +++ b/providers/ultradns-ws/src/test/resources/create_tcrecord.xml @@ -0,0 +1 @@ +identitycredential04053D8E57C7931F1.2.3.4300 \ No newline at end of file diff --git a/providers/ultradns-ws/src/test/resources/tcrecord_already_exists.xml b/providers/ultradns-ws/src/test/resources/tcrecord_already_exists.xml new file mode 100644 index 0000000000..484a7ec607 --- /dev/null +++ b/providers/ultradns-ws/src/test/resources/tcrecord_already_exists.xml @@ -0,0 +1 @@ +soap:ServerFault occurred while processing.2111Resource Record of type 1 with these attributes already exists in the system. \ No newline at end of file diff --git a/providers/ultradns-ws/src/test/resources/tcrecord_created.xml b/providers/ultradns-ws/src/test/resources/tcrecord_created.xml new file mode 100644 index 0000000000..bb98b00fc2 --- /dev/null +++ b/providers/ultradns-ws/src/test/resources/tcrecord_created.xml @@ -0,0 +1 @@ +06063DAC54F8D3D9 \ No newline at end of file