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

org.jclouds.aws.ec2.compute.functions.PresentSpotRequestsAndInstances 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.aws.ec2.compute.functions;

import static shaded.com.google.common.base.Preconditions.checkNotNull;
import static shaded.com.google.common.base.Predicates.containsPattern;
import static shaded.com.google.common.collect.Iterables.any;
import static shaded.com.google.common.collect.Iterables.toArray;
import static shaded.com.google.common.collect.Iterables.transform;
import static shaded.com.google.common.collect.Multimaps.index;
import static shaded.com.google.common.collect.Multimaps.transformValues;
import static org.jclouds.ec2.compute.domain.RegionAndName.nameFunction;
import static org.jclouds.ec2.compute.domain.RegionAndName.regionFunction;

import java.util.Collection;
import java.util.Map;
import java.util.Set;

import javax.inject.Singleton;

import org.jclouds.aws.ec2.AWSEC2Api;
import org.jclouds.aws.ec2.domain.AWSRunningInstance;
import org.jclouds.aws.ec2.domain.SpotInstanceRequest;
import org.jclouds.ec2.compute.domain.RegionAndName;
import org.jclouds.ec2.compute.functions.PresentInstances;
import org.jclouds.ec2.domain.RunningInstance;

import shaded.com.google.common.base.Function;
import shaded.com.google.common.base.Predicates;
import shaded.com.google.common.collect.ImmutableSet;
import shaded.com.google.common.collect.ImmutableSet.Builder;
import shaded.com.google.common.collect.Multimap;
import shaded.com.google.inject.Inject;

/**
 * returns either the instances present in the list, or spot instances, if they ids start with {@code sir-}. Makes a
 * single rest call per aggregate on region.
 */
@Singleton
public class PresentSpotRequestsAndInstances extends PresentInstances {

   private final AWSEC2Api client;
   private final Function spotConverter;

   @Inject
   public PresentSpotRequestsAndInstances(AWSEC2Api client, Function spotConverter) {
      super(client);
      this.client = checkNotNull(client, "client");
      this.spotConverter = checkNotNull(spotConverter, "spotConverter");
   }

   @Override
   public Set apply(Set regionAndIds) {
      if (checkNotNull(regionAndIds, "regionAndIds").isEmpty())
         return ImmutableSet.of();
      if (any(regionAndIds, Predicates.compose(containsPattern("sir-"), nameFunction())))
         return getSpots(regionAndIds);
      return super.apply(regionAndIds);
   }

   protected Set getSpots(Set regionAndIds) {
      Builder builder = ImmutableSet. builder();
      Multimap regionToSpotIds = transformValues(index(regionAndIds, regionFunction()), nameFunction());
      for (Map.Entry> entry : regionToSpotIds.asMap().entrySet()) {
         String region = entry.getKey();
         Collection spotIds = entry.getValue();
         logger.trace("looking for spots %s in region %s", spotIds, region);
         builder.addAll(transform(
                                  client.getSpotInstanceApi().get().describeSpotInstanceRequestsInRegion(region,
                     toArray(spotIds, String.class)), spotConverter));
      }
      return builder.build();
   }

   @Override
   public String toString() {
      return "presentSpotRequestsAndInstances()";
   }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy