Merge pull request #381 from danikov/vclouds-director-network

Issue 830 vCloud director Network
This commit is contained in:
Adrian Cole 2012-02-21 04:30:27 -08:00
commit 9f7d6cfeb7
6 changed files with 225 additions and 108 deletions

View File

@ -51,7 +51,7 @@ public class VCloudDirectorMediaType {
public static final String TASK = "application/vnd.vmware.vcloud.task+xml";
public static final String NETWORK = "application/vnd.vmware.vcloud.entity.network+xml";
public static final String NETWORK = "application/vnd.vmware.vcloud.network+xml";
public static final String ORG_NETWORK = "application/vnd.vmware.vcloud.orgNetwork+xml";
@ -75,7 +75,7 @@ public class VCloudDirectorMediaType {
public static final List<String> ALL = Arrays.asList(
SESSION, ERROR, ORG_LIST, METADATA, METADATA_ENTRY,
METADATA_VALUE, ORG, TASKS_LIST, TASK, ORG_NETWORK,
METADATA_VALUE, ORG, TASKS_LIST, TASK, NETWORK, ORG_NETWORK,
CATALOG, CATALOG_ITEM, CATALOG_ITEMS, CATALOGS_LIST, PROPERTY,
MEDIA, OWNER, VDC, ADMIN_USER
);

View File

@ -28,9 +28,9 @@ import org.jclouds.rest.annotations.ExceptionParser;
import org.jclouds.rest.annotations.JAXBResponseParser;
import org.jclouds.rest.annotations.RequestFilters;
import org.jclouds.vcloud.director.v1_5.domain.Metadata;
import org.jclouds.vcloud.director.v1_5.domain.MetadataEntry;
import org.jclouds.vcloud.director.v1_5.domain.MetadataValue;
import org.jclouds.vcloud.director.v1_5.domain.OrgNetwork;
import org.jclouds.vcloud.director.v1_5.domain.ReferenceType;
import org.jclouds.vcloud.director.v1_5.domain.Reference;
import org.jclouds.vcloud.director.v1_5.filters.AddVCloudAuthorizationToRequest;
import org.jclouds.vcloud.director.v1_5.functions.ReferenceToEndpoint;
import org.jclouds.vcloud.director.v1_5.functions.ThrowVCloudErrorOn4xx;
@ -46,33 +46,33 @@ import com.google.common.util.concurrent.ListenableFuture;
public interface NetworkAsyncClient {
/**
* @see NeworkClient#getNetwork(ReferenceType)
* @see NeworkClient#getNetwork(Reference)
*/
@GET
@Consumes
@JAXBResponseParser
@ExceptionParser(ThrowVCloudErrorOn4xx.class)
ListenableFuture<OrgNetwork> getNetwork(@EndpointParam(parser = ReferenceToEndpoint.class) ReferenceType<?> networkRef);
ListenableFuture<OrgNetwork> getNetwork(@EndpointParam(parser = ReferenceToEndpoint.class) Reference networkRef);
/**
* @see NeworkClient#getMetadata(ReferenceType)
* @see NeworkClient#getMetadata(Reference)
*/
@GET
@Path("/metadata")
@Consumes
@JAXBResponseParser
@ExceptionParser(ThrowVCloudErrorOn4xx.class)
ListenableFuture<Metadata> getMetadata(@EndpointParam(parser = ReferenceToEndpoint.class) ReferenceType<?> networkRef);
ListenableFuture<Metadata> getMetadata(@EndpointParam(parser = ReferenceToEndpoint.class) Reference networkRef);
/**
* @see NeworkClient#getMetadataEntry(ReferenceType, String)
* @see NeworkClient#getMetadataValue(Reference, String)
*/
@GET
@Path("/metadata/{key}")
@Consumes
@JAXBResponseParser
@ExceptionParser(ThrowVCloudErrorOn4xx.class)
ListenableFuture<MetadataEntry> getMetadataEntry(@EndpointParam(parser = ReferenceToEndpoint.class) ReferenceType<?> networkRef ,
ListenableFuture<MetadataValue> getMetadataValue(@EndpointParam(parser = ReferenceToEndpoint.class) Reference networkRef ,
@PathParam("key") String key);
}

View File

@ -22,9 +22,9 @@ import java.util.concurrent.TimeUnit;
import org.jclouds.concurrent.Timeout;
import org.jclouds.vcloud.director.v1_5.domain.Metadata;
import org.jclouds.vcloud.director.v1_5.domain.MetadataEntry;
import org.jclouds.vcloud.director.v1_5.domain.MetadataValue;
import org.jclouds.vcloud.director.v1_5.domain.OrgNetwork;
import org.jclouds.vcloud.director.v1_5.domain.ReferenceType;
import org.jclouds.vcloud.director.v1_5.domain.Reference;
/**
* Provides synchronous access to Network.
@ -42,20 +42,20 @@ public interface NetworkClient {
*
* @return the network or null if not found
*/
OrgNetwork getNetwork(ReferenceType<?> networkRef);
OrgNetwork getNetwork(Reference networkRef);
/**
* Retrieves an list of the network's metadata
*
* @return a list of metadata
*/
Metadata getMetadata(ReferenceType<?> networkRef);
Metadata getMetadata(Reference networkRef);
/**
* Retrieves a metadata entry
* Retrieves a metadata value
*
* @return the metadata entry, or null if not found
* @return the metadata value, or null if not found
*/
MetadataEntry getMetadataEntry(ReferenceType<?> networkRef, String key);
MetadataValue getMetadataValue(Reference networkRef, String key);
}

View File

@ -31,6 +31,7 @@ import org.jclouds.vcloud.director.v1_5.VCloudDirectorMediaType;
import com.google.common.base.Splitter;
import com.google.common.collect.Iterables;
import com.google.common.net.InetAddresses;
/**
* @author grkvlt@apache.org
@ -192,4 +193,122 @@ public class Checks {
assertTrue(Media.ImageType.ALL.contains(imageType),
"The Image type of a Media must be one of the allowed list");
}
public static void checkNetworkType(NetworkType<?> network) {
// Check optional fields
NetworkConfiguration config = network.getConfiguration();
if (config != null) {
checkNetworkConfiguration(config);
}
// Check parent type
checkEntityType(network);
}
public static void checkNetworkConfiguration(NetworkConfiguration config) {
// Check optional fields
if (config.getIpScope() != null) {
checkIpScope(config.getIpScope());
}
if (config.getParentNetwork() != null) {
checkReferenceType(config.getParentNetwork());
}
if (config.getNetworkFeatures() != null) {
checkNetworkFeatures(config.getNetworkFeatures());
}
if (config.getSyslogServerSettings() != null) {
checkSyslogServerSettings(config.getSyslogServerSettings());
}
if (config.getRouterInfo() != null) {
checkRouterInfo(config.getRouterInfo());
}
}
public static void checkIpScope(IpScope ipScope) {
// Check required fields
assertNotNull(ipScope.isInherited(), "isInherited attribute of IpScope must be set");
// Check optional fields
// NOTE dnsSuffix cannot be checked
if (ipScope.getGateway() != null) {
checkIpAddress(ipScope.getGateway());
}
if (ipScope.getNetmask() != null) {
checkIpAddress(ipScope.getNetmask());
}
if (ipScope.getDns1() != null) {
checkIpAddress(ipScope.getDns1());
}
if (ipScope.getDns2() != null) {
checkIpAddress(ipScope.getDns2());
}
if (ipScope.getIpRanges() != null) {
checkIpRanges(ipScope.getIpRanges());
}
if (ipScope.getAllocatedIpAddresses() != null) {
checkIpAddresses(ipScope.getAllocatedIpAddresses());
}
}
public static void checkNetworkFeatures(NetworkFeatures features) {
// Check optional fields
if (features.getNetworkServices() != null) {
for (NetworkService service : features.getNetworkServices()) {
checkNetworkService(service);
}
}
}
public static void checkSyslogServerSettings(SyslogServerSettings settings) {
// Check optional fields
if (settings.getSyslogServerIp1() != null) {
checkIpAddress(settings.getSyslogServerIp1());
}
if (settings.getSyslogServerIp2() != null) {
checkIpAddress(settings.getSyslogServerIp2());
}
}
public static void checkRouterInfo(RouterInfo routerInfo) {
// Check required fields
assertNotNull(routerInfo.getExternalIp(), "The external IP attribute of a Router Info must be set");
checkIpAddress(routerInfo.getExternalIp());
}
public static void checkNetworkService(NetworkService service) {
// NOTE isEnabled cannot be checked
}
public static void checkIpRanges(IpRanges ipRanges) {
// Check optional fields
for (IpRange range : ipRanges.getIpRanges()) {
checkIpRange(range);
}
}
public static void checkIpRange(IpRange range) {
// Check required fields
assertNotNull(range.getStartAddress(), "The start address attribute of an IP Range must be set");
checkIpAddress(range.getStartAddress());
assertNotNull(range.getEndAddress(), "The end address attribute of an IP Range must be set");
checkIpAddress(range.getEndAddress());
}
public static void checkIpAddresses(IpAddresses ipAddresses) {
// Check optional fields
for (String address : ipAddresses.getIpAddresses()) {
checkIpAddress(address);
}
}
public static void checkIpAddress(String ip) {
InetAddresses.isInetAddress(ip);
}
}

View File

@ -181,7 +181,7 @@ public class NetworkClientExpectTest extends BaseVCloudDirectorRestClientExpectT
Reference networkRef = Reference.builder().href(networkUri).build();
assertEquals(client.getNetworkClient().getMetadataEntry(networkRef, "KEY"), expected);
assertEquals(client.getNetworkClient().getMetadataValue(networkRef, "KEY"), expected);
}
public static OrgNetwork orgNetwork() {

View File

@ -18,20 +18,33 @@
*/
package org.jclouds.vcloud.director.v1_5.features;
import static org.jclouds.vcloud.director.v1_5.VCloudDirectorLiveTestConstants.OBJ_FIELD_ATTRB_REQ;
import static org.jclouds.vcloud.director.v1_5.VCloudDirectorLiveTestConstants.OBJ_FIELD_EQ;
import static org.jclouds.vcloud.director.v1_5.VCloudDirectorLiveTestConstants.OBJ_FIELD_REQ_LIVE;
import static org.jclouds.vcloud.director.v1_5.VCloudDirectorLiveTestConstants.OBJ_REQ_LIVE;
import static org.jclouds.vcloud.director.v1_5.VCloudDirectorLiveTestConstants.REF_REQ_LIVE;
import static org.jclouds.vcloud.director.v1_5.domain.Checks.checkResourceType;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.fail;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.assertTrue;
import java.net.URI;
import org.jclouds.vcloud.director.v1_5.VCloudDirectorException;
import org.jclouds.vcloud.director.v1_5.domain.Error;
import org.jclouds.vcloud.director.v1_5.domain.Checks;
import org.jclouds.vcloud.director.v1_5.domain.IpAddresses;
import org.jclouds.vcloud.director.v1_5.domain.Metadata;
import org.jclouds.vcloud.director.v1_5.domain.MetadataEntry;
import org.jclouds.vcloud.director.v1_5.domain.MetadataValue;
import org.jclouds.vcloud.director.v1_5.domain.OrgNetwork;
import org.jclouds.vcloud.director.v1_5.domain.Reference;
import org.jclouds.vcloud.director.v1_5.domain.ReferenceType;
import org.jclouds.vcloud.director.v1_5.internal.BaseVCloudDirectorClientLiveTest;
import org.testng.annotations.BeforeGroups;
import org.testng.annotations.Test;
import com.google.common.collect.Iterables;
/**
* Tests behavior of {@code NetworkClient}
*
@ -40,102 +53,87 @@ import org.testng.annotations.Test;
@Test(groups = { "live", "apitests" }, testName = "NetworkClientLiveTest")
public class NetworkClientLiveTest extends BaseVCloudDirectorClientLiveTest {
public static final String NETWORK = "network";
/*
* Convenience reference to API client.
*/
protected NetworkClient networkClient;
private Reference networkRef;
@BeforeGroups(groups = { "live" }, dependsOnMethods = { "setupClient" })
public void before() {
String networkId = "a604f3c2-0343-453e-ae1f-cddac5b7bd94"; // TODO: inject
networkRef = Reference.builder()
.type("application/vnd.vmware.vcloud.orgNetwork+xml")
.name("")
.href(URI.create(endpoint+"/network/"+networkId))
.id(networkId)
.build();
networkClient = context.getApi().getNetworkClient();
}
@Test(testName = "GET /network/{id}")
public void testWhenResponseIs2xxLoginReturnsValidNetwork() {
Reference networkRef = Reference.builder()
.href(URI.create(endpoint + "/network/" + networkId)).build();
OrgNetwork network = context.getApi().getNetworkClient().getNetwork(networkRef);
//TODO assert network is valid
}
@Test(testName = "GET /network/NOTAUUID", enabled=false)
public void testWhenResponseIs400ForInvalidNetworkId() {
Reference networkRef = Reference.builder()
.href(URI.create(endpoint + "/network/NOTAUUID")).build();
Error expected = Error.builder()
.message("validation error : EntityRef has incorrect type, expected type is com.vmware.vcloud.entity.network.")
.majorErrorCode(400)
.minorErrorCode("BAD_REQUEST")
.build();
try {
context.getApi().getNetworkClient().getNetwork(networkRef);
fail("Should give HTTP 400 error");
} catch (VCloudDirectorException vde) {
assertEquals(vde.getError(), expected);
} catch (Exception e) {
fail("Should have thrown a VCloudDirectorException");
public void testGetNetwork() {
// required for testing
assertNotNull(networkRef, String.format(REF_REQ_LIVE, NETWORK));
OrgNetwork network = networkClient.getNetwork(networkRef);
assertNotNull(network, String.format(OBJ_REQ_LIVE, NETWORK));
assertTrue(!network.getDescription().equals("DO NOT USE"), "Network isn't to be used for testing");
// parent type
Checks.checkNetworkType(network);
// optional
ReferenceType<?> networkPoolRef = network.getNetworkPool();
if (networkPoolRef != null) {
Checks.checkReferenceType(networkPoolRef);
}
}
@Test(testName = "GET /network/{catalog_id}", enabled=false)
public void testWhenResponseIs403ForCatalogIdUsedAsNetworkId() {
String catalogId = "7212e451-76e1-4631-b2de-ba1dfd8080e4";
Reference networkRef = Reference.builder().href(URI.create(endpoint + "/network/" + catalogId)).build();
Error expected = Error.builder()
.message("This operation is denied.")
.majorErrorCode(403)
.minorErrorCode("ACCESS_TO_RESOURCE_IS_FORBIDDEN")
.build();
try {
context.getApi().getNetworkClient().getNetwork(networkRef);
fail("Should give HTTP 403 error");
} catch (VCloudDirectorException vde) {
assertEquals(vde.getError(), expected);
} catch (Exception e) {
fail("Should have thrown a VCloudDirectorException");
}
}
@Test(testName = "GET /network/{fake_id}")
public void testWhenResponseIs403ForFakeNetworkId() {
Reference networkRef = Reference.builder()
.href(URI.create(endpoint + "/network/aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee")).build();
Error expected = Error.builder()
.message("This operation is denied.")
.majorErrorCode(403)
.minorErrorCode("ACCESS_TO_RESOURCE_IS_FORBIDDEN")
.build();
try {
context.getApi().getNetworkClient().getNetwork(networkRef);
fail("Should give HTTP 403 error");
} catch (VCloudDirectorException vde) {
assertEquals(vde.getError(), expected);
} catch (Exception e) {
fail("Should have thrown a VCloudDirectorException");
IpAddresses allowedExternalIpAddresses = network.getAllowedExternalIpAddresses();
if (allowedExternalIpAddresses != null) {
Checks.checkIpAddresses(allowedExternalIpAddresses);
}
}
@Test(testName = "GET /network/{id}/metadata")
public void testWhenResponseIs2xxLoginReturnsValidMetadataList() {
Reference networkRef = Reference.builder()
.href(URI.create(endpoint + "/network/" + networkId)).build();
Metadata expected = context.getApi().getNetworkClient().getMetadata(networkRef);
// assert metadata is valid
// assert has metadata in order to support subsequent test
// assign metadata key (todo- ordering)
public void testGetMetadata() {
Metadata metadata = networkClient.getMetadata(networkRef);
// required for testing
assertFalse(Iterables.isEmpty(metadata.getMetadataEntries()),
String.format(OBJ_FIELD_REQ_LIVE, NETWORK, "metadata.entries"));
// parent type
checkResourceType(metadata);
for (MetadataEntry entry : metadata.getMetadataEntries()) {
// required elements and attributes
assertNotNull(entry.getKey(),
String.format(OBJ_FIELD_ATTRB_REQ, networkClient, "MetadataEntry", entry.getKey(), "key"));
assertNotNull(entry.getValue(),
String.format(OBJ_FIELD_ATTRB_REQ, networkClient, "MetadataEntry", entry.getValue(), "value"));
// parent type
checkResourceType(entry);
}
}
String metadataKey = "key";
//TODO depends on previous
@Test(testName = "GET /network/{id}/metadata", enabled=false)
public void testWhenResponseIs2xxLoginReturnsValidMetadataEntry() {
Reference networkRef = Reference.builder()
.href(URI.create(endpoint + "/network/" + networkId)).build();
MetadataEntry expected = context.getApi().getNetworkClient().getMetadataEntry(networkRef, metadataKey);
// assert metadataEntry is valid
@Test(testName = "GET /network/{id}/metadata/{key}")
public void testGetMetadataValue() {
MetadataValue metadataValue = networkClient.getMetadataValue(networkRef, "key");
// Check parent type
checkResourceType(metadataValue);
// Check required elements and attributes
String value = metadataValue.getValue();
assertNotNull(value,
String.format(OBJ_FIELD_ATTRB_REQ, NETWORK, "MetadataEntry",
metadataValue.toString(), "value"));
assertEquals(value, "value",
String.format(OBJ_FIELD_EQ, NETWORK, "metadataEntry.value", "value", value));
}
}