Here are the examples of the java api class com.google.cloud.compute.Instance taken from open source projects.
1. ITComputeTest#testAttachAndDetachDisk()
Project: gcloud-java
File: ITComputeTest.java
File: ITComputeTest.java
@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); }
2. ITComputeTest#testCreateGetAndDeleteInstance()
Project: gcloud-java
File: ITComputeTest.java
File: ITComputeTest.java
@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(); }
3. ITComputeTest#testStartStopAndResetInstance()
Project: gcloud-java
File: ITComputeTest.java
File: ITComputeTest.java
@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(); }
4. ITComputeTest#testSetInstanceProperties()
Project: gcloud-java
File: ITComputeTest.java
File: ITComputeTest.java
@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(); }
5. ITComputeTest#testAddAndRemoveAccessConfig()
Project: gcloud-java
File: ITComputeTest.java
File: ITComputeTest.java
@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(); }