| 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.rimuhosting.miro.compute.suppliers; |
| 20 | |
| 21 | import java.util.Set; |
| 22 | |
| 23 | import javax.annotation.Resource; |
| 24 | import javax.inject.Inject; |
| 25 | import javax.inject.Named; |
| 26 | import javax.inject.Singleton; |
| 27 | |
| 28 | import org.jclouds.collect.Memoized; |
| 29 | import org.jclouds.compute.domain.Hardware; |
| 30 | import org.jclouds.compute.domain.HardwareBuilder; |
| 31 | import org.jclouds.compute.domain.Processor; |
| 32 | import org.jclouds.compute.domain.Volume; |
| 33 | import org.jclouds.compute.domain.internal.VolumeImpl; |
| 34 | import org.jclouds.compute.reference.ComputeServiceConstants; |
| 35 | import org.jclouds.domain.Location; |
| 36 | import org.jclouds.logging.Logger; |
| 37 | import org.jclouds.rimuhosting.miro.RimuHostingClient; |
| 38 | import org.jclouds.rimuhosting.miro.domain.PricingPlan; |
| 39 | |
| 40 | import com.google.common.base.Predicate; |
| 41 | import com.google.common.base.Supplier; |
| 42 | import com.google.common.collect.ImmutableList; |
| 43 | import com.google.common.collect.Iterables; |
| 44 | import com.google.common.collect.Sets; |
| 45 | |
| 46 | /** |
| 47 | * |
| 48 | * @author Adrian Cole |
| 49 | */ |
| 50 | @Singleton |
| 51 | public class RimuHostingHardwareSupplier implements Supplier<Set<? extends Hardware>> { |
| 52 | |
| 53 | @Resource |
| 54 | @Named(ComputeServiceConstants.COMPUTE_LOGGER) |
| 55 | protected Logger logger = Logger.NULL; |
| 56 | |
| 57 | private final RimuHostingClient sync; |
| 58 | private final Supplier<Set<? extends Location>> locations; |
| 59 | |
| 60 | @Inject |
| 61 | RimuHostingHardwareSupplier(RimuHostingClient sync, @Memoized Supplier<Set<? extends Location>> locations) { |
| 62 | this.sync = sync; |
| 63 | this.locations = locations; |
| 64 | } |
| 65 | |
| 66 | @Override |
| 67 | public Set<? extends Hardware> get() { |
| 68 | final Set<Hardware> sizes = Sets.newHashSet(); |
| 69 | logger.debug(">> providing sizes"); |
| 70 | for (final PricingPlan from : sync.getPricingPlanList()) { |
| 71 | try { |
| 72 | |
| 73 | final Location location = Iterables.find(locations.get(), new Predicate<Location>() { |
| 74 | |
| 75 | @Override |
| 76 | public boolean apply(Location input) { |
| 77 | return input.getId().equals(from.getDataCenter().getId()); |
| 78 | } |
| 79 | |
| 80 | }); |
| 81 | sizes.add(new HardwareBuilder().ids(from.getId()).location(location).processors( |
| 82 | ImmutableList.of(new Processor(1, 1.0))).ram(from.getRam()).volumes( |
| 83 | ImmutableList.<Volume> of(new VolumeImpl((float) from.getDiskSize(), true, true))).build()); |
| 84 | } catch (NullPointerException e) { |
| 85 | logger.warn("datacenter not present in " + from.getId()); |
| 86 | } |
| 87 | } |
| 88 | logger.debug("<< sizes(%d)", sizes.size()); |
| 89 | return sizes; |
| 90 | } |
| 91 | } |