Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Licensed to the Apache Software Foundation (ASF) under one or more
   * contributor license agreements.  See the NOTICE file distributed with
   * this work for additional information regarding copyright ownership.
   * The ASF 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.extensions;
 
 import static com.google.common.base.Preconditions.checkNotNull;
 import static com.google.common.base.Preconditions.checkState;
 import static com.google.common.collect.Iterables.find;
 import static org.jclouds.compute.config.ComputeServiceProperties.TIMEOUT_IMAGE_AVAILABLE;
 import static org.jclouds.location.predicates.LocationPredicates.idEquals;
 
 import java.util.Set;
 
 
 
CloudStack implementation of org.jclouds.compute.extensions.ImageExtension

Author(s):
Andrew Bayer
 
 public class CloudStackImageExtension implements ImageExtension {
 
    @Resource
    protected Logger logger = .;
 
    private final CloudStackApi client;
    private final ListeningExecutorService userExecutor;
    private final Supplier<Set<? extends Location>> locations;
    private final Predicate<StringjobComplete;
 
    @Inject
    public CloudStackImageExtension(CloudStackApi client,
                                    @Named(.ListeningExecutorService userExecutor,
                                    @Memoized Supplier<Set<? extends Location>> locations,
                                    @Named(Predicate<AtomicReference<Image>> imageAvailablePredicate,
                                    BlockUntilJobCompletesAndReturnResult blockUntilJobCompletesAndReturnResult,
                                    Predicate<StringjobComplete) {
       this. = checkNotNull(client"client");
       this. = checkNotNull(userExecutor"userExecutor");
       this. = checkNotNull(locations"locations");
       this. = checkNotNull(imageAvailablePredicate"imageAvailablePredicate");
       this. = checkNotNull(blockUntilJobCompletesAndReturnResult,
                                                                 "blockUntilJobCompletesAndReturnResult");
       this. = checkNotNull(jobComplete"jobComplete");
    }
      if (vm == null)
         throw new NoSuchElementException("Cannot find vm with id: " + id);
      return template;
   }
   public ListenableFuture<ImagecreateImage(ImageTemplate template) {
      checkState(template instanceof CloneImageTemplate,
               " cloudstack only currently supports creating images through cloning.");
      CloneImageTemplate cloneTemplate = (CloneImageTemplatetemplate;
      .apply(stopJob);
      Set<Volumevolumes = .getVolumeApi().listVolumes(ListVolumesOptions.Builder.virtualMachineId(vm.getId()));
      Volume volume = Iterables.getOnlyElement(volumes);
      
      CreateTemplateOptions options = CreateTemplateOptions.Builder.volumeId(volume.getId());
      AsyncCreateResponse templateJob = .getTemplateApi().createTemplate(TemplateMetadata.builder()
                                                                                  .name(cloneTemplate.getName())
                                                                                  .osTypeId(vm.getGuestOSId())
                                                                                  .displayText(cloneTemplate.getName())
                                                                                  .build(), options);
      Template newTemplate = .<Template>apply(templateJob);
      .info(">> Registered new template %s, waiting for it to become available."newTemplate.getId());
      final AtomicReference<Imageimage = Atomics.newReference(new ImageBuilder()
                                                                .location(find(.get(), idEquals(vm.getZoneId())))
                                                                .id(newTemplate.getId())
                                                                .providerId(newTemplate.getId())
                                                                .description(cloneTemplate.getName())
                                                                .operatingSystem(OperatingSystem.builder().description(cloneTemplate.getName()).build())
                                                                .status(..).build());
      return .submit(new Callable<Image>() {
         @Override
         public Image call() throws Exception {
            if (.apply(image))
               return image.get();
            // TODO: get rid of the expectation that the image will be available, as it is very brittle
            throw new UncheckedTimeoutException("Image was not created within the time limit: " + image.get());
         }
      });
   }
   public boolean deleteImage(String id) {
      try {
         AsyncCreateResponse deleteJob = .getTemplateApi().deleteTemplate(id);
         .apply(deleteJob.getJobId());
         return true;
      } catch (Exception e) {
         return false;
      }
   }
New to GrepCode? Check out our FAQ X