| 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.trmk.vcloud_0_8.compute.config; |
| 20 | |
| 21 | import static com.google.common.base.Preconditions.checkNotNull; |
| 22 | import static com.google.common.collect.Iterables.find; |
| 23 | |
| 24 | import java.util.Set; |
| 25 | |
| 26 | import javax.inject.Inject; |
| 27 | import javax.inject.Singleton; |
| 28 | |
| 29 | import org.jclouds.collect.Memoized; |
| 30 | import org.jclouds.compute.config.BindComputeSuppliersByClass; |
| 31 | import org.jclouds.compute.domain.Hardware; |
| 32 | import org.jclouds.compute.domain.Image; |
| 33 | import org.jclouds.domain.Location; |
| 34 | import org.jclouds.domain.LocationScope; |
| 35 | import org.jclouds.trmk.vcloud_0_8.compute.suppliers.OrgAndVDCToLocationSupplier; |
| 36 | import org.jclouds.trmk.vcloud_0_8.compute.suppliers.StaticHardwareSupplier; |
| 37 | import org.jclouds.trmk.vcloud_0_8.compute.suppliers.VAppTemplatesInOrgs; |
| 38 | import org.jclouds.trmk.vcloud_0_8.domain.ReferenceType; |
| 39 | import org.jclouds.trmk.vcloud_0_8.endpoints.VDC; |
| 40 | |
| 41 | import com.google.common.base.Predicate; |
| 42 | import com.google.common.base.Supplier; |
| 43 | |
| 44 | /** |
| 45 | * @author Adrian Cole |
| 46 | */ |
| 47 | public class TerremarkBindComputeSuppliersByClass extends BindComputeSuppliersByClass { |
| 48 | @Override |
| 49 | protected Class<? extends Supplier<Set<? extends Hardware>>> defineHardwareSupplier() { |
| 50 | return StaticHardwareSupplier.class; |
| 51 | } |
| 52 | |
| 53 | @Override |
| 54 | protected Class<? extends Supplier<Set<? extends Location>>> defineLocationSupplier() { |
| 55 | return OrgAndVDCToLocationSupplier.class; |
| 56 | } |
| 57 | |
| 58 | @Override |
| 59 | protected Class<? extends Supplier<Location>> defineDefaultLocationSupplier() { |
| 60 | return DefaultVDC.class; |
| 61 | } |
| 62 | |
| 63 | @Singleton |
| 64 | public static class DefaultVDC implements Supplier<Location> { |
| 65 | @Singleton |
| 66 | public static final class IsDefaultVDC implements Predicate<Location> { |
| 67 | private final ReferenceType defaultVDC; |
| 68 | |
| 69 | @Inject |
| 70 | IsDefaultVDC(@VDC ReferenceType defaultVDC) { |
| 71 | this.defaultVDC = checkNotNull(defaultVDC, "defaultVDC"); |
| 72 | } |
| 73 | |
| 74 | @Override |
| 75 | public boolean apply(Location input) { |
| 76 | return input.getScope() == LocationScope.ZONE && input.getId().equals(defaultVDC.getHref().toASCIIString()); |
| 77 | } |
| 78 | |
| 79 | @Override |
| 80 | public String toString() { |
| 81 | return "isDefaultVDC()"; |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | private final Supplier<Set<? extends Location>> locationsSupplier; |
| 86 | private final IsDefaultVDC isDefaultVDC; |
| 87 | |
| 88 | @Inject |
| 89 | DefaultVDC(@Memoized Supplier<Set<? extends Location>> locationsSupplier, IsDefaultVDC isDefaultVDC) { |
| 90 | this.locationsSupplier = checkNotNull(locationsSupplier, "locationsSupplierSupplier"); |
| 91 | this.isDefaultVDC = checkNotNull(isDefaultVDC, "isDefaultVDC"); |
| 92 | } |
| 93 | |
| 94 | @Override |
| 95 | public Location get() { |
| 96 | return find(locationsSupplier.get(), isDefaultVDC); |
| 97 | } |
| 98 | |
| 99 | } |
| 100 | |
| 101 | @Override |
| 102 | protected Class<? extends Supplier<Set<? extends Image>>> defineImageSupplier() { |
| 103 | return VAppTemplatesInOrgs.class; |
| 104 | } |
| 105 | |
| 106 | } |