| 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; |
| 20 | |
| 21 | import java.util.Map; |
| 22 | import java.util.Set; |
| 23 | import java.util.concurrent.ConcurrentMap; |
| 24 | import java.util.concurrent.ExecutorService; |
| 25 | |
| 26 | import javax.inject.Inject; |
| 27 | import javax.inject.Named; |
| 28 | import javax.inject.Provider; |
| 29 | import javax.inject.Singleton; |
| 30 | |
| 31 | import org.jclouds.Constants; |
| 32 | import org.jclouds.collect.Memoized; |
| 33 | import org.jclouds.compute.ComputeServiceContext; |
| 34 | import org.jclouds.compute.callables.RunScriptOnNode; |
| 35 | import org.jclouds.compute.domain.Hardware; |
| 36 | import org.jclouds.compute.domain.Image; |
| 37 | import org.jclouds.compute.domain.NodeMetadata; |
| 38 | import org.jclouds.compute.domain.TemplateBuilder; |
| 39 | import org.jclouds.compute.internal.BaseComputeService; |
| 40 | import org.jclouds.compute.internal.PersistNodeCredentials; |
| 41 | import org.jclouds.compute.options.TemplateOptions; |
| 42 | import org.jclouds.compute.reference.ComputeServiceConstants.Timeouts; |
| 43 | import org.jclouds.compute.strategy.CreateNodesInGroupThenAddToSet; |
| 44 | import org.jclouds.compute.strategy.DestroyNodeStrategy; |
| 45 | import org.jclouds.compute.strategy.GetNodeMetadataStrategy; |
| 46 | import org.jclouds.compute.strategy.InitializeRunScriptOnNodeOrPlaceInBadMap; |
| 47 | import org.jclouds.compute.strategy.ListNodesStrategy; |
| 48 | import org.jclouds.compute.strategy.RebootNodeStrategy; |
| 49 | import org.jclouds.compute.strategy.ResumeNodeStrategy; |
| 50 | import org.jclouds.compute.strategy.SuspendNodeStrategy; |
| 51 | import org.jclouds.domain.Credentials; |
| 52 | import org.jclouds.domain.Location; |
| 53 | import org.jclouds.scriptbuilder.functions.InitAdminAccess; |
| 54 | import org.jclouds.trmk.vcloud_0_8.compute.domain.KeyPairCredentials; |
| 55 | import org.jclouds.trmk.vcloud_0_8.compute.domain.OrgAndName; |
| 56 | import org.jclouds.trmk.vcloud_0_8.compute.functions.NodeMetadataToOrgAndName; |
| 57 | import org.jclouds.trmk.vcloud_0_8.compute.options.TerremarkVCloudTemplateOptions; |
| 58 | import org.jclouds.trmk.vcloud_0_8.compute.strategy.CleanupOrphanKeys; |
| 59 | |
| 60 | import com.google.common.base.Predicate; |
| 61 | import com.google.common.base.Supplier; |
| 62 | |
| 63 | /** |
| 64 | * @author Adrian Cole |
| 65 | */ |
| 66 | @Singleton |
| 67 | public class TerremarkVCloudComputeService extends BaseComputeService { |
| 68 | private final CleanupOrphanKeys cleanupOrphanKeys; |
| 69 | |
| 70 | @Inject |
| 71 | protected TerremarkVCloudComputeService(ComputeServiceContext context, Map<String, Credentials> credentialStore, |
| 72 | @Memoized Supplier<Set<? extends Image>> images, @Memoized Supplier<Set<? extends Hardware>> sizes, |
| 73 | @Memoized Supplier<Set<? extends Location>> locations, ListNodesStrategy listNodesStrategy, |
| 74 | GetNodeMetadataStrategy getNodeMetadataStrategy, CreateNodesInGroupThenAddToSet runNodesAndAddToSetStrategy, |
| 75 | RebootNodeStrategy rebootNodeStrategy, DestroyNodeStrategy destroyNodeStrategy, |
| 76 | ResumeNodeStrategy resumeNodeStrategy, SuspendNodeStrategy suspendNodeStrategy, |
| 77 | Provider<TemplateBuilder> templateBuilderProvider, Provider<TemplateOptions> templateOptionsProvider, |
| 78 | @Named("NODE_RUNNING") Predicate<NodeMetadata> nodeRunning, |
| 79 | @Named("NODE_TERMINATED") Predicate<NodeMetadata> nodeTerminated, |
| 80 | @Named("NODE_SUSPENDED") Predicate<NodeMetadata> nodeSuspended, |
| 81 | InitializeRunScriptOnNodeOrPlaceInBadMap.Factory initScriptRunnerFactory, |
| 82 | RunScriptOnNode.Factory runScriptOnNodeFactory, InitAdminAccess initAdminAccess, |
| 83 | PersistNodeCredentials persistNodeCredentials, Timeouts timeouts, |
| 84 | @Named(Constants.PROPERTY_USER_THREADS) ExecutorService executor, CleanupOrphanKeys cleanupOrphanKeys, |
| 85 | ConcurrentMap<OrgAndName, KeyPairCredentials> credentialsMap, NodeMetadataToOrgAndName nodeToOrgAndName) { |
| 86 | super(context, credentialStore, images, sizes, locations, listNodesStrategy, getNodeMetadataStrategy, |
| 87 | runNodesAndAddToSetStrategy, rebootNodeStrategy, destroyNodeStrategy, resumeNodeStrategy, |
| 88 | suspendNodeStrategy, templateBuilderProvider, templateOptionsProvider, nodeRunning, nodeTerminated, |
| 89 | nodeSuspended, initScriptRunnerFactory, initAdminAccess, runScriptOnNodeFactory, persistNodeCredentials, |
| 90 | timeouts, executor); |
| 91 | this.cleanupOrphanKeys = cleanupOrphanKeys; |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * like {@link BaseComputeService#destroyNodesMatching} except that this will |
| 96 | * clean implicit keypairs. |
| 97 | */ |
| 98 | @Override |
| 99 | public Set<? extends NodeMetadata> destroyNodesMatching(Predicate<NodeMetadata> filter) { |
| 100 | Set<? extends NodeMetadata> deadOnes = super.destroyNodesMatching(filter); |
| 101 | cleanupOrphanKeys.execute(deadOnes); |
| 102 | return deadOnes; |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * returns template options, except of type |
| 107 | * {@link TerremarkVCloudTemplateOptions}. |
| 108 | */ |
| 109 | @Override |
| 110 | public TerremarkVCloudTemplateOptions templateOptions() { |
| 111 | return TerremarkVCloudTemplateOptions.class.cast(super.templateOptions()); |
| 112 | } |
| 113 | |
| 114 | } |