| 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.cloudsigma.compute.config; |
| 20 | |
| 21 | import java.util.concurrent.TimeUnit; |
| 22 | |
| 23 | import javax.inject.Inject; |
| 24 | import javax.inject.Singleton; |
| 25 | |
| 26 | import org.jclouds.cloudsigma.CloudSigmaAsyncClient; |
| 27 | import org.jclouds.cloudsigma.CloudSigmaClient; |
| 28 | import org.jclouds.cloudsigma.compute.CloudSigmaComputeServiceAdapter; |
| 29 | import org.jclouds.cloudsigma.compute.functions.ParseOsFamilyVersion64BitFromImageName; |
| 30 | import org.jclouds.cloudsigma.compute.functions.PreinstalledDiskToImage; |
| 31 | import org.jclouds.cloudsigma.compute.functions.ServerInfoToNodeMetadata; |
| 32 | import org.jclouds.cloudsigma.compute.functions.ServerInfoToNodeMetadata.DeviceToVolume; |
| 33 | import org.jclouds.cloudsigma.compute.functions.ServerInfoToNodeMetadata.FindImageForId; |
| 34 | import org.jclouds.cloudsigma.compute.functions.ServerInfoToNodeMetadata.GetImageIdFromServer; |
| 35 | import org.jclouds.cloudsigma.domain.Device; |
| 36 | import org.jclouds.cloudsigma.domain.DriveInfo; |
| 37 | import org.jclouds.cloudsigma.domain.Server; |
| 38 | import org.jclouds.cloudsigma.domain.ServerInfo; |
| 39 | import org.jclouds.cloudsigma.predicates.DriveClaimed; |
| 40 | import org.jclouds.compute.ComputeServiceAdapter; |
| 41 | import org.jclouds.compute.config.ComputeServiceAdapterContextModule; |
| 42 | import org.jclouds.compute.domain.Hardware; |
| 43 | import org.jclouds.compute.domain.Image; |
| 44 | import org.jclouds.compute.domain.NodeMetadata; |
| 45 | import org.jclouds.compute.domain.OsFamily; |
| 46 | import org.jclouds.compute.domain.OsFamilyVersion64Bit; |
| 47 | import org.jclouds.compute.domain.TemplateBuilder; |
| 48 | import org.jclouds.compute.domain.Volume; |
| 49 | import org.jclouds.compute.reference.ComputeServiceConstants; |
| 50 | import org.jclouds.domain.Location; |
| 51 | import org.jclouds.functions.IdentityFunction; |
| 52 | import org.jclouds.location.suppliers.OnlyLocationOrFirstZone; |
| 53 | import org.jclouds.predicates.RetryablePredicate; |
| 54 | |
| 55 | import com.google.common.base.Function; |
| 56 | import com.google.common.base.Predicate; |
| 57 | import com.google.common.base.Predicates; |
| 58 | import com.google.common.base.Supplier; |
| 59 | import com.google.common.cache.Cache; |
| 60 | import com.google.common.cache.CacheBuilder; |
| 61 | import com.google.common.cache.CacheLoader; |
| 62 | import com.google.inject.Injector; |
| 63 | import com.google.inject.Provides; |
| 64 | import com.google.inject.TypeLiteral; |
| 65 | |
| 66 | /** |
| 67 | * |
| 68 | * @author Adrian Cole |
| 69 | */ |
| 70 | public class CloudSigmaComputeServiceContextModule |
| 71 | extends |
| 72 | ComputeServiceAdapterContextModule<CloudSigmaClient, CloudSigmaAsyncClient, ServerInfo, Hardware, DriveInfo, Location> { |
| 73 | |
| 74 | public CloudSigmaComputeServiceContextModule() { |
| 75 | super(CloudSigmaClient.class, CloudSigmaAsyncClient.class); |
| 76 | } |
| 77 | |
| 78 | @Override |
| 79 | protected TemplateBuilder provideTemplate(Injector injector, TemplateBuilder template) { |
| 80 | return template.osFamily(OsFamily.UBUNTU).imageNameMatches(".*automated SSH Access.*"); |
| 81 | } |
| 82 | |
| 83 | @SuppressWarnings({ "unchecked", "rawtypes" }) |
| 84 | @Override |
| 85 | protected void configure() { |
| 86 | super.configure(); |
| 87 | bind(new TypeLiteral<ComputeServiceAdapter<ServerInfo, Hardware, DriveInfo, Location>>() { |
| 88 | }).to(CloudSigmaComputeServiceAdapter.class); |
| 89 | bind(new TypeLiteral<Function<ServerInfo, NodeMetadata>>() { |
| 90 | }).to(ServerInfoToNodeMetadata.class); |
| 91 | bind(new TypeLiteral<Function<Hardware, Hardware>>() { |
| 92 | }).to((Class) IdentityFunction.class); |
| 93 | bind(new TypeLiteral<Function<DriveInfo, Image>>() { |
| 94 | }).to(PreinstalledDiskToImage.class); |
| 95 | bind(new TypeLiteral<Function<Location, Location>>() { |
| 96 | }).to((Class) IdentityFunction.class); |
| 97 | bind(new TypeLiteral<Function<Device, Volume>>() { |
| 98 | }).to(DeviceToVolume.class); |
| 99 | bind(new TypeLiteral<Function<Server, String>>() { |
| 100 | }).to(GetImageIdFromServer.class); |
| 101 | bind(new TypeLiteral<Function<String, Image>>() { |
| 102 | }).to(FindImageForId.class); |
| 103 | bind(new TypeLiteral<Function<String, OsFamilyVersion64Bit>>() { |
| 104 | }).to(ParseOsFamilyVersion64BitFromImageName.class); |
| 105 | bind(new TypeLiteral<Supplier<Location>>() { |
| 106 | }).to(OnlyLocationOrFirstZone.class); |
| 107 | } |
| 108 | |
| 109 | @Provides |
| 110 | @Singleton |
| 111 | protected Cache<String, DriveInfo> cache(GetDrive getDrive) { |
| 112 | return CacheBuilder.newBuilder().build(getDrive); |
| 113 | } |
| 114 | |
| 115 | @Singleton |
| 116 | public static class GetDrive extends CacheLoader<String, DriveInfo> { |
| 117 | private final CloudSigmaClient client; |
| 118 | |
| 119 | @Inject |
| 120 | public GetDrive(CloudSigmaClient client) { |
| 121 | this.client = client; |
| 122 | } |
| 123 | |
| 124 | @Override |
| 125 | public DriveInfo load(String input) { |
| 126 | return client.getDriveInfo(input); |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | @Provides |
| 131 | @Singleton |
| 132 | protected Predicate<DriveInfo> supplyDriveUnclaimed(DriveClaimed driveClaimed, |
| 133 | ComputeServiceConstants.Timeouts timeouts) { |
| 134 | return new RetryablePredicate<DriveInfo>(Predicates.not(driveClaimed), timeouts.nodeRunning, 1000, |
| 135 | TimeUnit.MILLISECONDS); |
| 136 | } |
| 137 | } |