mirror of https://github.com/apache/jclouds.git
A bunch of work to get things actually working with CS3.x api.
Note that AdvancedNetworkOptionsConverter is still...strange. I'm really not sure what it should be doing.
This commit is contained in:
parent
2be3fa34be
commit
e6eaa2e192
|
@ -41,6 +41,7 @@ import org.jclouds.cloudstack.compute.strategy.AdvancedNetworkOptionsConverter;
|
|||
import org.jclouds.cloudstack.compute.strategy.BasicNetworkOptionsConverter;
|
||||
import org.jclouds.cloudstack.compute.strategy.CloudStackComputeServiceAdapter;
|
||||
import org.jclouds.cloudstack.compute.strategy.OptionsConverter;
|
||||
import org.jclouds.cloudstack.domain.FirewallRule;
|
||||
import org.jclouds.cloudstack.domain.IPForwardingRule;
|
||||
import org.jclouds.cloudstack.domain.Network;
|
||||
import org.jclouds.cloudstack.domain.NetworkType;
|
||||
|
@ -53,6 +54,7 @@ import org.jclouds.cloudstack.domain.Zone;
|
|||
import org.jclouds.cloudstack.features.GuestOSClient;
|
||||
import org.jclouds.cloudstack.functions.StaticNATVirtualMachineInNetwork;
|
||||
import org.jclouds.cloudstack.functions.ZoneIdToZone;
|
||||
import org.jclouds.cloudstack.options.ListFirewallRulesOptions;
|
||||
import org.jclouds.cloudstack.predicates.JobComplete;
|
||||
import org.jclouds.cloudstack.suppliers.GetCurrentUser;
|
||||
import org.jclouds.cloudstack.suppliers.NetworksForCurrentUser;
|
||||
|
@ -108,6 +110,8 @@ public class CloudStackComputeServiceContextModule extends
|
|||
install(new FactoryModuleBuilder().build(StaticNATVirtualMachineInNetwork.Factory.class));
|
||||
bind(new TypeLiteral<CacheLoader<String, Set<IPForwardingRule>>>() {
|
||||
}).to(GetIPForwardingRulesByVirtualMachine.class);
|
||||
bind(new TypeLiteral<CacheLoader<String, Set<FirewallRule>>>() {
|
||||
}).to(GetFirewallRulesByVirtualMachine.class);
|
||||
bind(new TypeLiteral<CacheLoader<String, Zone>>() {
|
||||
}).to(ZoneIdToZone.class);
|
||||
bind(new TypeLiteral<Supplier<LoadingCache<String, Zone>>>() {
|
||||
|
@ -204,6 +208,34 @@ public class CloudStackComputeServiceContextModule extends
|
|||
}
|
||||
}
|
||||
|
||||
@Provides
|
||||
@Singleton
|
||||
protected LoadingCache<String, Set<FirewallRule>> getFirewallRulesByVirtualMachine(
|
||||
CacheLoader<String, Set<FirewallRule>> getFirewallRules) {
|
||||
return CacheBuilder.newBuilder().build(getFirewallRules);
|
||||
}
|
||||
|
||||
@Singleton
|
||||
public static class GetFirewallRulesByVirtualMachine extends CacheLoader<String, Set<FirewallRule>> {
|
||||
private final CloudStackClient client;
|
||||
|
||||
@Inject
|
||||
public GetFirewallRulesByVirtualMachine(CloudStackClient client) {
|
||||
this.client = checkNotNull(client, "client");
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws ResourceNotFoundException
|
||||
* when there is no ip forwarding rule available for the VM
|
||||
*/
|
||||
@Override
|
||||
public Set<FirewallRule> load(String input) {
|
||||
String publicIPId = client.getVirtualMachineClient().getVirtualMachine(input).getPublicIPId();
|
||||
Set<FirewallRule> rules = client.getFirewallClient().listFirewallRules(ListFirewallRulesOptions.Builder.ipAddressId(publicIPId));
|
||||
return rules != null ? rules : ImmutableSet.<FirewallRule>of();
|
||||
}
|
||||
}
|
||||
|
||||
@Provides
|
||||
@Singleton
|
||||
public Map<NetworkType, ? extends OptionsConverter> optionsConverters(){
|
||||
|
|
|
@ -134,6 +134,9 @@ public class VirtualMachineToNodeMetadata implements Function<VirtualMachine, No
|
|||
publicAddresses.add(from.getIPAddress());
|
||||
}
|
||||
}
|
||||
if (from.getPublicIP() != null) {
|
||||
publicAddresses.add(from.getPublicIP());
|
||||
}
|
||||
for (NIC nic : from.getNICs()) {
|
||||
if (nic.getIPAddress() != null) {
|
||||
if (isPrivateIPAddress(nic.getIPAddress())) {
|
||||
|
|
|
@ -20,9 +20,11 @@ package org.jclouds.cloudstack.compute.strategy;
|
|||
|
||||
import static com.google.common.base.Preconditions.checkArgument;
|
||||
import static com.google.common.base.Predicates.and;
|
||||
import static com.google.common.base.Predicates.not;
|
||||
import static com.google.common.collect.Iterables.filter;
|
||||
import static org.jclouds.cloudstack.predicates.NetworkPredicates.defaultNetworkInZone;
|
||||
import static org.jclouds.cloudstack.predicates.NetworkPredicates.supportsStaticNAT;
|
||||
import static org.jclouds.cloudstack.predicates.NetworkPredicates.isIsolatedNetwork;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
|
@ -50,6 +52,9 @@ public class AdvancedNetworkOptionsConverter implements OptionsConverter {
|
|||
checkArgument(!networks.isEmpty(), "please setup a network for zone: " + zoneId);
|
||||
Network defaultNetworkInZone = Iterables.getFirst(filter(networks.values(), and(defaultNetworkInZone(zoneId), supportsStaticNAT())), null);
|
||||
if(defaultNetworkInZone == null) {
|
||||
defaultNetworkInZone = Iterables.getFirst(filter(networks.values(), and(isIsolatedNetwork(), not(supportsStaticNAT()))), null);
|
||||
}
|
||||
if (defaultNetworkInZone == null) {
|
||||
throw new IllegalArgumentException("please choose a specific network in zone " + zoneId + ": " + networks);
|
||||
} else {
|
||||
options.networkId(defaultNetworkInZone.getId());
|
||||
|
|
|
@ -40,6 +40,8 @@ import javax.inject.Singleton;
|
|||
import org.jclouds.cloudstack.CloudStackClient;
|
||||
import org.jclouds.cloudstack.compute.options.CloudStackTemplateOptions;
|
||||
import org.jclouds.cloudstack.domain.AsyncCreateResponse;
|
||||
import org.jclouds.cloudstack.domain.Capabilities;
|
||||
import org.jclouds.cloudstack.domain.FirewallRule;
|
||||
import org.jclouds.cloudstack.domain.IPForwardingRule;
|
||||
import org.jclouds.cloudstack.domain.Network;
|
||||
import org.jclouds.cloudstack.domain.NetworkType;
|
||||
|
@ -48,10 +50,12 @@ import org.jclouds.cloudstack.domain.ServiceOffering;
|
|||
import org.jclouds.cloudstack.domain.Template;
|
||||
import org.jclouds.cloudstack.domain.VirtualMachine;
|
||||
import org.jclouds.cloudstack.domain.Zone;
|
||||
import org.jclouds.cloudstack.functions.CreateFirewallRulesForIP;
|
||||
import org.jclouds.cloudstack.functions.CreatePortForwardingRulesForIP;
|
||||
import org.jclouds.cloudstack.functions.StaticNATVirtualMachineInNetwork;
|
||||
import org.jclouds.cloudstack.functions.StaticNATVirtualMachineInNetwork.Factory;
|
||||
import org.jclouds.cloudstack.options.DeployVirtualMachineOptions;
|
||||
import org.jclouds.cloudstack.options.ListFirewallRulesOptions;
|
||||
import org.jclouds.cloudstack.strategy.BlockUntilJobCompletesAndReturnResult;
|
||||
import org.jclouds.collect.Memoized;
|
||||
import org.jclouds.compute.ComputeService;
|
||||
|
@ -89,6 +93,7 @@ public class CloudStackComputeServiceAdapter implements
|
|||
private final BlockUntilJobCompletesAndReturnResult blockUntilJobCompletesAndReturnResult;
|
||||
private final Factory staticNATVMInNetwork;
|
||||
private final CreatePortForwardingRulesForIP setupPortForwardingRulesForIP;
|
||||
private final CreateFirewallRulesForIP setupFirewallRulesForIP;
|
||||
private final LoadingCache<String, Set<IPForwardingRule>> vmToRules;
|
||||
private final Map<String, Credentials> credentialStore;
|
||||
private final Map<NetworkType, ? extends OptionsConverter> optionsConverters;
|
||||
|
@ -99,7 +104,9 @@ public class CloudStackComputeServiceAdapter implements
|
|||
@Memoized Supplier<Map<String, Network>> networkSupplier,
|
||||
BlockUntilJobCompletesAndReturnResult blockUntilJobCompletesAndReturnResult,
|
||||
StaticNATVirtualMachineInNetwork.Factory staticNATVMInNetwork,
|
||||
CreatePortForwardingRulesForIP setupPortForwardingRulesForIP, LoadingCache<String, Set<IPForwardingRule>> vmToRules,
|
||||
CreatePortForwardingRulesForIP setupPortForwardingRulesForIP,
|
||||
CreateFirewallRulesForIP setupFirewallRulesForIP,
|
||||
LoadingCache<String, Set<IPForwardingRule>> vmToRules,
|
||||
Map<String, Credentials> credentialStore, Map<NetworkType, ? extends OptionsConverter> optionsConverters,
|
||||
Supplier<LoadingCache<String, Zone>> zoneIdToZone) {
|
||||
this.client = checkNotNull(client, "client");
|
||||
|
@ -109,6 +116,7 @@ public class CloudStackComputeServiceAdapter implements
|
|||
"blockUntilJobCompletesAndReturnResult");
|
||||
this.staticNATVMInNetwork = checkNotNull(staticNATVMInNetwork, "staticNATVMInNetwork");
|
||||
this.setupPortForwardingRulesForIP = checkNotNull(setupPortForwardingRulesForIP, "setupPortForwardingRulesForIP");
|
||||
this.setupFirewallRulesForIP = checkNotNull(setupFirewallRulesForIP, "setupFirewallRulesForIP");
|
||||
this.vmToRules = checkNotNull(vmToRules, "vmToRules");
|
||||
this.credentialStore = checkNotNull(credentialStore, "credentialStore");
|
||||
this.optionsConverters = optionsConverters;
|
||||
|
@ -166,6 +174,7 @@ public class CloudStackComputeServiceAdapter implements
|
|||
AsyncCreateResponse job = client.getVirtualMachineClient().deployVirtualMachineInZone(zoneId, serviceOfferingId,
|
||||
templateId, options);
|
||||
VirtualMachine vm = blockUntilJobCompletesAndReturnResult.<VirtualMachine> apply(job);
|
||||
logger.debug("--- virtualmachine: %s", vm);
|
||||
LoginCredentials credentials = null;
|
||||
if (vm.isPasswordEnabled()) {
|
||||
assert vm.getPassword() != null : vm;
|
||||
|
@ -174,15 +183,23 @@ public class CloudStackComputeServiceAdapter implements
|
|||
credentials = LoginCredentials.fromCredentials(credentialStore.get("keypair#" + templateOptions.getKeyPair()));
|
||||
}
|
||||
if (templateOptions.shouldSetupStaticNat()) {
|
||||
Capabilities capabilities = client.getConfigurationClient().listCapabilities();
|
||||
// TODO: possibly not all network ids, do we want to do this
|
||||
for (String networkId : options.getNetworkIds()) {
|
||||
logger.debug(">> creating static NAT for virtualMachine(%s) in network(%s)", vm.getId(), networkId);
|
||||
PublicIPAddress ip = staticNATVMInNetwork.create(networks.get(networkId)).apply(vm);
|
||||
logger.trace("<< static NATed IPAddress(%s) to virtualMachine(%s)", ip.getId(), vm.getId());
|
||||
vm = client.getVirtualMachineClient().getVirtualMachine(vm.getId());
|
||||
List<Integer> ports = Ints.asList(templateOptions.getInboundPorts());
|
||||
if (capabilities.getCloudStackVersion().startsWith("2")) {
|
||||
logger.debug(">> setting up IP forwarding for IPAddress(%s) rules(%s)", ip.getId(), ports);
|
||||
Set<IPForwardingRule> rules = setupPortForwardingRulesForIP.apply(ip, ports);
|
||||
logger.trace("<< setup %d IP forwarding rules on IPAddress(%s)", rules.size(), ip.getId());
|
||||
} else {
|
||||
logger.debug(">> setting up firewall rules for IPAddress(%s) rules(%s)", ip.getId(), ports);
|
||||
Set<FirewallRule> rules = setupFirewallRulesForIP.apply(ip, ports);
|
||||
logger.trace("<< setup %d firewall rules on IPAddress(%s)", rules.size(), ip.getId());
|
||||
}
|
||||
}
|
||||
}
|
||||
return new NodeAndInitialCredentials<VirtualMachine>(vm, vm.getId() + "", credentials);
|
||||
|
@ -241,10 +258,13 @@ public class CloudStackComputeServiceAdapter implements
|
|||
// 1) Delete IP forwarding rules associated with IP.
|
||||
Set<String> ipAddresses = deleteIPForwardingRulesForVMAndReturnDistinctIPs(virtualMachineId);
|
||||
|
||||
// 2) Disable static nat rule for the IP.
|
||||
// 2) Delete firewall rules associated with IP.
|
||||
ipAddresses.addAll(deleteFirewallRulesForVMAndReturnDistinctIPs(virtualMachineId));
|
||||
|
||||
// 3) Disable static nat rule for the IP.
|
||||
disableStaticNATOnIPAddresses(ipAddresses);
|
||||
|
||||
// 3) Only after 1 and 2 release the IP address.
|
||||
// 4) Only after 1 and 2 release the IP address.
|
||||
disassociateIPAddresses(ipAddresses);
|
||||
|
||||
destroyVirtualMachine(virtualMachineId);
|
||||
|
@ -305,6 +325,23 @@ public class CloudStackComputeServiceAdapter implements
|
|||
return ipAddresses;
|
||||
}
|
||||
|
||||
public Set<String> deleteFirewallRulesForVMAndReturnDistinctIPs(String virtualMachineId) {
|
||||
// immutable doesn't permit duplicates
|
||||
Set<String> ipAddresses = Sets.newLinkedHashSet();
|
||||
|
||||
Set<FirewallRule> firewallRules = client.getFirewallClient()
|
||||
.listFirewallRules(ListFirewallRulesOptions.Builder.ipAddressId(client.getVirtualMachineClient().getVirtualMachine(virtualMachineId).getPublicIPId()));
|
||||
|
||||
for (FirewallRule rule : firewallRules) {
|
||||
if (!FirewallRule.State.fromValue("DELETEING").equals(rule.getState())) {
|
||||
ipAddresses.add(rule.getIpAddressId());
|
||||
client.getFirewallClient().deleteFirewallRule(rule.getId());
|
||||
logger.debug(">> deleting FirewallRule(%s)", rule.getId());
|
||||
}
|
||||
}
|
||||
return ipAddresses;
|
||||
}
|
||||
|
||||
public void awaitCompletion(Iterable<String> jobs) {
|
||||
logger.debug(">> awaiting completion of jobs(%s)", jobs);
|
||||
for (String job : jobs)
|
||||
|
|
|
@ -40,6 +40,12 @@ public enum GuestIPType {
|
|||
*/
|
||||
DIRECT,
|
||||
|
||||
/**
|
||||
* Not sure what exactly these *are* yet, but I know they exist in CS3.
|
||||
*/
|
||||
SHARED,
|
||||
ISOLATED,
|
||||
|
||||
UNRECOGNIZED;
|
||||
|
||||
@Override
|
||||
|
|
|
@ -71,6 +71,8 @@ public class VirtualMachine implements Comparable<VirtualMachine> {
|
|||
private Long networkKbsWrite;
|
||||
private String password;
|
||||
private boolean passwordEnabled;
|
||||
private String publicIP;
|
||||
private String publicIPId;
|
||||
private String rootDeviceId;
|
||||
private String rootDeviceType;
|
||||
private String serviceOfferingId;
|
||||
|
@ -225,6 +227,16 @@ public class VirtualMachine implements Comparable<VirtualMachine> {
|
|||
return this;
|
||||
}
|
||||
|
||||
public Builder publicIP(String publicIP) {
|
||||
this.publicIP = publicIP;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder publicIPId(String publicIPId) {
|
||||
this.publicIPId = publicIPId;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder rootDeviceId(String rootDeviceId) {
|
||||
this.rootDeviceId = rootDeviceId;
|
||||
return this;
|
||||
|
@ -294,7 +306,7 @@ public class VirtualMachine implements Comparable<VirtualMachine> {
|
|||
return new VirtualMachine(id, account, cpuCount, cpuSpeed, cpuUsed, displayName, created, domain, domainId,
|
||||
usesVirtualNetwork, group, groupId, guestOSId, HAEnabled, hostId, hostname, IPAddress, ISODisplayText,
|
||||
ISOId, ISOName, jobId, jobStatus, memory, name, networkKbsRead, networkKbsWrite, password,
|
||||
passwordEnabled, rootDeviceId, rootDeviceType, securityGroups, serviceOfferingId, serviceOfferingName,
|
||||
passwordEnabled, publicIP, publicIPId, rootDeviceId, rootDeviceType, securityGroups, serviceOfferingId, serviceOfferingName,
|
||||
state, templateDisplayText, templateId, templateName, zoneId, zoneName, nics, hypervisor);
|
||||
}
|
||||
}
|
||||
|
@ -366,6 +378,10 @@ public class VirtualMachine implements Comparable<VirtualMachine> {
|
|||
private String password;
|
||||
@SerializedName("passwordenabled")
|
||||
private boolean passwordEnabled;
|
||||
@SerializedName("publicip")
|
||||
private String publicIP;
|
||||
@SerializedName("publicipid")
|
||||
private String publicIPId;
|
||||
@SerializedName("rootdeviceid")
|
||||
private String rootDeviceId;
|
||||
@SerializedName("rootdevicetype")
|
||||
|
@ -395,7 +411,7 @@ public class VirtualMachine implements Comparable<VirtualMachine> {
|
|||
Date created, String domain, String domainId, boolean usesVirtualNetwork, String group, String groupId,
|
||||
String guestOSId, boolean hAEnabled, String hostId, String hostname, String iPAddress, String iSODisplayText,
|
||||
String iSOId, String iSOName, String jobId, Integer jobStatus, long memory, String name, Long networkKbsRead,
|
||||
Long networkKbsWrite, String password, boolean passwordEnabled, String rootDeviceId, String rootDeviceType,
|
||||
Long networkKbsWrite, String password, boolean passwordEnabled, String publicIP, String publicIPId, String rootDeviceId, String rootDeviceType,
|
||||
Set<SecurityGroup> securityGroups, String serviceOfferingId, String serviceOfferingName, State state,
|
||||
String templateDisplayText, String templateId, String templateName, String zoneId, String zoneName, Set<NIC> nics,
|
||||
String hypervisor) {
|
||||
|
@ -428,6 +444,8 @@ public class VirtualMachine implements Comparable<VirtualMachine> {
|
|||
this.networkKbsWrite = networkKbsWrite;
|
||||
this.password = password;
|
||||
this.passwordEnabled = passwordEnabled;
|
||||
this.publicIP = publicIP;
|
||||
this.publicIPId = publicIPId;
|
||||
this.rootDeviceId = rootDeviceId;
|
||||
this.rootDeviceType = rootDeviceType;
|
||||
this.securityGroups = ImmutableSet.copyOf(checkNotNull(securityGroups, "securityGroups"));
|
||||
|
@ -659,6 +677,20 @@ public class VirtualMachine implements Comparable<VirtualMachine> {
|
|||
return passwordEnabled;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return public IP of this virtual machine
|
||||
*/
|
||||
public String getPublicIP() {
|
||||
return publicIP;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return ID of the public IP of this virtual machine
|
||||
*/
|
||||
public String getPublicIPId() {
|
||||
return publicIPId;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return device ID of the root volume
|
||||
*/
|
||||
|
@ -786,6 +818,8 @@ public class VirtualMachine implements Comparable<VirtualMachine> {
|
|||
if (!Objects.equal(networkKbsWrite, that.networkKbsWrite)) return false;
|
||||
if (!Objects.equal(password, that.password)) return false;
|
||||
if (!Objects.equal(passwordEnabled, that.passwordEnabled)) return false;
|
||||
if (!Objects.equal(publicIP, that.publicIP)) return false;
|
||||
if (!Objects.equal(publicIPId, that.publicIPId)) return false;
|
||||
if (!Objects.equal(rootDeviceId, that.rootDeviceId)) return false;
|
||||
if (!Objects.equal(rootDeviceType, that.rootDeviceType)) return false;
|
||||
if (!Objects.equal(securityGroups, that.securityGroups)) return false;
|
||||
|
@ -809,7 +843,7 @@ public class VirtualMachine implements Comparable<VirtualMachine> {
|
|||
domain, domainId, usesVirtualNetwork, group, groupId, guestOSId,
|
||||
HAEnabled, hostId, hostname, IPAddress, ISODisplayText, ISOId,
|
||||
ISOName, jobId, jobStatus, memory, name, networkKbsRead,
|
||||
networkKbsWrite, password, passwordEnabled, rootDeviceId,
|
||||
networkKbsWrite, password, passwordEnabled, publicIP, publicIPId, rootDeviceId,
|
||||
rootDeviceType, securityGroups, serviceOfferingId,
|
||||
serviceOfferingName, state, templateDisplayText, templateId,
|
||||
templateName, zoneId, zoneName, nics, hypervisor);
|
||||
|
@ -846,6 +880,8 @@ public class VirtualMachine implements Comparable<VirtualMachine> {
|
|||
", networkKbsWrite=" + networkKbsWrite +
|
||||
", password='" + password + '\'' +
|
||||
", passwordEnabled=" + passwordEnabled +
|
||||
", publicIP='" + publicIP + '\'' +
|
||||
", publicIPId='" + publicIPId + '\'' +
|
||||
", rootDeviceId=" + rootDeviceId +
|
||||
", rootDeviceType='" + rootDeviceType + '\'' +
|
||||
", serviceOfferingId=" + serviceOfferingId +
|
||||
|
|
|
@ -87,6 +87,19 @@ public interface FirewallAsyncClient {
|
|||
ListenableFuture<AsyncCreateResponse> createFirewallRuleForIpAndProtocol(@QueryParam("ipaddressid") String ipAddressId,
|
||||
@QueryParam("protocol") FirewallRule.Protocol protocol, CreateFirewallRuleOptions... options);
|
||||
|
||||
/**
|
||||
* @see FirewallClient#createFirewallRuleForIpProtocolAndPort
|
||||
*/
|
||||
@GET
|
||||
@QueryParams(keys = "command", values = "createFirewallRule")
|
||||
@Unwrap
|
||||
@Consumes(MediaType.APPLICATION_JSON)
|
||||
ListenableFuture<AsyncCreateResponse> createFirewallRuleForIpProtocolAndPort(@QueryParam("ipaddressid") String ipAddressId,
|
||||
@QueryParam("protocol") FirewallRule.Protocol protocol,
|
||||
@QueryParam("startPort") int startPort,
|
||||
@QueryParam("endPort") int endPort);
|
||||
|
||||
|
||||
/**
|
||||
* @see FirewallClient#deleteFirewallRule
|
||||
*/
|
||||
|
|
|
@ -74,6 +74,13 @@ public interface FirewallClient {
|
|||
AsyncCreateResponse createFirewallRuleForIpAndProtocol(String ipAddressId,
|
||||
FirewallRule.Protocol protocol, CreateFirewallRuleOptions... options);
|
||||
|
||||
|
||||
AsyncCreateResponse createFirewallRuleForIpProtocolAndPort(String ipAddressId,
|
||||
FirewallRule.Protocol protocol,
|
||||
int startPort,
|
||||
int endPort);
|
||||
|
||||
|
||||
/**
|
||||
* Deletes a firewall rule
|
||||
*
|
||||
|
|
|
@ -0,0 +1,96 @@
|
|||
/**
|
||||
* 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.functions;
|
||||
|
||||
import static com.google.common.base.Preconditions.checkNotNull;
|
||||
import static com.google.common.base.Preconditions.checkState;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import javax.inject.Inject;
|
||||
import javax.inject.Named;
|
||||
import javax.inject.Singleton;
|
||||
|
||||
import org.jclouds.cloudstack.CloudStackClient;
|
||||
import org.jclouds.cloudstack.domain.AsyncCreateResponse;
|
||||
import org.jclouds.cloudstack.domain.FirewallRule;
|
||||
import org.jclouds.cloudstack.domain.PublicIPAddress;
|
||||
import org.jclouds.cloudstack.options.CreateFirewallRuleOptions;
|
||||
import org.jclouds.cloudstack.strategy.BlockUntilJobCompletesAndReturnResult;
|
||||
import org.jclouds.compute.reference.ComputeServiceConstants;
|
||||
import org.jclouds.logging.Logger;
|
||||
|
||||
import com.google.common.cache.LoadingCache;
|
||||
import com.google.common.collect.ImmutableSet;
|
||||
import com.google.common.collect.Iterables;
|
||||
import com.google.common.collect.ImmutableSet.Builder;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Adrian Cole
|
||||
*/
|
||||
@Singleton
|
||||
public class CreateFirewallRulesForIP {
|
||||
|
||||
@Resource
|
||||
@Named(ComputeServiceConstants.COMPUTE_LOGGER)
|
||||
protected Logger logger = Logger.NULL;
|
||||
|
||||
private final CloudStackClient client;
|
||||
private final BlockUntilJobCompletesAndReturnResult blockUntilJobCompletesAndReturnResult;
|
||||
private final LoadingCache<String, Set<FirewallRule>> getFirewallRulesByVirtualMachine;
|
||||
|
||||
@Inject
|
||||
public CreateFirewallRulesForIP(CloudStackClient client,
|
||||
BlockUntilJobCompletesAndReturnResult blockUntilJobCompletesAndReturnResult,
|
||||
LoadingCache<String, Set<FirewallRule>> getFirewallRulesByVirtualMachine) {
|
||||
this.client = checkNotNull(client, "client");
|
||||
this.blockUntilJobCompletesAndReturnResult = checkNotNull(blockUntilJobCompletesAndReturnResult,
|
||||
"blockUntilJobCompletesAndReturnResult");
|
||||
this.getFirewallRulesByVirtualMachine = checkNotNull(getFirewallRulesByVirtualMachine,
|
||||
"getFirewallRulesByVirtualMachine");
|
||||
}
|
||||
|
||||
public Set<FirewallRule> apply(PublicIPAddress ip, Iterable<Integer> ports) {
|
||||
return apply(ip, "tcp", ports);
|
||||
}
|
||||
|
||||
public Set<FirewallRule> apply(PublicIPAddress ip, String protocol, Iterable<Integer> ports) {
|
||||
checkState(ip.getVirtualMachineId() != null,
|
||||
"ip %s should be static NATed to a virtual machine before applying rules", ip);
|
||||
if (Iterables.size(ports) == 0)
|
||||
return ImmutableSet.<FirewallRule> of();
|
||||
Builder<AsyncCreateResponse> responses = ImmutableSet.builder();
|
||||
for (int port : ports) {
|
||||
AsyncCreateResponse response = client.getFirewallClient().createFirewallRuleForIpAndProtocol(ip.getId(), FirewallRule.Protocol.fromValue(protocol),
|
||||
CreateFirewallRuleOptions.Builder.startPort(port).endPort(port));
|
||||
logger.debug(">> creating firewall rule IPAddress(%s) for protocol(%s), port(%s); response(%s)",
|
||||
ip.getId(), protocol, port, response);
|
||||
responses.add(response);
|
||||
}
|
||||
Builder<FirewallRule> rules = ImmutableSet.builder();
|
||||
for (AsyncCreateResponse response : responses.build()) {
|
||||
FirewallRule rule = blockUntilJobCompletesAndReturnResult.<FirewallRule> apply(response);
|
||||
rules.add(rule);
|
||||
getFirewallRulesByVirtualMachine.asMap().put(ip.getVirtualMachineId(), ImmutableSet.of(rule));
|
||||
}
|
||||
return rules.build();
|
||||
}
|
||||
}
|
|
@ -93,6 +93,37 @@ public class NetworkPredicates {
|
|||
}
|
||||
}
|
||||
|
||||
public static enum IsSharedNetwork implements Predicate<Network> {
|
||||
INSTANCE;
|
||||
|
||||
@Override
|
||||
public boolean apply(Network arg0) {
|
||||
boolean network = isSharedNetwork.apply(checkNotNull(arg0, "network").getGuestIPType());
|
||||
return network;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return isSharedNetwork.toString();
|
||||
}
|
||||
}
|
||||
|
||||
public static enum IsIsolatedNetwork implements Predicate<Network> {
|
||||
INSTANCE;
|
||||
|
||||
@Override
|
||||
public boolean apply(Network arg0) {
|
||||
boolean network = isIsolatedNetwork.apply(checkNotNull(arg0, "network").getGuestIPType());
|
||||
return network;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return isIsolatedNetwork.toString();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private static class DefaultNetworkInZone implements Predicate<Network> {
|
||||
private final String zoneId;
|
||||
|
||||
|
@ -170,6 +201,10 @@ public class NetworkPredicates {
|
|||
|
||||
public static Predicate<GuestIPType> isVirtualNetwork = new GuestIPTypeIs(GuestIPType.VIRTUAL);
|
||||
|
||||
public static Predicate<GuestIPType> isIsolatedNetwork = new GuestIPTypeIs(GuestIPType.ISOLATED);
|
||||
|
||||
public static Predicate<GuestIPType> isSharedNetwork = new GuestIPTypeIs(GuestIPType.SHARED);
|
||||
|
||||
/**
|
||||
*
|
||||
* @return true, if the network supports static NAT.
|
||||
|
@ -202,6 +237,22 @@ public class NetworkPredicates {
|
|||
return IsVirtualNetwork.INSTANCE;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return true, if the network is an isolated network.
|
||||
*/
|
||||
public static Predicate<Network> isIsolatedNetwork() {
|
||||
return IsIsolatedNetwork.INSTANCE;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return true, if the network is a shared network.
|
||||
*/
|
||||
public static Predicate<Network> isSharedNetwork() {
|
||||
return IsSharedNetwork.INSTANCE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Filters for default networks in a specific zone.
|
||||
*
|
||||
|
|
|
@ -0,0 +1,82 @@
|
|||
/**
|
||||
* 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 java.util.Set;
|
||||
|
||||
import org.jclouds.cloudstack.domain.GuestIPType;
|
||||
import org.jclouds.cloudstack.domain.NIC;
|
||||
import org.jclouds.cloudstack.domain.TrafficType;
|
||||
import org.jclouds.cloudstack.domain.VirtualMachine;
|
||||
import org.jclouds.date.internal.SimpleDateFormatDateService;
|
||||
import org.jclouds.json.BaseSetParserTest;
|
||||
import org.jclouds.rest.annotations.SelectJson;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import com.google.common.collect.ImmutableSet;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Adrian Cole
|
||||
*/
|
||||
@Test(groups = "unit")
|
||||
public class ListVirtualMachinesResponse3xTest extends BaseSetParserTest<VirtualMachine> {
|
||||
|
||||
@Override
|
||||
public String resource() {
|
||||
return "/listvirtualmachinesresponse3x.json";
|
||||
}
|
||||
|
||||
@Override
|
||||
@SelectJson("virtualmachine")
|
||||
public Set<VirtualMachine> expected() {
|
||||
return ImmutableSet.of(VirtualMachine
|
||||
.builder()
|
||||
.id("fee2ccb3-c1f2-4e7b-8465-42b390e10dff")
|
||||
.name("cloudstack-r-611")
|
||||
.displayName("cloudstack-r-611")
|
||||
.account("jcloud2")
|
||||
.domainId("ea66e3a5-d007-42e8-a0de-ec5ce778a1d7")
|
||||
.domain("jCloud")
|
||||
.created(new SimpleDateFormatDateService().iso8601SecondsDateParse("2012-05-22T09:18:28-0700"))
|
||||
.state(VirtualMachine.State.RUNNING)
|
||||
.isHAEnabled(false)
|
||||
.zoneId("1")
|
||||
.zoneName("Santa Clara Zone")
|
||||
.templateId("5c65f152-a4bc-4405-a756-fd10841a9aa7")
|
||||
.templateName("jclouds-6d4bdc29")
|
||||
.templateDisplayText("jclouds live testCreateTemplate")
|
||||
.passwordEnabled(false)
|
||||
.serviceOfferingId("5305750d-df71-4da9-8cd0-e23c2236a6e2")
|
||||
.serviceOfferingName("Micro Instance")
|
||||
.cpuCount(1)
|
||||
.cpuSpeed(500)
|
||||
.memory(256)
|
||||
.guestOSId("6dcd58ce-1ec6-432c-af0b-9ab4ca9207d9")
|
||||
.rootDeviceId("0")
|
||||
.rootDeviceType("IscsiLUN")
|
||||
.publicIP("72.52.126.110")
|
||||
.publicIPId("e202aafb-ab41-4dc0-80e9-9fcd64fbf45c")
|
||||
.nics(ImmutableSet.of(NIC.builder().id("48640c5e-90f3-45bd-abd2-a108ca8957ac").
|
||||
networkId("c0d5db5b-f7d5-44e1-b854-21ecd1a09dbf").netmask("255.255.255.0").gateway("10.1.1.1")
|
||||
.IPAddress("10.1.1.227").trafficType(TrafficType.GUEST).guestIPType(GuestIPType.ISOLATED)
|
||||
.isDefault(true).build())).build());
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1 @@
|
|||
{ "listvirtualmachinesresponse" : { "count":1 ,"virtualmachine" : [ {"id":"fee2ccb3-c1f2-4e7b-8465-42b390e10dff","name":"cloudstack-r-611","displayname":"cloudstack-r-611","account":"jcloud2","domainid":"ea66e3a5-d007-42e8-a0de-ec5ce778a1d7","domain":"jCloud","created":"2012-05-22T09:18:28-0700","state":"Running","haenable":false,"zoneid":"1","zonename":"Santa Clara Zone","templateid":"5c65f152-a4bc-4405-a756-fd10841a9aa7","templatename":"jclouds-6d4bdc29","templatedisplaytext":"jclouds live testCreateTemplate","passwordenabled":false,"serviceofferingid":"5305750d-df71-4da9-8cd0-e23c2236a6e2","serviceofferingname":"Micro Instance","cpunumber":1,"cpuspeed":500,"memory":256,"guestosid":"6dcd58ce-1ec6-432c-af0b-9ab4ca9207d9","rootdeviceid":0,"rootdevicetype":"IscsiLUN","securitygroup":[],"nic":[{"id":"48640c5e-90f3-45bd-abd2-a108ca8957ac","networkid":"c0d5db5b-f7d5-44e1-b854-21ecd1a09dbf","netmask":"255.255.255.0","gateway":"10.1.1.1","ipaddress":"10.1.1.227","traffictype":"Guest","type":"Isolated","isdefault":true}],"publicipid":"e202aafb-ab41-4dc0-80e9-9fcd64fbf45c","publicip":"72.52.126.110"} ] } }"
|
Loading…
Reference in New Issue