com.google.cloud.compute.NetworkId

Here are the examples of the java api class com.google.cloud.compute.NetworkId taken from open source projects.

1. CreateInstance#main()

Project: gcloud-java
Source File: CreateInstance.java
View license
public static void main(String... args) throws InterruptedException, TimeoutException {
    Compute compute = ComputeOptions.defaultInstance().service();
    ImageId imageId = ImageId.of("debian-cloud", "debian-8-jessie-v20160329");
    NetworkId networkId = NetworkId.of("default");
    AttachedDisk attachedDisk = AttachedDisk.of(AttachedDisk.CreateDiskConfiguration.of(imageId));
    NetworkInterface networkInterface = NetworkInterface.of(networkId);
    InstanceId instanceId = InstanceId.of("us-central1-a", "instance-name");
    MachineTypeId machineTypeId = MachineTypeId.of("us-central1-a", "n1-standard-1");
    Operation operation = compute.create(InstanceInfo.of(instanceId, machineTypeId, attachedDisk, networkInterface));
    operation = operation.waitFor();
    if (operation.errors() == null) {
        // use instance
        Instance instance = compute.getInstance(instanceId);
    }
}

2. ITComputeTest#testCreateAndGetNetwork()

Project: gcloud-java
Source File: ITComputeTest.java
View license
@Test
public void testCreateAndGetNetwork() throws InterruptedException, TimeoutException {
    String name = BASE_RESOURCE_NAME + "create-and-get-network";
    NetworkId networkId = NetworkId.of(name);
    NetworkInfo networkInfo = NetworkInfo.of(networkId, StandardNetworkConfiguration.of("192.168.0.0/16"));
    Operation operation = compute.create(networkInfo);
    operation.waitFor();
    // test get network with selected fields
    Network network = compute.getNetwork(networkId.network(), Compute.NetworkOption.fields(Compute.NetworkField.CREATION_TIMESTAMP));
    assertEquals(networkId.network(), network.networkId().network());
    assertNull(network.generatedId());
    assertNotNull(network.creationTimestamp());
    assertNull(network.description());
    assertEquals(NetworkConfiguration.Type.STANDARD, network.configuration().type());
    StandardNetworkConfiguration remoteConfiguration = network.configuration();
    assertEquals("192.168.0.0/16", remoteConfiguration.ipRange());
    // test get network
    network = compute.getNetwork(networkId.network());
    assertEquals(networkId.network(), network.networkId().network());
    assertNotNull(network.generatedId());
    assertNotNull(network.creationTimestamp());
    assertEquals(NetworkConfiguration.Type.STANDARD, network.configuration().type());
    remoteConfiguration = network.configuration();
    assertEquals("192.168.0.0/16", remoteConfiguration.ipRange());
    operation = network.delete();
    operation.waitFor();
    assertNull(compute.getNetwork(name));
}

3. ITComputeTest#testListNetworks()

Project: gcloud-java
Source File: ITComputeTest.java
View license
@Test
public void testListNetworks() throws InterruptedException, TimeoutException {
    String name = BASE_RESOURCE_NAME + "list-network";
    NetworkId networkId = NetworkId.of(name);
    NetworkInfo networkInfo = NetworkInfo.of(networkId, StandardNetworkConfiguration.of("192.168.0.0/16"));
    Operation operation = compute.create(networkInfo);
    operation.waitFor();
    // test list
    Compute.NetworkFilter filter = Compute.NetworkFilter.equals(Compute.NetworkField.NAME, name);
    Page<Network> networkPage = compute.listNetworks(Compute.NetworkListOption.filter(filter));
    Iterator<Network> networkIterator = networkPage.iterateAll();
    int count = 0;
    while (networkIterator.hasNext()) {
        Network network = networkIterator.next();
        assertEquals(networkId.network(), network.networkId().network());
        assertNotNull(network.generatedId());
        assertNotNull(network.creationTimestamp());
        assertEquals(NetworkConfiguration.Type.STANDARD, network.configuration().type());
        StandardNetworkConfiguration remoteConfiguration = network.configuration();
        assertEquals("192.168.0.0/16", remoteConfiguration.ipRange());
        count++;
    }
    assertEquals(1, count);
    // test list with selected fields
    count = 0;
    networkPage = compute.listNetworks(Compute.NetworkListOption.filter(filter), Compute.NetworkListOption.fields(Compute.NetworkField.CREATION_TIMESTAMP));
    networkIterator = networkPage.iterateAll();
    while (networkIterator.hasNext()) {
        Network network = networkIterator.next();
        assertEquals(networkId.network(), network.networkId().network());
        assertNull(network.generatedId());
        assertNotNull(network.creationTimestamp());
        assertNull(network.description());
        assertEquals(NetworkConfiguration.Type.STANDARD, network.configuration().type());
        StandardNetworkConfiguration remoteConfiguration = network.configuration();
        assertEquals("192.168.0.0/16", remoteConfiguration.ipRange());
        count++;
    }
    assertEquals(1, count);
    operation = compute.deleteNetwork(networkId);
    operation.waitFor();
    assertNull(compute.getNetwork(name));
}

4. ITComputeTest#testCreateNetworkAndSubnetwork()

Project: gcloud-java
Source File: ITComputeTest.java
View license
@Test
public void testCreateNetworkAndSubnetwork() throws InterruptedException, TimeoutException {
    String networkName = BASE_RESOURCE_NAME + "create-subnetwork-network";
    NetworkId networkId = NetworkId.of(networkName);
    NetworkInfo networkInfo = NetworkInfo.of(networkId, SubnetNetworkConfiguration.of(false));
    Operation operation = compute.create(networkInfo);
    operation.waitFor();
    // test get network
    Network network = compute.getNetwork(networkId.network());
    assertEquals(networkId.network(), network.networkId().network());
    assertNotNull(network.generatedId());
    assertNotNull(network.creationTimestamp());
    assertEquals(NetworkConfiguration.Type.SUBNET, network.configuration().type());
    assertTrue(network.configuration() instanceof SubnetNetworkConfiguration);
    assertFalse(network.<SubnetNetworkConfiguration>configuration().autoCreateSubnetworks());
    String subnetworkName = BASE_RESOURCE_NAME + "create-subnetwork-subnetwork";
    SubnetworkId subnetworkId = SubnetworkId.of(REGION, subnetworkName);
    SubnetworkInfo subnetworkInfo = SubnetworkInfo.of(subnetworkId, networkId, "192.168.0.0/16");
    operation = compute.create(subnetworkInfo);
    operation.waitFor();
    // test get subnetwork with selected fields
    Subnetwork subnetwork = compute.getSubnetwork(subnetworkId, Compute.SubnetworkOption.fields(Compute.SubnetworkField.CREATION_TIMESTAMP));
    assertNull(subnetwork.generatedId());
    assertEquals(subnetworkId.subnetwork(), subnetwork.subnetworkId().subnetwork());
    assertNotNull(subnetwork.creationTimestamp());
    assertNull(subnetwork.description());
    assertNull(subnetwork.gatewayAddress());
    assertNull(subnetwork.network());
    assertNull(subnetwork.ipRange());
    // test get subnetwork
    subnetwork = compute.getSubnetwork(subnetworkId);
    assertNotNull(subnetwork.generatedId());
    assertEquals(subnetworkId.subnetwork(), subnetwork.subnetworkId().subnetwork());
    assertNotNull(subnetwork.creationTimestamp());
    assertNotNull(subnetwork.gatewayAddress());
    assertEquals(networkId.network(), subnetwork.network().network());
    assertEquals("192.168.0.0/16", subnetwork.ipRange());
    // test list subnetworks
    Compute.SubnetworkFilter filter = Compute.SubnetworkFilter.equals(Compute.SubnetworkField.NAME, subnetworkName);
    Page<Subnetwork> subnetworkPage = compute.listSubnetworks(REGION, Compute.SubnetworkListOption.filter(filter));
    Iterator<Subnetwork> subnetworkIterator = subnetworkPage.iterateAll();
    int count = 0;
    while (subnetworkIterator.hasNext()) {
        Subnetwork remoteSubnetwork = subnetworkIterator.next();
        assertNotNull(remoteSubnetwork.generatedId());
        assertEquals(subnetworkId.subnetwork(), remoteSubnetwork.subnetworkId().subnetwork());
        assertNotNull(remoteSubnetwork.creationTimestamp());
        assertNotNull(remoteSubnetwork.gatewayAddress());
        assertEquals(networkId.network(), remoteSubnetwork.network().network());
        assertEquals("192.168.0.0/16", remoteSubnetwork.ipRange());
        count++;
    }
    assertEquals(1, count);
    // test list subnetworks with selected fields
    subnetworkPage = compute.listSubnetworks(REGION, Compute.SubnetworkListOption.filter(filter), Compute.SubnetworkListOption.fields(Compute.SubnetworkField.CREATION_TIMESTAMP));
    subnetworkIterator = subnetworkPage.iterateAll();
    count = 0;
    while (subnetworkIterator.hasNext()) {
        Subnetwork remoteSubnetwork = subnetworkIterator.next();
        assertNull(remoteSubnetwork.generatedId());
        assertEquals(subnetworkId.subnetwork(), remoteSubnetwork.subnetworkId().subnetwork());
        assertNotNull(remoteSubnetwork.creationTimestamp());
        assertNull(remoteSubnetwork.description());
        assertNull(remoteSubnetwork.gatewayAddress());
        assertNull(remoteSubnetwork.network());
        assertNull(remoteSubnetwork.ipRange());
        count++;
    }
    assertEquals(1, count);
    operation = subnetwork.delete();
    operation.waitFor();
    operation = compute.deleteNetwork(networkId);
    operation.waitFor();
    assertNull(compute.getSubnetwork(subnetworkId));
    assertNull(compute.getNetwork(networkName));
}

5. ITComputeTest#testAggregatedListSubnetworks()

Project: gcloud-java
Source File: ITComputeTest.java
View license
@Test
public void testAggregatedListSubnetworks() throws InterruptedException, TimeoutException {
    String networkName = BASE_RESOURCE_NAME + "list-subnetwork-network";
    NetworkId networkId = NetworkId.of(networkName);
    NetworkInfo networkInfo = NetworkInfo.of(networkId, SubnetNetworkConfiguration.of(false));
    Operation operation = compute.create(networkInfo);
    operation.waitFor();
    String prefix = BASE_RESOURCE_NAME + "list-subnetwork";
    String[] regionNames = { "us-central1", "us-east1" };
    String[] subnetworkNames = { prefix + "1", prefix + "2" };
    String[] ipRanges = { "10.128.0.0/20", "10.132.0.0/20" };
    SubnetworkId firstSubnetworkId = SubnetworkId.of(regionNames[0], subnetworkNames[0]);
    SubnetworkId secondSubnetworkId = SubnetworkId.of(regionNames[1], subnetworkNames[1]);
    SubnetworkInfo firstSubnetworkInfo = SubnetworkInfo.of(firstSubnetworkId, networkId, ipRanges[0]);
    SubnetworkInfo secondSubnetworkInfo = SubnetworkInfo.of(secondSubnetworkId, networkId, ipRanges[1]);
    Operation firstOperation = compute.create(firstSubnetworkInfo);
    Operation secondOperation = compute.create(secondSubnetworkInfo);
    firstOperation.waitFor();
    secondOperation.waitFor();
    Set<String> regionSet = ImmutableSet.copyOf(regionNames);
    Set<String> subnetworkSet = ImmutableSet.copyOf(subnetworkNames);
    Set<String> rangeSet = ImmutableSet.copyOf(ipRanges);
    Compute.SubnetworkFilter subnetworkFilter = Compute.SubnetworkFilter.equals(Compute.SubnetworkField.NAME, prefix + "\\d");
    Page<Subnetwork> subnetworkPage = compute.listSubnetworks(Compute.SubnetworkAggregatedListOption.filter(subnetworkFilter));
    Iterator<Subnetwork> subnetworkIterator = subnetworkPage.iterateAll();
    int count = 0;
    while (subnetworkIterator.hasNext()) {
        Subnetwork remoteSubnetwork = subnetworkIterator.next();
        assertNotNull(remoteSubnetwork.generatedId());
        assertTrue(regionSet.contains(remoteSubnetwork.subnetworkId().region()));
        assertTrue(subnetworkSet.contains(remoteSubnetwork.subnetworkId().subnetwork()));
        assertNotNull(remoteSubnetwork.creationTimestamp());
        assertNotNull(remoteSubnetwork.gatewayAddress());
        assertEquals(networkId.network(), remoteSubnetwork.network().network());
        assertTrue(rangeSet.contains(remoteSubnetwork.ipRange()));
        count++;
    }
    assertEquals(2, count);
    firstOperation = compute.deleteSubnetwork(firstSubnetworkId);
    secondOperation = compute.deleteSubnetwork(secondSubnetworkId);
    firstOperation.waitFor();
    secondOperation.waitFor();
    operation = compute.deleteNetwork(networkId);
    operation.waitFor();
    assertNull(compute.getNetwork(networkName));
}

6. ITComputeTest#testCreateGetAndDeleteInstance()

Project: gcloud-java
Source File: ITComputeTest.java
View license
@Test
public void testCreateGetAndDeleteInstance() throws InterruptedException, TimeoutException {
    String instanceName = BASE_RESOURCE_NAME + "create-and-get-instance";
    String addressName = BASE_RESOURCE_NAME + "create-and-get-instance-address";
    // Create an address to assign to the instance
    AddressId addressId = RegionAddressId.of(REGION, addressName);
    AddressInfo addressInfo = AddressInfo.of(addressId);
    Operation operation = compute.create(addressInfo);
    operation.waitFor();
    Address address = compute.getAddress(addressId);
    // Create an instance
    InstanceId instanceId = InstanceId.of(ZONE, instanceName);
    NetworkId networkId = NetworkId.of("default");
    NetworkInterface networkInterface = NetworkInterface.builder(networkId).accessConfigurations(NetworkInterface.AccessConfig.builder().name("NAT").natIp(address.address()).build()).build();
    AttachedDisk disk1 = AttachedDisk.of("dev0", AttachedDisk.CreateDiskConfiguration.builder(IMAGE_ID).autoDelete(true).build());
    AttachedDisk disk2 = AttachedDisk.of("dev1", AttachedDisk.ScratchDiskConfiguration.of(DiskTypeId.of(ZONE, DISK_TYPE)));
    InstanceInfo instanceInfo = InstanceInfo.builder(instanceId, MachineTypeId.of(ZONE, "n1-standard-1")).attachedDisks(disk1, disk2).networkInterfaces(networkInterface).build();
    operation = compute.create(instanceInfo);
    operation.waitFor();
    // test get
    Instance remoteInstance = compute.getInstance(instanceId);
    assertEquals(instanceName, remoteInstance.instanceId().instance());
    assertEquals(ZONE, remoteInstance.instanceId().zone());
    assertEquals(InstanceInfo.Status.RUNNING, remoteInstance.status());
    assertEquals("n1-standard-1", remoteInstance.machineType().type());
    assertEquals(ZONE, remoteInstance.machineType().zone());
    assertNotNull(remoteInstance.creationTimestamp());
    Set<String> deviceSet = ImmutableSet.of("dev0", "dev1");
    assertEquals(2, remoteInstance.attachedDisks().size());
    for (AttachedDisk remoteAttachedDisk : remoteInstance.attachedDisks()) {
        assertTrue(deviceSet.contains(remoteAttachedDisk.deviceName()));
    }
    Assert.assertEquals(AttachedDisk.AttachedDiskConfiguration.Type.PERSISTENT, remoteInstance.attachedDisks().get(0).configuration().type());
    AttachedDisk.PersistentDiskConfiguration remoteConfiguration = remoteInstance.attachedDisks().get(0).configuration();
    assertEquals(instanceName, remoteConfiguration.sourceDisk().disk());
    assertEquals(ZONE, remoteConfiguration.sourceDisk().zone());
    assertTrue(remoteConfiguration.boot());
    assertTrue(remoteConfiguration.autoDelete());
    assertEquals(1, remoteInstance.networkInterfaces().size());
    NetworkInterface remoteNetworkInterface = remoteInstance.networkInterfaces().get(0);
    assertNotNull(remoteNetworkInterface.name());
    assertEquals("default", remoteNetworkInterface.network().network());
    List<NetworkInterface.AccessConfig> remoteAccessConfigurations = remoteNetworkInterface.accessConfigurations();
    assertNotNull(remoteAccessConfigurations);
    assertEquals(1, remoteAccessConfigurations.size());
    NetworkInterface.AccessConfig remoteAccessConfig = remoteAccessConfigurations.get(0);
    assertEquals(address.address(), remoteAccessConfig.natIp());
    assertEquals("NAT", remoteAccessConfig.name());
    assertNotNull(remoteInstance.metadata());
    assertNotNull(remoteInstance.tags());
    // test get with selected fields
    remoteInstance = compute.getInstance(instanceId, Compute.InstanceOption.fields(Compute.InstanceField.CREATION_TIMESTAMP));
    assertEquals(instanceName, remoteInstance.instanceId().instance());
    assertEquals(ZONE, remoteInstance.instanceId().zone());
    assertNull(remoteInstance.machineType());
    assertNotNull(remoteInstance.creationTimestamp());
    assertNull(remoteInstance.attachedDisks());
    assertNull(remoteInstance.networkInterfaces());
    assertNull(remoteInstance.metadata());
    assertNull(remoteInstance.tags());
    // test get default serial port output
    String serialPortOutput = remoteInstance.getSerialPortOutput();
    assertNotNull(serialPortOutput);
    // test get serial port output by number
    String newSerialPortOutput = remoteInstance.getSerialPortOutput(1);
    assertTrue(newSerialPortOutput.contains(serialPortOutput));
    operation = remoteInstance.delete();
    operation.waitFor();
    assertNull(compute.getInstance(instanceId));
    address.delete();
}

7. ITComputeTest#testStartStopAndResetInstance()

Project: gcloud-java
Source File: ITComputeTest.java
View license
@Test
public void testStartStopAndResetInstance() throws InterruptedException, TimeoutException {
    String instanceName = BASE_RESOURCE_NAME + "start-stop-reset-instance";
    InstanceId instanceId = InstanceId.of(ZONE, instanceName);
    NetworkId networkId = NetworkId.of("default");
    NetworkInterface networkInterface = NetworkInterface.builder(networkId).build();
    AttachedDisk disk = AttachedDisk.of("dev0", AttachedDisk.CreateDiskConfiguration.builder(IMAGE_ID).autoDelete(true).build());
    InstanceInfo instanceInfo = InstanceInfo.builder(instanceId, MachineTypeId.of(ZONE, MACHINE_TYPE)).attachedDisks(disk).networkInterfaces(networkInterface).build();
    Operation operation = compute.create(instanceInfo);
    operation.waitFor();
    Instance remoteInstance = compute.getInstance(instanceId, Compute.InstanceOption.fields(Compute.InstanceField.STATUS));
    assertEquals(InstanceInfo.Status.RUNNING, remoteInstance.status());
    operation = remoteInstance.stop();
    operation.waitFor();
    remoteInstance = compute.getInstance(instanceId, Compute.InstanceOption.fields(Compute.InstanceField.STATUS));
    assertEquals(InstanceInfo.Status.TERMINATED, remoteInstance.status());
    operation = remoteInstance.start();
    operation.waitFor();
    remoteInstance = compute.getInstance(instanceId, Compute.InstanceOption.fields(Compute.InstanceField.STATUS));
    assertEquals(InstanceInfo.Status.RUNNING, remoteInstance.status());
    operation = remoteInstance.reset();
    operation.waitFor();
    remoteInstance = compute.getInstance(instanceId, Compute.InstanceOption.fields(Compute.InstanceField.STATUS));
    assertEquals(InstanceInfo.Status.RUNNING, remoteInstance.status());
    remoteInstance.delete();
}

8. ITComputeTest#testSetInstanceProperties()

Project: gcloud-java
Source File: ITComputeTest.java
View license
@Test
public void testSetInstanceProperties() throws InterruptedException, TimeoutException {
    String instanceName = BASE_RESOURCE_NAME + "set-properties-instance";
    InstanceId instanceId = InstanceId.of(ZONE, instanceName);
    NetworkId networkId = NetworkId.of("default");
    NetworkInterface networkInterface = NetworkInterface.builder(networkId).build();
    AttachedDisk disk = AttachedDisk.of("dev0", AttachedDisk.CreateDiskConfiguration.builder(IMAGE_ID).autoDelete(true).build());
    InstanceInfo instanceInfo = InstanceInfo.builder(instanceId, MachineTypeId.of(ZONE, MACHINE_TYPE)).attachedDisks(disk).networkInterfaces(networkInterface).build();
    Operation operation = compute.create(instanceInfo);
    operation.waitFor();
    Instance remoteInstance = compute.getInstance(instanceId);
    // test set tags
    List<String> tags = ImmutableList.of("tag1", "tag2");
    operation = remoteInstance.setTags(tags);
    operation.waitFor();
    remoteInstance = compute.getInstance(instanceId);
    assertEquals(tags, remoteInstance.tags().values());
    // test set metadata
    Map<String, String> metadata = ImmutableMap.of("key", "value");
    operation = remoteInstance.setMetadata(metadata);
    operation.waitFor();
    remoteInstance = compute.getInstance(instanceId);
    assertEquals(metadata, remoteInstance.metadata().values());
    // test set machine type
    operation = remoteInstance.stop();
    operation.waitFor();
    operation = remoteInstance.setMachineType(MachineTypeId.of(ZONE, "n1-standard-1"));
    operation.waitFor();
    remoteInstance = compute.getInstance(instanceId);
    assertEquals("n1-standard-1", remoteInstance.machineType().type());
    assertEquals(ZONE, remoteInstance.machineType().zone());
    // test set scheduling options
    SchedulingOptions options = SchedulingOptions.standard(false, SchedulingOptions.Maintenance.TERMINATE);
    operation = remoteInstance.setSchedulingOptions(options);
    operation.waitFor();
    remoteInstance = compute.getInstance(instanceId);
    assertEquals(options, remoteInstance.schedulingOptions());
    remoteInstance.delete();
}

9. ITComputeTest#testAttachAndDetachDisk()

Project: gcloud-java
Source File: ITComputeTest.java
View license
@Test
public void testAttachAndDetachDisk() throws InterruptedException, TimeoutException {
    String instanceName = BASE_RESOURCE_NAME + "attach-and-detach-disk-instance";
    String diskName = BASE_RESOURCE_NAME + "attach-and-detach-disk";
    InstanceId instanceId = InstanceId.of(ZONE, instanceName);
    NetworkId networkId = NetworkId.of("default");
    NetworkInterface networkInterface = NetworkInterface.builder(networkId).build();
    AttachedDisk disk = AttachedDisk.of("dev0", AttachedDisk.CreateDiskConfiguration.builder(IMAGE_ID).autoDelete(true).build());
    InstanceInfo instanceInfo = InstanceInfo.builder(instanceId, MachineTypeId.of(ZONE, MACHINE_TYPE)).attachedDisks(disk).networkInterfaces(networkInterface).build();
    Operation instanceOperation = compute.create(instanceInfo);
    DiskId diskId = DiskId.of(ZONE, diskName);
    Operation diskOperation = compute.create(DiskInfo.of(diskId, StandardDiskConfiguration.of(DiskTypeId.of(ZONE, "pd-ssd"))));
    instanceOperation.waitFor();
    diskOperation.waitFor();
    Instance remoteInstance = compute.getInstance(instanceId);
    // test attach disk
    instanceOperation = remoteInstance.attachDisk("dev1", AttachedDisk.PersistentDiskConfiguration.builder(diskId).build());
    instanceOperation.waitFor();
    remoteInstance = compute.getInstance(instanceId);
    Set<String> deviceSet = ImmutableSet.of("dev0", "dev1");
    assertEquals(2, remoteInstance.attachedDisks().size());
    for (AttachedDisk remoteAttachedDisk : remoteInstance.attachedDisks()) {
        assertTrue(deviceSet.contains(remoteAttachedDisk.deviceName()));
    }
    // test set disk auto-delete
    instanceOperation = remoteInstance.setDiskAutoDelete("dev1", true);
    instanceOperation.waitFor();
    remoteInstance = compute.getInstance(instanceId);
    assertEquals(2, remoteInstance.attachedDisks().size());
    for (AttachedDisk remoteAttachedDisk : remoteInstance.attachedDisks()) {
        assertTrue(deviceSet.contains(remoteAttachedDisk.deviceName()));
        assertTrue(remoteAttachedDisk.configuration().autoDelete());
    }
    // test detach disk
    instanceOperation = remoteInstance.detachDisk("dev1");
    instanceOperation.waitFor();
    remoteInstance = compute.getInstance(instanceId);
    assertEquals(1, remoteInstance.attachedDisks().size());
    assertEquals("dev0", remoteInstance.attachedDisks().get(0).deviceName());
    remoteInstance.delete();
    compute.deleteDisk(diskId);
}

10. ITComputeTest#testAddAndRemoveAccessConfig()

Project: gcloud-java
Source File: ITComputeTest.java
View license
@Test
public void testAddAndRemoveAccessConfig() throws InterruptedException, TimeoutException {
    String instanceName = BASE_RESOURCE_NAME + "add-and-remove-access-instance";
    String addressName = BASE_RESOURCE_NAME + "add-and-remove-access-address";
    InstanceId instanceId = InstanceId.of(ZONE, instanceName);
    NetworkId networkId = NetworkId.of("default");
    NetworkInterface networkInterface = NetworkInterface.builder(networkId).build();
    AttachedDisk disk = AttachedDisk.of("dev0", AttachedDisk.CreateDiskConfiguration.builder(IMAGE_ID).autoDelete(true).build());
    InstanceInfo instanceInfo = InstanceInfo.builder(instanceId, MachineTypeId.of(ZONE, MACHINE_TYPE)).attachedDisks(disk).networkInterfaces(networkInterface).build();
    Operation instanceOperation = compute.create(instanceInfo);
    AddressId addressId = RegionAddressId.of(REGION, addressName);
    AddressInfo addressInfo = AddressInfo.of(addressId);
    Operation addressOperation = compute.create(addressInfo);
    addressOperation.waitFor();
    instanceOperation.waitFor();
    Address remoteAddress = compute.getAddress(addressId);
    Instance remoteInstance = compute.getInstance(instanceId);
    String networkInterfaceName = remoteInstance.networkInterfaces().get(0).name();
    // test add access config
    NetworkInterface.AccessConfig accessConfig = NetworkInterface.AccessConfig.builder().natIp(remoteAddress.address()).name("NAT").build();
    instanceOperation = remoteInstance.addAccessConfig(networkInterfaceName, accessConfig);
    instanceOperation.waitFor();
    remoteInstance = compute.getInstance(instanceId);
    List<NetworkInterface.AccessConfig> accessConfigurations = remoteInstance.networkInterfaces().get(0).accessConfigurations();
    assertEquals(1, accessConfigurations.size());
    assertEquals("NAT", accessConfigurations.get(0).name());
    // test delete access config
    instanceOperation = remoteInstance.deleteAccessConfig(networkInterfaceName, "NAT");
    instanceOperation.waitFor();
    remoteInstance = compute.getInstance(instanceId);
    assertTrue(remoteInstance.networkInterfaces().get(0).accessConfigurations().isEmpty());
    remoteInstance.delete();
    remoteAddress.delete();
}

11. CreateAddressDiskAndInstance#main()

View license
public static void main(String... args) throws InterruptedException, TimeoutException {
    // Create a service object
    // Credentials are inferred from the environment.
    Compute compute = ComputeOptions.defaultInstance().service();
    // Create an external region address
    RegionAddressId addressId = RegionAddressId.of("us-central1", "test-address");
    Operation operation = compute.create(AddressInfo.of(addressId));
    // Wait for operation to complete
    operation = operation.waitFor();
    if (operation.errors() == null) {
        System.out.println("Address " + addressId + " was successfully created");
    } else {
        // inspect operation.errors()
        throw new RuntimeException("Address creation failed");
    }
    // Create a persistent disk
    ImageId imageId = ImageId.of("debian-cloud", "debian-8-jessie-v20160329");
    DiskId diskId = DiskId.of("us-central1-a", "test-disk");
    ImageDiskConfiguration diskConfiguration = ImageDiskConfiguration.of(imageId);
    DiskInfo disk = DiskInfo.of(diskId, diskConfiguration);
    operation = compute.create(disk);
    // Wait for operation to complete
    operation = operation.waitFor();
    if (operation.errors() == null) {
        System.out.println("Disk " + diskId + " was successfully created");
    } else {
        // inspect operation.errors()
        throw new RuntimeException("Disk creation failed");
    }
    // Create a virtual machine instance
    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 = 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");
    }
}