| 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.softlayer.compute.functions; |
| 20 | |
| 21 | import static com.google.common.base.Preconditions.checkNotNull; |
| 22 | |
| 23 | import javax.inject.Inject; |
| 24 | |
| 25 | import static com.google.common.base.Strings.nullToEmpty; |
| 26 | import org.jclouds.domain.Location; |
| 27 | import org.jclouds.domain.LocationBuilder; |
| 28 | import org.jclouds.domain.LocationScope; |
| 29 | import org.jclouds.location.suppliers.JustProvider; |
| 30 | import org.jclouds.softlayer.domain.Address; |
| 31 | import org.jclouds.softlayer.domain.Datacenter; |
| 32 | |
| 33 | import com.google.common.base.Function; |
| 34 | import com.google.common.collect.ImmutableMap; |
| 35 | import com.google.common.collect.ImmutableSet; |
| 36 | import com.google.common.collect.Iterables; |
| 37 | |
| 38 | /** |
| 39 | * Converts an Datacenter into a Location. |
| 40 | */ |
| 41 | public class DatacenterToLocation implements Function<Datacenter,Location> { |
| 42 | private final JustProvider provider; |
| 43 | |
| 44 | // allow us to lazy discover the provider of a resource |
| 45 | @Inject |
| 46 | public DatacenterToLocation(JustProvider provider) { |
| 47 | this.provider = checkNotNull(provider, "provider"); |
| 48 | } |
| 49 | |
| 50 | @Override |
| 51 | public Location apply(Datacenter datacenter) { |
| 52 | return new LocationBuilder().scope(LocationScope.ZONE) |
| 53 | .metadata(ImmutableMap.<String, Object>of()) |
| 54 | .description(datacenter.getLongName()) |
| 55 | .id(Long.toString(datacenter.getId())) |
| 56 | .iso3166Codes(createIso3166Codes(datacenter.getLocationAddress())) |
| 57 | .parent(Iterables.getOnlyElement(provider.get())) |
| 58 | .build(); |
| 59 | } |
| 60 | |
| 61 | private Iterable<String> createIso3166Codes(Address address) { |
| 62 | if (address== null) return ImmutableSet.<String> of(); |
| 63 | |
| 64 | final String country = nullToEmpty(address.getCountry()).trim(); |
| 65 | if (country.isEmpty()) return ImmutableSet.<String> of(); |
| 66 | |
| 67 | final String state = nullToEmpty(address.getState()).trim(); |
| 68 | if (state.isEmpty()) return ImmutableSet.<String> of(address.getCountry()); |
| 69 | |
| 70 | return ImmutableSet.<String> of("" + country + "-" + state); |
| 71 | |
| 72 | |
| 73 | } |
| 74 | } |