Issue 112: added getIpAddressesForNetwork

git-svn-id: http://jclouds.googlecode.com/svn/trunk@2492 3d8758e0-26b5-11de-8745-db77d3ebf521
This commit is contained in:
adrian.f.cole 2009-12-21 05:10:30 +00:00
parent bdb38fdf23
commit 29e92bfe60
9 changed files with 366 additions and 26 deletions

View File

@ -23,6 +23,7 @@
*/
package org.jclouds.vcloud.terremark;
import static javax.ws.rs.core.MediaType.APPLICATION_XML;
import static org.jclouds.vcloud.VCloudMediaType.VAPP_XML;
import static org.jclouds.vcloud.VCloudMediaType.VDC_XML;
@ -37,7 +38,6 @@ import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import org.jclouds.rest.annotations.Endpoint;
import org.jclouds.rest.annotations.ExceptionParser;
@ -56,6 +56,7 @@ import org.jclouds.vcloud.terremark.binders.TerremarkBindInstantiateVAppTemplate
import org.jclouds.vcloud.terremark.domain.ComputeOptions;
import org.jclouds.vcloud.terremark.domain.CustomizationParameters;
import org.jclouds.vcloud.terremark.domain.InternetService;
import org.jclouds.vcloud.terremark.domain.IpAddress;
import org.jclouds.vcloud.terremark.domain.Node;
import org.jclouds.vcloud.terremark.domain.Protocol;
import org.jclouds.vcloud.terremark.domain.PublicIpAddress;
@ -67,6 +68,7 @@ import org.jclouds.vcloud.terremark.xml.ComputeOptionsHandler;
import org.jclouds.vcloud.terremark.xml.CustomizationParametersHandler;
import org.jclouds.vcloud.terremark.xml.InternetServiceHandler;
import org.jclouds.vcloud.terremark.xml.InternetServicesHandler;
import org.jclouds.vcloud.terremark.xml.IpAddressesHandler;
import org.jclouds.vcloud.terremark.xml.NodeHandler;
import org.jclouds.vcloud.terremark.xml.NodesHandler;
import org.jclouds.vcloud.terremark.xml.PublicIpAddressesHandler;
@ -104,7 +106,7 @@ public interface TerremarkVCloudAsyncClient extends VCloudAsyncClient {
Future<? extends VDC> getVDC(@PathParam("vDCId") String vDCId);
/**
* @see TerremarkVCloudClient#instantiateVAppTemplate
* @see TerremarkVCloudClient#instantiateVAppTemplateInVDC
*/
@POST
@Endpoint(org.jclouds.vcloud.endpoints.VCloudApi.class)
@ -124,7 +126,7 @@ public interface TerremarkVCloudAsyncClient extends VCloudAsyncClient {
@POST
@Endpoint(org.jclouds.vcloud.endpoints.VCloudApi.class)
@Path("/vdc/{vDCId}/internetServices")
@Produces(MediaType.APPLICATION_XML)
@Produces(APPLICATION_XML)
@XMLResponseParser(InternetServiceHandler.class)
@MapBinder(AddInternetServiceOptions.class)
Future<? extends InternetService> addInternetServiceToVDC(@PathParam("vDCId") String vDCId,
@ -148,7 +150,7 @@ public interface TerremarkVCloudAsyncClient extends VCloudAsyncClient {
@POST
@Endpoint(org.jclouds.vcloud.endpoints.VCloudApi.class)
@Path("/publicIps/{ipId}/InternetServices")
@Produces(MediaType.APPLICATION_XML)
@Produces(APPLICATION_XML)
@XMLResponseParser(InternetServiceHandler.class)
@MapBinder(AddInternetServiceOptions.class)
Future<? extends InternetService> addInternetServiceToExistingIp(
@ -218,7 +220,7 @@ public interface TerremarkVCloudAsyncClient extends VCloudAsyncClient {
@POST
@Endpoint(org.jclouds.vcloud.endpoints.VCloudApi.class)
@Path("/internetServices/{internetServiceId}/nodes")
@Produces(MediaType.APPLICATION_XML)
@Produces(APPLICATION_XML)
@XMLResponseParser(NodeHandler.class)
@MapBinder(AddNodeOptions.class)
Future<? extends Node> addNode(
@ -303,4 +305,15 @@ public interface TerremarkVCloudAsyncClient extends VCloudAsyncClient {
@XMLResponseParser(CustomizationParametersHandler.class)
Future<? extends CustomizationParameters> getCustomizationOptionsOfCatalogItem(
@PathParam("catalogItemId") String catalogItemId);
/**
* @see TerremarkVCloudClient#getIpAddressesForNetwork
*/
@GET
@Endpoint(org.jclouds.vcloud.endpoints.VCloudApi.class)
@Path("/network/{networkId}/ipAddresses")
@Consumes(APPLICATION_XML)
@XMLResponseParser(IpAddressesHandler.class)
Future<? extends SortedSet<IpAddress>> getIpAddressesForNetwork(
@PathParam("networkId") String networkId);
}

View File

@ -33,6 +33,7 @@ import org.jclouds.vcloud.options.InstantiateVAppTemplateOptions;
import org.jclouds.vcloud.terremark.domain.ComputeOptions;
import org.jclouds.vcloud.terremark.domain.CustomizationParameters;
import org.jclouds.vcloud.terremark.domain.InternetService;
import org.jclouds.vcloud.terremark.domain.IpAddress;
import org.jclouds.vcloud.terremark.domain.Node;
import org.jclouds.vcloud.terremark.domain.Protocol;
import org.jclouds.vcloud.terremark.domain.PublicIpAddress;
@ -134,4 +135,6 @@ public interface TerremarkVCloudClient extends VCloudClient {
SortedSet<Node> getNodes(int internetServiceId);
SortedSet<IpAddress> getIpAddressesForNetwork(String networkId);
}

View File

@ -0,0 +1,128 @@
/**
*
* Copyright (C) 2009 Cloud Conscious, LLC. <info@cloudconscious.com>
*
* ====================================================================
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF 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.vcloud.terremark.domain;
import static com.google.common.base.Preconditions.checkNotNull;
import java.net.InetAddress;
import javax.annotation.Nullable;
import com.google.common.base.CaseFormat;
/**
* @author Adrian Cole
*/
public class IpAddress implements Comparable<IpAddress> {
public static enum Status {
AVAILABLE, ASSIGNED;
public String value() {
return CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, name());
}
@Override
public String toString() {
return value();
}
public static Status fromValue(String status) {
return valueOf(CaseFormat.UPPER_CAMEL.to(CaseFormat.UPPER_UNDERSCORE, checkNotNull(status,
"status")));
}
}
private final InetAddress address;
private final Status status;
@Nullable
private final String server;
public IpAddress(InetAddress address, Status status, String server) {
this.address = address;
this.status = status;
this.server = server;
}
public InetAddress getAddress() {
return address;
}
public Status getStatus() {
return status;
}
public String getServer() {
return server;
}
@Override
public String toString() {
return "IpAddress [address=" + address + ", server=" + server + ", status=" + status + "]";
}
@Override
public int compareTo(IpAddress o) {
return (this == o) ? 0 : getAddress().getHostAddress().compareTo(
o.getAddress().getHostAddress());
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((address == null) ? 0 : address.hashCode());
result = prime * result + ((server == null) ? 0 : server.hashCode());
result = prime * result + ((status == null) ? 0 : status.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
IpAddress other = (IpAddress) obj;
if (address == null) {
if (other.address != null)
return false;
} else if (!address.equals(other.address))
return false;
if (server == null) {
if (other.server != null)
return false;
} else if (!server.equals(other.server))
return false;
if (status == null) {
if (other.status != null)
return false;
} else if (!status.equals(other.status))
return false;
return true;
}
}

View File

@ -0,0 +1,112 @@
/**
*
* Copyright (C) 2009 Cloud Conscious, LLC. <info@cloudconscious.com>
*
* ====================================================================
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF 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.vcloud.terremark.xml;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.SortedSet;
import javax.annotation.Nullable;
import javax.annotation.Resource;
import org.jclouds.http.functions.ParseSax;
import org.jclouds.logging.Logger;
import org.jclouds.vcloud.terremark.domain.IpAddress;
import org.jclouds.vcloud.terremark.domain.IpAddress.Status;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import com.google.common.collect.Sets;
/**
* @author Adrian Cole
*/
public class IpAddressesHandler extends ParseSax.HandlerWithResult<SortedSet<IpAddress>> {
protected StringBuilder currentText = new StringBuilder();
@Resource
protected Logger logger = Logger.NULL;
private SortedSet<IpAddress> addresses = Sets.newTreeSet();
private InetAddress address;
private Status status;
@Nullable
private String server;
private boolean skip;
public SortedSet<IpAddress> getResult() {
return addresses;
}
public void startElement(String uri, String localName, String qName, Attributes attributes)
throws SAXException {
if (attributes.getIndex("xsi:nil") != -1) {
skip = true;
return;
} else {
skip = false;
}
}
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
if (qName.equals("Name")) {
address = parseInetAddress(currentOrNull());
} else if (qName.equals("Status")) {
status = IpAddress.Status.fromValue(currentOrNull());
} else if (!skip && qName.equals("Server")) {
server = currentOrNull();
} else if (qName.equals("IpAddress")) {
addresses.add(new IpAddress(address, status, server));
address = null;
status = null;
server = null;
}
currentText = new StringBuilder();
}
public void characters(char ch[], int start, int length) {
currentText.append(ch, start, length);
}
protected String currentOrNull() {
String returnVal = currentText.toString().trim();
return returnVal.equals("") ? null : returnVal;
}
private InetAddress parseInetAddress(String string) {
String[] byteStrings = string.split("\\.");
byte[] bytes = new byte[4];
for (int i = 0; i < 4; i++) {
bytes[i] = (byte) Integer.parseInt(byteStrings[i]);
}
try {
return InetAddress.getByAddress(bytes);
} catch (UnknownHostException e) {
logger.warn(e, "error parsing ipAddress", currentText);
}
return null;
}
}

View File

@ -24,9 +24,7 @@
package org.jclouds.vcloud.terremark.xml;
import java.net.InetAddress;
import java.net.URI;
import java.net.UnknownHostException;
import java.util.Map;
import java.util.SortedSet;
import javax.annotation.Resource;
@ -38,8 +36,6 @@ import org.jclouds.vcloud.VCloudMediaType;
import org.jclouds.vcloud.domain.NamedResource;
import org.jclouds.vcloud.domain.ResourceAllocation;
import org.jclouds.vcloud.domain.VAppStatus;
import org.jclouds.vcloud.domain.internal.NamedResourceImpl;
import org.jclouds.vcloud.endpoints.internal.VAppRoot;
import org.jclouds.vcloud.terremark.domain.TerremarkVApp;
import org.jclouds.vcloud.terremark.domain.TerremarkVirtualSystem;
import org.jclouds.vcloud.terremark.domain.internal.TerremarkVAppImpl;
@ -82,14 +78,11 @@ public class TerremarkVAppHandler extends ParseSax.HandlerWithResult<TerremarkVA
private String operatingSystemDescription;
private boolean inOs;
private String networkName;
@Inject
@VAppRoot
private String vAppRoot;
public TerremarkVApp getResult() {
return new TerremarkVAppImpl(vApp.getId(), vApp.getName(), vApp.getType(), vApp.getLocation(), status,
size, vDC, computeOptions, customizationOptions, networkToAddresses,
operatingSystemDescription, system, allocations);
return new TerremarkVAppImpl(vApp.getId(), vApp.getName(), vApp.getType(),
vApp.getLocation(), status, size, vDC, computeOptions, customizationOptions,
networkToAddresses, operatingSystemDescription, system, allocations);
}
public void startElement(String uri, String localName, String qName, Attributes attributes)
@ -110,7 +103,7 @@ public class TerremarkVAppHandler extends ParseSax.HandlerWithResult<TerremarkVA
this.customizationOptions = Utils.newNamedResource(attributes);
}
} else if (qName.equals("VApp")) {
vApp = newNamedResource(attributes);
vApp = Utils.newNamedResource(attributes);
status = VAppStatus.fromValue(attributes.getValue(attributes.getIndex("status")));
size = Integer.parseInt(attributes.getValue(attributes.getIndex("size")));
} else if (qName.equals("OperatingSystemSection")) {
@ -166,14 +159,4 @@ public class TerremarkVAppHandler extends ParseSax.HandlerWithResult<TerremarkVA
return null;
}
public NamedResource newNamedResource(Attributes attributes) {
return new NamedResourceImpl(attributes.getValue(attributes.getIndex("href")).replace(
vAppRoot + "/", ""), attributes.getValue(attributes.getIndex("name")), attributes
.getValue(attributes.getIndex("type")), URI.create(attributes.getValue(attributes
.getIndex("href"))));
}
public void putNamedResource(Map<String, NamedResource> map, Attributes attributes) {
map.put(attributes.getValue(attributes.getIndex("name")), newNamedResource(attributes));
}
}

View File

@ -63,6 +63,7 @@ import org.jclouds.vcloud.terremark.xml.ComputeOptionsHandler;
import org.jclouds.vcloud.terremark.xml.CustomizationParametersHandler;
import org.jclouds.vcloud.terremark.xml.InternetServiceHandler;
import org.jclouds.vcloud.terremark.xml.InternetServicesHandler;
import org.jclouds.vcloud.terremark.xml.IpAddressesHandler;
import org.jclouds.vcloud.terremark.xml.NodeHandler;
import org.jclouds.vcloud.terremark.xml.NodesHandler;
import org.jclouds.vcloud.terremark.xml.TerremarkVAppHandler;
@ -82,6 +83,23 @@ import com.google.inject.TypeLiteral;
@Test(groups = "unit", sequential = true, testName = "vcloud.TerremarkVCloudAsyncClientTest")
public class TerremarkVCloudAsyncClientTest extends RestClientTest<TerremarkVCloudAsyncClient> {
public void testGetIpAddressesForNetwork() throws SecurityException, NoSuchMethodException, IOException {
Method method = TerremarkVCloudAsyncClient.class.getMethod("getIpAddressesForNetwork", String.class);
GeneratedHttpRequest<TerremarkVCloudAsyncClient> httpMethod = processor.createRequest(method, "2");
assertRequestLineEquals(httpMethod, "GET http://vcloud/network/2/ipAddresses HTTP/1.1");
assertHeadersEqual(httpMethod, "Accept: application/xml\n");
assertPayloadEquals(httpMethod, null);
assertResponseParserClassEquals(method, httpMethod, ParseSax.class);
assertSaxResponseParserClassEquals(method, IpAddressesHandler.class);
assertExceptionParserClassEquals(method, null);
checkFilters(httpMethod);
}
public void testGetDefaultVDC() throws SecurityException, NoSuchMethodException, IOException {
Method method = TerremarkVCloudAsyncClient.class.getMethod("getDefaultVDC");
GeneratedHttpRequest<TerremarkVCloudAsyncClient> httpMethod = processor.createRequest(method);

View File

@ -54,11 +54,13 @@ import org.jclouds.vcloud.domain.NamedResource;
import org.jclouds.vcloud.domain.ResourceType;
import org.jclouds.vcloud.domain.Task;
import org.jclouds.vcloud.domain.VAppStatus;
import org.jclouds.vcloud.domain.VDC;
import org.jclouds.vcloud.options.CloneVAppOptions;
import org.jclouds.vcloud.predicates.TaskSuccess;
import org.jclouds.vcloud.terremark.domain.ComputeOptions;
import org.jclouds.vcloud.terremark.domain.CustomizationParameters;
import org.jclouds.vcloud.terremark.domain.InternetService;
import org.jclouds.vcloud.terremark.domain.IpAddress;
import org.jclouds.vcloud.terremark.domain.Node;
import org.jclouds.vcloud.terremark.domain.Protocol;
import org.jclouds.vcloud.terremark.domain.PublicIpAddress;
@ -96,6 +98,17 @@ public class TerremarkVCloudClientLiveTest extends VCloudClientLiveTest {
public static final String PREFIX = System.getProperty("user.name") + "-terremark";
@Test
public void testGetIpAddressesForNetwork() throws Exception {
VDC response = tmClient.getDefaultVDC();
for (NamedResource resource : response.getAvailableNetworks().values()) {
if (resource.getType().equals(VCloudMediaType.NETWORK_XML)) {
SortedSet<IpAddress> addresses = tmClient.getIpAddressesForNetwork(resource.getId());
assertNotNull(addresses);
}
}
}
@Test
public void testGetAllInternetServices() throws Exception {
for (InternetService service : tmClient.getAllInternetServicesInVDC(tmClient.getDefaultVDC()

View File

@ -0,0 +1,57 @@
/**
*
* Copyright (C) 2009 Cloud Conscious, LLC. <info@cloudconscious.com>
*
* ====================================================================
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF 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.vcloud.terremark.xml;
import static org.testng.Assert.assertEquals;
import java.io.InputStream;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.SortedSet;
import org.jclouds.http.functions.BaseHandlerTest;
import org.jclouds.vcloud.terremark.domain.IpAddress;
import org.jclouds.vcloud.terremark.domain.IpAddress.Status;
import org.testng.annotations.Test;
import com.google.common.collect.ImmutableSortedSet;
/**
* Tests behavior of {@code IpAddressesHandler}
*
* @author Adrian Cole
*/
@Test(groups = "unit", testName = "vcloud.IpAddressesHandlerTest")
public class IpAddressesHandlerTest extends BaseHandlerTest {
public void test1() throws UnknownHostException {
InputStream is = getClass().getResourceAsStream("/terremark/ipAddresses.xml");
SortedSet<IpAddress> result = factory.create(injector.getInstance(IpAddressesHandler.class))
.parse(is);
assertEquals(result, ImmutableSortedSet.of(new IpAddress(InetAddress
.getByName("10.114.34.131"), Status.ASSIGNED, "testforjcloud2"), new IpAddress(
InetAddress.getByName("10.114.34.132"), Status.AVAILABLE, null)));
}
}

View File

@ -0,0 +1,13 @@
<IpAddresses xmlns="urn:tmrk:vCloudExpress-1.0"
xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<IpAddress>
<Name>10.114.34.131</Name>
<Status>Assigned</Status>
<Server>testforjcloud2</Server>
</IpAddress>
<IpAddress>
<Name>10.114.34.132</Name>
<Status>Available</Status>
<Server i:nil="true" />
</IpAddress>
</IpAddresses>