All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jclouds.aws.ec2.compute.functions.RunningInstanceToNodeMetadata Maven / Gradle / Ivy

The newest version!
/**
 *
 * Copyright (C) 2010 Cloud Conscious, LLC. 
 *
 * ====================================================================
 * 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.aws.ec2.compute.functions;

import static com.google.common.base.Preconditions.checkNotNull;
import static org.jclouds.util.Utils.nullSafeSet;

import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Set;
import java.util.Map.Entry;

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

import org.jclouds.aws.ec2.compute.domain.RegionAndName;
import org.jclouds.aws.ec2.domain.InstanceState;
import org.jclouds.aws.ec2.domain.RootDeviceType;
import org.jclouds.aws.ec2.domain.RunningInstance;
import org.jclouds.aws.ec2.domain.RunningInstance.EbsBlockDevice;
import org.jclouds.collect.Memoized;
import org.jclouds.compute.domain.Hardware;
import org.jclouds.compute.domain.HardwareBuilder;
import org.jclouds.compute.domain.Image;
import org.jclouds.compute.domain.NodeMetadata;
import org.jclouds.compute.domain.NodeMetadataBuilder;
import org.jclouds.compute.domain.NodeState;
import org.jclouds.compute.domain.Volume;
import org.jclouds.compute.domain.internal.VolumeImpl;
import org.jclouds.domain.Credentials;
import org.jclouds.domain.Location;
import org.jclouds.logging.Logger;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Function;
import com.google.common.base.Predicate;
import com.google.common.base.Supplier;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;

/**
 * @author Adrian Cole
 */
@Singleton
public class RunningInstanceToNodeMetadata implements Function {

   @Resource
   protected Logger logger = Logger.NULL;

   protected final Supplier> locations;
   protected final Supplier> hardware;
   protected final Map instanceToImage;
   protected final Map credentialStore;
   protected final Map instanceToNodeState;

   @Inject
   RunningInstanceToNodeMetadata(Map instanceToNodeState,
            Map credentialStore, Map instanceToImage,
            @Memoized Supplier> locations, @Memoized Supplier> hardware) {
      this.locations = checkNotNull(locations, "locations");
      this.hardware = checkNotNull(hardware, "hardware");
      this.instanceToImage = checkNotNull(instanceToImage, "instanceToImage");
      this.instanceToNodeState = checkNotNull(instanceToNodeState, "instanceToNodeState");
      this.credentialStore = checkNotNull(credentialStore, "credentialStore");
   }

   @Override
   public NodeMetadata apply(RunningInstance instance) {
      NodeMetadataBuilder builder = new NodeMetadataBuilder();
      String providerId = checkNotNull(instance, "instance").getId();
      builder.providerId(providerId);
      builder.id(instance.getRegion() + "/" + providerId);
      String tag = getTagForInstance(instance);
      builder.tag(tag);
      builder.credentials(credentialStore.get("node#" + instance.getRegion() + "/" + providerId));
      builder.state(instanceToNodeState.get(instance.getInstanceState()));
      builder.publicAddresses(nullSafeSet(instance.getIpAddress()));
      builder.privateAddresses(nullSafeSet(instance.getPrivateIpAddress()));
      builder.hardware(parseHardware(instance));
      Location location = getLocationForAvailabilityZoneOrRegion(instance);
      builder.location(location);
      builder.imageId(instance.getRegion() + "/" + instance.getImageId());

      // extract the operating system from the image
      Image image = instanceToImage.get(new RegionAndName(instance.getRegion(), instance.getImageId()));
      if (image != null)
         builder.operatingSystem(image.getOperatingSystem());

      return builder.build();
   }

   protected Hardware parseHardware(final RunningInstance instance) {
      Hardware hardware = getHardwareForInstance(instance);

      if (hardware != null) {
         hardware = HardwareBuilder.fromHardware(hardware).volumes(addEBS(instance, hardware.getVolumes())).build();
      }
      return hardware;
   }

   @VisibleForTesting
   static List addEBS(final RunningInstance instance, Iterable volumes) {
      Iterable ebsVolumes = Iterables.transform(instance.getEbsBlockDevices().entrySet(),
               new Function, Volume>() {

                  @Override
                  public Volume apply(Entry from) {
                     return new VolumeImpl(from.getValue().getVolumeId(), Volume.Type.SAN, null, from.getKey(),
                              instance.getRootDeviceName() != null
                                       && instance.getRootDeviceName().equals(from.getKey()), true);
                  }
               });

      if (instance.getRootDeviceType() == RootDeviceType.EBS) {
         volumes = Iterables.filter(volumes, new Predicate() {

            @Override
            public boolean apply(Volume input) {
               return !input.isBootDevice();
            }

         });

      }
      return Lists.newArrayList(Iterables.concat(volumes, ebsVolumes));

   }

   @VisibleForTesting
   String getTagForInstance(final RunningInstance instance) {
      String tag = String.format("NOTAG-%s", instance.getId());// default
      try {
         tag = Iterables.getOnlyElement(Iterables.filter(instance.getGroupIds(), new Predicate() {

            @Override
            public boolean apply(String input) {
               return input.startsWith("jclouds#") && input.endsWith("#" + instance.getRegion());
            }

         })).substring(8).replaceAll("#" + instance.getRegion() + "$", "");
      } catch (NoSuchElementException e) {
         logger.debug("no tag parsed from %s's groups: %s", instance.getId(), instance.getGroupIds());
      } catch (IllegalArgumentException e) {
         logger
                  .debug("too many groups match %s; %s's groups: %s", "jclouds#", instance.getId(), instance
                           .getGroupIds());
      }
      return tag;
   }

   @VisibleForTesting
   Hardware getHardwareForInstance(final RunningInstance instance) {
      try {
         return Iterables.find(hardware.get(), new Predicate() {

            @Override
            public boolean apply(Hardware input) {
               return input.getId().equals(instance.getInstanceType());
            }

         });
      } catch (NoSuchElementException e) {
         logger.debug("couldn't match instance type %s in: %s", instance.getInstanceType(), hardware.get());
         return null;
      }
   }

   private Location getLocationForAvailabilityZoneOrRegion(final RunningInstance instance) {
      Location location = findLocationWithId(instance.getAvailabilityZone());
      if (location == null)
         location = findLocationWithId(instance.getRegion());
      return location;
   }

   private Location findLocationWithId(final String locationId) {
      try {
         Location location = Iterables.find(locations.get(), new Predicate() {

            @Override
            public boolean apply(Location input) {
               return input.getId().equals(locationId);
            }

         });
         return location;

      } catch (NoSuchElementException e) {
         logger.debug("couldn't match instance location %s in: %s", locationId, locations.get());
         return null;
      }
   }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy