Package org.jclouds.openstack.nova.v2_0.extensions

Examples of org.jclouds.openstack.nova.v2_0.extensions.FloatingIPApi


   public AtomicReference<NodeMetadata> apply(AtomicReference<NodeMetadata> input) {
      checkState(nodeRunning.apply(input), "node never achieved state running %s", input.get());
      NodeMetadata node = input.get();
      // node's location is a host
      String zoneId = node.getLocation().getParent().getId();
      FloatingIPApi floatingIpApi = novaApi.getFloatingIPExtensionForZone(zoneId).get();

      FloatingIP ip = null;
      try {
         logger.debug(">> allocating or reassigning floating ip for node(%s)", node.getId());
         ip = floatingIpApi.create();
      } catch (InsufficientResourcesException e) {
         logger.trace("<< [%s] allocating a new floating ip for node(%s)", e.getMessage(), node.getId());
         logger.trace(">> searching for existing, unassigned floating ip for node(%s)", node.getId());
         ArrayList<FloatingIP> unassignedIps = Lists.newArrayList(Iterables.filter(floatingIpApi.list(),
                  new Predicate<FloatingIP>() {

                     @Override
                     public boolean apply(FloatingIP arg0) {
                        return arg0.getFixedIp() == null;
                     }

                  }));
         // try to prevent multiple parallel launches from choosing the same ip.
         Collections.shuffle(unassignedIps);
         ip = Iterables.getLast(unassignedIps);
      }
      logger.debug(">> adding floatingIp(%s) to node(%s)", ip.getIp(), node.getId());

      floatingIpApi.addToServer(ip.getIp(), node.getProviderId());
      input.set(NodeMetadataBuilder.fromNodeMetadata(node).publicAddresses(ImmutableSet.of(ip.getIp())).build());
      floatingIpCache.invalidate(ZoneAndId.fromSlashEncoded(node.getId()));
      return input;
   }
View Full Code Here


      this.floatingIpCache = checkNotNull(floatingIpCache, "floatingIpCache");
   }

   @Override
   public ZoneAndId apply(ZoneAndId id) {
      FloatingIPApi floatingIpApi = novaApi.getFloatingIPExtensionForZone(id.getZone()).get();
      for (FloatingIP ip : floatingIpCache.getUnchecked(id)) {
         logger.debug(">> removing floatingIp(%s) from node(%s)", ip, id);
         floatingIpApi.removeFromServer(ip.getIp(), id.getId());
         logger.debug(">> deallocating floatingIp(%s)", ip);
         floatingIpApi.delete(ip.getId());
      }
      floatingIpCache.invalidate(id);
      return id;
   }
View Full Code Here

public class LoadFloatingIpsForInstanceTest {

   @Test
   public void testReturnsPublicIpOnMatch() throws Exception {
      NovaApi api = createMock(NovaApi.class);
      FloatingIPApi ipApi = createMock(FloatingIPApi.class);
      FloatingIP testIp = FloatingIP.builder().id("1").ip("1.1.1.1").fixedIp("10.1.1.1").instanceId("i-blah").build();

      expect(api.getFloatingIPExtensionForZone("Zone")).andReturn((Optional) Optional.of(ipApi)).atLeastOnce();
      expect(ipApi.list()).andReturn((FluentIterable) FluentIterable.from(ImmutableSet.<FloatingIP> of(testIp)))
               .atLeastOnce();

      replay(api);
      replay(ipApi);
View Full Code Here

   }

   @Test
   public void testReturnsNullWhenNotFound() throws Exception {
      NovaApi api = createMock(NovaApi.class);
      FloatingIPApi ipApi = createMock(FloatingIPApi.class);

      expect(api.getFloatingIPExtensionForZone("Zone")).andReturn((Optional) Optional.of(ipApi)).atLeastOnce();

      expect(ipApi.list()).andReturn((FluentIterable) FluentIterable.from(ImmutableSet.<FloatingIP> of()))
      .atLeastOnce();

      replay(api);
      replay(ipApi);
View Full Code Here

   }

   @Test
   public void testReturnsNullWhenNotAssigned() throws Exception {
      NovaApi api = createMock(NovaApi.class);
      FloatingIPApi ipApi = createMock(FloatingIPApi.class);

      expect(api.getFloatingIPExtensionForZone("Zone")).andReturn((Optional) Optional.of(ipApi)).atLeastOnce();

      expect(ipApi.list()).andReturn((FluentIterable) FluentIterable.from(ImmutableSet.<FloatingIP> of(FloatingIP.builder().id("1").ip("1.1.1.1").build())))
      .atLeastOnce();

      replay(api);
      replay(ipApi);
View Full Code Here

   public AtomicReference<NodeMetadata> apply(AtomicReference<NodeAndNovaTemplateOptions> input) {
      checkState(nodeRunning.apply(input.get().getNodeMetadata()), "node never achieved state running %s", input.get().getNodeMetadata());
      NodeMetadata node = input.get().getNodeMetadata().get();
      // node's location is a host
      String zoneId = node.getLocation().getParent().getId();
      FloatingIPApi floatingIpApi = novaApi.getFloatingIPExtensionForZone(zoneId).get();
      Optional<Set<String>> poolNames = input.get().getNovaTemplateOptions().get().getFloatingIpPoolNames();

      Optional<FloatingIP> ip = allocateFloatingIPForNode(floatingIpApi, poolNames, node.getId());
      if (!ip.isPresent()) {
         throw new InsufficientResourcesException("Failed to allocate a FloatingIP for node(" + node.getId() + ")");
      }
      logger.debug(">> adding floatingIp(%s) to node(%s)", ip.get().getIp(), node.getId());

      floatingIpApi.addToServer(ip.get().getIp(), node.getProviderId());
      input.get().getNodeMetadata().set(NodeMetadataBuilder.fromNodeMetadata(node).publicAddresses(ImmutableSet.of(ip.get().getIp())).build());
      floatingIpCache.invalidate(ZoneAndId.fromSlashEncoded(node.getId()));
      return input.get().getNodeMetadata();
   }
View Full Code Here

      this.floatingIpCache = checkNotNull(floatingIpCache, "floatingIpCache");
   }

   @Override
   public ZoneAndId apply(ZoneAndId id) {
      FloatingIPApi floatingIpApi = novaApi.getFloatingIPExtensionForZone(id.getZone()).get();
      for (FloatingIP ip : floatingIpCache.getUnchecked(id)) {
         logger.debug(">> removing floatingIp(%s) from node(%s)", ip, id);
         floatingIpApi.removeFromServer(ip.getIp(), id.getId());
         logger.debug(">> deallocating floatingIp(%s)", ip);
         floatingIpApi.delete(ip.getId());
      }
      floatingIpCache.invalidate(id);
      return id;
   }
View Full Code Here

   public AtomicReference<NodeMetadata> apply(AtomicReference<NodeMetadata> input) {
      checkState(nodeRunning.apply(input), "node never achieved state running %s", input.get());
      NodeMetadata node = input.get();
      // node's location is a host
      String zoneId = node.getLocation().getParent().getId();
      FloatingIPApi floatingIpApi = novaApi.getFloatingIPExtensionForZone(zoneId).get();

      FloatingIP ip = null;
      try {
         logger.debug(">> allocating or reassigning floating ip for node(%s)", node.getId());
         ip = floatingIpApi.create();
      } catch (InsufficientResourcesException e) {
         logger.trace("<< [%s] allocating a new floating ip for node(%s)", e.getMessage(), node.getId());
         logger.trace(">> searching for existing, unassigned floating ip for node(%s)", node.getId());
         ArrayList<FloatingIP> unassignedIps = Lists.newArrayList(Iterables.filter(floatingIpApi.list(),
                  new Predicate<FloatingIP>() {

                     @Override
                     public boolean apply(FloatingIP arg0) {
                        return arg0.getFixedIp() == null;
                     }

                  }));
         // try to prevent multiple parallel launches from choosing the same ip.
         Collections.shuffle(unassignedIps);
         ip = Iterables.getLast(unassignedIps);
      }
      logger.debug(">> adding floatingIp(%s) to node(%s)", ip.getIp(), node.getId());

      floatingIpApi.addToServer(ip.getIp(), node.getProviderId());
      input.set(NodeMetadataBuilder.fromNodeMetadata(node).publicAddresses(ImmutableSet.of(ip.getIp())).build());
      floatingIpCache.invalidate(ZoneAndId.fromSlashEncoded(node.getId()));
      return input;
   }
View Full Code Here

      this.floatingIpCache = checkNotNull(floatingIpCache, "floatingIpCache");
   }

   @Override
   public ZoneAndId apply(ZoneAndId id) {
      FloatingIPApi floatingIpApi = novaApi.getFloatingIPExtensionForZone(id.getZone()).get();
      for (FloatingIP ip : floatingIpCache.getUnchecked(id)) {
         logger.debug(">> removing floatingIp(%s) from node(%s)", ip, id);
         floatingIpApi.removeFromServer(ip.getIp(), id.getId());
         logger.debug(">> deallocating floatingIp(%s)", ip);
         floatingIpApi.delete(ip.getId());
      }
      floatingIpCache.invalidate(id);
      return id;
   }
View Full Code Here

public class LoadFloatingIpsForInstanceTest {

   @Test
   public void testReturnsPublicIpOnMatch() throws Exception {
      NovaApi api = createMock(NovaApi.class);
      FloatingIPApi ipApi = createMock(FloatingIPApi.class);
      FloatingIP testIp = FloatingIP.builder().id("1").ip("1.1.1.1").fixedIp("10.1.1.1").instanceId("i-blah").build();

      expect(api.getFloatingIPExtensionForZone("Zone")).andReturn((Optional) Optional.of(ipApi)).atLeastOnce();
      expect(ipApi.list()).andReturn((FluentIterable) FluentIterable.from(ImmutableSet.<FloatingIP> of(testIp)))
               .atLeastOnce();

      replay(api);
      replay(ipApi);
View Full Code Here

TOP

Related Classes of org.jclouds.openstack.nova.v2_0.extensions.FloatingIPApi

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.