From 0abfaf5184742fd02a4b598d29d996d31735931e Mon Sep 17 00:00:00 2001 From: adriancole Date: Sun, 7 Apr 2013 21:52:34 -0700 Subject: [PATCH] added ability to read the regions available in ultradns --- .../jclouds/ultradns/ws/UltraDNSWSApi.java | 21 ++- .../jclouds/ultradns/ws/domain/Region.java | 127 ++++++++++++++++++ .../ultradns/ws/xml/RegionListHandler.java | 59 ++++++++ .../ultradns/ws/UltraDNSWSApiExpectTest.java | 18 +++ .../ultradns/ws/UltraDNSWSApiLiveTest.java | 19 +++ .../GetAvailableRegionsResponseTest.java | 65 +++++++++ .../src/test/resources/list_regions.xml | 1 + .../src/test/resources/regions.xml | 14 ++ 8 files changed, 319 insertions(+), 5 deletions(-) create mode 100644 providers/ultradns-ws/src/main/java/org/jclouds/ultradns/ws/domain/Region.java create mode 100644 providers/ultradns-ws/src/main/java/org/jclouds/ultradns/ws/xml/RegionListHandler.java create mode 100644 providers/ultradns-ws/src/test/java/org/jclouds/ultradns/ws/parse/GetAvailableRegionsResponseTest.java create mode 100644 providers/ultradns-ws/src/test/resources/list_regions.xml create mode 100644 providers/ultradns-ws/src/test/resources/regions.xml diff --git a/providers/ultradns-ws/src/main/java/org/jclouds/ultradns/ws/UltraDNSWSApi.java b/providers/ultradns-ws/src/main/java/org/jclouds/ultradns/ws/UltraDNSWSApi.java index 827038d621..2869d26115 100644 --- a/providers/ultradns-ws/src/main/java/org/jclouds/ultradns/ws/UltraDNSWSApi.java +++ b/providers/ultradns-ws/src/main/java/org/jclouds/ultradns/ws/UltraDNSWSApi.java @@ -22,6 +22,7 @@ import java.io.Closeable; import javax.inject.Named; import javax.ws.rs.POST; +import java.util.Map; import org.jclouds.rest.annotations.Delegate; import org.jclouds.rest.annotations.Payload; @@ -30,6 +31,7 @@ import org.jclouds.rest.annotations.RequestFilters; import org.jclouds.rest.annotations.VirtualHost; import org.jclouds.rest.annotations.XMLResponseParser; import org.jclouds.ultradns.ws.domain.Account; +import org.jclouds.ultradns.ws.domain.Region; import org.jclouds.ultradns.ws.features.ResourceRecordApi; import org.jclouds.ultradns.ws.features.RoundRobinPoolApi; import org.jclouds.ultradns.ws.features.TaskApi; @@ -58,13 +60,22 @@ public interface UltraDNSWSApi extends Closeable { Account getCurrentAccount(); /** - * Provides synchronous access to Zone features. + * Lists the directional regions available in the account. + */ + @Named("getAvailableRegions") + @POST + @XMLResponseParser(RegionListHandler.class) + @Payload("") + Map getRegionsById(); + + /** + * Provides access to Zone features. */ @Delegate ZoneApi getZoneApi(); /** - * Provides synchronous access to Resource Record features. + * Provides access to Resource Record features. * * @param zoneName * zoneName including a trailing dot @@ -73,7 +84,7 @@ public interface UltraDNSWSApi extends Closeable { ResourceRecordApi getResourceRecordApiForZone(@PayloadParam("zoneName") String zoneName); /** - * Provides synchronous access to Round Robin Pool features. + * Provides access to Round Robin Pool features. * * @param zoneName * zoneName including a trailing dot @@ -82,7 +93,7 @@ public interface UltraDNSWSApi extends Closeable { RoundRobinPoolApi getRoundRobinPoolApiForZone(@PayloadParam("zoneName") String zoneName); /** - * Provides synchronous access to Traffic Controller Pool features. + * Provides access to Traffic Controller Pool features. * * @param zoneName * zoneName including a trailing dot @@ -91,7 +102,7 @@ public interface UltraDNSWSApi extends Closeable { TrafficControllerPoolApi getTrafficControllerPoolApiForZone(@PayloadParam("zoneName") String zoneName); /** - * Provides synchronous access to Task features. + * Provides access to Task features. */ @Delegate TaskApi getTaskApi(); diff --git a/providers/ultradns-ws/src/main/java/org/jclouds/ultradns/ws/domain/Region.java b/providers/ultradns-ws/src/main/java/org/jclouds/ultradns/ws/domain/Region.java new file mode 100644 index 0000000000..7b0e819837 --- /dev/null +++ b/providers/ultradns-ws/src/main/java/org/jclouds/ultradns/ws/domain/Region.java @@ -0,0 +1,127 @@ +/** + * 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.domain; + +import static com.google.common.base.Objects.equal; +import static com.google.common.base.Objects.toStringHelper; +import static com.google.common.base.Preconditions.checkNotNull; + +import java.util.Set; + +import com.google.common.base.Objects; +import com.google.common.collect.ForwardingSet; +import com.google.common.collect.ImmutableSet; + +/** + * A region is a set of territory names. + * + * @author Adrian Cole + */ +public class Region extends ForwardingSet { + + private final String name; + private final Set territoryNames; + + private Region(String name, Set territoryNames) { + this.name = checkNotNull(name, "name"); + this.territoryNames = checkNotNull(territoryNames, "territoryNames of %s", name); + } + + public String getName() { + return name; + } + + public Set getTerritoryNames() { + return territoryNames; + } + + @Override + protected Set delegate() { + return territoryNames; + } + + @Override + public int hashCode() { + return Objects.hashCode(name, territoryNames); + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null || getClass() != obj.getClass()) + return false; + Region that = Region.class.cast(obj); + return equal(this.name, that.name) && equal(this.territoryNames, that.territoryNames); + } + + @Override + public String toString() { + return toStringHelper("").add("name", name).add("territoryNames", territoryNames).toString(); + } + + public static Builder builder() { + return new Builder(); + } + + public Builder toBuilder() { + return builder().from(this); + } + + public final static class Builder { + private String name; + private ImmutableSet.Builder territoryNames = ImmutableSet. builder(); + + /** + * @see Region#getName() + */ + public Builder name(String name) { + this.name = name; + return this; + } + + /** + * adds to current territoryNames + * + * @see Region#getTerritoryNames() + */ + public Builder addTerritoryName(String territoryName) { + this.territoryNames.add(territoryName); + return this; + } + + /** + * replaces current territoryNames + * + * @see Region#getTerritoryNames() + */ + public Builder territoryNames(Iterable territoryNames) { + this.territoryNames = ImmutableSet. builder().addAll(territoryNames); + return this; + } + + public Region build() { + return new Region(name, territoryNames.build()); + } + + public Builder from(Region in) { + return name(in.getName()).territoryNames(in.getTerritoryNames()); + } + } +} diff --git a/providers/ultradns-ws/src/main/java/org/jclouds/ultradns/ws/xml/RegionListHandler.java b/providers/ultradns-ws/src/main/java/org/jclouds/ultradns/ws/xml/RegionListHandler.java new file mode 100644 index 0000000000..6c47f3bf3e --- /dev/null +++ b/providers/ultradns-ws/src/main/java/org/jclouds/ultradns/ws/xml/RegionListHandler.java @@ -0,0 +1,59 @@ +/** + * 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 org.jclouds.util.SaxUtils.cleanseAttributes; +import static org.jclouds.util.SaxUtils.equalsOrSuffix; + +import java.util.Map; + +import org.jclouds.http.functions.ParseSax; +import org.jclouds.ultradns.ws.domain.Region; +import org.xml.sax.Attributes; + +import com.google.common.base.Splitter; +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.ImmutableMap.Builder; + +/** + * + * @author Adrian Cole + */ +public class RegionListHandler extends ParseSax.HandlerForGeneratedRequestWithResult> { + + private final Builder regions = ImmutableMap. builder(); + + @Override + public Map getResult() { + return regions.build(); + } + + @Override + public void startElement(String url, String name, String qName, Attributes attrs) { + if (equalsOrSuffix(qName, "Region")) { + Map attributes = cleanseAttributes(attrs); + int id = Integer.parseInt(attributes.get("RegionID")); + Iterable territories = Splitter.on(';').split(attributes.get("TerritoryName")); + Region region = Region.builder() + .name(attributes.get("RegionName")) + .territoryNames(territories).build(); + regions.put(id, region); + } + } +} diff --git a/providers/ultradns-ws/src/test/java/org/jclouds/ultradns/ws/UltraDNSWSApiExpectTest.java b/providers/ultradns-ws/src/test/java/org/jclouds/ultradns/ws/UltraDNSWSApiExpectTest.java index 7398d45c39..c216a7209d 100644 --- a/providers/ultradns-ws/src/test/java/org/jclouds/ultradns/ws/UltraDNSWSApiExpectTest.java +++ b/providers/ultradns-ws/src/test/java/org/jclouds/ultradns/ws/UltraDNSWSApiExpectTest.java @@ -26,6 +26,7 @@ import org.jclouds.http.HttpRequest; import org.jclouds.http.HttpResponse; import org.jclouds.ultradns.ws.internal.BaseUltraDNSWSApiExpectTest; import org.jclouds.ultradns.ws.parse.GetAccountsListOfUserResponseTest; +import org.jclouds.ultradns.ws.parse.GetAvailableRegionsResponseTest; import org.testng.annotations.Test; /** @@ -50,4 +51,21 @@ public class UltraDNSWSApiExpectTest extends BaseUltraDNSWSApiExpectTest { success.getCurrentAccount().toString(), new GetAccountsListOfUserResponseTest().expected().toString()); } + + HttpRequest getRegionsById = HttpRequest.builder().method("POST") + .endpoint("https://ultra-api.ultradns.com:8443/UltraDNS_WS/v01") + .addHeader("Host", "ultra-api.ultradns.com:8443") + .payload(payloadFromResourceWithContentType("/list_regions.xml", "application/xml")).build(); + + HttpResponse getRegionsByIdResponse = HttpResponse.builder().statusCode(200) + .payload(payloadFromResourceWithContentType("/regions.xml", "application/xml")).build(); + + public void testGetRegionsByIdWhenResponseIs2xx() { + + UltraDNSWSApi success = requestSendsResponse(getRegionsById, getRegionsByIdResponse); + + assertEquals( + success.getRegionsById().toString(), + new GetAvailableRegionsResponseTest().expected().toString()); + } } diff --git a/providers/ultradns-ws/src/test/java/org/jclouds/ultradns/ws/UltraDNSWSApiLiveTest.java b/providers/ultradns-ws/src/test/java/org/jclouds/ultradns/ws/UltraDNSWSApiLiveTest.java index bfc2f492a6..d6046fd1e7 100644 --- a/providers/ultradns-ws/src/test/java/org/jclouds/ultradns/ws/UltraDNSWSApiLiveTest.java +++ b/providers/ultradns-ws/src/test/java/org/jclouds/ultradns/ws/UltraDNSWSApiLiveTest.java @@ -18,9 +18,14 @@ */ package org.jclouds.ultradns.ws; +import static org.testng.Assert.assertFalse; import static org.testng.Assert.assertNotNull; +import static org.testng.Assert.assertTrue; + +import java.util.Map.Entry; import org.jclouds.ultradns.ws.domain.Account; +import org.jclouds.ultradns.ws.domain.Region; import org.jclouds.ultradns.ws.internal.BaseUltraDNSWSApiLiveTest; import org.testng.annotations.Test; @@ -40,4 +45,18 @@ public class UltraDNSWSApiLiveTest extends BaseUltraDNSWSApiLiveTest { assertNotNull(account.getId(), "Id cannot be null for " + account); assertNotNull(account.getName(), "Name cannot be null for " + account); } + + @Test + public void testListRegions() { + for (Entry region : api.getRegionsById().entrySet()) { + checkRegion(region); + } + } + + private void checkRegion(Entry region) { + assertTrue(region.getKey() > 0, "Id cannot be negative " + region); + assertNotNull(region.getValue().getName(), "Name cannot be null " + region); + assertNotNull(region.getValue().getTerritoryNames(), "TerritoryNames cannot be null " + region); + assertFalse(region.getValue().getTerritoryNames().isEmpty(), "TerritoryNames cannot be empty " + region); + } } diff --git a/providers/ultradns-ws/src/test/java/org/jclouds/ultradns/ws/parse/GetAvailableRegionsResponseTest.java b/providers/ultradns-ws/src/test/java/org/jclouds/ultradns/ws/parse/GetAvailableRegionsResponseTest.java new file mode 100644 index 0000000000..303ab7c640 --- /dev/null +++ b/providers/ultradns-ws/src/test/java/org/jclouds/ultradns/ws/parse/GetAvailableRegionsResponseTest.java @@ -0,0 +1,65 @@ +/** + * 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 java.util.Map; + +import org.jclouds.http.functions.BaseHandlerTest; +import org.jclouds.ultradns.ws.domain.Region; +import org.jclouds.ultradns.ws.xml.RegionListHandler; +import org.testng.annotations.Test; + +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.ImmutableSet; + +/** + * @author Adrian Cole + */ +@Test(testName = "GetAvailableRegionsResponseTest") +public class GetAvailableRegionsResponseTest extends BaseHandlerTest { + + public void test() { + InputStream is = getClass().getResourceAsStream("/regions.xml"); + + Map expected = expected(); + + RegionListHandler handler = injector.getInstance(RegionListHandler.class); + Map result = factory.create(handler).parse(is); + + assertEquals(result.toString(), expected.toString()); + } + + public Map expected() { + return ImmutableMap. builder() + .put(14, Region.builder() + .name("Anonymous Proxy (A1)") + .addTerritoryName("Anonymous Proxy").build()) + .put(3, Region.builder() + .name("Antarctica") + .territoryNames(ImmutableSet. builder() + .add("Antarctica") + .add("Bouvet Island") + .add("French Southern Territories").build()) + .build()) + .build(); + } +} diff --git a/providers/ultradns-ws/src/test/resources/list_regions.xml b/providers/ultradns-ws/src/test/resources/list_regions.xml new file mode 100644 index 0000000000..e87cecb59c --- /dev/null +++ b/providers/ultradns-ws/src/test/resources/list_regions.xml @@ -0,0 +1 @@ +identitycredential \ No newline at end of file diff --git a/providers/ultradns-ws/src/test/resources/regions.xml b/providers/ultradns-ws/src/test/resources/regions.xml new file mode 100644 index 0000000000..d4c646d546 --- /dev/null +++ b/providers/ultradns-ws/src/test/resources/regions.xml @@ -0,0 +1,14 @@ + + + + + + + + + + \ No newline at end of file