diff --git a/sandbox-apis/cloudstack/src/main/java/org/jclouds/cloudstack/features/NetworkAsyncClient.java b/sandbox-apis/cloudstack/src/main/java/org/jclouds/cloudstack/features/NetworkAsyncClient.java index 2754c6933e..20021c1dc7 100644 --- a/sandbox-apis/cloudstack/src/main/java/org/jclouds/cloudstack/features/NetworkAsyncClient.java +++ b/sandbox-apis/cloudstack/src/main/java/org/jclouds/cloudstack/features/NetworkAsyncClient.java @@ -88,7 +88,7 @@ public interface NetworkAsyncClient { */ @GET @QueryParams(keys = "command", values = "deleteNetwork") - @SelectJson("network") + @SelectJson("jobid") @Consumes(MediaType.APPLICATION_JSON) @ExceptionParser(ReturnNullOnNotFoundOr404.class) ListenableFuture deleteNetwork(@QueryParam("id") long id); diff --git a/sandbox-apis/cloudstack/src/main/java/org/jclouds/cloudstack/options/ListNetworksOptions.java b/sandbox-apis/cloudstack/src/main/java/org/jclouds/cloudstack/options/ListNetworksOptions.java index c29a1e0eb0..dd031cfb55 100644 --- a/sandbox-apis/cloudstack/src/main/java/org/jclouds/cloudstack/options/ListNetworksOptions.java +++ b/sandbox-apis/cloudstack/src/main/java/org/jclouds/cloudstack/options/ListNetworksOptions.java @@ -20,7 +20,6 @@ package org.jclouds.cloudstack.options; import org.jclouds.cloudstack.domain.NetworkType; import org.jclouds.cloudstack.domain.TrafficType; -import org.jclouds.http.options.BaseHttpRequestOptions; import com.google.common.collect.ImmutableSet; @@ -32,7 +31,7 @@ import com.google.common.collect.ImmutableSet; * /> * @author Adrian Cole */ -public class ListNetworksOptions extends BaseHttpRequestOptions { +public class ListNetworksOptions extends AccountInDomainOptions { public static final ListNetworksOptions NONE = new ListNetworksOptions(); @@ -81,27 +80,6 @@ public class ListNetworksOptions extends BaseHttpRequestOptions { return this; } - /** - * - * @param account - * account who will own the VLAN. If VLAN is Zone wide, this - * parameter should be ommited - */ - public ListNetworksOptions account(String account) { - this.queryParameters.replaceValues("account", ImmutableSet.of(account)); - return this; - } - - /** - * @param domainId - * domain ID of the account owning a VLAN - */ - public ListNetworksOptions domainId(long domainId) { - this.queryParameters.replaceValues("domainid", ImmutableSet.of(domainId + "")); - return this; - - } - /** * @param zoneId * the Zone ID of the network @@ -121,6 +99,22 @@ public class ListNetworksOptions extends BaseHttpRequestOptions { return this; } + /** + * {@inheritDoc} + */ + @Override + public ListNetworksOptions accountInDomain(String account, long domain) { + return ListNetworksOptions.class.cast(super.accountInDomain(account, domain)); + } + + /** + * {@inheritDoc} + */ + @Override + public ListNetworksOptions domainId(long domainId) { + return ListNetworksOptions.class.cast(super.domainId(domainId)); + } + public static class Builder { /** * @see ListNetworksOptions#isDefault @@ -154,22 +148,6 @@ public class ListNetworksOptions extends BaseHttpRequestOptions { return options.type(type); } - /** - * @see ListNetworksOptions#domainId - */ - public static ListNetworksOptions domainId(long id) { - ListNetworksOptions options = new ListNetworksOptions(); - return options.domainId(id); - } - - /** - * @see ListNetworksOptions#account - */ - public static ListNetworksOptions account(String account) { - ListNetworksOptions options = new ListNetworksOptions(); - return options.account(account); - } - /** * @see ListNetworksOptions#id */ @@ -193,6 +171,22 @@ public class ListNetworksOptions extends BaseHttpRequestOptions { ListNetworksOptions options = new ListNetworksOptions(); return options.trafficType(trafficType); } + + /** + * @see ListNetworksOptions#accountInDomain + */ + public static ListNetworksOptions accountInDomain(String account, long domain) { + ListNetworksOptions options = new ListNetworksOptions(); + return options.accountInDomain(account, domain); + } + + /** + * @see ListNetworksOptions#domainId + */ + public static ListNetworksOptions domainId(long domainId) { + ListNetworksOptions options = new ListNetworksOptions(); + return options.domainId(domainId); + } } } diff --git a/sandbox-apis/cloudstack/src/test/java/org/jclouds/cloudstack/compute/CloudStackComputeServiceAdapterLiveTest.java b/sandbox-apis/cloudstack/src/test/java/org/jclouds/cloudstack/compute/CloudStackComputeServiceAdapterLiveTest.java index ee15cfce49..1df66f7bd5 100644 --- a/sandbox-apis/cloudstack/src/test/java/org/jclouds/cloudstack/compute/CloudStackComputeServiceAdapterLiveTest.java +++ b/sandbox-apis/cloudstack/src/test/java/org/jclouds/cloudstack/compute/CloudStackComputeServiceAdapterLiveTest.java @@ -78,7 +78,7 @@ public class CloudStackComputeServiceAdapterLiveTest extends BaseCloudStackClien @Override protected void configure() { - bindProperties(binder(), new CloudStackPropertiesBuilder(new Properties()).build()); + bindProperties(binder(), CloudStackComputeServiceAdapterLiveTest.this.setupProperties()); bind(CloudStackClient.class).toInstance(context.getApi()); } diff --git a/sandbox-apis/cloudstack/src/test/java/org/jclouds/cloudstack/features/BaseCloudStackClientLiveTest.java b/sandbox-apis/cloudstack/src/test/java/org/jclouds/cloudstack/features/BaseCloudStackClientLiveTest.java index c44f7dfe4a..75b4737160 100644 --- a/sandbox-apis/cloudstack/src/test/java/org/jclouds/cloudstack/features/BaseCloudStackClientLiveTest.java +++ b/sandbox-apis/cloudstack/src/test/java/org/jclouds/cloudstack/features/BaseCloudStackClientLiveTest.java @@ -74,6 +74,7 @@ public class BaseCloudStackClientLiveTest extends BaseVersionedServiceLiveTest { protected RetryablePredicate virtualMachineRunning; protected RetryablePredicate virtualMachineDestroyed; protected SshClient.Factory sshFactory; + protected User currentUser; protected String password = "password"; protected Injector injector; @@ -82,6 +83,7 @@ public class BaseCloudStackClientLiveTest extends BaseVersionedServiceLiveTest { protected ComputeServiceContext computeContext; + protected void checkSSH(IPSocket socket) { socketTester.apply(socket); SshClient client = sshFactory.create(socket, new Credentials("root", password)); @@ -108,7 +110,6 @@ public class BaseCloudStackClientLiveTest extends BaseVersionedServiceLiveTest { client = context.getApi(); // check access Iterable users = Iterables.concat(client.getAccountClient().listAccounts()); - User currentUser; Predicate apiKeyMatches = UserPredicates.apiKeyEquals(identity); try { currentUser = Iterables.find(users, apiKeyMatches); diff --git a/sandbox-apis/cloudstack/src/test/java/org/jclouds/cloudstack/features/NetworkClientLiveTest.java b/sandbox-apis/cloudstack/src/test/java/org/jclouds/cloudstack/features/NetworkClientLiveTest.java index f5849678c7..479acd8c1c 100644 --- a/sandbox-apis/cloudstack/src/test/java/org/jclouds/cloudstack/features/NetworkClientLiveTest.java +++ b/sandbox-apis/cloudstack/src/test/java/org/jclouds/cloudstack/features/NetworkClientLiveTest.java @@ -19,6 +19,10 @@ package org.jclouds.cloudstack.features; import static com.google.common.collect.Iterables.find; +import static com.google.common.collect.Iterables.getOnlyElement; +import static org.jclouds.cloudstack.options.ListNetworksOptions.Builder.accountInDomain; +import static org.jclouds.cloudstack.options.ListNetworksOptions.Builder.id; +import static org.jclouds.cloudstack.options.ListNetworksOptions.Builder.zoneId; import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertTrue; @@ -30,14 +34,13 @@ import org.jclouds.cloudstack.domain.GuestIPType; import org.jclouds.cloudstack.domain.Network; import org.jclouds.cloudstack.domain.NetworkOffering; import org.jclouds.cloudstack.domain.Zone; -import org.jclouds.cloudstack.options.ListNetworksOptions; import org.jclouds.cloudstack.predicates.NetworkOfferingPredicates; import org.jclouds.cloudstack.predicates.ZonePredicates; import org.testng.annotations.AfterGroups; import org.testng.annotations.BeforeGroups; import org.testng.annotations.Test; -import com.google.common.collect.Iterables; +import com.google.common.base.Predicate; /** * Tests behavior of {@code NetworkClientLiveTest} @@ -54,6 +57,9 @@ public class NetworkClientLiveTest extends BaseCloudStackClientLiveTest { private Network network; + // only delete networks we create + private boolean weCreatedNetwork; + @BeforeGroups(groups = "live") public void setupClient() { super.setupClient(); @@ -62,7 +68,7 @@ public class NetworkClientLiveTest extends BaseCloudStackClientLiveTest { // you can create guest direct network by Admin user, but since we are // not admin, let's try to create a guest virtual one zone = find(client.getZoneClient().listZones(), ZonePredicates.supportsGuestVirtualNetworks()); - offering = Iterables.find(client.getOfferingClient().listNetworkOfferings(), + offering = find(client.getOfferingClient().listNetworkOfferings(), NetworkOfferingPredicates.supportsGuestVirtualNetworks()); networksSupported = true; } catch (NoSuchElementException e) { @@ -72,34 +78,39 @@ public class NetworkClientLiveTest extends BaseCloudStackClientLiveTest { public void testCreateNetwork() throws Exception { if (!networksSupported) return; - network = client.getNetworkClient().createNetworkInZone(zone.getId(), offering.getId(), prefix, prefix); + try { + network = client.getNetworkClient().createNetworkInZone(zone.getId(), offering.getId(), prefix, prefix); + weCreatedNetwork = true; + } catch (IllegalStateException e) { + network = find( + client.getNetworkClient().listNetworks( + zoneId(zone.getId()).accountInDomain(currentUser.getAccount(), currentUser.getDomainId())), + new Predicate() { + + @Override + public boolean apply(Network arg0) { + return arg0.getNetworkOfferingId() == offering.getId(); + } + + }); + } checkNetwork(network); } - @AfterGroups(groups = "live") - protected void tearDown() { - if (network != null) { - Long jobId = client.getNetworkClient().deleteNetwork(network.getId()); - if (jobId != null) - jobComplete.apply(jobId); - } - super.tearDown(); - } - + @Test(dependsOnMethods = "testCreateNetwork") public void testListNetworks() throws Exception { if (!networksSupported) return; - Set response = client.getNetworkClient().listNetworks(); + Set response = client.getNetworkClient().listNetworks( + accountInDomain(network.getAccount(), network.getDomainId())); assert null != response; long networkCount = response.size(); assertTrue(networkCount >= 0); for (Network network : response) { - Network newDetails = Iterables.getOnlyElement(client.getNetworkClient().listNetworks( - ListNetworksOptions.Builder.id(network.getId()))); + Network newDetails = getOnlyElement(client.getNetworkClient().listNetworks(id(network.getId()))); assertEquals(network, newDetails); assertEquals(network, client.getNetworkClient().getNetwork(network.getId())); checkNetwork(network); - } } @@ -142,4 +153,15 @@ public class NetworkClientLiveTest extends BaseCloudStackClientLiveTest { break; } } + + @AfterGroups(groups = "live") + protected void tearDown() { + if (network != null && weCreatedNetwork) { + Long jobId = client.getNetworkClient().deleteNetwork(network.getId()); + if (jobId != null) + jobComplete.apply(jobId); + } + super.tearDown(); + } + } diff --git a/sandbox-apis/cloudstack/src/test/java/org/jclouds/cloudstack/options/ListNetworksOptionsTest.java b/sandbox-apis/cloudstack/src/test/java/org/jclouds/cloudstack/options/ListNetworksOptionsTest.java index 2339d6ef05..34b7df66dd 100644 --- a/sandbox-apis/cloudstack/src/test/java/org/jclouds/cloudstack/options/ListNetworksOptionsTest.java +++ b/sandbox-apis/cloudstack/src/test/java/org/jclouds/cloudstack/options/ListNetworksOptionsTest.java @@ -18,7 +18,7 @@ */ package org.jclouds.cloudstack.options; -import static org.jclouds.cloudstack.options.ListNetworksOptions.Builder.account; +import static org.jclouds.cloudstack.options.ListNetworksOptions.Builder.accountInDomain; import static org.jclouds.cloudstack.options.ListNetworksOptions.Builder.domainId; import static org.jclouds.cloudstack.options.ListNetworksOptions.Builder.id; import static org.jclouds.cloudstack.options.ListNetworksOptions.Builder.isDefault; @@ -94,13 +94,14 @@ public class ListNetworksOptionsTest { } public void testAccountId() { - ListNetworksOptions options = new ListNetworksOptions().account("moo"); - assertEquals(ImmutableList.of("moo"), options.buildQueryParameters().get("account")); + ListNetworksOptions options = new ListNetworksOptions().accountInDomain("moo", 1); + assertEquals(ImmutableList.of("1"), options.buildQueryParameters().get("domainid")); } public void testAccountIdStatic() { - ListNetworksOptions options = account("moo"); + ListNetworksOptions options = accountInDomain("moo", 1l); assertEquals(ImmutableList.of("moo"), options.buildQueryParameters().get("account")); + assertEquals(ImmutableList.of("1"), options.buildQueryParameters().get("domainid")); } public void testTrafficType() { diff --git a/sandbox-apis/cloudstack/src/test/java/org/jclouds/cloudstack/parse/DeleteNetworkResponseTest.java b/sandbox-apis/cloudstack/src/test/java/org/jclouds/cloudstack/parse/DeleteNetworkResponseTest.java new file mode 100644 index 0000000000..5c2c50b0c0 --- /dev/null +++ b/sandbox-apis/cloudstack/src/test/java/org/jclouds/cloudstack/parse/DeleteNetworkResponseTest.java @@ -0,0 +1,43 @@ +/** + * 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 org.jclouds.json.BaseItemParserTest; +import org.jclouds.rest.annotations.SelectJson; +import org.testng.annotations.Test; + +/** + * + * @author Adrian Cole + */ +@Test(groups = "unit", testName = "DeleteNetworkResponseTest") +public class DeleteNetworkResponseTest extends BaseItemParserTest { + + @Override + public String resource() { + return "/deletenetworkresponse.json"; + } + + @Override + @SelectJson("jobid") + public Long expected() { + return 45612l; + } + +} diff --git a/sandbox-apis/cloudstack/src/test/resources/deletenetworkresponse.json b/sandbox-apis/cloudstack/src/test/resources/deletenetworkresponse.json new file mode 100644 index 0000000000..8902011c31 --- /dev/null +++ b/sandbox-apis/cloudstack/src/test/resources/deletenetworkresponse.json @@ -0,0 +1 @@ +{ "deletenetworkresponse" : {"jobid":45612} } \ No newline at end of file