在Google计算引擎上启动和停止实例

3
我希望能够在谷歌计算引擎上开始 / 恢复和停止 / 暂停实例,但它会出现“java.lang.UnsupportedOperationException”。是否有其他替代方法可以执行这些操作?请注意保留 HTML 标记。
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";
      String groupName = "newgroup";
      credential = getCredentialFromJsonKeyFile(credential);
      Iterable<Module> modules = ImmutableSet.<Module> of(
              new SshjSshClientModule(),
              new SLF4JLoggingModule(),
              new EnterpriseConfigurationModule());
      ContextBuilder builder = ContextBuilder.newBuilder(provider)
              .credentials(identity, credential)
              .modules(modules);
      ComputeService compute=builder.buildView(ComputeServiceContext.class).getComputeService();

      compute.suspendNode("Instance id");
      //compute.suspendNodesMatching(Predicates.<NodeMetadata> and(inGroup(groupName)));
      System.out.println("suspended");
      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;
      }
   }
}

输出:

暂停节点(节点ID)

异常线程“main”java.lang.UnsupportedOperationException:GCE不支持暂停

在org.jclouds.googlecomputeengine.compute.GoogleComputeEngineServiceAdapter.suspendNode(GoogleComputeEngineServiceAdapter.java: 251)

在org.jclouds.compute.strategy.impl.AdaptingComputeServiceStrategies.suspendNode(AdaptingComputeServiceStrategies.java: 171)

在org.jclouds.compute.internal.BaseComputeService.suspendNode(BaseComputeService.java: 503)

在org.jclouds.examples.compute.basics.Example.main(Example.java: 79)


我的回答对你的问题有效吗? - José Ricardo Pla
2个回答

1

在便携式jclouds ComputeService中不直接支持此功能,但可以从ComputeServiceContext获取GoogleComputeEngineApi和InstanceApi,并在其中使用start / stop方法。

顺带一提,当前正在进行一项补丁工作,以添加对ComputeService中启动/停止操作的支持:https://github.com/jclouds/jclouds-labs-google/pull/141


1
您可以通过 API 停止一个实例。
POST https://www.googleapis.com/compute/v1/projects/<project>/zones/<zone>/instances/<instance>/stop

何处:

  • project 在 URL 中是您的项目 ID。
  • URL 中的 zone 是请求的区域名称。
  • URL 中的 instance 是要停止的实例的名称。

这里是文档


有没有使用jclouds或google api java客户端的方法来实现它? - Devansh Agarwal
如果您访问https://developers.google.com/api-client-library/java/apis/compute/v1,您将获得适用于Java的Compute Engine API客户端库,您可以浏览“JavaDoc参考”。您应该会看到“setStatus”作为一个选项。 - Boyan
这是一种替代的工作方式。 - José Ricardo Pla

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接