| 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | * contributor license agreements. See the NOTICE file distributed with |
| 4 | * this work for additional information regarding copyright ownership. |
| 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | * (the "License"); you may not use this file except in compliance with |
| 7 | * the License. You may obtain a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | package org.jclouds.glesys.compute.functions; |
| 18 | |
| 19 | import static com.google.common.base.Preconditions.checkNotNull; |
| 20 | import static org.jclouds.location.predicates.LocationPredicates.idEquals; |
| 21 | |
| 22 | import java.util.Set; |
| 23 | |
| 24 | import javax.inject.Inject; |
| 25 | import javax.inject.Singleton; |
| 26 | |
| 27 | import org.jclouds.collect.Memoized; |
| 28 | import org.jclouds.compute.domain.Hardware; |
| 29 | import org.jclouds.compute.domain.HardwareBuilder; |
| 30 | import org.jclouds.compute.domain.Processor; |
| 31 | import org.jclouds.compute.domain.Volume; |
| 32 | import org.jclouds.compute.domain.internal.VolumeImpl; |
| 33 | import org.jclouds.compute.predicates.ImagePredicates; |
| 34 | import org.jclouds.domain.Location; |
| 35 | import org.jclouds.glesys.domain.ServerSpec; |
| 36 | |
| 37 | import com.google.common.base.Function; |
| 38 | import com.google.common.base.Supplier; |
| 39 | import com.google.common.collect.FluentIterable; |
| 40 | import com.google.common.collect.ImmutableList; |
| 41 | |
| 42 | /** |
| 43 | * |
| 44 | * @author Adrian Cole |
| 45 | */ |
| 46 | @Singleton |
| 47 | public class ServerSpecToHardware implements Function<ServerSpec, Hardware> { |
| 48 | |
| 49 | private final Supplier<Set<? extends Location>> locations; |
| 50 | |
| 51 | @Inject |
| 52 | ServerSpecToHardware(@Memoized Supplier<Set<? extends Location>> locations) { |
| 53 | this.locations = checkNotNull(locations, "locations"); |
| 54 | } |
| 55 | |
| 56 | @Override |
| 57 | public Hardware apply(ServerSpec spec) { |
| 58 | Location location = FluentIterable.from(locations.get()).firstMatch(idEquals(spec.getDatacenter())).orNull(); |
| 59 | assert location != null : String.format("no location matched ServerSpec %s", spec); |
| 60 | return new HardwareBuilder().ids(spec.toString()).ram(spec.getMemorySizeMB()).processors( |
| 61 | ImmutableList.of(new Processor(spec.getCpuCores(), 1.0))).volumes( |
| 62 | ImmutableList.<Volume> of(new VolumeImpl((float) spec.getDiskSizeGB(), true, true))).hypervisor( |
| 63 | spec.getPlatform()).location(location).supportsImage( |
| 64 | ImagePredicates.idEquals(spec.getTemplateName())).build(); |
| 65 | } |
| 66 | } |