Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Licensed to jclouds, Inc. (jclouds) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. jclouds licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
 
 package org.jclouds.cloudstack.compute;
 import static com.google.common.base.Preconditions.checkNotNull;
 import static org.jclouds.compute.config.ComputeServiceProperties.TIMEOUT_NODE_RUNNING;
 import static org.jclouds.compute.config.ComputeServiceProperties.TIMEOUT_NODE_SUSPENDED;
 import static org.jclouds.compute.config.ComputeServiceProperties.TIMEOUT_NODE_TERMINATED;
 import static org.jclouds.cloudstack.predicates.SshKeyPairPredicates.nameMatches;
 import static org.jclouds.cloudstack.predicates.ZonePredicates.supportsSecurityGroups;
 
 import java.util.Map;
 import java.util.Set;
 
 
 

Author(s):
Adrian Cole
 
 public class CloudStackComputeService extends BaseComputeService {
    protected final CloudStackApi client;
    protected final LoadingCache<StringSshKeyPairkeyPairCache;
    protected final Function<Set<? extends NodeMetadata>, Multimap<StringString>> orphanedGroupsByZoneId;
    protected final Supplier<LoadingCache<StringZone>> zoneIdToZone;
 
    @Inject
    protected CloudStackComputeService(ComputeServiceContext contextMap<StringCredentialscredentialStore,
             @Memoized Supplier<Set<? extends Image>> images, @Memoized Supplier<Set<? extends Hardware>> sizes,
             @Memoized Supplier<Set<? extends Location>> locationsListNodesStrategy listNodesStrategy,
             GetImageStrategy getImageStrategyGetNodeMetadataStrategy getNodeMetadataStrategy,
             CreateNodesInGroupThenAddToSet runNodesAndAddToSetStrategyRebootNodeStrategy rebootNodeStrategy,
             DestroyNodeStrategy destroyNodeStrategyResumeNodeStrategy startNodeStrategy,
            SuspendNodeStrategy stopNodeStrategyProvider<TemplateBuildertemplateBuilderProvider,
            @Named("DEFAULT"Provider<TemplateOptionstemplateOptionsProvider,
            @Named(Predicate<AtomicReference<NodeMetadata>> nodeRunning,
            @Named(Predicate<AtomicReference<NodeMetadata>> nodeTerminated,
            @Named(Predicate<AtomicReference<NodeMetadata>> nodeSuspended,
            InitializeRunScriptOnNodeOrPlaceInBadMap.Factory initScriptRunnerFactory,
            RunScriptOnNode.Factory runScriptOnNodeFactoryInitAdminAccess initAdminAccess,
            PersistNodeCredentials persistNodeCredentialsTimeouts timeouts,
            @Named(.ListeningExecutorService userExecutorCloudStackApi client,
            LoadingCache<ZoneAndNameSecurityGroupsecurityGroupMap,
            LoadingCache<StringSshKeyPairkeyPairCache,
            Function<Set<? extends NodeMetadata>, Multimap<StringString>> orphanedGroupsByZoneId,
            GroupNamingConvention.Factory namingConvention,
            Supplier<LoadingCache<StringZone>> zoneIdToZone,
            Optional<ImageExtensionimageExtension,
            Optional<SecurityGroupExtensionsecurityGroupExtension) {
      super(contextcredentialStoreimagessizeslocationslistNodesStrategygetImageStrategy,
               getNodeMetadataStrategyrunNodesAndAddToSetStrategyrebootNodeStrategydestroyNodeStrategy,
               startNodeStrategystopNodeStrategytemplateBuilderProvidertemplateOptionsProvidernodeRunning,
               nodeTerminatednodeSuspendedinitScriptRunnerFactoryinitAdminAccessrunScriptOnNodeFactory,
               persistNodeCredentialstimeoutsuserExecutorimageExtensionsecurityGroupExtension);
      this. = checkNotNull(zoneIdToZone"zoneIdToZone");
      this. = checkNotNull(client"client");
      this. = checkNotNull(securityGroupMap"securityGroupMap");
      this. = checkNotNull(keyPairCache"keyPairCache");
      this. = checkNotNull(orphanedGroupsByZoneId"orphanedGroupsByZoneId");
      this. = checkNotNull(namingConvention"namingConvention");
   }
   protected void cleanUpIncidentalResourcesOfDeadNodes(Set<? extends NodeMetadatadeadNodes) {
      Multimap<StringStringzoneToZoneAndGroupNames = .apply(deadNodes);
      for (String zoneId : zoneToZoneAndGroupNames.keySet()) {
         cleanOrphanedGroupsInZone(ImmutableSet.copyOf(zoneToZoneAndGroupNames.get(zoneId)), zoneId);
      }
   }
   protected void cleanOrphanedGroupsInZone(Set<StringgroupsString zoneId) {
      cleanupOrphanedSecurityGroupsInZone(groupszoneId);
      cleanupOrphanedKeyPairsInZone(groupszoneId);
   }
   private void cleanupOrphanedSecurityGroupsInZone(Set<StringgroupsString zoneId) {
      Zone zone = .get().getUnchecked(zoneId);
      if (supportsSecurityGroups().apply(zone)) {
         for (String group : groups) {
            for (SecurityGroup securityGroup : Iterables.filter(.getSecurityGroupApi().listSecurityGroups(),
                     SecurityGroupPredicates.nameMatches(.create().containsGroup(group)))) {
               ZoneAndName zoneAndName = ZoneAndName.fromZoneAndName(zoneIdsecurityGroup.getName());
               .debug(">> deleting securityGroup(%s)"zoneAndName);
               .getSecurityGroupApi().deleteSecurityGroup(securityGroup.getId());
               // TODO: test this clear happens
               .invalidate(zoneAndName);
               .debug("<< deleted securityGroup(%s)"zoneAndName);
            }
         }
      }
   }
   private void cleanupOrphanedKeyPairsInZone(Set<StringgroupsString zoneId) {
      for (String group : groups) {
         for (SshKeyPair pair : Iterables.filter(.getSSHKeyPairApi().listSSHKeyPairs(),
                                                 nameMatches(.create().containsGroup(group)))) {
            .debug(">> deleting keypair(%s)"pair.getName());
            .getSSHKeyPairApi().deleteSSHKeyPair(pair.getName());
            // TODO: test this clear happens
            .invalidate(pair.getName());
            .debug("<< deleted keypair(%s)"pair.getName());
         }
      }
   }

   
      return CloudStackTemplateOptions.class.cast(super.templateOptions());
   }
   
   
New to GrepCode? Check out our FAQ X