| 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.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.collect.ImmutableSet; |
| 46 | import com.google.common.collect.Iterables; |
| 47 | import com.google.common.collect.ImmutableSet.Builder; |
| 48 | |
| 49 | /** |
| 50 | * |
| 51 | * @author Adrian Cole |
| 52 | */ |
| 53 | @Singleton |
| 54 | public class BYONComputeServiceAdapter implements JCloudsNativeComputeServiceAdapter { |
| 55 | private final Supplier<Map<String, Node>> nodes; |
| 56 | private final NodeToNodeMetadata converter; |
| 57 | private final JustProvider locationSupplier; |
| 58 | |
| 59 | @Inject |
| 60 | public BYONComputeServiceAdapter(Supplier<Map<String, Node>> nodes, NodeToNodeMetadata converter, |
| 61 | JustProvider locationSupplier) { |
| 62 | this.nodes = checkNotNull(nodes, "nodes"); |
| 63 | this.converter = checkNotNull(converter, "converter"); |
| 64 | this.locationSupplier = checkNotNull(locationSupplier, "locationSupplier"); |
| 65 | } |
| 66 | |
| 67 | @Override |
| 68 | public NodeMetadata createNodeWithGroupEncodedIntoNameThenStoreCredentials(String tag, String name, |
| 69 | Template template, Map<String, Credentials> credentialStore) { |
| 70 | throw new UnsupportedOperationException(); |
| 71 | } |
| 72 | |
| 73 | @Override |
| 74 | public Iterable<Hardware> listHardwareProfiles() { |
| 75 | return ImmutableSet.<Hardware> of(); |
| 76 | } |
| 77 | |
| 78 | @Override |
| 79 | public Iterable<Image> listImages() { |
| 80 | return ImmutableSet.<Image> of(); |
| 81 | } |
| 82 | |
| 83 | @Override |
| 84 | public Iterable<NodeMetadata> listNodes() { |
| 85 | return Iterables.transform(nodes.get().values(), converter); |
| 86 | } |
| 87 | |
| 88 | @Override |
| 89 | public Iterable<Location> listLocations() { |
| 90 | Builder<Location> locations = ImmutableSet.builder(); |
| 91 | Location provider = Iterables.getOnlyElement(locationSupplier.get()); |
| 92 | Set<String> zones = ImmutableSet.copyOf(Iterables.filter(Iterables.transform(nodes.get().values(), |
| 93 | new Function<Node, String>() { |
| 94 | |
| 95 | @Override |
| 96 | public String apply(Node arg0) { |
| 97 | return arg0.getLocationId(); |
| 98 | } |
| 99 | }), Predicates.notNull())); |
| 100 | if (zones.size() == 0) |
| 101 | return locations.add(provider).build(); |
| 102 | else |
| 103 | for (String zone : zones) { |
| 104 | locations.add(new LocationBuilder().scope(LocationScope.ZONE).id(zone).description(zone).parent(provider) |
| 105 | .build()); |
| 106 | } |
| 107 | return locations.build(); |
| 108 | } |
| 109 | |
| 110 | @Override |
| 111 | public NodeMetadata getNode(String id) { |
| 112 | Node node = nodes.get().get(id); |
| 113 | return node != null ? converter.apply(node) : null; |
| 114 | } |
| 115 | |
| 116 | @Override |
| 117 | public void destroyNode(final String id) { |
| 118 | throw new UnsupportedOperationException(); |
| 119 | } |
| 120 | |
| 121 | @Override |
| 122 | public void rebootNode(String id) { |
| 123 | throw new UnsupportedOperationException(); |
| 124 | } |
| 125 | |
| 126 | @Override |
| 127 | public void resumeNode(String id) { |
| 128 | throw new UnsupportedOperationException(); |
| 129 | } |
| 130 | |
| 131 | @Override |
| 132 | public void suspendNode(String id) { |
| 133 | throw new UnsupportedOperationException(); |
| 134 | } |
| 135 | } |