Skip to content
Permalink
Browse files
JCLOUDS-1208 Implement the TemplateOptions.inboundPorts in ProfitBric…
…ks REST
  • Loading branch information
alibazlamit authored and nacx committed Jan 25, 2017
1 parent f749c88 commit b083f4c2b1aaf005797d2999910745bab8d110a4
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 32 deletions.
@@ -17,33 +17,39 @@
package org.apache.jclouds.profitbricks.rest.compute;

import static com.google.common.base.Preconditions.checkArgument;
import com.google.common.base.Predicate;
import static com.google.common.base.Strings.isNullOrEmpty;
import com.google.common.base.Supplier;
import com.google.common.base.Throwables;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables;
import static com.google.common.collect.Iterables.contains;
import static com.google.common.collect.Iterables.filter;
import com.google.common.collect.Lists;
import static com.google.common.util.concurrent.Futures.getUnchecked;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.ListeningExecutorService;
import com.google.inject.Inject;
import static java.lang.String.format;
import static org.apache.jclouds.profitbricks.rest.config.ProfitBricksComputeProperties.POLL_PREDICATE_DATACENTER;
import static org.apache.jclouds.profitbricks.rest.config.ProfitBricksComputeProperties.POLL_PREDICATE_NIC;
import static org.apache.jclouds.profitbricks.rest.config.ProfitBricksComputeProperties.POLL_PREDICATE_SERVER;
import static org.jclouds.Constants.PROPERTY_USER_THREADS;
import static org.jclouds.compute.config.ComputeServiceProperties.TIMEOUT_NODE_RUNNING;
import static org.jclouds.compute.config.ComputeServiceProperties.TIMEOUT_NODE_SUSPENDED;

import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;

import javax.annotation.Resource;
import javax.inject.Named;
import javax.inject.Singleton;

import org.apache.jclouds.profitbricks.rest.ProfitBricksApi;
import org.apache.jclouds.profitbricks.rest.compute.concurrent.ProvisioningJob;
import org.apache.jclouds.profitbricks.rest.compute.concurrent.ProvisioningManager;
import org.apache.jclouds.profitbricks.rest.compute.function.ProvisionableToImage;
import org.apache.jclouds.profitbricks.rest.compute.strategy.TemplateWithDataCenter;
import static org.apache.jclouds.profitbricks.rest.config.ProfitBricksComputeProperties.POLL_PREDICATE_DATACENTER;
import static org.apache.jclouds.profitbricks.rest.config.ProfitBricksComputeProperties.POLL_PREDICATE_NIC;
import static org.apache.jclouds.profitbricks.rest.config.ProfitBricksComputeProperties.POLL_PREDICATE_SERVER;
import org.apache.jclouds.profitbricks.rest.domain.DataCenter;
import org.apache.jclouds.profitbricks.rest.domain.FirewallRule;
import org.apache.jclouds.profitbricks.rest.domain.Image;
import org.apache.jclouds.profitbricks.rest.domain.Lan;
import org.apache.jclouds.profitbricks.rest.domain.Nic;
@@ -60,7 +66,10 @@
import org.apache.jclouds.profitbricks.rest.ids.VolumeRef;
import org.apache.jclouds.profitbricks.rest.util.Passwords;
import org.apache.jclouds.profitbricks.rest.util.Trackables;
import static org.jclouds.Constants.PROPERTY_USER_THREADS;
import org.jclouds.compute.ComputeServiceAdapter;
import static org.jclouds.compute.config.ComputeServiceProperties.TIMEOUT_NODE_RUNNING;
import static org.jclouds.compute.config.ComputeServiceProperties.TIMEOUT_NODE_SUSPENDED;
import org.jclouds.compute.domain.Hardware;
import org.jclouds.compute.domain.HardwareBuilder;
import org.jclouds.compute.domain.Processor;
@@ -70,23 +79,13 @@
import org.jclouds.compute.options.TemplateOptions;
import org.jclouds.compute.reference.ComputeServiceConstants;
import org.jclouds.compute.util.ComputeServiceUtils;
import static org.jclouds.compute.util.ComputeServiceUtils.getPortRangesFromList;
import org.jclouds.domain.Location;
import org.jclouds.domain.LocationScope;
import org.jclouds.domain.LoginCredentials;
import org.jclouds.logging.Logger;
import org.jclouds.rest.ResourceNotFoundException;

import com.google.common.base.Predicate;
import com.google.common.base.Supplier;
import com.google.common.base.Throwables;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.ListeningExecutorService;
import com.google.inject.Inject;

@Singleton
public class ProfitBricksComputeServiceAdapter implements ComputeServiceAdapter<ServerInDataCenter, Hardware, Provisionable, Location> {

@@ -149,6 +148,7 @@ protected NodeAndInitialCredentials<ServerInDataCenter> createNodeWithGroupEncod
final String loginUser = isNullOrEmpty(options.getLoginUser()) ? "root" : options.getLoginUser();
final String password = options.hasLoginPassword() ? options.getLoginPassword() : Passwords.generate();
final org.jclouds.compute.domain.Image image = template.getImage();
final int[] inboundPorts = template.getOptions().getInboundPorts();

// provision all volumes based on hardware
List<? extends Volume> volumes = hardware.getVolumes();
@@ -175,12 +175,12 @@ protected NodeAndInitialCredentials<ServerInDataCenter> createNodeWithGroupEncod
type(VolumeType.HDD);

org.apache.jclouds.profitbricks.rest.domain.Volume vol = (org.apache.jclouds.profitbricks.rest.domain.Volume) provisioningManager
.provision(jobFactory.create(dataCenterId, new Supplier<Object>() {
@Override
public Object get() {
return api.volumeApi().createVolume(request.build());
}
}));
.provision(jobFactory.create(dataCenterId, new Supplier<Object>() {
@Override
public Object get() {
return api.volumeApi().createVolume(request.build());
}
}));

volumeIds.add(vol.id());
logger.trace(">> provisioning complete for volume. returned id='%s'", vol.id());
@@ -281,15 +281,33 @@ public boolean apply(Lan input) {
.name("jclouds" + name)
.dhcp(Boolean.TRUE)
.lan(lanId)
.firewallActive(Boolean.FALSE)
.firewallActive(inboundPorts.length > 0)
.serverId(server.id()).
build());

trackables.waitUntilRequestCompleted(nic);
waitNICUntilAvailable.apply(NicRef.create(dataCenterId, server.id(), nic.id()));
waitDcUntilAvailable.apply(dataCenterId);
waitServerUntilAvailable.apply(ServerRef.create(dataCenterId, server.id()));

Map<Integer, Integer> portsRange = getPortRangesFromList(inboundPorts);

for (Map.Entry<Integer, Integer> range : portsRange.entrySet()) {
FirewallRule rule = api.firewallApi().create(
FirewallRule.Request.creatingBuilder()
.dataCenterId(dataCenterId)
.serverId(server.id())
.nicId(nic.id())
.name(server.properties().name() + " jclouds-firewall")
.protocol(FirewallRule.Protocol.TCP)
.portRangeStart(range.getKey())
.portRangeEnd(range.getValue())
.build()
);
trackables.waitUntilRequestCompleted(rule);

}

//connect the rest of volumes to server;delete if fails
final int volumeCount = volumeIds.size();
for (int j = 1; j < volumeCount; j++) { // skip first; already connected
@@ -400,7 +418,7 @@ public List<Snapshot> call() throws Exception {
ImmutableList.Builder<Provisionable> provisionables = ImmutableList.builder();
provisionables.addAll(getUnchecked(images));
provisionables.addAll(getUnchecked(snapshots));

return provisionables.build();
}

@@ -18,24 +18,34 @@

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import static com.google.common.collect.Iterables.getOnlyElement;
import com.google.inject.Module;
import org.apache.jclouds.profitbricks.rest.ProfitBricksApi;
import org.apache.jclouds.profitbricks.rest.config.ProfitBricksRateLimitModule;
import org.apache.jclouds.profitbricks.rest.domain.FirewallRule;
import org.apache.jclouds.profitbricks.rest.domain.Server;
import org.apache.jclouds.profitbricks.rest.domain.options.DepthOptions;
import org.apache.jclouds.profitbricks.rest.domain.zonescoped.DataCenterAndId;
import static org.assertj.core.api.Assertions.assertThat;
import org.jclouds.compute.RunNodesException;
import org.jclouds.compute.domain.ExecResponse;
import org.jclouds.compute.domain.NodeMetadata;
import org.jclouds.compute.domain.Template;
import org.jclouds.compute.internal.BaseComputeServiceLiveTest;
import org.jclouds.compute.predicates.NodePredicates;
import static org.jclouds.compute.predicates.NodePredicates.inGroup;

import org.apache.jclouds.profitbricks.rest.config.ProfitBricksRateLimitModule;
import org.jclouds.logging.config.LoggingModule;
import org.jclouds.logging.slf4j.config.SLF4JLoggingModule;
import org.jclouds.sshj.config.SshjSshClientModule;
import org.testng.Assert;
import org.testng.annotations.Test;

@Test(groups = "live", singleThreaded = true, testName = "ProfitBricksComputeServiceLiveTest")
public class ProfitBricksComputeServiceLiveTest extends BaseComputeServiceLiveTest {

static ProfitBricksApi pbApi;

public ProfitBricksComputeServiceLiveTest() {
provider = "profitbricks-rest";
}
@@ -49,7 +59,7 @@ protected Module getSshModule() {
protected LoggingModule getLoggingModule() {
return new SLF4JLoggingModule();
}

@Override
protected Iterable<Module> setupModules() {
ImmutableSet.Builder<Module> modules = ImmutableSet.builder();
@@ -83,6 +93,23 @@ protected void checkOsMatchesTemplate(NodeMetadata node) {
// Not enough description from API to match template
}

@Override
protected void createAndRunAServiceInGroup(String group) throws RunNodesException {
super.createAndRunAServiceInGroup(group);
pbApi = client.getContext().unwrapApi(ProfitBricksApi.class);

int matches = 0;
NodeMetadata node = Iterables.getOnlyElement(client.listNodesDetailsMatching(NodePredicates.inGroup(group)));
DataCenterAndId datacenterAndId = DataCenterAndId.fromSlashEncoded(node.getId());
Server server = pbApi.serverApi().getServer(datacenterAndId.getDataCenter(), datacenterAndId.getId(), new DepthOptions().depth(5));
for (FirewallRule rule : server.entities().nics().items().get(0).entities().firewallrules().items()) {
if (rule.properties().portRangeStart() == 22 || rule.properties().portRangeStart() == 8080) {
matches++;
}
}
Assert.assertEquals(2, matches);
}

@Override
@Test
public void testCreateNodeWithCustomHardware() throws Exception {
@@ -113,4 +140,5 @@ public void testCreateNodeWithCustomHardwareUsingMins() throws Exception {
client.destroyNodesMatching(inGroup(group + "custom"));
}
}

}

0 comments on commit b083f4c

Please sign in to comment.