| 1 | /** |
| 2 | * Licensed to jclouds, Inc. (jclouds) under one or more |
| 3 | * contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. jclouds licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
| 19 | package org.jclouds.elasticstack.compute.config; |
| 20 | |
| 21 | import java.io.IOException; |
| 22 | import java.util.List; |
| 23 | import java.util.Map; |
| 24 | import java.util.concurrent.TimeUnit; |
| 25 | |
| 26 | import javax.inject.Inject; |
| 27 | import javax.inject.Singleton; |
| 28 | |
| 29 | import org.jclouds.compute.ComputeServiceAdapter; |
| 30 | import org.jclouds.compute.config.ComputeServiceAdapterContextModule; |
| 31 | import org.jclouds.compute.domain.Hardware; |
| 32 | import org.jclouds.compute.domain.Image; |
| 33 | import org.jclouds.compute.domain.NodeMetadata; |
| 34 | import org.jclouds.compute.domain.Volume; |
| 35 | import org.jclouds.compute.reference.ComputeServiceConstants; |
| 36 | import org.jclouds.domain.Location; |
| 37 | import org.jclouds.elasticstack.ElasticStackAsyncClient; |
| 38 | import org.jclouds.elasticstack.ElasticStackClient; |
| 39 | import org.jclouds.elasticstack.compute.ElasticStackComputeServiceAdapter; |
| 40 | import org.jclouds.elasticstack.compute.functions.ServerInfoToNodeMetadata; |
| 41 | import org.jclouds.elasticstack.compute.functions.ServerInfoToNodeMetadata.DeviceToVolume; |
| 42 | import org.jclouds.elasticstack.compute.functions.ServerInfoToNodeMetadata.FindImageForId; |
| 43 | import org.jclouds.elasticstack.compute.functions.ServerInfoToNodeMetadata.GetImageIdFromServer; |
| 44 | import org.jclouds.elasticstack.compute.functions.WellKnownImageToImage; |
| 45 | import org.jclouds.elasticstack.domain.Device; |
| 46 | import org.jclouds.elasticstack.domain.DriveInfo; |
| 47 | import org.jclouds.elasticstack.domain.Server; |
| 48 | import org.jclouds.elasticstack.domain.ServerInfo; |
| 49 | import org.jclouds.elasticstack.domain.WellKnownImage; |
| 50 | import org.jclouds.elasticstack.predicates.DriveClaimed; |
| 51 | import org.jclouds.functions.IdentityFunction; |
| 52 | import org.jclouds.json.Json; |
| 53 | import org.jclouds.location.Provider; |
| 54 | import org.jclouds.location.suppliers.OnlyLocationOrFirstZone; |
| 55 | import org.jclouds.predicates.RetryablePredicate; |
| 56 | import org.jclouds.util.Strings2; |
| 57 | |
| 58 | import com.google.common.base.Function; |
| 59 | import com.google.common.base.Predicate; |
| 60 | import com.google.common.base.Predicates; |
| 61 | import com.google.common.base.Supplier; |
| 62 | import com.google.common.cache.Cache; |
| 63 | import com.google.common.cache.CacheBuilder; |
| 64 | import com.google.common.cache.CacheLoader; |
| 65 | import com.google.common.collect.Maps; |
| 66 | import com.google.inject.Provides; |
| 67 | import com.google.inject.TypeLiteral; |
| 68 | |
| 69 | /** |
| 70 | * |
| 71 | * @author Adrian Cole |
| 72 | */ |
| 73 | public class ElasticStackComputeServiceContextModule |
| 74 | extends |
| 75 | ComputeServiceAdapterContextModule<ElasticStackClient, ElasticStackAsyncClient, ServerInfo, Hardware, DriveInfo, Location> { |
| 76 | |
| 77 | public ElasticStackComputeServiceContextModule() { |
| 78 | super(ElasticStackClient.class, ElasticStackAsyncClient.class); |
| 79 | } |
| 80 | |
| 81 | @SuppressWarnings( { "unchecked", "rawtypes" }) |
| 82 | @Override |
| 83 | protected void configure() { |
| 84 | super.configure(); |
| 85 | bind(new TypeLiteral<ComputeServiceAdapter<ServerInfo, Hardware, DriveInfo, Location>>() { |
| 86 | }).to(ElasticStackComputeServiceAdapter.class); |
| 87 | bind(new TypeLiteral<Function<ServerInfo, NodeMetadata>>() { |
| 88 | }).to(ServerInfoToNodeMetadata.class); |
| 89 | bind(new TypeLiteral<Function<Image, Image>>() { |
| 90 | }).to((Class) IdentityFunction.class); |
| 91 | bind(new TypeLiteral<Function<Hardware, Hardware>>() { |
| 92 | }).to((Class) IdentityFunction.class); |
| 93 | bind(new TypeLiteral<Function<Location, Location>>() { |
| 94 | }).to((Class) IdentityFunction.class); |
| 95 | bind(new TypeLiteral<Function<Device, Volume>>() { |
| 96 | }).to(DeviceToVolume.class); |
| 97 | bind(new TypeLiteral<Function<Server, String>>() { |
| 98 | }).to(GetImageIdFromServer.class); |
| 99 | bind(new TypeLiteral<Function<String, Image>>() { |
| 100 | }).to(FindImageForId.class); |
| 101 | bind(new TypeLiteral<Function<DriveInfo, Image>>() { |
| 102 | }).to(WellKnownImageToImage.class); |
| 103 | bind(new TypeLiteral<Supplier<Location>>() { |
| 104 | }).to(OnlyLocationOrFirstZone.class); |
| 105 | } |
| 106 | |
| 107 | @Provides |
| 108 | @Singleton |
| 109 | protected Cache<String, DriveInfo> cache(GetDrive getDrive) { |
| 110 | return CacheBuilder.newBuilder().build(getDrive); |
| 111 | } |
| 112 | |
| 113 | @Singleton |
| 114 | public static class GetDrive extends CacheLoader<String, DriveInfo> { |
| 115 | private final ElasticStackClient client; |
| 116 | |
| 117 | @Inject |
| 118 | public GetDrive(ElasticStackClient client) { |
| 119 | this.client = client; |
| 120 | } |
| 121 | |
| 122 | @Override |
| 123 | public DriveInfo load(String input) { |
| 124 | return client.getDriveInfo(input); |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | @Singleton |
| 129 | @Provides |
| 130 | protected Map<String, WellKnownImage> provideImages(Json json, @Provider String providerName) throws IOException { |
| 131 | List<WellKnownImage> wellKnowns = json.fromJson(Strings2.toStringAndClose(getClass().getResourceAsStream( |
| 132 | "/" + providerName + "/preinstalled_images.json")), new TypeLiteral<List<WellKnownImage>>() { |
| 133 | }.getType()); |
| 134 | return Maps.uniqueIndex(wellKnowns, new Function<WellKnownImage, String>() { |
| 135 | |
| 136 | @Override |
| 137 | public String apply(WellKnownImage input) { |
| 138 | return input.getUuid(); |
| 139 | } |
| 140 | |
| 141 | }); |
| 142 | } |
| 143 | |
| 144 | @Provides |
| 145 | @Singleton |
| 146 | protected Predicate<DriveInfo> supplyDriveUnclaimed(DriveClaimed driveClaimed, |
| 147 | ComputeServiceConstants.Timeouts timeouts) { |
| 148 | return new RetryablePredicate<DriveInfo>(Predicates.not(driveClaimed), timeouts.nodeRunning, 1000, |
| 149 | TimeUnit.MILLISECONDS); |
| 150 | } |
| 151 | } |