2015-05-25 82 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

這是做這項工作的另一種方式。 –

相關問題