| 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.functions; |
| 20 | |
| 21 | import java.util.Map; |
| 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.compute.domain.NodeMetadata; |
| 29 | import org.jclouds.compute.reference.ComputeServiceConstants; |
| 30 | import org.jclouds.logging.Logger; |
| 31 | import org.jclouds.trmk.vcloud_0_8.TerremarkVCloudClient; |
| 32 | import org.jclouds.trmk.vcloud_0_8.compute.domain.OrgAndName; |
| 33 | import org.jclouds.trmk.vcloud_0_8.domain.Org; |
| 34 | import org.jclouds.trmk.vcloud_0_8.endpoints.VDC; |
| 35 | |
| 36 | import com.google.common.base.Function; |
| 37 | import com.google.common.base.Supplier; |
| 38 | |
| 39 | /** |
| 40 | * |
| 41 | * @author Adrian Cole |
| 42 | * |
| 43 | */ |
| 44 | @Singleton |
| 45 | public class NodeMetadataToOrgAndName implements Function<NodeMetadata, OrgAndName> { |
| 46 | |
| 47 | @Resource |
| 48 | @Named(ComputeServiceConstants.COMPUTE_LOGGER) |
| 49 | protected Logger logger = Logger.NULL; |
| 50 | |
| 51 | final Supplier<Map<String, String>> vdcToOrg; |
| 52 | |
| 53 | private final TerremarkVCloudClient client; |
| 54 | |
| 55 | @Inject |
| 56 | NodeMetadataToOrgAndName(TerremarkVCloudClient client, @VDC Supplier<Map<String, String>> vdcToOrg) { |
| 57 | this.vdcToOrg = vdcToOrg; |
| 58 | this.client = client; |
| 59 | } |
| 60 | |
| 61 | @Override |
| 62 | public OrgAndName apply(NodeMetadata from) { |
| 63 | if (from.getGroup() != null) { |
| 64 | Org org = client.findOrgNamed(vdcToOrg.get().get(from.getLocation().getId())); |
| 65 | if (org == null) { |
| 66 | logger.warn("did not find an association for vdc %s in %s", from.getLocation().getId(), vdcToOrg); |
| 67 | } else { |
| 68 | return new OrgAndName(org.getHref(), from.getGroup()); |
| 69 | } |
| 70 | } |
| 71 | return null; |
| 72 | } |
| 73 | } |