使用jcloud api

时间:2015-05-20 07:27:02

标签: google-cloud-platform jclouds

我想使用Jcloud api在Google云端的项目中添加和列出所有实例/ VM。在这段代码中,我假设一个节点是一个实例。 我已根据需要设置了所有变量,并从json文件中提取了私钥。上下文构建成功进行。
images = compute.listImages()=>列出谷歌提供的所有图像。
nodes = compute.listNodes()=>应该列出节点,但是给出空指针异常。

输出=>

  

没有图像246

     
    

线程“main”中的异常java.lang.NullPointerException:group         在com.google.common.base.Preconditions.checkNotNull(Preconditions.java:229)         at org.jclouds.compute.internal.FormatSharedNamesAndAppendUniqueStringToThoseWhichRepeat.checkGroup(FormatSharedNamesAndAppendUniqueStringToThoseWhichRepeat.java:124)         at org.jclouds.compute.internal.FormatSharedNamesAndAppendUniqueStringToThoseWhichRepeat.sharedNameForGroup(FormatSharedNamesAndAppendUniqueStringToThoseWhichRepeat.java:120)         在org.jclouds.googlecomputeengine.compute.functions.FirewallTagNamingConvention $ Factory.get(FirewallTagNamingConvention.java:39)         at org.jclouds.googlecomputeengine.compute.functions.InstanceToNodeMetadata.apply(InstanceToNodeMetadata.java:68)         at org.jclouds.googlecomputeengine.compute.functions.InstanceToNodeMetadata.apply(InstanceToNodeMetadata.java:43)         在com.google.common.base.Functions $ FunctionComposition.apply(Functions.java:211)         在com.google.common.collect.Iterators $ 8.transform(Iterators.java:794)         在com.google.common.collect.TransformedIterator.next(TransformedIterator.java:48)         在com.google.common.collect.Iterators $ 7.computeNext(Iterators.java:646)         在com.google.common.collect.AbstractIterator.tryToComputeNext(AbstractIterator.java:143)         在com.google.common.collect.AbstractIterator.hasNext(AbstractIterator.java:138)         在com.google.common.collect.Iterators.addAll(Iterators.java:356)         在com.google.common.collect.Iterables.addAll(Iterables.java:350)         在com.google.common.collect.Sets.newLinkedHashSet(Sets.java:328)         at org.jclouds.compute.internal.BaseComputeService.listNodes(BaseComputeService.java:335)         在org.jclouds.examples.compute.basics.Example.main(Example.java:54)

  
    package org.jclouds.examples.compute.basics;
    import static com.google.common.base.Charsets.UTF_8;
    import static org.jclouds.compute.config.ComputeServiceProperties.TIMEOUT_SCRIPT_COMPLETE;

    import java.io.File;
    import java.io.IOException;
    import java.util.Properties;
    import java.util.Set;
    import java.util.concurrent.TimeUnit;

    import org.jclouds.ContextBuilder;
    import org.jclouds.compute.ComputeService;
    import org.jclouds.compute.ComputeServiceContext;
    import org.jclouds.compute.domain.ComputeMetadata;
    import org.jclouds.compute.domain.Image;
    import org.jclouds.domain.Credentials;
    import org.jclouds.enterprise.config.EnterpriseConfigurationModule;
    import org.jclouds.googlecloud.GoogleCredentialsFromJson;
    import org.jclouds.logging.slf4j.config.SLF4JLoggingModule;
    import org.jclouds.sshj.config.SshjSshClientModule;

    import com.google.common.base.Supplier;
    import com.google.common.collect.ImmutableSet;
    import com.google.common.io.Files;
    import com.google.inject.Module;

    public class Example {

     public static void main(String[] args) 
     {
      String provider = "google-compute-engine";
      String identity = "***@developer.gserviceaccount.com";
      String credential = "path to private key file ";
      credential = getCredentialFromJsonKeyFile(credential);

      Properties properties = new Properties();
      long scriptTimeout = TimeUnit.MILLISECONDS.convert(20, TimeUnit.MINUTES);
      properties.setProperty(TIMEOUT_SCRIPT_COMPLETE, scriptTimeout + "");
      Iterable<Module> modules = ImmutableSet.<Module> of(
            new SshjSshClientModule(),new SLF4JLoggingModule(),
            new EnterpriseConfigurationModule());

      ContextBuilder builder = ContextBuilder.newBuilder(provider)
                                             .credentials(identity, credential)
                                             .modules(modules)
                                             .overrides(properties);
      ComputeService compute=builder.buildView(ComputeServiceContext.class).getComputeService();

      Set<? extends Image> images = compute.listImages();
      System.out.printf(">> No of images %d%n", images.size());

      Set<? extends ComputeMetadata> nodes = compute.listNodes();
      System.out.printf(">> No of nodes/instances %d%n", nodes.size());

      compute.getContext().close();     
    }

    private static String getCredentialFromJsonKeyFile(String filename) {
      try {
         String fileContents = Files.toString(new File(filename), UTF_8);
         Supplier<Credentials> credentialSupplier = new GoogleCredentialsFromJson(fileContents);
         String credential = credentialSupplier.get().credential;
         return credential;
      } catch (IOException e) {
         System.err.println("Exception reading private key from '%s': " + filename);
         e.printStackTrace();
         System.exit(1);
         return null;
      }
    }
    }

0 个答案:

没有答案