org.jclouds.compute.config.ComputeServiceAdapterContextModule Maven / Gradle / Ivy
/**
* Licensed to jclouds, Inc. (jclouds) under one or more
* contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. jclouds 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.compute.config;
import static com.google.common.base.Functions.compose;
import static com.google.common.base.Predicates.notNull;
import static com.google.common.collect.Iterables.filter;
import static com.google.common.collect.Iterables.transform;
import java.util.Set;
import javax.inject.Inject;
import javax.inject.Singleton;
import org.jclouds.collect.TransformingSetSupplier;
import org.jclouds.compute.ComputeServiceAdapter;
import org.jclouds.compute.domain.Hardware;
import org.jclouds.compute.domain.Image;
import org.jclouds.compute.domain.ImageBuilder;
import org.jclouds.compute.strategy.CreateNodeWithGroupEncodedIntoName;
import org.jclouds.compute.strategy.DestroyNodeStrategy;
import org.jclouds.compute.strategy.GetNodeMetadataStrategy;
import org.jclouds.compute.strategy.ListNodesStrategy;
import org.jclouds.compute.strategy.PopulateDefaultLoginCredentialsForImageStrategy;
import org.jclouds.compute.strategy.RebootNodeStrategy;
import org.jclouds.compute.strategy.ResumeNodeStrategy;
import org.jclouds.compute.strategy.SuspendNodeStrategy;
import org.jclouds.compute.strategy.impl.AdaptingComputeServiceStrategies;
import org.jclouds.domain.Location;
import org.jclouds.domain.LoginCredentials;
import org.jclouds.location.suppliers.LocationsSupplier;
import com.google.common.base.Function;
import com.google.common.base.Supplier;
import com.google.common.collect.ImmutableSet;
import com.google.inject.AbstractModule;
import com.google.inject.Provides;
/**
*
* @author Adrian Cole
*/
public class ComputeServiceAdapterContextModule extends BaseComputeServiceContextModule {
/**
* install this, if you want to use your computeservice adapter to handle locations. Note that if
* you do this, you'll want to instantiate a subclass to prevent type erasure.
*
* ex.
*
* install(new LocationsFromComputeServiceAdapterModule(){});
*
* not
*
* install(new LocationsFromComputeServiceAdapterModule());
*
*/
public static class LocationsFromComputeServiceAdapterModule extends AbstractModule {
@Override
protected void configure() {
}
@Provides
@Singleton
protected LocationsSupplier supplyLocationsFromComputeServiceAdapter(
final ComputeServiceAdapter adapter, final Function transformer) {
return new LocationsSupplier() {
@Override
public Set extends Location> get() {
Iterable locations = filter(adapter.listLocations(), notNull());
return ImmutableSet. copyOf(transform(locations, transformer));
}
};
}
}
@Provides
@Singleton
protected Supplier> provideHardware(final ComputeServiceAdapter adapter,
Function transformer) {
return new TransformingSetSupplier(new Supplier>() {
@Override
public Iterable get() {
return adapter.listHardwareProfiles();
}
}, transformer);
}
@Provides
@Singleton
protected Supplier> provideImages(final ComputeServiceAdapter adapter,
Function transformer, AddDefaultCredentialsToImage addDefaultCredentialsToImage) {
return new TransformingSetSupplier(new Supplier>() {
@Override
public Iterable get() {
return filter(adapter.listImages(), notNull());
}
}, compose(addDefaultCredentialsToImage, transformer));
}
@Singleton
public static class AddDefaultCredentialsToImage implements Function {
private final PopulateDefaultLoginCredentialsForImageStrategy credsForImage;
@Inject
public AddDefaultCredentialsToImage(PopulateDefaultLoginCredentialsForImageStrategy credsForImage) {
this.credsForImage = credsForImage;
}
@Override
public Image apply(Image arg0) {
if (arg0 == null)
return null;
LoginCredentials credentials = credsForImage.apply(arg0);
return credentials != null ? ImageBuilder.fromImage(arg0).defaultCredentials(credentials).build() : arg0;
}
@Override
public String toString() {
return "addDefaultCredentialsToImage()";
}
}
@Provides
@Singleton
protected CreateNodeWithGroupEncodedIntoName defineAddNodeWithTagStrategy(
AdaptingComputeServiceStrategies in) {
return in;
}
@Provides
@Singleton
protected DestroyNodeStrategy defineDestroyNodeStrategy(AdaptingComputeServiceStrategies in) {
return in;
}
@Provides
@Singleton
protected GetNodeMetadataStrategy defineGetNodeMetadataStrategy(AdaptingComputeServiceStrategies in) {
return in;
}
@Provides
@Singleton
protected ListNodesStrategy defineListNodesStrategy(AdaptingComputeServiceStrategies in) {
return in;
}
@Provides
@Singleton
protected RebootNodeStrategy defineRebootNodeStrategy(AdaptingComputeServiceStrategies in) {
return in;
}
@Provides
@Singleton
protected ResumeNodeStrategy defineStartNodeStrategy(AdaptingComputeServiceStrategies in) {
return in;
}
@Provides
@Singleton
protected SuspendNodeStrategy defineStopNodeStrategy(AdaptingComputeServiceStrategies in) {
return in;
}
} © 2015 - 2025 Weber Informatics LLC | Privacy Policy