| 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.byon.internal; |
| 20 | |
| 21 | import static com.google.common.base.Preconditions.checkNotNull; |
| 22 | |
| 23 | import java.util.Map; |
| 24 | import java.util.Set; |
| 25 | |
| 26 | import javax.inject.Inject; |
| 27 | import javax.inject.Singleton; |
| 28 | |
| 29 | import org.jclouds.byon.Node; |
| 30 | import org.jclouds.byon.functions.NodeToNodeMetadata; |
| 31 | import org.jclouds.compute.JCloudsNativeComputeServiceAdapter; |
| 32 | import org.jclouds.compute.domain.Hardware; |
| 33 | import org.jclouds.compute.domain.Image; |
| 34 | import org.jclouds.compute.domain.NodeMetadata; |
| 35 | import org.jclouds.compute.domain.Template; |
| 36 | import org.jclouds.domain.Credentials; |
| 37 | import org.jclouds.domain.Location; |
| 38 | import org.jclouds.domain.LocationBuilder; |
| 39 | import org.jclouds.domain.LocationScope; |
| 40 | import org.jclouds.location.suppliers.JustProvider; |
| 41 | |
| 42 | import com.google.common.base.Function; |
| 43 | import com.google.common.base.Predicates; |
| 44 | import com.google.common.base.Supplier; |
| 45 | import com.google.common.cache.Cache; |
| 46 | import com.google.common.collect.ImmutableSet; |
| 47 | import com.google.common.collect.ImmutableSet.Builder; |
| 48 | import com.google.common.collect.Iterables; |
| 49 | import com.google.common.util.concurrent.UncheckedExecutionException; |
| 50 | |
| 51 | /** |
| 52 | * |
| 53 | * @author Adrian Cole |
| 54 | */ |
| 55 | @Singleton |
| 56 | public class BYONComputeServiceAdapter implements JCloudsNativeComputeServiceAdapter { |
| 57 | private final Supplier<Cache<String, Node>> nodes; |
| 58 | private final NodeToNodeMetadata converter; |
| 59 | private final JustProvider locationSupplier; |
| 60 | |
| 61 | @Inject |
| 62 | public BYONComputeServiceAdapter(Supplier<Cache<String, Node>> nodes, NodeToNodeMetadata converter, |
| 63 | JustProvider locationSupplier) { |
| 64 | this.nodes = checkNotNull(nodes, "nodes"); |
| 65 | this.converter = checkNotNull(converter, "converter"); |
| 66 | this.locationSupplier = checkNotNull(locationSupplier, "locationSupplier"); |
| 67 | } |
| 68 | |
| 69 | @Override |
| 70 | public NodeMetadata createNodeWithGroupEncodedIntoNameThenStoreCredentials(String tag, String name, |
| 71 | Template template, Map<String, Credentials> credentialStore) { |
| 72 | throw new UnsupportedOperationException(); |
| 73 | } |
| 74 | |
| 75 | @Override |
| 76 | public Iterable<Hardware> listHardwareProfiles() { |
| 77 | return ImmutableSet.<Hardware> of(); |
| 78 | } |
| 79 | |
| 80 | @Override |
| 81 | public Iterable<Image> listImages() { |
| 82 | return ImmutableSet.<Image> of(); |
| 83 | } |
| 84 | |
| 85 | @Override |
| 86 | public Iterable<NodeMetadata> listNodes() { |
| 87 | return Iterables.transform(nodes.get().asMap().values(), converter); |
| 88 | } |
| 89 | |
| 90 | @Override |
| 91 | public Iterable<Location> listLocations() { |
| 92 | Builder<Location> locations = ImmutableSet.builder(); |
| 93 | Location provider = Iterables.getOnlyElement(locationSupplier.get()); |
| 94 | Set<String> zones = ImmutableSet.copyOf(Iterables.filter(Iterables.transform(nodes.get().asMap().values(), |
| 95 | new Function<Node, String>() { |
| 96 | |
| 97 | @Override |
| 98 | public String apply(Node arg0) { |
| 99 | return arg0.getLocationId(); |
| 100 | } |
| 101 | }), Predicates.notNull())); |
| 102 | if (zones.size() == 0) |
| 103 | return locations.add(provider).build(); |
| 104 | else |
| 105 | for (String zone : zones) { |
| 106 | locations.add(new LocationBuilder().scope(LocationScope.ZONE).id(zone).description(zone).parent(provider) |
| 107 | .build()); |
| 108 | } |
| 109 | return locations.build(); |
| 110 | } |
| 111 | |
| 112 | @Override |
| 113 | public NodeMetadata getNode(String id) { |
| 114 | |
| 115 | Node node = null; |
| 116 | try { |
| 117 | node = nodes.get().getUnchecked(id); |
| 118 | } catch (UncheckedExecutionException e) { |
| 119 | |
| 120 | } |
| 121 | return node != null ? converter.apply(node) : null; |
| 122 | } |
| 123 | |
| 124 | @Override |
| 125 | public void destroyNode(final String id) { |
| 126 | throw new UnsupportedOperationException(); |
| 127 | } |
| 128 | |
| 129 | @Override |
| 130 | public void rebootNode(String id) { |
| 131 | throw new UnsupportedOperationException(); |
| 132 | } |
| 133 | |
| 134 | @Override |
| 135 | public void resumeNode(String id) { |
| 136 | throw new UnsupportedOperationException(); |
| 137 | } |
| 138 | |
| 139 | @Override |
| 140 | public void suspendNode(String id) { |
| 141 | throw new UnsupportedOperationException(); |
| 142 | } |
| 143 | } |