Merge pull request #318 from andreisavu/zone-api

Implemented Zone API for Global Admin
This commit is contained in:
Adrian Cole 2012-01-16 13:41:55 -08:00
commit 721c7274a4
15 changed files with 955 additions and 9 deletions

View File

@ -29,6 +29,7 @@ import org.jclouds.cloudstack.features.GlobalOfferingAsyncClient;
import org.jclouds.cloudstack.features.GlobalStoragePoolAsyncClient;
import org.jclouds.cloudstack.features.GlobalUsageAsyncClient;
import org.jclouds.cloudstack.features.GlobalUserAsyncClient;
import org.jclouds.cloudstack.features.GlobalZoneAsyncClient;
import org.jclouds.rest.annotations.Delegate;
/**
@ -107,4 +108,11 @@ public interface CloudStackGlobalAsyncClient extends CloudStackDomainAsyncClient
@Delegate
@Override
GlobalDomainAsyncClient getDomainClient();
/**
* Provides asynchronous access to Zone
*/
@Delegate
@Override
GlobalZoneAsyncClient getZoneClient();
}

View File

@ -30,6 +30,7 @@ import org.jclouds.cloudstack.features.GlobalOfferingClient;
import org.jclouds.cloudstack.features.GlobalStoragePoolClient;
import org.jclouds.cloudstack.features.GlobalUsageClient;
import org.jclouds.cloudstack.features.GlobalUserClient;
import org.jclouds.cloudstack.features.GlobalZoneClient;
import org.jclouds.concurrent.Timeout;
import org.jclouds.rest.annotations.Delegate;
@ -110,4 +111,11 @@ public interface CloudStackGlobalClient extends CloudStackDomainClient {
@Delegate
@Override
GlobalDomainClient getDomainClient();
/**
* Provides synchronous access to Zone
*/
@Delegate
@Override
GlobalZoneClient getZoneClient();
}

View File

@ -70,6 +70,8 @@ import org.jclouds.cloudstack.features.GlobalUsageAsyncClient;
import org.jclouds.cloudstack.features.GlobalUsageClient;
import org.jclouds.cloudstack.features.GlobalUserAsyncClient;
import org.jclouds.cloudstack.features.GlobalUserClient;
import org.jclouds.cloudstack.features.GlobalZoneAsyncClient;
import org.jclouds.cloudstack.features.GlobalZoneClient;
import org.jclouds.cloudstack.features.GuestOSAsyncClient;
import org.jclouds.cloudstack.features.GuestOSClient;
import org.jclouds.cloudstack.features.HypervisorAsyncClient;
@ -131,6 +133,7 @@ public class CloudStackRestClientModule extends RestClientModule<CloudStackClien
public static final Map<Class<?>, Class<?>> DELEGATE_MAP = ImmutableMap.<Class<?>, Class<?>> builder()//
.put(ZoneClient.class, ZoneAsyncClient.class)//
.put(GlobalZoneClient.class, GlobalZoneAsyncClient.class)//
.put(TemplateClient.class, TemplateAsyncClient.class)//
.put(OfferingClient.class, OfferingAsyncClient.class)//
.put(NetworkClient.class, NetworkAsyncClient.class)//

View File

@ -18,6 +18,8 @@
*/
package org.jclouds.cloudstack.domain;
import static com.google.common.base.CaseFormat.UPPER_CAMEL;
import static com.google.common.base.CaseFormat.UPPER_UNDERSCORE;
import static com.google.common.base.Preconditions.checkNotNull;
import javax.annotation.Nullable;
@ -27,9 +29,29 @@ import com.google.common.collect.ImmutableList;
import com.google.gson.annotations.SerializedName;
/**
* @author Adrian Cole
* @author Adrian Cole, Andrei Savu
*/
public class Zone implements Comparable<Zone> {
public static enum AllocationState {
DISABLED,
ENABLED,
UNKNOWN;
public static AllocationState fromValue(String value) {
try{
return valueOf(value.toUpperCase());
} catch (IllegalArgumentException e) {
return UNKNOWN;
}
}
@Override
public String toString() {
return UPPER_UNDERSCORE.to(UPPER_CAMEL, name());
}
}
public static Builder builder() {
return new Builder();
}
@ -47,7 +69,7 @@ public class Zone implements Comparable<Zone> {
private NetworkType networkType;
private String VLAN;
private boolean securityGroupsEnabled;
private String allocationState;
private AllocationState allocationState;
private String dhcpProvider;
private String zoneToken;
@ -112,7 +134,7 @@ public class Zone implements Comparable<Zone> {
}
public Builder allocationState(String allocationState) {
public Builder allocationState(AllocationState allocationState) {
this.allocationState = allocationState;
return this;
}
@ -159,8 +181,7 @@ public class Zone implements Comparable<Zone> {
@SerializedName("securitygroupsenabled")
private boolean securityGroupsEnabled;
@SerializedName("allocationstate")
//TODO Change to enum?
private String allocationState;
private AllocationState allocationState;
@SerializedName("dhcpprovider")
private String dhcpProvider;
@SerializedName("zonetoken")
@ -175,7 +196,7 @@ public class Zone implements Comparable<Zone> {
public Zone(long id, String description, String displayText, List<String> DNS, String domain, long domainId,
String guestCIDRAddress, List<String> internalDNS, String name, NetworkType networkType,
String vLAN, boolean securityGroupsEnabled, String allocationState, String dhcpProvider, String zoneToken) {
String vLAN, boolean securityGroupsEnabled, AllocationState allocationState, String dhcpProvider, String zoneToken) {
this.id = id;
this.description = description;
this.displayText = displayText;
@ -293,7 +314,7 @@ public class Zone implements Comparable<Zone> {
/**
* @return the allocation state of the cluster
*/
public String getAllocationState() {
public AllocationState getAllocationState() {
return allocationState;
}

View File

@ -0,0 +1,81 @@
/**
* 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.cloudstack.features;
import com.google.common.util.concurrent.ListenableFuture;
import org.jclouds.cloudstack.domain.NetworkType;
import org.jclouds.cloudstack.domain.Zone;
import org.jclouds.cloudstack.filters.QuerySigner;
import org.jclouds.cloudstack.options.CreateZoneOptions;
import org.jclouds.cloudstack.options.UpdateZoneOptions;
import org.jclouds.rest.annotations.ExceptionParser;
import org.jclouds.rest.annotations.QueryParams;
import org.jclouds.rest.annotations.RequestFilters;
import org.jclouds.rest.annotations.SelectJson;
import org.jclouds.rest.functions.ReturnNullOnNotFoundOr404;
import org.jclouds.rest.functions.ReturnVoidOnNotFoundOr404;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
/**
* Provides asynchronous access to CloudStack Account features available to Global
* Admin users.
*
* @author Adrian Cole, Andrei Savu
* @see <a href=
* "http://download.cloud.com/releases/2.2.0/api_2.2.12/TOC_Global_Admin.html"
* />
*/
@RequestFilters(QuerySigner.class)
@QueryParams(keys = "response", values = "json")
public interface GlobalZoneAsyncClient extends ZoneAsyncClient {
/**
* @see GlobalZoneClient#createZone
*/
@GET
@QueryParams(keys = "command", values = "createZone")
@SelectJson("zone")
@Consumes(MediaType.APPLICATION_JSON)
@ExceptionParser(ReturnNullOnNotFoundOr404.class)
ListenableFuture<Zone> createZone(@QueryParam("name") String name, @QueryParam("networktype") NetworkType networkType,
@QueryParam("dns1") String externalDns1, @QueryParam("internaldns1") String internalDns1, CreateZoneOptions... options);
/**
* @see GlobalZoneClient#updateZone
*/
@GET
@QueryParams(keys = "command", values = "updateZone")
@SelectJson("zone")
@Consumes(MediaType.APPLICATION_JSON)
@ExceptionParser(ReturnNullOnNotFoundOr404.class)
ListenableFuture<Zone> updateZone(@QueryParam("id") long id, UpdateZoneOptions... options);
/**
* @see GlobalZoneClient#deleteZone
*/
@GET
@QueryParams(keys = "command", values = "deleteZone")
@Consumes(MediaType.APPLICATION_JSON)
@ExceptionParser(ReturnVoidOnNotFoundOr404.class)
ListenableFuture<Void> deleteZone(@QueryParam("id") long id);
}

View File

@ -0,0 +1,79 @@
/**
* 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.cloudstack.features;
import org.jclouds.cloudstack.domain.NetworkType;
import org.jclouds.cloudstack.domain.Zone;
import org.jclouds.cloudstack.options.CreateZoneOptions;
import org.jclouds.cloudstack.options.UpdateZoneOptions;
import org.jclouds.concurrent.Timeout;
import java.util.concurrent.TimeUnit;
/**
* Provides synchronous access to CloudStack Zone features available to Global
* Admin users.
*
* @author Andrei Savu
* @see <a href=
* "http://download.cloud.com/releases/2.2.0/api_2.2.12/TOC_Global_Admin.html"
* />
*/
@Timeout(duration = 60, timeUnit = TimeUnit.SECONDS)
public interface GlobalZoneClient extends ZoneClient {
/**
* Create a new Zone
*
* @param name
* the name of the Zone
* @param networkType
* network type of the zone, can be Basic or Advanced
* @param dns1
* the first DNS for the Zone
* @param internalDns1
* the first internal DNS for the Zone
* @param options
* optional arguments
* @return
* zone instance or null
*/
Zone createZone(String name, NetworkType networkType, String dns1,
String internalDns1, CreateZoneOptions... options);
/**
* Update a zone
*
* @param id
* the ID of the Zone
* @param options
* optional arguments
* @return
*/
Zone updateZone(long id, UpdateZoneOptions... options);
/**
* Delete a zone with a specific ID
*
* @param zoneId
* the ID of the Zone
*/
Void deleteZone(long zoneId);
}

View File

@ -0,0 +1,176 @@
/**
* 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.cloudstack.options;
import com.google.common.collect.ImmutableSet;
import org.jclouds.cloudstack.domain.Zone;
import org.jclouds.http.options.BaseHttpRequestOptions;
import org.jclouds.javax.annotation.Nullable;
/**
* Options used to control how a zone is created
*
* @see <a href=
* "http://download.cloud.com/releases/2.2.0/api_2.2.12/global_admin/createZone.html"
* />
* @author Andrei Savu
*/
public class CreateZoneOptions extends BaseHttpRequestOptions {
public static final CreateZoneOptions NONE = new CreateZoneOptions();
/**
* @param allocationState
* allocation state of this Zone for allocation of new resources
*/
public CreateZoneOptions allocationState(Zone.AllocationState allocationState) {
this.queryParameters.replaceValues("allocationstate", ImmutableSet.of(allocationState.toString()));
return this;
}
/**
* @param dns2
* the second DNS for the Zone
*/
public CreateZoneOptions dns2(String dns2) {
this.queryParameters.replaceValues("dns2", ImmutableSet.of(dns2));
return this;
}
/**
* @param internalDns2
* the second internal DNS for the Zone
*/
public CreateZoneOptions internalDns2(String internalDns2) {
this.queryParameters.replaceValues("internaldns2", ImmutableSet.of(internalDns2));
return this;
}
/**
* @param domainName
* network domain name for the networks in zone
*/
public CreateZoneOptions domainName(String domainName) {
this.queryParameters.replaceValues("domain", ImmutableSet.of(domainName));
return this;
}
/**
* @param domainId
* the ID of the containing domain; null for public zones
*/
public CreateZoneOptions domainId(@Nullable long domainId) {
this.queryParameters.replaceValues("domainid", ImmutableSet.of(domainId + ""));
return this;
}
/**
* @param guestCIDRAddress
* the guest CIDR address for the Zone
*/
public CreateZoneOptions guestCIDRAddress(String guestCIDRAddress) {
this.queryParameters.replaceValues("guestcidraddress", ImmutableSet.of(guestCIDRAddress));
return this;
}
/**
* @param securityGroupEnabled
* true if network is security group enabled, false otherwise
*/
public CreateZoneOptions securityGroupEnabled(boolean securityGroupEnabled) {
this.queryParameters.replaceValues("securitygroupenabled", ImmutableSet.of(securityGroupEnabled + ""));
return this;
}
/**
* @param vlan
* the VLAN for the Zone
*/
public CreateZoneOptions vlan(String vlan) {
this.queryParameters.replaceValues("vlan", ImmutableSet.of(vlan));
return this;
}
public static class Builder {
/**
* @see CreateZoneOptions#allocationState
*/
public static CreateZoneOptions allocationState(Zone.AllocationState allocationState) {
CreateZoneOptions options = new CreateZoneOptions();
return options.allocationState(allocationState);
}
/**
* @see CreateZoneOptions#dns2
*/
public static CreateZoneOptions dns2(String dns2) {
CreateZoneOptions options = new CreateZoneOptions();
return options.dns2(dns2);
}
/**
* @see CreateZoneOptions#internalDns2
*/
public static CreateZoneOptions internalDns2(String internalDns2) {
CreateZoneOptions options = new CreateZoneOptions();
return options.internalDns2(internalDns2);
}
/**
* @see CreateZoneOptions#domainName
*/
public static CreateZoneOptions domainName(String domainName) {
CreateZoneOptions options = new CreateZoneOptions();
return options.domainName(domainName);
}
/**
* @see CreateZoneOptions#domainId
*/
public static CreateZoneOptions domainId(@Nullable long domainId) {
CreateZoneOptions options = new CreateZoneOptions();
return options.domainId(domainId);
}
/**
* @see CreateZoneOptions#guestCIDRAddress
*/
public static CreateZoneOptions guestCIDRAddress(String guestCIDRAddress) {
CreateZoneOptions options = new CreateZoneOptions();
return options.guestCIDRAddress(guestCIDRAddress);
}
/**
* @see CreateZoneOptions#securityGroupEnabled
*/
public static CreateZoneOptions securityGroupEnabled(boolean securityGroupEnabled) {
CreateZoneOptions options = new CreateZoneOptions();
return options.securityGroupEnabled(securityGroupEnabled);
}
/**
* @see CreateZoneOptions#vlan
*/
public static CreateZoneOptions vlan(String vlan) {
CreateZoneOptions options = new CreateZoneOptions();
return options.vlan(vlan);
}
}
}

View File

@ -0,0 +1,260 @@
/**
* 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.cloudstack.options;
import com.google.common.collect.ImmutableSet;
import org.jclouds.cloudstack.domain.Zone;
import org.jclouds.http.options.BaseHttpRequestOptions;
import java.util.List;
import static com.google.common.base.Preconditions.checkArgument;
/**
* Options used to control how a zone is updated
*
* @see <a href=
* "http://download.cloud.com/releases/2.2.0/api_2.2.12/global_admin/updateZone.html"
* />
* @author Andrei Savu
*/
public class UpdateZoneOptions extends BaseHttpRequestOptions {
public static final UpdateZoneOptions NONE = new UpdateZoneOptions();
/**
* @param allocationState
* allocation state of this Zone for allocation of new resources
*/
public UpdateZoneOptions allocationState(Zone.AllocationState allocationState) {
this.queryParameters.replaceValues("allocationstate", ImmutableSet.of(allocationState.toString()));
return this;
}
/**
* @param details
* the details for the Zone
*/
public UpdateZoneOptions details(String details) {
this.queryParameters.replaceValues("details", ImmutableSet.of(details));
return this;
}
/**
* @param dhcpProvider
* the dhcp Provider for the Zone
*/
public UpdateZoneOptions dhcpProvider(String dhcpProvider) {
this.queryParameters.replaceValues("dhcpprovider", ImmutableSet.of(dhcpProvider));
return this;
}
/**
* @param externalDnsServers
* the list of external DNS servers
*/
public UpdateZoneOptions externalDns(List<String> externalDnsServers) {
checkArgument(externalDnsServers.size() == 1 || externalDnsServers.size() == 2,
"The list of DNS servers should have 1 or 2 elements");
this.queryParameters.replaceValues("dns1",
ImmutableSet.of(externalDnsServers.get(0)));
if (externalDnsServers.size() == 2) {
this.queryParameters.replaceValues("dns2",
ImmutableSet.of(externalDnsServers.get(1)));
}
return this;
}
/**
* @param internalDnsServers
* the list of internal DNS for the Zone
*/
public UpdateZoneOptions internalDns(List<String> internalDnsServers) {
checkArgument(internalDnsServers.size() == 1 || internalDnsServers.size() == 2,
"The list of internal DNS servers should have 1 or 2 elements");
this.queryParameters.replaceValues("internaldns1",
ImmutableSet.of(internalDnsServers.get(0)));
if (internalDnsServers.size() == 2) {
this.queryParameters.replaceValues("internaldns2",
ImmutableSet.of(internalDnsServers.get(1)));
}
return this;
}
/**
* @param dnsSearchOrder
* the dns search order list
*/
public UpdateZoneOptions dnsSearchOrder(String dnsSearchOrder) {
this.queryParameters.replaceValues("dnssearchorder", ImmutableSet.of(dnsSearchOrder));
return this;
}
/**
* @param domainName
* network domain name for the networks in zone
*/
public UpdateZoneOptions domainName(String domainName) {
this.queryParameters.replaceValues("domain", ImmutableSet.of(domainName));
return this;
}
/**
* @param guestCIDRAddress
* the guest CIDR address for the Zone
*/
public UpdateZoneOptions guestCIDRAddress(String guestCIDRAddress) {
this.queryParameters.replaceValues("guestcidraddress", ImmutableSet.of(guestCIDRAddress));
return this;
}
/**
* @param securityGroupEnabled
* true if network is security group enabled, false otherwise
*/
public UpdateZoneOptions securityGroupEnabled(boolean securityGroupEnabled) {
this.queryParameters.replaceValues("securitygroupenabled", ImmutableSet.of(securityGroupEnabled + ""));
return this;
}
/**
* You can only make a private Zone public, not the other way around
*/
public UpdateZoneOptions makePublic() {
this.queryParameters.replaceValues("ispublic", ImmutableSet.of("true"));
return this;
}
/**
* @param name
* the name of the Zone
*/
public UpdateZoneOptions name(String name) {
this.queryParameters.replaceValues("name", ImmutableSet.of(name));
return this;
}
/**
* @param vlan
* the VLAN for the Zone
*/
public UpdateZoneOptions vlan(String vlan) {
this.queryParameters.replaceValues("vlan", ImmutableSet.of(vlan));
return this;
}
public static class Builder {
/**
* @see UpdateZoneOptions#allocationState
*/
public static UpdateZoneOptions allocationState(Zone.AllocationState allocationState) {
UpdateZoneOptions options = new UpdateZoneOptions();
return options.allocationState(allocationState);
}
/**
* @see UpdateZoneOptions#details
*/
public static UpdateZoneOptions details(String details) {
UpdateZoneOptions options = new UpdateZoneOptions();
return options.details(details);
}
/**
* @see UpdateZoneOptions#dhcpProvider
*/
public static UpdateZoneOptions dhcpProvider(String dhcpProvider) {
UpdateZoneOptions options = new UpdateZoneOptions();
return options.dhcpProvider(dhcpProvider);
}
/**
* @see UpdateZoneOptions#externalDns
*/
public static UpdateZoneOptions externalDns(List<String> externalDnsServers) {
UpdateZoneOptions options = new UpdateZoneOptions();
return options.externalDns(externalDnsServers);
}
/**
* @see UpdateZoneOptions#internalDns
*/
public static UpdateZoneOptions internalDns(List<String> internalDnsServers) {
UpdateZoneOptions options = new UpdateZoneOptions();
return options.internalDns(internalDnsServers);
}
/**
* @see UpdateZoneOptions#dnsSearchOrder
*/
public static UpdateZoneOptions dnsSearchOrder(String dnsSearchOrder) {
UpdateZoneOptions options = new UpdateZoneOptions();
return options.dnsSearchOrder(dnsSearchOrder);
}
/**
* @see UpdateZoneOptions#domainName
*/
public static UpdateZoneOptions domainName(String domainName) {
UpdateZoneOptions options = new UpdateZoneOptions();
return options.domainName(domainName);
}
/**
* @see UpdateZoneOptions#guestCIDRAddress
*/
public static UpdateZoneOptions guestCIDRAddress(String guestCIDRAddress) {
UpdateZoneOptions options = new UpdateZoneOptions();
return options.guestCIDRAddress(guestCIDRAddress);
}
/**
* @see UpdateZoneOptions#securityGroupEnabled
*/
public static UpdateZoneOptions securityGroupEnabled(boolean securityGroupEnabled) {
UpdateZoneOptions options = new UpdateZoneOptions();
return options.securityGroupEnabled(securityGroupEnabled);
}
/**
* @see UpdateZoneOptions#makePublic
*/
public static UpdateZoneOptions makePublic() {
UpdateZoneOptions options = new UpdateZoneOptions();
return options.makePublic();
}
/**
* @see UpdateZoneOptions#name
*/
public static UpdateZoneOptions name(String name) {
UpdateZoneOptions options = new UpdateZoneOptions();
return options.name(name);
}
/**
* @see UpdateZoneOptions#vlan
*/
public static UpdateZoneOptions vlan(String vlan) {
UpdateZoneOptions options = new UpdateZoneOptions();
return options.vlan(vlan);
}
}
}

View File

@ -0,0 +1,137 @@
/**
* 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.cloudstack.features;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMultimap;
import org.jclouds.cloudstack.CloudStackContext;
import org.jclouds.cloudstack.domain.NetworkType;
import org.jclouds.cloudstack.domain.Zone;
import org.jclouds.http.HttpRequest;
import org.jclouds.http.HttpResponse;
import org.testng.annotations.Test;
import java.net.URI;
import static org.jclouds.cloudstack.options.UpdateZoneOptions.Builder.name;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNull;
/**
* Test the CloudStack GlobalZoneClient
*
* @author Andrei Savu
*/
@Test(groups = "unit", testName = "GlobalZoneClientExpectTest")
public class GlobalZoneClientExpectTest extends BaseCloudStackRestClientExpectTest<GlobalZoneClient> {
public void testCreateZoneWhenResponseIs2xxAnd404() {
HttpRequest request = HttpRequest.builder()
.method("GET")
.endpoint(
URI.create("http://localhost:8080/client/api?response=json&command=createZone&" +
"name=test-zone&dns1=8.8.8.8&networktype=Basic&internaldns1=10.10.10.10&" +
"apiKey=identity&signature=hWNmM2%2BTsfb5DelQa%2FGJLN5DVWE%3D"))
.headers(
ImmutableMultimap.<String, String>builder()
.put("Accept", "application/json")
.build())
.build();
GlobalZoneClient client = requestSendsResponse(request,
HttpResponse.builder()
.statusCode(200)
.payload(payloadFromResource("/createzoneresponse.json"))
.build());
assertEquals(client.createZone("test-zone", NetworkType.BASIC, "8.8.8.8", "10.10.10.10"),
Zone.builder()
.id(6)
.name("test-zone")
.DNS(ImmutableList.of("8.8.8.8"))
.internalDNS(ImmutableList.of("10.10.10.10"))
.networkType(NetworkType.BASIC)
.securityGroupsEnabled(true)
.allocationState(Zone.AllocationState.ENABLED)
.zoneToken("7b6e27df-30a6-3024-9d8b-7971a3127f64")
.dhcpProvider("DhcpServer").build());
client = requestSendsResponse(request, HttpResponse.builder().statusCode(404).build());
assertNull(client.createZone("test-zone", NetworkType.BASIC, "8.8.8.8", "10.10.10.10"));
}
public void testUpdateZoneWhenResponseIs2xxAnd404() {
HttpRequest request = HttpRequest.builder()
.method("GET")
.endpoint(
URI.create("http://localhost:8080/client/api?response=json&command=updateZone&" +
"id=6&name=test-zone&dns1=8.8.8.8&apiKey=identity&signature=v19FdHKHztdT0IRloYFFn0eNbWM%3D"))
.headers(
ImmutableMultimap.<String, String>builder()
.put("Accept", "application/json")
.build())
.build();
GlobalZoneClient client = requestSendsResponse(request,
HttpResponse.builder()
.statusCode(200)
.payload(payloadFromResource("/updatezoneresponse.json"))
.build());
assertEquals(client.updateZone(6, name("test-zone").externalDns(ImmutableList.of("8.8.8.8"))),
Zone.builder()
.id(6)
.name("test-zone")
.DNS(ImmutableList.of("8.8.8.8"))
.internalDNS(ImmutableList.of("10.10.10.10"))
.networkType(NetworkType.BASIC)
.securityGroupsEnabled(true)
.allocationState(Zone.AllocationState.ENABLED)
.zoneToken("7b6e27df-30a6-3024-9d8b-7971a3127f64")
.dhcpProvider("DhcpServer").build());
client = requestSendsResponse(request, HttpResponse.builder().statusCode(404).build());
assertNull(client.updateZone(6, name("test-zone").externalDns(ImmutableList.of("8.8.8.8"))));
}
public void testDeleteZone() {
GlobalZoneClient client = requestSendsResponse(
HttpRequest.builder()
.method("GET")
.endpoint(
URI.create("http://localhost:8080/client/api?response=json&" +
"command=deleteZone&id=6&apiKey=identity&signature=TfkzSIK8kzGJnIYo3DofECyuOII%3D"))
.headers(
ImmutableMultimap.<String, String>builder()
.put("Accept", "application/json")
.build())
.build(),
HttpResponse.builder()
.statusCode(200)
.payload(payloadFromResource("/deletezoneresponse.json"))
.build());
client.deleteZone(6);
}
@Override
protected GlobalZoneClient clientFrom(CloudStackContext context) {
return context.getGlobalContext().getApi().getZoneClient();
}
}

View File

@ -0,0 +1,67 @@
/**
* 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.cloudstack.features;
import com.google.common.collect.ImmutableList;
import org.jclouds.cloudstack.domain.NetworkType;
import org.jclouds.cloudstack.domain.Zone;
import org.testng.annotations.Test;
import static org.jclouds.cloudstack.options.UpdateZoneOptions.Builder.name;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNotNull;
/**
* Tests behavior of {@code GlobalZoneClient}
*
* @author Andrei Savu
*/
@Test(groups = "live", singleThreaded = true, testName = "GlobalZoneClientLiveTest")
public class GlobalZoneClientLiveTest extends BaseCloudStackClientLiveTest {
@Test
public void testCreateUpdateDeleteZone() {
assert globalAdminEnabled;
Zone zone = null;
String zoneName = prefix + "-zone";
try {
zone = globalAdminClient.getZoneClient().createZone(zoneName,
NetworkType.BASIC, "8.8.8.8", "10.10.10.10");
assertNotNull(zone);
assertEquals(zone, globalAdminClient.getZoneClient().getZone(zone.getId()));
assertEquals(zone.getNetworkType(), NetworkType.BASIC);
assertEquals(zone.getDNS(), ImmutableList.of("8.8.8.8"));
assertEquals(zone.getInternalDNS(), ImmutableList.of("10.10.10.10"));
Zone updated = globalAdminClient.getZoneClient().updateZone(zone.getId(),
name(zoneName + "-2").externalDns(ImmutableList.of("8.8.4.4")));
assertEquals(updated.getId(), zone.getId());
assertEquals(updated.getDNS(), ImmutableList.of("8.8.4.4"));
} finally {
if (zone != null) {
globalAdminClient.getZoneClient().deleteZone(zone.getId());
}
}
}
}

View File

@ -0,0 +1,97 @@
/**
* 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.cloudstack.features;
import com.google.common.collect.ImmutableMultimap;
import com.google.common.collect.ImmutableSet;
import org.jclouds.cloudstack.CloudStackContext;
import org.jclouds.cloudstack.domain.NetworkType;
import org.jclouds.cloudstack.domain.Zone;
import org.jclouds.http.HttpRequest;
import org.jclouds.http.HttpResponse;
import org.testng.annotations.Test;
import java.net.URI;
import static org.testng.Assert.assertEquals;
/**
* Test the CloudStack ZoneClient
*
* @author Andrei Savu
*/
@Test(groups = "unit", testName = "ZoneClientExpectTest")
public class ZoneClientExpectTest extends BaseCloudStackRestClientExpectTest<ZoneClient> {
public void testListZonesWhenResponseIs2xx() {
ZoneClient client = requestSendsResponse(
HttpRequest.builder()
.method("GET")
.endpoint(
URI.create("http://localhost:8080/client/api?response=json&" +
"command=listZones&apiKey=identity&signature=wLSqVlxuiLXZcHi9IoSAwXNRGFs%3D"))
.headers(
ImmutableMultimap.<String, String>builder()
.put("Accept", "application/json")
.build())
.build(),
HttpResponse.builder()
.statusCode(200)
.payload(payloadFromResource("/listzonesresponse.json"))
.build());
assertEquals(client.listZones(),
ImmutableSet.of(
Zone.builder()
.id(1)
.name("San Jose 1")
.networkType(NetworkType.ADVANCED)
.securityGroupsEnabled(false).build(),
Zone.builder()
.id(2)
.name("Chicago")
.networkType(NetworkType.ADVANCED)
.securityGroupsEnabled(true).build()));
}
public void testListZonesWhenResponseIs404() {
ZoneClient client = requestSendsResponse(
HttpRequest.builder()
.method("GET")
.endpoint(
URI.create("http://localhost:8080/client/api?response=json&" +
"command=listZones&apiKey=identity&signature=wLSqVlxuiLXZcHi9IoSAwXNRGFs%3D"))
.headers(
ImmutableMultimap.<String, String>builder()
.put("Accept", "application/json")
.build())
.build(),
HttpResponse.builder()
.statusCode(404)
.build());
assertEquals(client.listZones(), ImmutableSet.of());
}
@Override
protected ZoneClient clientFrom(CloudStackContext context) {
return context.getProviderSpecificContext().getApi().getZoneClient();
}
}

View File

@ -61,8 +61,8 @@ public class ZoneClientLiveTest extends BaseCloudStackClientLiveTest {
break;
case BASIC:
assert zone.getVLAN() == null : zone;
assert zone.getDNS().size() == 0 : zone;
assert zone.getInternalDNS().size() == 0 : zone;
assert zone.getDNS().size() >= 0 : zone;
assert zone.getInternalDNS().size() >= 0 : zone;
assert zone.getDomain() == null : zone;
assert zone.getDomainId() <= 0 : zone;
assert zone.getGuestCIDRAddress() == null : zone;

View File

@ -0,0 +1,4 @@
{ "createzoneresponse" : { "zone" :
{"id":6,"name":"test-zone","dns1":"8.8.8.8","internaldns1":"10.10.10.10","networktype":"Basic",
"securitygroupsenabled":true,"allocationstate":"Enabled","zonetoken":"7b6e27df-30a6-3024-9d8b-7971a3127f64",
"dhcpprovider":"DhcpServer"} } }

View File

@ -0,0 +1 @@
{ "deletezoneresponse" : { "success" : "true"} }

View File

@ -0,0 +1,4 @@
{ "updatezoneresponse" : { "zone" :
{"id":6,"name":"test-zone","dns1":"8.8.8.8","internaldns1":"10.10.10.10","networktype":"Basic",
"securitygroupsenabled":true,"allocationstate":"Enabled","zonetoken":"7b6e27df-30a6-3024-9d8b-7971a3127f64",
"dhcpprovider":"DhcpServer"} } }