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

org.jclouds.ec2.compute.config.EC2ComputeServiceDependenciesModule Maven / Gradle / Ivy

The newest version!
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF 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.ec2.compute.config;

import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static org.jclouds.ec2.reference.EC2Constants.PROPERTY_EC2_TIMEOUT_SECURITYGROUP_PRESENT;
import static org.jclouds.util.Predicates2.retry;

import java.util.Map;
import java.util.concurrent.ConcurrentMap;

import javax.inject.Named;
import javax.inject.Singleton;

import org.jclouds.compute.ComputeService;
import org.jclouds.compute.domain.Image;
import org.jclouds.compute.domain.NodeMetadata;
import org.jclouds.compute.domain.NodeMetadata.Status;
import org.jclouds.compute.domain.SecurityGroup;
import org.jclouds.compute.domain.TemplateBuilder;
import org.jclouds.compute.extensions.ImageExtension;
import org.jclouds.compute.extensions.SecurityGroupExtension;
import org.jclouds.compute.options.TemplateOptions;
import org.jclouds.domain.LoginCredentials;
import org.jclouds.ec2.compute.EC2ComputeService;
import org.jclouds.ec2.compute.domain.PasswordDataAndPrivateKey;
import org.jclouds.ec2.compute.domain.RegionAndName;
import org.jclouds.ec2.compute.extensions.EC2ImageExtension;
import org.jclouds.ec2.compute.extensions.EC2SecurityGroupExtension;
import org.jclouds.ec2.compute.functions.AddElasticIpsToNodemetadata;
import org.jclouds.ec2.compute.functions.CreateUniqueKeyPair;
import org.jclouds.ec2.compute.functions.CredentialsForInstance;
import org.jclouds.ec2.compute.functions.EC2ImageParser;
import org.jclouds.ec2.compute.functions.EC2SecurityGroupIdFromName;
import org.jclouds.ec2.compute.functions.EC2SecurityGroupToSecurityGroup;
import org.jclouds.ec2.compute.functions.PasswordCredentialsFromWindowsInstance;
import org.jclouds.ec2.compute.functions.RunningInstanceToNodeMetadata;
import org.jclouds.ec2.compute.functions.WindowsLoginCredentialsFromEncryptedData;
import org.jclouds.ec2.compute.internal.EC2TemplateBuilderImpl;
import org.jclouds.ec2.compute.loaders.CreateSecurityGroupIfNeeded;
import org.jclouds.ec2.compute.loaders.LoadPublicIpForInstanceOrNull;
import org.jclouds.ec2.compute.loaders.RegionAndIdToImage;
import org.jclouds.ec2.compute.options.EC2TemplateOptions;
import org.jclouds.ec2.compute.predicates.SecurityGroupPresent;
import org.jclouds.ec2.domain.Image.ImageState;
import org.jclouds.ec2.domain.InstanceState;
import org.jclouds.ec2.domain.KeyPair;
import org.jclouds.ec2.domain.RunningInstance;
import org.jclouds.ec2.reference.EC2Constants;

import shaded.com.google.common.annotations.VisibleForTesting;
import shaded.com.google.common.base.Function;
import shaded.com.google.common.base.Functions;
import shaded.com.google.common.base.Optional;
import shaded.com.google.common.base.Predicate;
import shaded.com.google.common.cache.CacheBuilder;
import shaded.com.google.common.cache.CacheLoader;
import shaded.com.google.common.cache.LoadingCache;
import shaded.com.google.common.collect.ImmutableMap;
import shaded.com.google.common.collect.Maps;
import shaded.com.google.inject.AbstractModule;
import shaded.com.google.inject.Injector;
import shaded.com.google.inject.Provides;
import shaded.com.google.inject.TypeLiteral;
import shaded.com.google.inject.name.Names;

public class EC2ComputeServiceDependenciesModule extends AbstractModule {

   public static final Map toPortableNodeStatus = ImmutableMap
            . builder()
            .put(InstanceState.PENDING, Status.PENDING)
            .put(InstanceState.RUNNING, Status.RUNNING)
            .put(InstanceState.SHUTTING_DOWN, Status.PENDING)
            .put(InstanceState.TERMINATED, Status.TERMINATED)
            .put(InstanceState.STOPPING, Status.PENDING)
            .put(InstanceState.STOPPED, Status.SUSPENDED)
            .put(InstanceState.UNRECOGNIZED, Status.UNRECOGNIZED)
            .build();
   
   @Singleton
   @Provides
   protected final Map toPortableNodeStatus() {
      return toPortableNodeStatus;
   }
   
   @VisibleForTesting
   public static final Map toPortableImageStatus = ImmutableMap
            . builder()
            .put(ImageState.AVAILABLE, Image.Status.AVAILABLE)
            .put(ImageState.DEREGISTERED, Image.Status.DELETED)
            .put(ImageState.UNRECOGNIZED, Image.Status.UNRECOGNIZED).build();

   @Singleton
   @Provides
   protected final Map toPortableImageStatus() {
      return toPortableImageStatus;
   }
   
   @Override
   protected void configure() {
      bind(TemplateBuilder.class).to(EC2TemplateBuilderImpl.class);
      bind(TemplateOptions.class).to(EC2TemplateOptions.class);
      bind(ComputeService.class).to(EC2ComputeService.class);
      bind(new TypeLiteral>>() {
      }).to(CredentialsForInstance.class);
      bind(new TypeLiteral>() {
      }).to(CreateUniqueKeyPair.class);
      bind(new TypeLiteral>() {
      }).to(RegionAndIdToImage.class);
      bind(new TypeLiteral>() {
      }).annotatedWith(Names.named("SECURITY")).to(CreateSecurityGroupIfNeeded.class);
      bind(new TypeLiteral>() {
      }).annotatedWith(Names.named("ELASTICIP")).to(LoadPublicIpForInstanceOrNull.class);
      bind(new TypeLiteral>() {
      }).annotatedWith(Names.named("SECGROUP_NAME_TO_ID")).to(EC2SecurityGroupIdFromName.class);
      bind(new TypeLiteral>() {
      }).to(WindowsLoginCredentialsFromEncryptedData.class);
      bind(new TypeLiteral>() {
      }).to(PasswordCredentialsFromWindowsInstance.class);
      bind(new TypeLiteral>() {
      }).to(EC2ImageParser.class);
      bind(new TypeLiteral>() {
      }).to(EC2SecurityGroupToSecurityGroup.class);
      bind(new TypeLiteral() {
      }).to(EC2ImageExtension.class);
      bind(new TypeLiteral() {
      }).to(EC2SecurityGroupExtension.class);
   }

   /**
    * only add the overhead of looking up ips when we have enabled the auto-allocate functionality
    */
   @Provides
   @Singleton
   public final Function bindNodeConverter(RunningInstanceToNodeMetadata baseConverter,
              AddElasticIpsToNodemetadata addElasticIpsToNodemetadata,
            @Named(EC2Constants.PROPERTY_EC2_AUTO_ALLOCATE_ELASTIC_IPS) boolean autoAllocateElasticIps) {
      if (!autoAllocateElasticIps)
         return baseConverter;
      return Functions.compose(addElasticIpsToNodemetadata, baseConverter);
   }

   @Provides
   @Singleton
   protected final LoadingCache> credentialsMap(CacheLoader> in) {
      return CacheBuilder.newBuilder().build(in);
   }

   @Provides
   @Singleton
   protected final ConcurrentMap keypairMap(Injector i) {
      return Maps.newConcurrentMap();
   }

   @Provides
   @Singleton
   @Named("SECURITY")
   protected final LoadingCache securityGroupMap(
            @Named("SECURITY") CacheLoader in) {
      return CacheBuilder.newBuilder().build(in);
   }

   @Provides
   @Singleton
   @Named("ELASTICIP")
   protected final LoadingCache instanceToElasticIp(
            @Named("ELASTICIP") CacheLoader in) {
      return CacheBuilder.newBuilder().build(in);
   }

   @Provides
   @Singleton
   @Named("SECURITY")
   protected final Predicate securityGroupEventualConsistencyDelay(SecurityGroupPresent in,
         @Named(PROPERTY_EC2_TIMEOUT_SECURITYGROUP_PRESENT) long msDelay) {
      return retry(in, msDelay, 100L, MILLISECONDS);
   }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy