2015-05-25 62 views
2

我想在Google计算引擎上启动/恢复并停止/挂起实例,但它给出了“java.lang.UnsupportedOperationException”。是否存在任何替代方法 来执行这些操作?在Google计算引擎上启动和停止实例

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)

异常 “主” java.lang.UnsupportedOperationException:挂起不是由GCE

支持

at org.jclouds.googlecomputeengine.compute.GoogleComputeEngineServiceAdapter.suspendNode(GoogleComputeEngineServ iceAdapter.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)

+0

我做你的问题答案的工作? –

回答

1

它不能直接在便携式jclouds ComputeService支持,但是从ComputeServiceContext你可以得到GoogleComputeEngineApi和InstanceApi,并使用开始/停止在那里的方法。

仅供参考,有一个持续的补丁,增加了对支持启动/停止操作的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 

其中:

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

这里的docs

+0

有没有办法使用jclouds或谷歌API的Java客户端? –

+0

如果您访问https://developers.google.com/api-client-library/java/apis/compute/v1,您将获得Java的Compute Engine API客户端库,您可以在其中浏览“JavaDoc参考”。您应该看到“setStatus”作为选项。 – Boyan

+0

这是做这项工作的另一种方式。 –

相关问题