When creating a server, allow attaching server to custom network

https://issues.apache.org/jira/browse/JCLOUDS-202
This commit is contained in:
Zack Shoylev 2013-07-24 11:58:22 -05:00 committed by Everett Toews
parent dae2c67d79
commit a9cb3cf068
2 changed files with 85 additions and 3 deletions

View File

@ -111,6 +111,7 @@ public class CreateServerOptions implements MapBinder {
private List<File> personality = Lists.newArrayList();
private byte[] userData;
private String diskConfig;
private Set<String> networks = ImmutableSet.of();
@Override
public boolean equals(Object object) {
@ -121,7 +122,8 @@ public class CreateServerOptions implements MapBinder {
final CreateServerOptions other = CreateServerOptions.class.cast(object);
return equal(keyName, other.keyName) && equal(securityGroupNames, other.securityGroupNames)
&& equal(metadata, other.metadata) && equal(personality, other.personality)
&& equal(adminPass, other.adminPass) && equal(diskConfig, other.diskConfig);
&& equal(adminPass, other.adminPass) && equal(diskConfig, other.diskConfig)
&& equal(adminPass, other.adminPass) && equal(networks, other.networks);
} else {
return false;
}
@ -129,7 +131,7 @@ public class CreateServerOptions implements MapBinder {
@Override
public int hashCode() {
return Objects.hashCode(keyName, securityGroupNames, metadata, personality, adminPass);
return Objects.hashCode(keyName, securityGroupNames, metadata, personality, adminPass, networks);
}
protected ToStringHelper string() {
@ -146,6 +148,8 @@ public class CreateServerOptions implements MapBinder {
if (diskConfig != null)
toString.add("diskConfig", diskConfig);
toString.add("userData", userData == null ? null : new String(userData));
if (!networks.isEmpty())
toString.add("networks", networks);
return toString;
}
@ -167,6 +171,7 @@ public class CreateServerOptions implements MapBinder {
String user_data;
@Named("OS-DCF:diskConfig")
String diskConfig;
Set<Map<String, String>> networks;
private ServerRequest(String name, String imageRef, String flavorRef) {
this.name = name;
@ -198,10 +203,18 @@ public class CreateServerOptions implements MapBinder {
if (adminPass != null) {
server.adminPass = adminPass;
}
if (diskConfig != null) {
server.diskConfig = diskConfig;
}
if (!networks.isEmpty()) {
server.networks = Sets.newLinkedHashSet(); // ensures ordering is preserved - helps testing and more intuitive for users.
for (String network : networks) {
server.networks.add(ImmutableMap.of("uuid", network));
}
}
return bindToRequest(request, ImmutableMap.of("server", server));
}
@ -218,7 +231,6 @@ public class CreateServerOptions implements MapBinder {
}
}
/**
* You may further customize a cloud server by injecting data into the file
* system of the cloud server itself. This is useful, for example, for
@ -314,6 +326,17 @@ public class CreateServerOptions implements MapBinder {
/**
*
* Get custom networks specified for the server.
* @return A set of uuids defined by Neutron (previously Quantum)
* @see <a href="https://wiki.openstack.org/wiki/Neutron/APIv2-specification#Network">Neutron Networks<a/>
*
*/
public Set<String> getNetworks() {
return networks;
}
/**
*
* @see #getSecurityGroupNames
*/
public CreateServerOptions securityGroupNames(String... securityGroupNames) {
@ -353,6 +376,24 @@ public class CreateServerOptions implements MapBinder {
return this;
}
/**
*
* @see #getNetworks
*/
public CreateServerOptions networks(String... networks) {
return networks(ImmutableSet.copyOf(networks));
}
/**
* @see #getNetworks
*/
public CreateServerOptions networks(Iterable<String> networks) {
for (String network : checkNotNull(networks, "networks"))
checkNotNull(emptyToNull(network), "all networks must be non-empty");
this.networks = ImmutableSet.copyOf(networks);
return this;
}
public static class Builder {
/**
@ -407,6 +448,22 @@ public class CreateServerOptions implements MapBinder {
CreateServerOptions options = new CreateServerOptions();
return CreateServerOptions.class.cast(options.diskConfig(diskConfig));
}
/**
* @see CreateServerOptions#getNetworks
*/
public static CreateServerOptions networks(String... networks) {
CreateServerOptions options = new CreateServerOptions();
return CreateServerOptions.class.cast(options.networks(networks));
}
/**
* @see CreateServerOptions#getNetworks
*/
public static CreateServerOptions networks(Iterable<String> networks) {
CreateServerOptions options = new CreateServerOptions();
return CreateServerOptions.class.cast(options.networks(networks));
}
}
@Override

View File

@ -128,6 +128,31 @@ public class ServerApiExpectTest extends BaseNovaApiExpectTest {
new ParseCreatedServerTest().expected().toString());
}
public void testCreateServerWithNetworksWhenResponseIs202() throws Exception {
HttpRequest createServer = HttpRequest
.builder()
.method("POST")
.endpoint("https://az-1.region-a.geo-1.compute.hpcloudsvc.com/v1.1/3456/servers")
.addHeader("Accept", "application/json")
.addHeader("X-Auth-Token", authToken)
.payload(payloadFromStringWithContentType(
"{\"server\":{\"name\":\"test-e92\",\"imageRef\":\"1241\",\"flavorRef\":\"100\",\"networks\":[{\"uuid\":\"b3856ac0-f481-11e2-b778-0800200c9a66\"},{\"uuid\":\"bf0f0f90-f481-11e2-b778-0800200c9a66\"}]}}","application/json"))
.build();
HttpResponse createServerResponse = HttpResponse.builder().statusCode(202).message("HTTP/1.1 202 Accepted")
.payload(payloadFromResourceWithContentType("/new_server.json","application/json; charset=UTF-8")).build();
NovaApi apiWithNewServer = requestsSendResponses(keystoneAuthWithUsernameAndPasswordAndTenantName,
responseWithKeystoneAccess, createServer, createServerResponse);
assertEquals(apiWithNewServer.getServerApiForZone("az-1.region-a.geo-1").create("test-e92", "1241",
"100", new CreateServerOptions().networks("b3856ac0-f481-11e2-b778-0800200c9a66", "bf0f0f90-f481-11e2-b778-0800200c9a66")).toString(),
new ParseCreatedServerTest().expected().toString());
}
public void testCreateServerWithDiskConfigAuto() throws Exception {
HttpRequest createServer = HttpRequest