| 1 | /** |
| 2 | * |
| 3 | * Copyright (C) 2011 Cloud Conscious, LLC. <info@cloudconscious.com> |
| 4 | * |
| 5 | * ==================================================================== |
| 6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | * you may not use this file except in compliance with the License. |
| 8 | * 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, software |
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | * See the License for the specific language governing permissions and |
| 16 | * limitations under the License. |
| 17 | * ==================================================================== |
| 18 | */ |
| 19 | package org.jclouds.cloudservers.compute.suppliers; |
| 20 | |
| 21 | import static org.jclouds.cloudservers.options.ListOptions.Builder.withDetails; |
| 22 | |
| 23 | import java.util.Set; |
| 24 | |
| 25 | import javax.annotation.Resource; |
| 26 | import javax.inject.Inject; |
| 27 | import javax.inject.Named; |
| 28 | import javax.inject.Singleton; |
| 29 | |
| 30 | import org.jclouds.compute.domain.Hardware; |
| 31 | import org.jclouds.compute.reference.ComputeServiceConstants; |
| 32 | import org.jclouds.logging.Logger; |
| 33 | import org.jclouds.cloudservers.CloudServersClient; |
| 34 | import org.jclouds.cloudservers.domain.Flavor; |
| 35 | |
| 36 | import com.google.common.base.Function; |
| 37 | import com.google.common.base.Supplier; |
| 38 | import com.google.common.collect.Iterables; |
| 39 | import com.google.common.collect.Sets; |
| 40 | |
| 41 | /** |
| 42 | * |
| 43 | * @author Adrian Cole |
| 44 | */ |
| 45 | @Singleton |
| 46 | public class CloudServersHardwareSupplier implements Supplier<Set<? extends Hardware>> { |
| 47 | @Resource |
| 48 | @Named(ComputeServiceConstants.COMPUTE_LOGGER) |
| 49 | protected Logger logger = Logger.NULL; |
| 50 | private final CloudServersClient sync; |
| 51 | private final Function<Flavor, Hardware> flavorToHardware; |
| 52 | |
| 53 | @Inject |
| 54 | CloudServersHardwareSupplier(CloudServersClient sync, Function<Flavor, Hardware> flavorToHardware) { |
| 55 | this.sync = sync; |
| 56 | this.flavorToHardware = flavorToHardware; |
| 57 | } |
| 58 | |
| 59 | @Override |
| 60 | public Set<? extends Hardware> get() { |
| 61 | final Set<Hardware> hardware; |
| 62 | logger.debug(">> providing hardware"); |
| 63 | hardware = Sets.newLinkedHashSet(Iterables.transform(sync.listFlavors(withDetails()), flavorToHardware)); |
| 64 | logger.debug("<< hardware(%d)", hardware.size()); |
| 65 | return hardware; |
| 66 | } |
| 67 | } |