| 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.cloudloadbalancers.loadbalancer.functions; |
| 20 | |
| 21 | import java.util.Map; |
| 22 | |
| 23 | import javax.inject.Inject; |
| 24 | import javax.inject.Singleton; |
| 25 | |
| 26 | import org.jclouds.cloudloadbalancers.domain.LoadBalancer; |
| 27 | import org.jclouds.cloudloadbalancers.domain.VirtualIP; |
| 28 | import org.jclouds.domain.Location; |
| 29 | import org.jclouds.loadbalancer.domain.LoadBalancerMetadata; |
| 30 | import org.jclouds.loadbalancer.domain.LoadBalancerType; |
| 31 | import org.jclouds.loadbalancer.domain.internal.LoadBalancerMetadataImpl; |
| 32 | |
| 33 | import com.google.common.base.Function; |
| 34 | import com.google.common.base.Supplier; |
| 35 | import com.google.common.collect.ImmutableMap; |
| 36 | import com.google.common.collect.Iterables; |
| 37 | |
| 38 | /** |
| 39 | * |
| 40 | * @author Adrian Cole |
| 41 | */ |
| 42 | @Singleton |
| 43 | public class LoadBalancerToLoadBalancerMetadata implements Function<LoadBalancer, LoadBalancerMetadata> { |
| 44 | protected final Supplier<Map<String, ? extends Location>> locationMap; |
| 45 | protected final Supplier<Location> defaultLocationSupplier; |
| 46 | |
| 47 | @Inject |
| 48 | public LoadBalancerToLoadBalancerMetadata(Supplier<Location> defaultLocationSupplier, |
| 49 | Supplier<Map<String, ? extends Location>> locationMap) { |
| 50 | this.locationMap = locationMap; |
| 51 | this.defaultLocationSupplier = defaultLocationSupplier; |
| 52 | } |
| 53 | |
| 54 | @Override |
| 55 | public LoadBalancerMetadata apply(LoadBalancer input) { |
| 56 | |
| 57 | Location location = locationMap.get().get(input.getRegion()); |
| 58 | |
| 59 | String id = input.getRegion() + "/" + input.getId(); |
| 60 | // TODO Builder |
| 61 | return new LoadBalancerMetadataImpl(LoadBalancerType.LB, input.getName(), input.getName(), id, location, null, |
| 62 | ImmutableMap.<String, String> of(), Iterables.transform(input.getVirtualIPs(), |
| 63 | new Function<VirtualIP, String>() { |
| 64 | |
| 65 | @Override |
| 66 | public String apply(VirtualIP arg0) { |
| 67 | return arg0.getAddress(); |
| 68 | } |
| 69 | |
| 70 | })); |
| 71 | } |
| 72 | } |