1

私はGcloud-javaを使っていくつかのVMインスタンスを管理しています。新しいインスタンスを作成するためのコードが明確であり、以下の通りです:GCEブート時に既存のインスタンスに外部IPを追加する方法

Address externalIp = compute.getAddress(addressId); 

InstanceId instanceId = InstanceId.of("us-central1-a", "test-instance"); 

NetworkId networkId = NetworkId.of("default"); 

PersistentDiskConfiguration attachConfiguration = 
     PersistentDiskConfiguration.builder(diskId).boot(true).build(); 

AttachedDisk attachedDisk = AttachedDisk.of("dev0", attachConfiguration); 

NetworkInterface networkInterface = NetworkInterface.builder(networkId) 
    .accessConfigurations(AccessConfig.of(externalIp.address())) 
    .build(); 

MachineTypeId machineTypeId = MachineTypeId.of("us-central1-a", "n1-standard-1"); 

InstanceInfo instance = 
    InstanceInfo.of(instanceId, machineTypeId, attachedDisk, networkInterface); 

Operation operation = compute.create(instance); 
// Wait for operation to complete 
operation = operation.waitFor(); 

if (operation.errors() == null) { 
    System.out.println("Instance " + instanceId + " was successfully created"); 
} else { 
    // inspect operation.errors() 
    throw new RuntimeException("Instance creation failed"); 
} 

しかし、私は、私が開始するインスタンスを既存備わっていて、私は外部IPを添付したい場合、私は何をすべき?

私はこの方法で試しました。まず、RegionAddressIdを作成し、networkInterfaceを作成するためのアドレスを取得します。

RegionAddressId addressId = RegionAddressId.of("europe-west1", "test-address"); 
    Operation operationAdd = compute.create(AddressInfo.of(addressId)); 
    operationAdd = operationAdd.waitFor(); 

Address externalIp = compute.getAddress(addressId); 
NetworkId networkId = NetworkId.of("default"); 
NetworkInterface networkInterface = NetworkInterface.builder(networkId) 
      .accessConfigurations(NetworkInterface.AccessConfig.of(externalIp.address())) 
      .build(); 

ザ・私は私のインスタンスを取得し、結果は私のインスタンスは、私が取得する方法がわからない、別の外部IPを使用して起動されていることであるAccessConfigの

InstanceId instanceId = InstanceId.of("my-server", "europe-west1-b","my-instance"); 
    Instance instance = compute.getInstance(instanceId); 
    instance.addAccessConfig("default", NetworkInterface.AccessConfig.of(externalIp.address())); 
    Operation operation = instance.start(); 

を追加します。 正しい手順は何ですか? ありがとう

答えて

1

私は自分で解決策を見つけました。

Compute compute = ComputeOptions.defaultInstance().service(); 

InstanceId instanceId = InstanceId.of("my-server", "europe-west1-b","my-instance"); 

Operation operation = compute.start(instanceId); 

Operation completedOperation = operation.waitFor(); 
if (completedOperation == null) { 
    // operation no longer exists 
} else if (completedOperation.errors() != null) { 
    // operation failed, handle error 
} 

Instance instance = compute.getInstance(instanceId); 
String publicIp = 
    instance.networkInterfaces().get(0).accessConfigurations().get(0).natIp(); 

私は私はあなたがあなたの問題を解決してきたと聞いて、インスタンス

+0

グレート取得(操作が完了した後)その後、計算の開始方法を使用してインスタンスを起動して!この質問に「クローズ」と表示されるように[あなた自身の回答を受け入れてください](http://blog.stackoverflow.com/2009/01/accept-your-own-answers/)してください。ありがとう! –

関連する問題