View Javadoc

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.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  }