From 710efb584236374ebde01cb1eec2aebcf6de1046 Mon Sep 17 00:00:00 2001 From: Richard Downer Date: Mon, 23 Jan 2012 11:05:41 +0200 Subject: [PATCH 1/4] Add VlanIPRange domain object and parser test --- .../cloudstack/domain/VlanIPRange.java | 305 ++++++++++++++++++ .../parse/ListVlanIPRangesResponseTest.java | 76 +++++ .../resources/listvlaniprangesresponse.json | 1 + 3 files changed, 382 insertions(+) create mode 100644 apis/cloudstack/src/main/java/org/jclouds/cloudstack/domain/VlanIPRange.java create mode 100644 apis/cloudstack/src/test/java/org/jclouds/cloudstack/parse/ListVlanIPRangesResponseTest.java create mode 100644 apis/cloudstack/src/test/resources/listvlaniprangesresponse.json diff --git a/apis/cloudstack/src/main/java/org/jclouds/cloudstack/domain/VlanIPRange.java b/apis/cloudstack/src/main/java/org/jclouds/cloudstack/domain/VlanIPRange.java new file mode 100644 index 0000000000..3f482997b0 --- /dev/null +++ b/apis/cloudstack/src/main/java/org/jclouds/cloudstack/domain/VlanIPRange.java @@ -0,0 +1,305 @@ +/** + * 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.domain; + +import com.google.gson.annotations.SerializedName; + +/** + * Represents the data object used in CloudStack's "Vlan" API. + * + * @author Richard Downer + */ +public class VlanIPRange implements Comparable { + + public static Builder builder() { + return new Builder(); + } + + public static class Builder { + + private long id; + private String description; + private boolean forVirtualNetwork; + private long zoneId; + private String vlan; + private String account; + private long domainId; + private String domain; + private long podId; + private String podName; + private String gateway; + private String netmask; + private String startIP; + private String endIP; + private long networkId; + + public Builder id(long id) { + this.id = id; + return this; + } + + public Builder description(String description) { + this.description = description; + return this; + } + + public Builder forVirtualNetwork(boolean forVirtualNetwork) { + this.forVirtualNetwork = forVirtualNetwork; + return this; + } + + public Builder zoneId(long zoneId) { + this.zoneId = zoneId; + return this; + } + + public Builder vlan(long vlan) { + this.vlan = vlan+""; + return this; + } + + public Builder vlan(String vlan) { + this.vlan = vlan; + return this; + } + + public Builder account(String account) { + this.account = account; + return this; + } + + public Builder domainId(long domainId) { + this.domainId = domainId; + return this; + } + + public Builder domain(String domain) { + this.domain = domain; + return this; + } + + public Builder podId(long podId) { + this.podId = podId; + return this; + } + + public Builder podName(String podName) { + this.podName = podName; + return this; + } + + public Builder gateway(String gateway) { + this.gateway = gateway; + return this; + } + + public Builder netmask(String netmask) { + this.netmask = netmask; + return this; + } + + public Builder startIP(String startIP) { + this.startIP = startIP; + return this; + } + + public Builder endIP(String endIP) { + this.endIP = endIP; + return this; + } + + public Builder networkId(long networkId) { + this.networkId = networkId; + return this; + } + + public VlanIPRange build() { + return new VlanIPRange(id, description, forVirtualNetwork, zoneId, vlan, account, domainId, domain, podId, podName, gateway, netmask, startIP, endIP, networkId); + } + } + + private long id; + private String description; + @SerializedName("forvirtualnetwork") private boolean forVirtualNetwork; + @SerializedName("zoneid") private long zoneId; + private String vlan; + private String account; + @SerializedName("domainid") private long domainId; + private String domain; + @SerializedName("podid") private long podId; + @SerializedName("podname") private String podName; + private String gateway; + private String netmask; + @SerializedName("startip") private String startIP; + @SerializedName("endip") private String endIP; + @SerializedName("networkid") private long networkId; + + /* just for the deserializer */ + VlanIPRange() {} + + public VlanIPRange(long id, String description, boolean forVirtualNetwork, long zoneId, String vlan, String account, long domainId, String domain, long podId, String podName, String gateway, String netmask, String startIP, String endIP, long networkId) { + this.id = id; + this.description = description; + this.forVirtualNetwork = forVirtualNetwork; + this.zoneId = zoneId; + this.vlan = vlan; + this.account = account; + this.domainId = domainId; + this.domain = domain; + this.podId = podId; + this.podName = podName; + this.gateway = gateway; + this.netmask = netmask; + this.startIP = startIP; + this.endIP = endIP; + this.networkId = networkId; + } + + public long getId() { + return id; + } + + public String getDescription() { + return description; + } + + public boolean isForVirtualNetwork() { + return forVirtualNetwork; + } + + public long getZoneId() { + return zoneId; + } + + public String getVlan() { + return vlan; + } + + public String getAccount() { + return account; + } + + public long getDomainId() { + return domainId; + } + + public String getDomain() { + return domain; + } + + public long getPodId() { + return podId; + } + + public String getPodName() { + return podName; + } + + public String getGateway() { + return gateway; + } + + public String getNetmask() { + return netmask; + } + + public String getStartIP() { + return startIP; + } + + public String getEndIP() { + return endIP; + } + + public long getNetworkId() { + return networkId; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + VlanIPRange that = (VlanIPRange) o; + + if (domainId != that.domainId) return false; + if (forVirtualNetwork != that.forVirtualNetwork) return false; + if (id != that.id) return false; + if (networkId != that.networkId) return false; + if (podId != that.podId) return false; + if (zoneId != that.zoneId) return false; + if (account != null ? !account.equals(that.account) : that.account != null) return false; + if (description != null ? !description.equals(that.description) : that.description != null) return false; + if (domain != null ? !domain.equals(that.domain) : that.domain != null) return false; + if (endIP != null ? !endIP.equals(that.endIP) : that.endIP != null) return false; + if (gateway != null ? !gateway.equals(that.gateway) : that.gateway != null) return false; + if (netmask != null ? !netmask.equals(that.netmask) : that.netmask != null) return false; + if (podName != null ? !podName.equals(that.podName) : that.podName != null) return false; + if (startIP != null ? !startIP.equals(that.startIP) : that.startIP != null) return false; + if (vlan != null ? !vlan.equals(that.vlan) : that.vlan != null) return false; + + return true; + } + + @Override + public int hashCode() { + int result = (int) (id ^ (id >>> 32)); + result = 31 * result + (description != null ? description.hashCode() : 0); + result = 31 * result + (forVirtualNetwork ? 1 : 0); + result = 31 * result + (int) (zoneId ^ (zoneId >>> 32)); + result = 31 * result + (vlan != null ? vlan.hashCode() : 0); + result = 31 * result + (account != null ? account.hashCode() : 0); + result = 31 * result + (int) (domainId ^ (domainId >>> 32)); + result = 31 * result + (domain != null ? domain.hashCode() : 0); + result = 31 * result + (int) (podId ^ (podId >>> 32)); + result = 31 * result + (podName != null ? podName.hashCode() : 0); + result = 31 * result + (gateway != null ? gateway.hashCode() : 0); + result = 31 * result + (netmask != null ? netmask.hashCode() : 0); + result = 31 * result + (startIP != null ? startIP.hashCode() : 0); + result = 31 * result + (endIP != null ? endIP.hashCode() : 0); + result = 31 * result + (int) (networkId ^ (networkId >>> 32)); + return result; + } + + @Override + public String toString() { + return "VlanIPRange{" + + "id=" + id + + ", description='" + description + '\'' + + ", forVirtualNetwork=" + forVirtualNetwork + + ", zoneId=" + zoneId + + ", vlan='" + vlan + '\'' + + ", account='" + account + '\'' + + ", domainId=" + domainId + + ", domain='" + domain + '\'' + + ", podId=" + podId + + ", podName='" + podName + '\'' + + ", gateway='" + gateway + '\'' + + ", netmask='" + netmask + '\'' + + ", startIP='" + startIP + '\'' + + ", endIP='" + endIP + '\'' + + ", networkId=" + networkId + + '}'; + } + + @Override + public int compareTo(VlanIPRange other) { + return Long.valueOf(this.id).compareTo(other.id); + } +} diff --git a/apis/cloudstack/src/test/java/org/jclouds/cloudstack/parse/ListVlanIPRangesResponseTest.java b/apis/cloudstack/src/test/java/org/jclouds/cloudstack/parse/ListVlanIPRangesResponseTest.java new file mode 100644 index 0000000000..74f94338ab --- /dev/null +++ b/apis/cloudstack/src/test/java/org/jclouds/cloudstack/parse/ListVlanIPRangesResponseTest.java @@ -0,0 +1,76 @@ +/** + * 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.parse; + +import com.google.common.collect.ImmutableSet; +import org.jclouds.cloudstack.domain.VlanIPRange; +import org.jclouds.json.BaseSetParserTest; +import org.jclouds.rest.annotations.SelectJson; +import org.testng.annotations.Test; + +import java.util.Set; + +/** + * + * @author Richard Downer + */ +@Test(groups = "unit") +public class ListVlanIPRangesResponseTest extends BaseSetParserTest { + + @Override + public String resource() { + return "/listvlaniprangesresponse.json"; + } + + @Override + @SelectJson("vlaniprange") + public Set expected() { + VlanIPRange range1 = VlanIPRange.builder() + .id(1) + .forVirtualNetwork(true) + .zoneId(1) + .vlan(127) + .account("system") + .domainId(1) + .domain("ROOT") + .gateway("10.27.27.254") + .netmask("255.255.255.0") + .startIP("10.27.27.50") + .endIP("10.27.27.100") + .networkId(200) + .build(); + VlanIPRange range2 = VlanIPRange.builder() + .id(2) + .forVirtualNetwork(false) + .zoneId(2) + .vlan("untagged") + .account("system") + .domainId(1) + .domain("ROOT") + .podId(2) + .podName("Dev Pod 2") + .gateway("10.22.22.254") + .netmask("255.255.255.0") + .startIP("10.22.22.51") + .endIP("10.22.22.100") + .networkId(209) + .build(); + return ImmutableSet.of(range1, range2); + } +} diff --git a/apis/cloudstack/src/test/resources/listvlaniprangesresponse.json b/apis/cloudstack/src/test/resources/listvlaniprangesresponse.json new file mode 100644 index 0000000000..2eaae46a70 --- /dev/null +++ b/apis/cloudstack/src/test/resources/listvlaniprangesresponse.json @@ -0,0 +1 @@ +{ "listvlaniprangesresponse" : { "count":2 ,"vlaniprange" : [ {"id":1,"forvirtualnetwork":true,"zoneid":1,"vlan":"127","account":"system","domainid":1,"domain":"ROOT","gateway":"10.27.27.254","netmask":"255.255.255.0","startip":"10.27.27.50","endip":"10.27.27.100","networkid":200}, {"id":2,"forvirtualnetwork":false,"zoneid":2,"vlan":"untagged","account":"system","domainid":1,"domain":"ROOT","podid":2,"podname":"Dev Pod 2","gateway":"10.22.22.254","netmask":"255.255.255.0","startip":"10.22.22.51","endip":"10.22.22.100","networkid":209} ] } } \ No newline at end of file From 9ca399049a01ff85a15d91a3a9acc2060df4e212 Mon Sep 17 00:00:00 2001 From: Richard Downer Date: Mon, 23 Jan 2012 13:12:45 +0200 Subject: [PATCH 2/4] Add empty Vlan[Async]Client module and tests --- .../CloudStackGlobalAsyncClient.java | 7 ++++ .../cloudstack/CloudStackGlobalClient.java | 7 ++++ .../config/CloudStackRestClientModule.java | 3 ++ .../features/GlobalVlanAsyncClient.java | 37 +++++++++++++++++++ .../cloudstack/features/GlobalVlanClient.java | 36 ++++++++++++++++++ .../features/GlobalVlanClientExpectTest.java | 37 +++++++++++++++++++ .../features/GlobalVlanClientLiveTest.java | 36 ++++++++++++++++++ 7 files changed, 163 insertions(+) create mode 100644 apis/cloudstack/src/main/java/org/jclouds/cloudstack/features/GlobalVlanAsyncClient.java create mode 100644 apis/cloudstack/src/main/java/org/jclouds/cloudstack/features/GlobalVlanClient.java create mode 100644 apis/cloudstack/src/test/java/org/jclouds/cloudstack/features/GlobalVlanClientExpectTest.java create mode 100644 apis/cloudstack/src/test/java/org/jclouds/cloudstack/features/GlobalVlanClientLiveTest.java diff --git a/apis/cloudstack/src/main/java/org/jclouds/cloudstack/CloudStackGlobalAsyncClient.java b/apis/cloudstack/src/main/java/org/jclouds/cloudstack/CloudStackGlobalAsyncClient.java index dbbb6ced8b..521251719c 100644 --- a/apis/cloudstack/src/main/java/org/jclouds/cloudstack/CloudStackGlobalAsyncClient.java +++ b/apis/cloudstack/src/main/java/org/jclouds/cloudstack/CloudStackGlobalAsyncClient.java @@ -30,6 +30,7 @@ import org.jclouds.cloudstack.features.GlobalPodAsyncClient; import org.jclouds.cloudstack.features.GlobalStoragePoolAsyncClient; import org.jclouds.cloudstack.features.GlobalUsageAsyncClient; import org.jclouds.cloudstack.features.GlobalUserAsyncClient; +import org.jclouds.cloudstack.features.GlobalVlanAsyncClient; import org.jclouds.cloudstack.features.GlobalZoneAsyncClient; import org.jclouds.rest.annotations.Delegate; @@ -122,4 +123,10 @@ public interface CloudStackGlobalAsyncClient extends CloudStackDomainAsyncClient */ @Delegate GlobalPodAsyncClient getPodClient(); + + /** + * Provides asynchronous access to Vlan + */ + @Delegate + GlobalVlanAsyncClient getVlanClient(); } diff --git a/apis/cloudstack/src/main/java/org/jclouds/cloudstack/CloudStackGlobalClient.java b/apis/cloudstack/src/main/java/org/jclouds/cloudstack/CloudStackGlobalClient.java index 005719eab4..4ed21766a2 100644 --- a/apis/cloudstack/src/main/java/org/jclouds/cloudstack/CloudStackGlobalClient.java +++ b/apis/cloudstack/src/main/java/org/jclouds/cloudstack/CloudStackGlobalClient.java @@ -31,6 +31,7 @@ import org.jclouds.cloudstack.features.GlobalPodClient; import org.jclouds.cloudstack.features.GlobalStoragePoolClient; import org.jclouds.cloudstack.features.GlobalUsageClient; import org.jclouds.cloudstack.features.GlobalUserClient; +import org.jclouds.cloudstack.features.GlobalVlanClient; import org.jclouds.cloudstack.features.GlobalZoneClient; import org.jclouds.concurrent.Timeout; import org.jclouds.rest.annotations.Delegate; @@ -125,4 +126,10 @@ public interface CloudStackGlobalClient extends CloudStackDomainClient { */ @Delegate GlobalPodClient getPodClient(); + + /** + * Provides synchronous access to Vlan + */ + @Delegate + GlobalVlanClient getVlanClient(); } diff --git a/apis/cloudstack/src/main/java/org/jclouds/cloudstack/config/CloudStackRestClientModule.java b/apis/cloudstack/src/main/java/org/jclouds/cloudstack/config/CloudStackRestClientModule.java index 1c11d63063..68023513f3 100644 --- a/apis/cloudstack/src/main/java/org/jclouds/cloudstack/config/CloudStackRestClientModule.java +++ b/apis/cloudstack/src/main/java/org/jclouds/cloudstack/config/CloudStackRestClientModule.java @@ -72,6 +72,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.GlobalVlanAsyncClient; +import org.jclouds.cloudstack.features.GlobalVlanClient; import org.jclouds.cloudstack.features.GlobalZoneAsyncClient; import org.jclouds.cloudstack.features.GlobalZoneClient; import org.jclouds.cloudstack.features.GuestOSAsyncClient; @@ -172,6 +174,7 @@ public class CloudStackRestClientModule extends RestClientModule + * + * @see org.jclouds.cloudstack.features.GlobalVlanClient + * @see + * @author Richard Downer + */ +@RequestFilters(QuerySigner.class) +@QueryParams(keys = "response", values = "json") +public interface GlobalVlanAsyncClient { + +} diff --git a/apis/cloudstack/src/main/java/org/jclouds/cloudstack/features/GlobalVlanClient.java b/apis/cloudstack/src/main/java/org/jclouds/cloudstack/features/GlobalVlanClient.java new file mode 100644 index 0000000000..7a6f10400a --- /dev/null +++ b/apis/cloudstack/src/main/java/org/jclouds/cloudstack/features/GlobalVlanClient.java @@ -0,0 +1,36 @@ +/** + * 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.concurrent.Timeout; + +import java.util.concurrent.TimeUnit; + +/** + * Provides synchronous access to CloudStack VLAN features. + *

+ * + * @see GlobalOfferingAsyncClient + * @see + * @author Richard Downer + */ +@Timeout(duration = 60, timeUnit = TimeUnit.SECONDS) +public interface GlobalVlanClient { + +} diff --git a/apis/cloudstack/src/test/java/org/jclouds/cloudstack/features/GlobalVlanClientExpectTest.java b/apis/cloudstack/src/test/java/org/jclouds/cloudstack/features/GlobalVlanClientExpectTest.java new file mode 100644 index 0000000000..8529fce4d5 --- /dev/null +++ b/apis/cloudstack/src/test/java/org/jclouds/cloudstack/features/GlobalVlanClientExpectTest.java @@ -0,0 +1,37 @@ +/** + * 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.CloudStackContext; +import org.testng.annotations.Test; + +/** + * Test the CloudStack VlanClient + * + * @author Richard Downer + */ +@Test(groups = "unit", testName = "GlobalVlanClientExpectTest") +public class GlobalVlanClientExpectTest extends BaseCloudStackRestClientExpectTest { + + + @Override + protected GlobalVlanClient clientFrom(CloudStackContext context) { + return context.getGlobalContext().getApi().getVlanClient(); + } +} \ No newline at end of file diff --git a/apis/cloudstack/src/test/java/org/jclouds/cloudstack/features/GlobalVlanClientLiveTest.java b/apis/cloudstack/src/test/java/org/jclouds/cloudstack/features/GlobalVlanClientLiveTest.java new file mode 100644 index 0000000000..b77054fee0 --- /dev/null +++ b/apis/cloudstack/src/test/java/org/jclouds/cloudstack/features/GlobalVlanClientLiveTest.java @@ -0,0 +1,36 @@ +/** + * 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.testng.annotations.AfterClass; +import org.testng.annotations.Test; + +/** + * Tests behavior of {@code GlobalVlanClient} + * + * @author Richard Downer + */ +@Test(groups = "live", singleThreaded = true, testName = "GlobalVlanClientLiveTest") +public class GlobalVlanClientLiveTest extends BaseCloudStackClientLiveTest { + + @AfterClass + public void testFixtureTearDown() { + } + +} From b15a258fde9c0e5138a8c347e74bf3933b48a42a Mon Sep 17 00:00:00 2001 From: Richard Downer Date: Wed, 25 Jan 2012 14:12:04 +0200 Subject: [PATCH 3/4] Add listVlanIpRanges() and getVlanIpRange() --- .../features/GlobalVlanAsyncClient.java | 40 ++++++ .../cloudstack/features/GlobalVlanClient.java | 18 +++ .../options/ListVlanIPRangesOptions.java | 122 +++++++++++++++++ .../features/GlobalVlanClientExpectTest.java | 79 +++++++++++ .../features/GlobalVlanClientLiveTest.java | 34 +++++ .../options/ListVlanIPRangesOptionsTest.java | 127 ++++++++++++++++++ 6 files changed, 420 insertions(+) create mode 100644 apis/cloudstack/src/main/java/org/jclouds/cloudstack/options/ListVlanIPRangesOptions.java create mode 100644 apis/cloudstack/src/test/java/org/jclouds/cloudstack/options/ListVlanIPRangesOptionsTest.java diff --git a/apis/cloudstack/src/main/java/org/jclouds/cloudstack/features/GlobalVlanAsyncClient.java b/apis/cloudstack/src/main/java/org/jclouds/cloudstack/features/GlobalVlanAsyncClient.java index 4cc75df5b8..7d07d1ff64 100644 --- a/apis/cloudstack/src/main/java/org/jclouds/cloudstack/features/GlobalVlanAsyncClient.java +++ b/apis/cloudstack/src/main/java/org/jclouds/cloudstack/features/GlobalVlanAsyncClient.java @@ -18,9 +18,23 @@ */ package org.jclouds.cloudstack.features; +import com.google.common.util.concurrent.ListenableFuture; +import org.jclouds.cloudstack.domain.VlanIPRange; import org.jclouds.cloudstack.filters.QuerySigner; +import org.jclouds.cloudstack.options.ListVlanIPRangesOptions; +import org.jclouds.rest.annotations.ExceptionParser; +import org.jclouds.rest.annotations.OnlyElement; import org.jclouds.rest.annotations.QueryParams; import org.jclouds.rest.annotations.RequestFilters; +import org.jclouds.rest.annotations.SelectJson; +import org.jclouds.rest.functions.ReturnEmptySetOnNotFoundOr404; +import org.jclouds.rest.functions.ReturnNullOnNotFoundOr404; + +import javax.ws.rs.Consumes; +import javax.ws.rs.GET; +import javax.ws.rs.QueryParam; +import javax.ws.rs.core.MediaType; +import java.util.Set; /** * Provides asynchronous access to cloudstack via their REST API. @@ -34,4 +48,30 @@ import org.jclouds.rest.annotations.RequestFilters; @QueryParams(keys = "response", values = "json") public interface GlobalVlanAsyncClient { + /** + * Get the details of an IP range by its id. + * @param id the required IP range. + * @return the requested IP range. + */ + @GET + @QueryParams(keys = "command", values = "listVlanIpRanges") + @SelectJson("vlaniprange") + @OnlyElement + @Consumes(MediaType.APPLICATION_JSON) + @ExceptionParser(ReturnNullOnNotFoundOr404.class) + ListenableFuture getVlanIPRange(@QueryParam("id") long id); + + /** + * Lists all VLAN IP ranges. + * + * @param options optional arguments. + * @return the list of IP ranges that match the criteria. + */ + @GET + @QueryParams(keys = "command", values = "listVlanIpRanges") + @SelectJson("vlaniprange") + @Consumes(MediaType.APPLICATION_JSON) + @ExceptionParser(ReturnEmptySetOnNotFoundOr404.class) + ListenableFuture> listVlanIPRanges(ListVlanIPRangesOptions... options); + } diff --git a/apis/cloudstack/src/main/java/org/jclouds/cloudstack/features/GlobalVlanClient.java b/apis/cloudstack/src/main/java/org/jclouds/cloudstack/features/GlobalVlanClient.java index 7a6f10400a..2050336b6a 100644 --- a/apis/cloudstack/src/main/java/org/jclouds/cloudstack/features/GlobalVlanClient.java +++ b/apis/cloudstack/src/main/java/org/jclouds/cloudstack/features/GlobalVlanClient.java @@ -18,8 +18,11 @@ */ package org.jclouds.cloudstack.features; +import org.jclouds.cloudstack.domain.VlanIPRange; +import org.jclouds.cloudstack.options.ListVlanIPRangesOptions; import org.jclouds.concurrent.Timeout; +import java.util.Set; import java.util.concurrent.TimeUnit; /** @@ -33,4 +36,19 @@ import java.util.concurrent.TimeUnit; @Timeout(duration = 60, timeUnit = TimeUnit.SECONDS) public interface GlobalVlanClient { + /** + * Get the details of an IP range by its id. + * @param id the required IP range. + * @return the requested IP range. + */ + VlanIPRange getVlanIPRange(long id); + + /** + * Lists all VLAN IP ranges. + * + * @param options optional arguments. + * @return the list of IP ranges that match the criteria. + */ + Set listVlanIPRanges(ListVlanIPRangesOptions... options); + } diff --git a/apis/cloudstack/src/main/java/org/jclouds/cloudstack/options/ListVlanIPRangesOptions.java b/apis/cloudstack/src/main/java/org/jclouds/cloudstack/options/ListVlanIPRangesOptions.java new file mode 100644 index 0000000000..ee923076b9 --- /dev/null +++ b/apis/cloudstack/src/main/java/org/jclouds/cloudstack/options/ListVlanIPRangesOptions.java @@ -0,0 +1,122 @@ +/** + * 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; + +/** + * Options to the listVLANIPRanges() API call + * + * @author Richard Downer + */ +public class ListVlanIPRangesOptions extends AccountInDomainOptions { + + public static class Builder { + + public static ListVlanIPRangesOptions accountInDomain(String account, long domainId) { + return new ListVlanIPRangesOptions().accountInDomain(account, domainId); + } + + public static ListVlanIPRangesOptions domainId(long domainId) { + return new ListVlanIPRangesOptions().domainId(domainId); + } + + public static ListVlanIPRangesOptions forVirtualNetwork(boolean forVirtualNetwork) { + return new ListVlanIPRangesOptions().forVirtualNetwork(forVirtualNetwork); + } + + public static ListVlanIPRangesOptions id(long id) { + return new ListVlanIPRangesOptions().id(id); + } + + public static ListVlanIPRangesOptions keyword(String keyword) { + return new ListVlanIPRangesOptions().keyword(keyword); + } + + public static ListVlanIPRangesOptions networkId(long networkId) { + return new ListVlanIPRangesOptions().networkId(networkId); + } + + public static ListVlanIPRangesOptions podId(long podId) { + return new ListVlanIPRangesOptions().podId(podId); + } + + public static ListVlanIPRangesOptions vlan(String vlan) { + return new ListVlanIPRangesOptions().vlan(vlan); + } + + public static ListVlanIPRangesOptions vlan(long vlan) { + return new ListVlanIPRangesOptions().vlan(vlan+""); + } + + public static ListVlanIPRangesOptions zoneId(long zoneId) { + return new ListVlanIPRangesOptions().zoneId(zoneId); + } + } + + @Override + public ListVlanIPRangesOptions accountInDomain(String account, long domainId) { + return (ListVlanIPRangesOptions) super.accountInDomain(account, domainId); + } + + @Override + public ListVlanIPRangesOptions domainId(long domainId) { + return (ListVlanIPRangesOptions) super.domainId(domainId); + } + + public ListVlanIPRangesOptions forVirtualNetwork(boolean forVirtualNetwork) { + this.queryParameters.replaceValues("forvirtualnetwork", ImmutableSet.of(forVirtualNetwork+"")); + return this; + } + + public ListVlanIPRangesOptions id(long id) { + this.queryParameters.replaceValues("id", ImmutableSet.of(id+"")); + return this; + } + + public ListVlanIPRangesOptions keyword(String keyword) { + this.queryParameters.replaceValues("keyword", ImmutableSet.of(keyword)); + return this; + } + + public ListVlanIPRangesOptions networkId(long networkId) { + this.queryParameters.replaceValues("networkid", ImmutableSet.of(networkId+"")); + return this; + } + + public ListVlanIPRangesOptions podId(long podId) { + this.queryParameters.replaceValues("podid", ImmutableSet.of(podId+"")); + return this; + } + + public ListVlanIPRangesOptions vlan(String vlan) { + this.queryParameters.replaceValues("vlan", ImmutableSet.of(vlan)); + return this; + } + + public ListVlanIPRangesOptions vlan(long vlan) { + this.queryParameters.replaceValues("vlan", ImmutableSet.of(vlan+"")); + return this; + } + + public ListVlanIPRangesOptions zoneId(long zoneId) { + this.queryParameters.replaceValues("zoneid", ImmutableSet.of(zoneId+"")); + return this; + } +} diff --git a/apis/cloudstack/src/test/java/org/jclouds/cloudstack/features/GlobalVlanClientExpectTest.java b/apis/cloudstack/src/test/java/org/jclouds/cloudstack/features/GlobalVlanClientExpectTest.java index 8529fce4d5..242d3d9265 100644 --- a/apis/cloudstack/src/test/java/org/jclouds/cloudstack/features/GlobalVlanClientExpectTest.java +++ b/apis/cloudstack/src/test/java/org/jclouds/cloudstack/features/GlobalVlanClientExpectTest.java @@ -18,9 +18,18 @@ */ 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.VlanIPRange; +import org.jclouds.http.HttpRequest; +import org.jclouds.http.HttpResponse; import org.testng.annotations.Test; +import java.net.URI; + +import static junit.framework.Assert.assertEquals; + /** * Test the CloudStack VlanClient * @@ -29,6 +38,76 @@ import org.testng.annotations.Test; @Test(groups = "unit", testName = "GlobalVlanClientExpectTest") public class GlobalVlanClientExpectTest extends BaseCloudStackRestClientExpectTest { + public void testListVlanIpRangesWhenResponseIs2xx() { + GlobalVlanClient client = requestSendsResponse( + HttpRequest.builder() + .method("GET") + .endpoint( + URI.create("http://localhost:8080/client/api?response=json&" + + "command=listVlanIpRanges&apiKey=identity&signature=mS38BVfJjz1Y8bk0EdjJTeusJ0w%3D")) + .headers( + ImmutableMultimap.builder() + .put("Accept", "application/json") + .build()) + .build(), + HttpResponse.builder() + .statusCode(200) + .payload(payloadFromResource("/listvlaniprangesresponse.json")) + .build()); + + VlanIPRange range1 = VlanIPRange.builder() + .id(1) + .forVirtualNetwork(true) + .zoneId(1) + .vlan(127) + .account("system") + .domainId(1) + .domain("ROOT") + .gateway("10.27.27.254") + .netmask("255.255.255.0") + .startIP("10.27.27.50") + .endIP("10.27.27.100") + .networkId(200) + .build(); + + VlanIPRange range2 = VlanIPRange.builder() + .id(2) + .forVirtualNetwork(false) + .zoneId(2) + .vlan("untagged") + .account("system") + .domainId(1) + .domain("ROOT") + .podId(2) + .podName("Dev Pod 2") + .gateway("10.22.22.254") + .netmask("255.255.255.0") + .startIP("10.22.22.51") + .endIP("10.22.22.100") + .networkId(209) + .build(); + + assertEquals(client.listVlanIPRanges(), ImmutableSet.of(range1, range2)); + } + + public void testListVlanIpRangesWhenResponseIs404() { + GlobalVlanClient client = requestSendsResponse( + HttpRequest.builder() + .method("GET") + .endpoint( + URI.create("http://localhost:8080/client/api?response=json&" + + "command=listVlanIpRanges&apiKey=identity&signature=mS38BVfJjz1Y8bk0EdjJTeusJ0w%3D")) + .headers( + ImmutableMultimap.builder() + .put("Accept", "application/json") + .build()) + .build(), + HttpResponse.builder() + .statusCode(404) + .build()); + + assertEquals(client.listVlanIPRanges(), ImmutableSet.of()); + } @Override protected GlobalVlanClient clientFrom(CloudStackContext context) { diff --git a/apis/cloudstack/src/test/java/org/jclouds/cloudstack/features/GlobalVlanClientLiveTest.java b/apis/cloudstack/src/test/java/org/jclouds/cloudstack/features/GlobalVlanClientLiveTest.java index b77054fee0..a86ceab556 100644 --- a/apis/cloudstack/src/test/java/org/jclouds/cloudstack/features/GlobalVlanClientLiveTest.java +++ b/apis/cloudstack/src/test/java/org/jclouds/cloudstack/features/GlobalVlanClientLiveTest.java @@ -18,9 +18,19 @@ */ package org.jclouds.cloudstack.features; +import com.google.common.base.Strings; +import com.google.common.collect.Iterables; +import org.jclouds.cloudstack.domain.VlanIPRange; +import org.jclouds.cloudstack.options.ListVlanIPRangesOptions; import org.testng.annotations.AfterClass; import org.testng.annotations.Test; +import java.util.Set; + +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertFalse; +import static org.testng.Assert.assertTrue; + /** * Tests behavior of {@code GlobalVlanClient} * @@ -29,6 +39,30 @@ import org.testng.annotations.Test; @Test(groups = "live", singleThreaded = true, testName = "GlobalVlanClientLiveTest") public class GlobalVlanClientLiveTest extends BaseCloudStackClientLiveTest { + public void testListVlanIPRanges() throws Exception { + Set response = globalAdminClient.getVlanClient().listVlanIPRanges(); + assert null != response; + long rangeCount = response.size(); + assertTrue(rangeCount >= 0); + for (VlanIPRange range : response) { + VlanIPRange newDetails = Iterables.getOnlyElement(globalAdminClient.getVlanClient().listVlanIPRanges( + ListVlanIPRangesOptions.Builder.id(range.getId()))); + assertEquals(range, newDetails); + assertEquals(range, globalAdminClient.getVlanClient().getVlanIPRange(range.getId())); + assertFalse(range.getId() <= 0); + assertFalse(range.getZoneId() <= 0); + assertFalse(Strings.isNullOrEmpty(range.getVlan())); + assertFalse(Strings.isNullOrEmpty(range.getAccount())); + assertFalse(range.getDomainId() <= 0); + assertFalse(Strings.isNullOrEmpty(range.getDomain())); + assertFalse(Strings.isNullOrEmpty(range.getGateway())); + assertFalse(Strings.isNullOrEmpty(range.getNetmask())); + assertFalse(Strings.isNullOrEmpty(range.getStartIP())); + assertFalse(Strings.isNullOrEmpty(range.getEndIP())); + assertFalse(range.getNetworkId() <= 0); + } + } + @AfterClass public void testFixtureTearDown() { } diff --git a/apis/cloudstack/src/test/java/org/jclouds/cloudstack/options/ListVlanIPRangesOptionsTest.java b/apis/cloudstack/src/test/java/org/jclouds/cloudstack/options/ListVlanIPRangesOptionsTest.java new file mode 100644 index 0000000000..9c160ecdd8 --- /dev/null +++ b/apis/cloudstack/src/test/java/org/jclouds/cloudstack/options/ListVlanIPRangesOptionsTest.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.cloudstack.options; + +import com.google.common.collect.ImmutableList; +import org.testng.annotations.Test; + +import static org.jclouds.cloudstack.options.ListVlanIPRangesOptions.Builder.*; +import static org.testng.Assert.assertEquals; + +/** + * Tests behavior of {@code ListVlanIPRangesOptions} + * + * @author Richard Downer + */ +@Test(groups = "unit") +public class ListVlanIPRangesOptionsTest { + + public void testAccountInDomain() { + ListVlanIPRangesOptions options = new ListVlanIPRangesOptions().accountInDomain("fred", 19); + assertEquals(ImmutableList.of("fred"), options.buildQueryParameters().get("account")); + assertEquals(ImmutableList.of("19"), options.buildQueryParameters().get("domainid")); + } + + public void testAccountInDomainStatic() { + ListVlanIPRangesOptions options = accountInDomain("fred", 19); + assertEquals(ImmutableList.of("fred"), options.buildQueryParameters().get("account")); + assertEquals(ImmutableList.of("19"), options.buildQueryParameters().get("domainid")); + } + + public void testDomainId() { + ListVlanIPRangesOptions options = new ListVlanIPRangesOptions().domainId(19); + assertEquals(ImmutableList.of("19"), options.buildQueryParameters().get("domainid")); + } + + public void testDomainIdStatic() { + ListVlanIPRangesOptions options = domainId(19); + assertEquals(ImmutableList.of("19"), options.buildQueryParameters().get("domainid")); + } + + public void testForVirtualNetwork() { + ListVlanIPRangesOptions options = new ListVlanIPRangesOptions().forVirtualNetwork(true); + assertEquals(ImmutableList.of("true"), options.buildQueryParameters().get("forvirtualnetwork")); + } + + public void testForVirtualNetworkStatic() { + ListVlanIPRangesOptions options = forVirtualNetwork(true); + assertEquals(ImmutableList.of("true"), options.buildQueryParameters().get("forvirtualnetwork")); + } + + public void testId() { + ListVlanIPRangesOptions options = new ListVlanIPRangesOptions().id(6); + assertEquals(ImmutableList.of("6"), options.buildQueryParameters().get("id")); + } + + public void testIdStatic() { + ListVlanIPRangesOptions options = id(6); + assertEquals(ImmutableList.of("6"), options.buildQueryParameters().get("id")); + } + + public void testKeyword() { + ListVlanIPRangesOptions options = new ListVlanIPRangesOptions().keyword("fred"); + assertEquals(ImmutableList.of("fred"), options.buildQueryParameters().get("keyword")); + } + + public void testKeywordStatic() { + ListVlanIPRangesOptions options = keyword("fred"); + assertEquals(ImmutableList.of("fred"), options.buildQueryParameters().get("keyword")); + } + + public void testNetworkId() { + ListVlanIPRangesOptions options = new ListVlanIPRangesOptions().networkId(209); + assertEquals(ImmutableList.of("209"), options.buildQueryParameters().get("networkid")); + } + + public void testNetworkIdStatic() { + ListVlanIPRangesOptions options = networkId(209); + assertEquals(ImmutableList.of("209"), options.buildQueryParameters().get("networkid")); + } + + public void testPodId() { + ListVlanIPRangesOptions options = new ListVlanIPRangesOptions().podId(13); + assertEquals(ImmutableList.of("13"), options.buildQueryParameters().get("podid")); + } + + public void testPodIdStatic() { + ListVlanIPRangesOptions options = podId(13); + assertEquals(ImmutableList.of("13"), options.buildQueryParameters().get("podid")); + } + + public void testVlan() { + ListVlanIPRangesOptions options = new ListVlanIPRangesOptions().vlan(1001); + assertEquals(ImmutableList.of("1001"), options.buildQueryParameters().get("vlan")); + } + + public void testVlanStatic() { + ListVlanIPRangesOptions options = vlan(1001); + assertEquals(ImmutableList.of("1001"), options.buildQueryParameters().get("vlan")); + } + + public void testZoneId() { + ListVlanIPRangesOptions options = new ListVlanIPRangesOptions().zoneId(6); + assertEquals(ImmutableList.of("6"), options.buildQueryParameters().get("zoneid")); + } + + public void testZoneIdStatic() { + ListVlanIPRangesOptions options = zoneId(6); + assertEquals(ImmutableList.of("6"), options.buildQueryParameters().get("zoneid")); + } + +} From 266049780769879ba7d8708d24bdb68aa6958681 Mon Sep 17 00:00:00 2001 From: Richard Downer Date: Thu, 26 Jan 2012 14:33:41 +0200 Subject: [PATCH 4/4] Add create/deleteVlanIPRange() --- .../features/GlobalVlanAsyncClient.java | 27 ++++ .../cloudstack/features/GlobalVlanClient.java | 16 +++ .../options/CreateVlanIPRangeOptions.java | 123 +++++++++++++++++ .../features/GlobalVlanClientExpectTest.java | 68 ++++++++++ .../features/GlobalVlanClientLiveTest.java | 36 ++++- .../options/CreateVlanIPRangeOptionsTest.java | 127 ++++++++++++++++++ .../resources/createvlaniprangeresponse.json | 1 + 7 files changed, 395 insertions(+), 3 deletions(-) create mode 100644 apis/cloudstack/src/main/java/org/jclouds/cloudstack/options/CreateVlanIPRangeOptions.java create mode 100644 apis/cloudstack/src/test/java/org/jclouds/cloudstack/options/CreateVlanIPRangeOptionsTest.java create mode 100644 apis/cloudstack/src/test/resources/createvlaniprangeresponse.json diff --git a/apis/cloudstack/src/main/java/org/jclouds/cloudstack/features/GlobalVlanAsyncClient.java b/apis/cloudstack/src/main/java/org/jclouds/cloudstack/features/GlobalVlanAsyncClient.java index 7d07d1ff64..878199378a 100644 --- a/apis/cloudstack/src/main/java/org/jclouds/cloudstack/features/GlobalVlanAsyncClient.java +++ b/apis/cloudstack/src/main/java/org/jclouds/cloudstack/features/GlobalVlanAsyncClient.java @@ -21,6 +21,7 @@ package org.jclouds.cloudstack.features; import com.google.common.util.concurrent.ListenableFuture; import org.jclouds.cloudstack.domain.VlanIPRange; import org.jclouds.cloudstack.filters.QuerySigner; +import org.jclouds.cloudstack.options.CreateVlanIPRangeOptions; import org.jclouds.cloudstack.options.ListVlanIPRangesOptions; import org.jclouds.rest.annotations.ExceptionParser; import org.jclouds.rest.annotations.OnlyElement; @@ -29,6 +30,7 @@ import org.jclouds.rest.annotations.RequestFilters; import org.jclouds.rest.annotations.SelectJson; import org.jclouds.rest.functions.ReturnEmptySetOnNotFoundOr404; import org.jclouds.rest.functions.ReturnNullOnNotFoundOr404; +import org.jclouds.rest.functions.ReturnVoidOnNotFoundOr404; import javax.ws.rs.Consumes; import javax.ws.rs.GET; @@ -74,4 +76,29 @@ public interface GlobalVlanAsyncClient { @ExceptionParser(ReturnEmptySetOnNotFoundOr404.class) ListenableFuture> listVlanIPRanges(ListVlanIPRangesOptions... options); + /** + * Creates a VLAN IP range. + * + * @param startIP the beginning IP address in the VLAN IP range + * @param endIP the ending IP address in the VLAN IP range + * @param options optional arguments + * @return the newly-create IP range. + */ + @GET + @QueryParams(keys = "command", values = "createVlanIpRange") + @SelectJson("vlaniprange") + @Consumes(MediaType.APPLICATION_JSON) + @ExceptionParser(ReturnNullOnNotFoundOr404.class) + ListenableFuture createVlanIPRange(@QueryParam("startip") String startIP, @QueryParam("endip") String endIP, CreateVlanIPRangeOptions... options); + + /** + * Deletes a VLAN IP range. + * @param rangeId the id of the VLAN IP range + * @return void + */ + @GET + @QueryParams(keys = "command", values = "deleteVlanIpRange") + @Consumes(MediaType.APPLICATION_JSON) + @ExceptionParser(ReturnVoidOnNotFoundOr404.class) + ListenableFuture deleteVlanIPRange(@QueryParam("id") long rangeId); } diff --git a/apis/cloudstack/src/main/java/org/jclouds/cloudstack/features/GlobalVlanClient.java b/apis/cloudstack/src/main/java/org/jclouds/cloudstack/features/GlobalVlanClient.java index 2050336b6a..3ac3f51820 100644 --- a/apis/cloudstack/src/main/java/org/jclouds/cloudstack/features/GlobalVlanClient.java +++ b/apis/cloudstack/src/main/java/org/jclouds/cloudstack/features/GlobalVlanClient.java @@ -19,6 +19,7 @@ package org.jclouds.cloudstack.features; import org.jclouds.cloudstack.domain.VlanIPRange; +import org.jclouds.cloudstack.options.CreateVlanIPRangeOptions; import org.jclouds.cloudstack.options.ListVlanIPRangesOptions; import org.jclouds.concurrent.Timeout; @@ -51,4 +52,19 @@ public interface GlobalVlanClient { */ Set listVlanIPRanges(ListVlanIPRangesOptions... options); + /** + * Creates a VLAN IP range. + * + * @param startIP the beginning IP address in the VLAN IP range + * @param endIP the ending IP address in the VLAN IP range + * @param options optional arguments + * @return the newly-create IP range. + */ + VlanIPRange createVlanIPRange(String startIP, String endIP, CreateVlanIPRangeOptions... options); + + /** + * Deletes a VLAN IP range. + * @param rangeId the id of the VLAN IP range + */ + void deleteVlanIPRange(long rangeId); } diff --git a/apis/cloudstack/src/main/java/org/jclouds/cloudstack/options/CreateVlanIPRangeOptions.java b/apis/cloudstack/src/main/java/org/jclouds/cloudstack/options/CreateVlanIPRangeOptions.java new file mode 100644 index 0000000000..f977854643 --- /dev/null +++ b/apis/cloudstack/src/main/java/org/jclouds/cloudstack/options/CreateVlanIPRangeOptions.java @@ -0,0 +1,123 @@ +/** + * 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; + +/** + * Options to the createVlanIPRange API call. + * + * @author Richard Downer + */ +public class CreateVlanIPRangeOptions extends AccountInDomainOptions { + + public static class Builder { + + public static CreateVlanIPRangeOptions accountInDomain(String account, long domain) { + return new CreateVlanIPRangeOptions().accountInDomain(account, domain); + } + + public static CreateVlanIPRangeOptions domainId(long domainId) { + return new CreateVlanIPRangeOptions().domainId(domainId); + } + + public static CreateVlanIPRangeOptions forVirtualNetwork(boolean forVirtualNetwork) { + return new CreateVlanIPRangeOptions().forVirtualNetwork(forVirtualNetwork); + } + + public static CreateVlanIPRangeOptions zoneId(long zoneId) { + return new CreateVlanIPRangeOptions().zoneId(zoneId); + } + + public static CreateVlanIPRangeOptions vlan(long vlan) { + return new CreateVlanIPRangeOptions().vlan(vlan); + } + + public static CreateVlanIPRangeOptions vlan(String vlan) { + return new CreateVlanIPRangeOptions().vlan(vlan); + } + + public static CreateVlanIPRangeOptions podId(long podId) { + return new CreateVlanIPRangeOptions().podId(podId); + } + + public static CreateVlanIPRangeOptions gateway(String gateway) { + return new CreateVlanIPRangeOptions().gateway(gateway); + } + + public static CreateVlanIPRangeOptions netmask(String netmask) { + return new CreateVlanIPRangeOptions().netmask(netmask); + } + + public static CreateVlanIPRangeOptions networkId(long networkId) { + return new CreateVlanIPRangeOptions().networkId(networkId); + } + + } + + @Override + public CreateVlanIPRangeOptions accountInDomain(String account, long domain) { + return (CreateVlanIPRangeOptions) super.accountInDomain(account, domain); + } + + @Override + public CreateVlanIPRangeOptions domainId(long domainId) { + return (CreateVlanIPRangeOptions) super.domainId(domainId); + } + + public CreateVlanIPRangeOptions forVirtualNetwork(boolean forVirtualNetwork) { + this.queryParameters.replaceValues("forvirtualnetwork", ImmutableSet.of(forVirtualNetwork+"")); + return this; + } + + public CreateVlanIPRangeOptions zoneId(long zoneId) { + this.queryParameters.replaceValues("zoneid", ImmutableSet.of(zoneId+"")); + return this; + } + + public CreateVlanIPRangeOptions vlan(long vlan) { + this.queryParameters.replaceValues("vlan", ImmutableSet.of(vlan+"")); + return this; + } + + public CreateVlanIPRangeOptions vlan(String vlan) { + this.queryParameters.replaceValues("vlan", ImmutableSet.of(vlan)); + return this; + } + + public CreateVlanIPRangeOptions podId(long podId) { + this.queryParameters.replaceValues("podid", ImmutableSet.of(podId+"")); + return this; + } + + public CreateVlanIPRangeOptions gateway(String gateway) { + this.queryParameters.replaceValues("gateway", ImmutableSet.of(gateway)); + return this; + } + + public CreateVlanIPRangeOptions netmask(String netmask) { + this.queryParameters.replaceValues("netmask", ImmutableSet.of(netmask)); + return this; + } + + public CreateVlanIPRangeOptions networkId(long networkId) { + this.queryParameters.replaceValues("networkid", ImmutableSet.of(networkId+"")); + return this; + } +} diff --git a/apis/cloudstack/src/test/java/org/jclouds/cloudstack/features/GlobalVlanClientExpectTest.java b/apis/cloudstack/src/test/java/org/jclouds/cloudstack/features/GlobalVlanClientExpectTest.java index 242d3d9265..80cad2a989 100644 --- a/apis/cloudstack/src/test/java/org/jclouds/cloudstack/features/GlobalVlanClientExpectTest.java +++ b/apis/cloudstack/src/test/java/org/jclouds/cloudstack/features/GlobalVlanClientExpectTest.java @@ -22,6 +22,7 @@ import com.google.common.collect.ImmutableMultimap; import com.google.common.collect.ImmutableSet; import org.jclouds.cloudstack.CloudStackContext; import org.jclouds.cloudstack.domain.VlanIPRange; +import org.jclouds.cloudstack.options.CreateVlanIPRangeOptions; import org.jclouds.http.HttpRequest; import org.jclouds.http.HttpResponse; import org.testng.annotations.Test; @@ -109,6 +110,73 @@ public class GlobalVlanClientExpectTest extends BaseCloudStackRestClientExpectTe assertEquals(client.listVlanIPRanges(), ImmutableSet.of()); } + public void testCreateVlanIpRangeWhenResponseIs2xx() { + GlobalVlanClient client = requestSendsResponse( + HttpRequest.builder() + .method("GET") + .endpoint( + URI.create("http://localhost:8080/client/api?response=json&" + + "command=createVlanIpRange&startip=10.22.22.51&endip=10.22.22.100&forvirtualnetwork=false&zoneid=2&vlan=untagged&account=system&domainid=1&podid=2&gateway=10.22.22.254&netmask=255.255.255.0&networkid=209&apiKey=identity&signature=XgDjPYAQNLMVCuSMGRA6QjV8mOY%3D")) + .headers( + ImmutableMultimap.builder() + .put("Accept", "application/json") + .build()) + .build(), + HttpResponse.builder() + .statusCode(200) + .payload(payloadFromResource("/createvlaniprangeresponse.json")) + .build()); + + VlanIPRange actual = client.createVlanIPRange("10.22.22.51", "10.22.22.100", new CreateVlanIPRangeOptions() + .forVirtualNetwork(false) + .zoneId(2) + .vlan("untagged") + .accountInDomain("system", 1) + .podId(2) + .gateway("10.22.22.254") + .netmask("255.255.255.0") + .networkId(209)); + + VlanIPRange expected = VlanIPRange.builder() + .id(2) + .forVirtualNetwork(false) + .zoneId(2) + .vlan("untagged") + .account("system") + .domainId(1) + .domain("ROOT") + .podId(2) + .podName("Dev Pod 2") + .gateway("10.22.22.254") + .netmask("255.255.255.0") + .startIP("10.22.22.51") + .endIP("10.22.22.100") + .networkId(209) + .build(); + + assertEquals(actual, expected); + } + + public void testDeleteVlanIpRangeWhenResponseIs2xx() { + GlobalVlanClient client = requestSendsResponse( + HttpRequest.builder() + .method("GET") + .endpoint( + URI.create("http://localhost:8080/client/api?response=json&" + + "command=deleteVlanIpRange&id=1&apiKey=identity&signature=tTBbpdCndgHXdR397fbbJaN1RZU%3D")) + .headers( + ImmutableMultimap.builder() + .put("Accept", "application/json") + .build()) + .build(), + HttpResponse.builder() + .statusCode(200) + .payload(payloadFromResource("/createvlaniprangeresponse.json")) + .build()); + + client.deleteVlanIPRange(1); + } + @Override protected GlobalVlanClient clientFrom(CloudStackContext context) { return context.getGlobalContext().getApi().getVlanClient(); diff --git a/apis/cloudstack/src/test/java/org/jclouds/cloudstack/features/GlobalVlanClientLiveTest.java b/apis/cloudstack/src/test/java/org/jclouds/cloudstack/features/GlobalVlanClientLiveTest.java index a86ceab556..af3e539ba9 100644 --- a/apis/cloudstack/src/test/java/org/jclouds/cloudstack/features/GlobalVlanClientLiveTest.java +++ b/apis/cloudstack/src/test/java/org/jclouds/cloudstack/features/GlobalVlanClientLiveTest.java @@ -20,16 +20,21 @@ package org.jclouds.cloudstack.features; import com.google.common.base.Strings; import com.google.common.collect.Iterables; +import org.jclouds.cloudstack.domain.Network; +import org.jclouds.cloudstack.domain.NetworkOffering; import org.jclouds.cloudstack.domain.VlanIPRange; +import org.jclouds.cloudstack.domain.Zone; +import org.jclouds.cloudstack.options.CreateVlanIPRangeOptions; import org.jclouds.cloudstack.options.ListVlanIPRangesOptions; +import org.jclouds.cloudstack.predicates.NetworkOfferingPredicates; +import org.jclouds.cloudstack.predicates.ZonePredicates; import org.testng.annotations.AfterClass; import org.testng.annotations.Test; import java.util.Set; -import static org.testng.Assert.assertEquals; -import static org.testng.Assert.assertFalse; -import static org.testng.Assert.assertTrue; +import static com.google.common.collect.Iterables.find; +import static org.testng.Assert.*; /** * Tests behavior of {@code GlobalVlanClient} @@ -39,6 +44,9 @@ import static org.testng.Assert.assertTrue; @Test(groups = "live", singleThreaded = true, testName = "GlobalVlanClientLiveTest") public class GlobalVlanClientLiveTest extends BaseCloudStackClientLiveTest { + private Network network; + private VlanIPRange range; + public void testListVlanIPRanges() throws Exception { Set response = globalAdminClient.getVlanClient().listVlanIPRanges(); assert null != response; @@ -63,8 +71,30 @@ public class GlobalVlanClientLiveTest extends BaseCloudStackClientLiveTest { } } + public void testCreateVlanIPRange() { + Zone zone = Iterables.find(client.getZoneClient().listZones(), ZonePredicates.supportsAdvancedNetworks()); + NetworkOffering offering = find(client.getOfferingClient().listNetworkOfferings(), NetworkOfferingPredicates.supportsGuestVirtualNetworks()); + + network = client.getNetworkClient().createNetworkInZone(zone.getId(), offering.getId(), "net-"+prefix, "jclouds test "+prefix); + + range = globalAdminClient.getVlanClient().createVlanIPRange("172.19.1.1", "172.19.1.199", CreateVlanIPRangeOptions.Builder + .accountInDomain(user.getAccount(), user.getDomainId()) + .forVirtualNetwork(true) + .vlan(1001) + .networkId(network.getId()) + ); + } + @AfterClass public void testFixtureTearDown() { + if (range != null) { + globalAdminClient.getVlanClient().deleteVlanIPRange(range.getId()); + range = null; + } + if (network != null) { + client.getNetworkClient().deleteNetwork(network.getId()); + network = null; + } } } diff --git a/apis/cloudstack/src/test/java/org/jclouds/cloudstack/options/CreateVlanIPRangeOptionsTest.java b/apis/cloudstack/src/test/java/org/jclouds/cloudstack/options/CreateVlanIPRangeOptionsTest.java new file mode 100644 index 0000000000..4a072a4999 --- /dev/null +++ b/apis/cloudstack/src/test/java/org/jclouds/cloudstack/options/CreateVlanIPRangeOptionsTest.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.cloudstack.options; + +import com.google.common.collect.ImmutableSet; +import org.testng.annotations.Test; + +import static org.jclouds.cloudstack.options.CreateVlanIPRangeOptions.Builder.*; +import static org.testng.Assert.assertEquals; + +/** + * Tests behavior of {@code CreateVlanIPRangeOptions} + * + * @author Richard Downer + */ +@Test(groups = "unit") +public class CreateVlanIPRangeOptionsTest { + + public void testAccountInDomain() { + CreateVlanIPRangeOptions options = new CreateVlanIPRangeOptions().accountInDomain("fred", 6); + assertEquals(ImmutableSet.of("fred"), options.buildQueryParameters().get("account")); + assertEquals(ImmutableSet.of("6"), options.buildQueryParameters().get("domainid")); + } + + public void testAccountInDomainStatic() { + CreateVlanIPRangeOptions options = accountInDomain("fred", 6); + assertEquals(ImmutableSet.of("fred"), options.buildQueryParameters().get("account")); + assertEquals(ImmutableSet.of("6"), options.buildQueryParameters().get("domainid")); + } + + public void testDomainId() { + CreateVlanIPRangeOptions options = new CreateVlanIPRangeOptions().domainId(6); + assertEquals(ImmutableSet.of("6"), options.buildQueryParameters().get("domainid")); + } + + public void testDomainIdStatic() { + CreateVlanIPRangeOptions options = domainId(6); + assertEquals(ImmutableSet.of("6"), options.buildQueryParameters().get("domainid")); + } + + public void testForVirtualNetwork() { + CreateVlanIPRangeOptions options = new CreateVlanIPRangeOptions().forVirtualNetwork(true); + assertEquals(ImmutableSet.of("true"), options.buildQueryParameters().get("forvirtualnetwork")); + } + + public void testForVirtualNetworkStatic() { + CreateVlanIPRangeOptions options = forVirtualNetwork(true); + assertEquals(ImmutableSet.of("true"), options.buildQueryParameters().get("forvirtualnetwork")); + } + + public void testGateway() { + CreateVlanIPRangeOptions options = new CreateVlanIPRangeOptions().gateway("192.168.42.24"); + assertEquals(ImmutableSet.of("192.168.42.24"), options.buildQueryParameters().get("gateway")); + } + + public void testGatewayStatic() { + CreateVlanIPRangeOptions options = gateway("192.168.42.24"); + assertEquals(ImmutableSet.of("192.168.42.24"), options.buildQueryParameters().get("gateway")); + } + + public void testNetmask() { + CreateVlanIPRangeOptions options = new CreateVlanIPRangeOptions().netmask("255.255.255.240"); + assertEquals(ImmutableSet.of("255.255.255.240"), options.buildQueryParameters().get("netmask")); + } + + public void testNetmaskStatic() { + CreateVlanIPRangeOptions options = netmask("255.255.255.240"); + assertEquals(ImmutableSet.of("255.255.255.240"), options.buildQueryParameters().get("netmask")); + } + + public void testNetworkId() { + CreateVlanIPRangeOptions options = new CreateVlanIPRangeOptions().networkId(9); + assertEquals(ImmutableSet.of("9"), options.buildQueryParameters().get("networkid")); + } + + public void testNetworkIdStatic() { + CreateVlanIPRangeOptions options = networkId(9); + assertEquals(ImmutableSet.of("9"), options.buildQueryParameters().get("networkid")); + } + + public void testPodId() { + CreateVlanIPRangeOptions options = new CreateVlanIPRangeOptions().podId(8); + assertEquals(ImmutableSet.of("8"), options.buildQueryParameters().get("podid")); + } + + public void testPodIdStatic() { + CreateVlanIPRangeOptions options = podId(8); + assertEquals(ImmutableSet.of("8"), options.buildQueryParameters().get("podid")); + } + + public void testVlan() { + CreateVlanIPRangeOptions options = new CreateVlanIPRangeOptions().vlan(1001); + assertEquals(ImmutableSet.of("1001"), options.buildQueryParameters().get("vlan")); + } + + public void testVlanStatic() { + CreateVlanIPRangeOptions options = vlan(1001); + assertEquals(ImmutableSet.of("1001"), options.buildQueryParameters().get("vlan")); + } + + public void testZoneId() { + CreateVlanIPRangeOptions options = new CreateVlanIPRangeOptions().zoneId(7); + assertEquals(ImmutableSet.of("7"), options.buildQueryParameters().get("zoneid")); + } + + public void testZoneIdStatic() { + CreateVlanIPRangeOptions options = zoneId(7); + assertEquals(ImmutableSet.of("7"), options.buildQueryParameters().get("zoneid")); + } + +} diff --git a/apis/cloudstack/src/test/resources/createvlaniprangeresponse.json b/apis/cloudstack/src/test/resources/createvlaniprangeresponse.json new file mode 100644 index 0000000000..b463074874 --- /dev/null +++ b/apis/cloudstack/src/test/resources/createvlaniprangeresponse.json @@ -0,0 +1 @@ +{ "createvlaniprangeresponse" : { "vlaniprange" : {"id":2,"forvirtualnetwork":false,"zoneid":2,"vlan":"untagged","account":"system","domainid":1,"domain":"ROOT","podid":2,"podname":"Dev Pod 2","gateway":"10.22.22.254","netmask":"255.255.255.0","startip":"10.22.22.51","endip":"10.22.22.100","networkid":209} } } \ No newline at end of file