| 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.softlayer.compute.config; |
| 20 | |
| 21 | import static com.google.common.base.Preconditions.checkNotNull; |
| 22 | import static com.google.common.collect.Iterables.find; |
| 23 | import static org.jclouds.Constants.PROPERTY_SESSION_INTERVAL; |
| 24 | import static org.jclouds.softlayer.predicates.ProductPackagePredicates.named; |
| 25 | import static org.jclouds.softlayer.reference.SoftLayerConstants.PROPERTY_SOFTLAYER_VIRTUALGUEST_PACKAGE_NAME; |
| 26 | import static org.jclouds.softlayer.reference.SoftLayerConstants.PROPERTY_SOFTLAYER_VIRTUALGUEST_PRICES; |
| 27 | |
| 28 | import javax.inject.Named; |
| 29 | import javax.inject.Singleton; |
| 30 | |
| 31 | import org.jclouds.collect.Memoized; |
| 32 | import org.jclouds.compute.ComputeServiceAdapter; |
| 33 | import org.jclouds.compute.config.ComputeServiceAdapterContextModule; |
| 34 | import org.jclouds.compute.domain.NodeMetadata; |
| 35 | import org.jclouds.compute.domain.OsFamily; |
| 36 | import org.jclouds.compute.domain.TemplateBuilder; |
| 37 | import org.jclouds.compute.options.TemplateOptions; |
| 38 | import org.jclouds.domain.Location; |
| 39 | import org.jclouds.location.suppliers.OnlyLocationOrFirstZone; |
| 40 | import org.jclouds.rest.suppliers.MemoizedRetryOnTimeOutButNotOnAuthorizationExceptionSupplier; |
| 41 | import org.jclouds.softlayer.SoftLayerAsyncClient; |
| 42 | import org.jclouds.softlayer.SoftLayerClient; |
| 43 | import org.jclouds.softlayer.compute.functions.DatacenterToLocation; |
| 44 | import org.jclouds.softlayer.compute.functions.ProductItemToImage; |
| 45 | import org.jclouds.softlayer.compute.functions.ProductItemsToHardware; |
| 46 | import org.jclouds.softlayer.compute.functions.VirtualGuestToNodeMetadata; |
| 47 | import org.jclouds.softlayer.compute.options.SoftLayerTemplateOptions; |
| 48 | import org.jclouds.softlayer.compute.strategy.SoftLayerComputeServiceAdapter; |
| 49 | import org.jclouds.softlayer.domain.Datacenter; |
| 50 | import org.jclouds.softlayer.domain.ProductItem; |
| 51 | import org.jclouds.softlayer.domain.ProductItemPrice; |
| 52 | import org.jclouds.softlayer.domain.ProductPackage; |
| 53 | import org.jclouds.softlayer.domain.VirtualGuest; |
| 54 | import org.jclouds.softlayer.features.AccountClient; |
| 55 | import org.jclouds.softlayer.features.ProductPackageClient; |
| 56 | |
| 57 | import com.google.common.base.Function; |
| 58 | import com.google.common.base.Splitter; |
| 59 | import com.google.common.base.Supplier; |
| 60 | import com.google.common.collect.Iterables; |
| 61 | import com.google.inject.Injector; |
| 62 | import com.google.inject.Provides; |
| 63 | import com.google.inject.TypeLiteral; |
| 64 | |
| 65 | /** |
| 66 | * |
| 67 | * @author Adrian Cole |
| 68 | */ |
| 69 | public class SoftLayerComputeServiceContextModule |
| 70 | extends |
| 71 | ComputeServiceAdapterContextModule<SoftLayerClient, SoftLayerAsyncClient, VirtualGuest, Iterable<ProductItem>, ProductItem, Datacenter> { |
| 72 | |
| 73 | public SoftLayerComputeServiceContextModule() { |
| 74 | super(SoftLayerClient.class, SoftLayerAsyncClient.class); |
| 75 | } |
| 76 | |
| 77 | @Override |
| 78 | protected void configure() { |
| 79 | super.configure(); |
| 80 | bind(new TypeLiteral<ComputeServiceAdapter<VirtualGuest, Iterable<ProductItem>, ProductItem, Datacenter>>() { |
| 81 | }).to(SoftLayerComputeServiceAdapter.class); |
| 82 | bind(new TypeLiteral<Function<VirtualGuest, NodeMetadata>>() { |
| 83 | }).to(VirtualGuestToNodeMetadata.class); |
| 84 | bind(new TypeLiteral<Function<ProductItem, org.jclouds.compute.domain.Image>>() { |
| 85 | }).to(ProductItemToImage.class); |
| 86 | bind(new TypeLiteral<Function<Iterable<ProductItem>, org.jclouds.compute.domain.Hardware>>() { |
| 87 | }).to(ProductItemsToHardware.class); |
| 88 | bind(new TypeLiteral<Function<Datacenter, Location>>() { |
| 89 | }).to(DatacenterToLocation.class); |
| 90 | bind(new TypeLiteral<Supplier<Location>>() { |
| 91 | }).to(OnlyLocationOrFirstZone.class); |
| 92 | bind(TemplateOptions.class).to(SoftLayerTemplateOptions.class); |
| 93 | } |
| 94 | |
| 95 | protected TemplateBuilder provideTemplate(Injector injector, TemplateBuilder template) { |
| 96 | return template.osFamily(OsFamily.UBUNTU).osVersionMatches("1[10].[10][04]").os64Bit(true).osDescriptionMatches( |
| 97 | ".*Minimal Install.*"); |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Many requests need the same productPackage, which is in this case the package for virtual |
| 102 | * guests. We may at some point need to make an annotation qualifying it as such. ex. @VirtualGuest |
| 103 | */ |
| 104 | @Provides |
| 105 | @Singleton |
| 106 | @Memoized |
| 107 | public Supplier<ProductPackage> getProductPackage(@Named(PROPERTY_SESSION_INTERVAL) long seconds, |
| 108 | final SoftLayerClient client, |
| 109 | @Named(PROPERTY_SOFTLAYER_VIRTUALGUEST_PACKAGE_NAME) final String virtualGuestPackageName) { |
| 110 | return new MemoizedRetryOnTimeOutButNotOnAuthorizationExceptionSupplier<ProductPackage>(authException, seconds, |
| 111 | new Supplier<ProductPackage>() { |
| 112 | @Override |
| 113 | public ProductPackage get() { |
| 114 | AccountClient accountClient = client.getAccountClient(); |
| 115 | ProductPackageClient productPackageClient = client.getProductPackageClient(); |
| 116 | ProductPackage p = find(accountClient.getActivePackages(), named(virtualGuestPackageName)); |
| 117 | return productPackageClient.getProductPackage(p.getId()); |
| 118 | } |
| 119 | }); |
| 120 | } |
| 121 | |
| 122 | // TODO: check the prices really do exist |
| 123 | @Provides |
| 124 | @Singleton |
| 125 | public Iterable<ProductItemPrice> prices(@Named(PROPERTY_SOFTLAYER_VIRTUALGUEST_PRICES) String prices) { |
| 126 | return Iterables.transform(Splitter.on(',').split(checkNotNull(prices, "prices")), |
| 127 | new Function<String, ProductItemPrice>() { |
| 128 | @Override |
| 129 | public ProductItemPrice apply(String arg0) { |
| 130 | return ProductItemPrice.builder().id(Integer.parseInt(arg0)).build(); |
| 131 | } |
| 132 | }); |
| 133 | } |
| 134 | |
| 135 | } |