fixed network predicates

This commit is contained in:
Adrian Cole 2011-05-10 17:16:53 -07:00
parent 67d5d8f1e7
commit 6ca5a99484
6 changed files with 173 additions and 79 deletions

View File

@ -50,6 +50,7 @@ public class NetworkOffering implements Comparable<NetworkOffering> {
private boolean isDefault; private boolean isDefault;
private boolean supportsVLAN; private boolean supportsVLAN;
private TrafficType trafficType; private TrafficType trafficType;
private GuestIPType guestIPType;
private Set<String> tags = ImmutableSet.of(); private Set<String> tags = ImmutableSet.of();
public Builder id(long id) { public Builder id(long id) {
@ -102,6 +103,11 @@ public class NetworkOffering implements Comparable<NetworkOffering> {
return this; return this;
} }
public Builder guestIPType(GuestIPType guestIPType) {
this.guestIPType = guestIPType;
return this;
}
public Builder tags(Set<String> tags) { public Builder tags(Set<String> tags) {
this.tags = ImmutableSet.copyOf(checkNotNull(tags, "tags")); this.tags = ImmutableSet.copyOf(checkNotNull(tags, "tags"));
return this; return this;
@ -109,7 +115,7 @@ public class NetworkOffering implements Comparable<NetworkOffering> {
public NetworkOffering build() { public NetworkOffering build() {
return new NetworkOffering(id, name, displayText, created, availability, supportsVLAN, maxConnections, return new NetworkOffering(id, name, displayText, created, availability, supportsVLAN, maxConnections,
isDefault, trafficType, networkRate, tags); isDefault, trafficType, guestIPType, networkRate, tags);
} }
} }
@ -127,13 +133,15 @@ public class NetworkOffering implements Comparable<NetworkOffering> {
private boolean supportsVLAN; private boolean supportsVLAN;
@SerializedName("traffictype") @SerializedName("traffictype")
private TrafficType trafficType; private TrafficType trafficType;
@SerializedName("guestiptype")
private GuestIPType guestIPType;
@SerializedName("networkrate") @SerializedName("networkrate")
private int networkRate = -1; private int networkRate = -1;
private String tags; private String tags;
public NetworkOffering(long id, String name, String displayText, @Nullable Date created, String availability, public NetworkOffering(long id, String name, String displayText, @Nullable Date created, String availability,
boolean supportsVLAN, @Nullable Integer maxConnections, boolean isDefault, TrafficType trafficType, boolean supportsVLAN, @Nullable Integer maxConnections, boolean isDefault, TrafficType trafficType,
int networkRate, Set<String> tags) { GuestIPType guestIPType, int networkRate, Set<String> tags) {
this.id = id; this.id = id;
this.name = name; this.name = name;
this.displayText = displayText; this.displayText = displayText;
@ -143,6 +151,7 @@ public class NetworkOffering implements Comparable<NetworkOffering> {
this.maxConnections = maxConnections; this.maxConnections = maxConnections;
this.isDefault = isDefault; this.isDefault = isDefault;
this.trafficType = trafficType; this.trafficType = trafficType;
this.guestIPType = guestIPType;
this.networkRate = networkRate; this.networkRate = networkRate;
this.tags = tags.size() == 0 ? null : Joiner.on(',').join(tags); this.tags = tags.size() == 0 ? null : Joiner.on(',').join(tags);
} }
@ -207,7 +216,8 @@ public class NetworkOffering implements Comparable<NetworkOffering> {
/** /**
* *
* @return the max number of concurrent connection the network offering supports * @return the max number of concurrent connection the network offering
* supports
*/ */
@Nullable @Nullable
public Integer getMaxConnections() { public Integer getMaxConnections() {
@ -230,6 +240,14 @@ public class NetworkOffering implements Comparable<NetworkOffering> {
return trafficType; return trafficType;
} }
/**
*
* @return the guest ip type for this network offering
*/
public GuestIPType getGuestIPType() {
return guestIPType;
}
/** /**
* *
* @return data transfer rate in megabits per second allowed. * @return data transfer rate in megabits per second allowed.

View File

@ -0,0 +1,61 @@
/**
*
* Copyright (C) 2011 Cloud Conscious, LLC. <info@cloudconscious.com>
*
* ====================================================================
* Licensed 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.predicates;
import static com.google.common.base.Predicates.alwaysTrue;
import org.jclouds.cloudstack.domain.GuestIPType;
import org.jclouds.cloudstack.domain.NetworkOffering;
import org.jclouds.cloudstack.domain.TrafficType;
import com.google.common.base.Predicate;
/**
*
* @author Adrian Cole
*/
public class NetworkOfferingPredicates {
/**
*
* @return true, if the offering supports creation of GuestVirtual Networks
*/
public static Predicate<NetworkOffering> supportsGuestVirtualNetworks() {
return new Predicate<NetworkOffering>() {
@Override
public boolean apply(NetworkOffering arg0) {
return arg0.getTrafficType() == TrafficType.GUEST && arg0.getGuestIPType() == GuestIPType.VIRTUAL;
}
@Override
public String toString() {
return "supportsGuestVirtualNetworks()";
}
};
}
/**
*
* @return always returns true.
*/
public static Predicate<NetworkOffering> any() {
return alwaysTrue();
}
}

View File

@ -19,46 +19,64 @@
package org.jclouds.cloudstack.predicates; package org.jclouds.cloudstack.predicates;
import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Predicates.alwaysTrue;
import static com.google.common.base.Predicates.and;
import static com.google.common.base.Predicates.not;
import org.jclouds.cloudstack.domain.NetworkType; import org.jclouds.cloudstack.domain.NetworkType;
import org.jclouds.cloudstack.domain.Zone; import org.jclouds.cloudstack.domain.Zone;
import com.google.common.base.Predicate; import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
/** /**
* *
* @author Adrian Cole * @author Adrian Cole
*/ */
public class ZonePredicates { public class ZonePredicates {
public static class SupportsNetworkType implements Predicate<Zone> {
private final org.jclouds.cloudstack.domain.NetworkType type;
public SupportsNetworkType(org.jclouds.cloudstack.domain.NetworkType type) {
this.type = checkNotNull(type, "type");
}
@Override
public boolean apply(Zone input) {
return type.equals(checkNotNull(input, "zone").getNetworkType());
}
@Override
public String toString() {
return "supportsNetworkType(" + type + ")";
}
}
static Predicate<Zone> supportsAdvancedNetworks = new SupportsNetworkType(
org.jclouds.cloudstack.domain.NetworkType.ADVANCED);
/** /**
* *
* @return true, if the zone supports {@link NetworkType.ADVANCED} * @return true, if the zone supports {@link NetworkType.ADVANCED}
*/ */
public static Predicate<Zone> supportsAdvancedNetworks() { public static Predicate<Zone> supportsAdvancedNetworks() {
return supportsAdvancedNetworks; return new Predicate<Zone>() {
@Override
public boolean apply(Zone input) {
return NetworkType.ADVANCED.equals(checkNotNull(input, "zone").getNetworkType());
}
@Override
public String toString() {
return "supportsAvancedNetworks()";
}
};
}
/**
*
* @return true, if the zone supports security groups
*/
public static Predicate<Zone> supportsSecurityGroups() {
return new Predicate<Zone>() {
@Override
public boolean apply(Zone input) {
return input.isSecurityGroupsEnabled();
}
@Override
public String toString() {
return "supportsSecurityGroups()";
}
};
}
/**
*
* @return true, if the zone supports creation of GuestVirtual Networks
*/
public static Predicate<Zone> supportsGuestVirtualNetworks() {
return and(supportsAdvancedNetworks(), not(supportsSecurityGroups()));
} }
/** /**
@ -66,6 +84,6 @@ public class ZonePredicates {
* @return always returns true. * @return always returns true.
*/ */
public static Predicate<Zone> any() { public static Predicate<Zone> any() {
return Predicates.alwaysTrue(); return alwaysTrue();
} }
} }

View File

@ -75,7 +75,8 @@ public class AccountClientLiveTest extends BaseCloudStackClientLiveTest {
assert account.getVMLimit() == null || account.getVMLimit() >= 0 : account; assert account.getVMLimit() == null || account.getVMLimit() >= 0 : account;
assert account.getVMsRunning() >= 0 : account; assert account.getVMsRunning() >= 0 : account;
assert account.getVMsStopped() >= 0 : account; assert account.getVMsStopped() >= 0 : account;
assert account.getVMs() >= 0 : account; // TODO update to 2.2.4 as this is a bug in 2.2.3
// assert account.getVMs() >= 0 : account;
assert account.getVolumesAvailable() == null || account.getVolumesAvailable() >= 0 : account; assert account.getVolumesAvailable() == null || account.getVolumesAvailable() >= 0 : account;
assert account.getVolumeLimit() == null || account.getVolumeLimit() >= 0 : account; assert account.getVolumeLimit() == null || account.getVolumeLimit() >= 0 : account;
assert account.getVolumes() >= 0 : account; assert account.getVolumes() >= 0 : account;

View File

@ -30,12 +30,12 @@ import org.jclouds.cloudstack.domain.Network;
import org.jclouds.cloudstack.domain.NetworkOffering; import org.jclouds.cloudstack.domain.NetworkOffering;
import org.jclouds.cloudstack.domain.Zone; import org.jclouds.cloudstack.domain.Zone;
import org.jclouds.cloudstack.options.ListNetworksOptions; import org.jclouds.cloudstack.options.ListNetworksOptions;
import org.jclouds.cloudstack.predicates.NetworkOfferingPredicates;
import org.jclouds.cloudstack.predicates.ZonePredicates; import org.jclouds.cloudstack.predicates.ZonePredicates;
import org.testng.annotations.AfterGroups; import org.testng.annotations.AfterGroups;
import org.testng.annotations.BeforeGroups; import org.testng.annotations.BeforeGroups;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import com.google.common.base.Predicate;
import com.google.common.collect.Iterables; import com.google.common.collect.Iterables;
/** /**
@ -58,21 +58,17 @@ public class NetworkClientLiveTest extends BaseCloudStackClientLiveTest {
super.setupClient(); super.setupClient();
try { try {
zone = find(client.getZoneClient().listZones(), ZonePredicates.supportsAdvancedNetworks()); // you can create guest direct network by Admin user, but since we are
offering = Iterables.find(client.getOfferingClient().listNetworkOfferings(), new Predicate<NetworkOffering>() { // not admin, let's try to create a guest virtual one
zone = find(client.getZoneClient().listZones(), ZonePredicates.supportsGuestVirtualNetworks());
@Override offering = Iterables.find(client.getOfferingClient().listNetworkOfferings(),
public boolean apply(NetworkOffering arg0) { NetworkOfferingPredicates.supportsGuestVirtualNetworks());
return "Optional".equals(arg0.getAvailability());
}
});
networksSupported = true; networksSupported = true;
} catch (NoSuchElementException e) { } catch (NoSuchElementException e) {
} }
} }
public void testCreateNetworks() throws Exception { public void testCreateNetwork() throws Exception {
if (!networksSupported) if (!networksSupported)
return; return;
network = client.getNetworkClient().createNetworkInZone(zone.getId(), offering.getId(), prefix, prefix); network = client.getNetworkClient().createNetworkInZone(zone.getId(), offering.getId(), prefix, prefix);

View File

@ -72,8 +72,8 @@ public class VirtualMachineClientLiveTest extends BaseCloudStackClientLiveTest {
static final Ordering<ServiceOffering> DEFAULT_SIZE_ORDERING = new Ordering<ServiceOffering>() { static final Ordering<ServiceOffering> DEFAULT_SIZE_ORDERING = new Ordering<ServiceOffering>() {
public int compare(ServiceOffering left, ServiceOffering right) { public int compare(ServiceOffering left, ServiceOffering right) {
return ComparisonChain.start().compare(left.getCpuNumber(), right.getCpuNumber()).compare(left.getMemory(), return ComparisonChain.start().compare(left.getCpuNumber(), right.getCpuNumber())
right.getMemory()).result(); .compare(left.getMemory(), right.getMemory()).result();
} }
}; };
@ -83,16 +83,16 @@ public class VirtualMachineClientLiveTest extends BaseCloudStackClientLiveTest {
if (networks.size() > 0) { if (networks.size() > 0) {
return createVirtualMachineInNetwork(get(networks, 0), client, jobComplete, virtualMachineRunning); return createVirtualMachineInNetwork(get(networks, 0), client, jobComplete, virtualMachineRunning);
} else { } else {
return createVirtualMachineWithSecurityGroupInZone(find(client.getZoneClient().listZones(), return createVirtualMachineWithSecurityGroupInZone(
new Predicate<Zone>() { find(client.getZoneClient().listZones(), new Predicate<Zone>() {
@Override @Override
public boolean apply(Zone arg0) { public boolean apply(Zone arg0) {
return arg0.isSecurityGroupsEnabled(); return arg0.isSecurityGroupsEnabled();
} }
}).getId(), get(client.getSecurityGroupClient().listSecurityGroups(), 0).getId(), client, }).getId(), get(client.getSecurityGroupClient().listSecurityGroups(), 0).getId(), client, jobComplete,
jobComplete, virtualMachineRunning); virtualMachineRunning);
} }
} }
@ -122,8 +122,8 @@ public class VirtualMachineClientLiveTest extends BaseCloudStackClientLiveTest {
Predicate<Template> templatePredicate = Predicates.<Template> and(TemplatePredicates.isReady(), Predicate<Template> templatePredicate = Predicates.<Template> and(TemplatePredicates.isReady(),
hypervisorPredicate, osTypePredicate); hypervisorPredicate, osTypePredicate);
Iterable<Template> templates = filter(client.getTemplateClient().listTemplates( Iterable<Template> templates = filter(
ListTemplatesOptions.Builder.zoneId(zoneId)), templatePredicate); client.getTemplateClient().listTemplates(ListTemplatesOptions.Builder.zoneId(zoneId)), templatePredicate);
if (Iterables.any(templates, TemplatePredicates.isPasswordEnabled())) { if (Iterables.any(templates, TemplatePredicates.isPasswordEnabled())) {
templates = filter(templates, TemplatePredicates.isPasswordEnabled()); templates = filter(templates, TemplatePredicates.isPasswordEnabled());
} }
@ -135,8 +135,8 @@ public class VirtualMachineClientLiveTest extends BaseCloudStackClientLiveTest {
System.out.printf("serviceOfferingId %d, templateId %d, zoneId %d, options %s%n", serviceOfferingId, templateId, System.out.printf("serviceOfferingId %d, templateId %d, zoneId %d, options %s%n", serviceOfferingId, templateId,
zoneId, options); zoneId, options);
AsyncCreateResponse job = client.getVirtualMachineClient().deployVirtualMachineInZone(serviceOfferingId, templateId, AsyncCreateResponse job = client.getVirtualMachineClient().deployVirtualMachineInZone(zoneId, serviceOfferingId,
zoneId, options); templateId, options);
assert jobComplete.apply(job.getJobId()); assert jobComplete.apply(job.getJobId());
AsyncJob<VirtualMachine> jobWithResult = client.getAsyncJobClient().<VirtualMachine> getAsyncJob(job.getJobId()); AsyncJob<VirtualMachine> jobWithResult = client.getAsyncJobClient().<VirtualMachine> getAsyncJob(job.getJobId());
if (jobWithResult.getError() != null) if (jobWithResult.getError() != null)