google-compute-enginegoogle-cloud-platformgcloud-java

GCE how to add external IP to existing instance at boot


I'm using Gcloud-java to manage some VM instances. The code to create a new instance is clear and is the following:

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");
}

But what should I do if I have en existing instance that I want to start and I want to attach an external IP?

I've tried in this way: first I create a RegionAddressId and get an Address with which I create the 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();

The I get my instance and add the 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();

The result is that my instance is booted with another external IP that I don't know how to obtain. What is the correct procedure? Thanks


Solution

  • I've found by myself the solution.

    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();
    

    I start the instance using the start method of Compute and then (after the operation is completed) I get the instance