@Override InstanceInfo parse(String... args) throws Exception { String message; if (args.length == 5) { String zone = args[0]; String instance = args[1]; InstanceId instanceId = InstanceId.of(zone, instance); MachineTypeId machineTypeId = MachineTypeId.of(zone, args[2]); DiskId diskId = DiskId.of(zone, args[3]); AttachedDisk disk = AttachedDisk.of(PersistentDiskConfiguration.newBuilder(diskId).setBoot(true).build()); NetworkInterface networkInterface = NetworkInterface.of(args[4]); return InstanceInfo.of(instanceId, machineTypeId, disk, networkInterface); } else if (args.length > 5) { message = "Too many arguments."; } else { message = "Missing required arguments."; } throw new IllegalArgumentException(message); }
@Test public void testToBuilderIncomplete() { InstanceInfo instanceInfo = InstanceInfo.of(INSTANCE_ID, MACHINE_TYPE, ATTACHED_DISK, NETWORK_INTERFACE); assertEquals(instanceInfo, instanceInfo.toBuilder().build()); }
@Test public void testSetProjectId() { InstanceInfo instance = InstanceInfo.of( InstanceId.of("zone", "instance"), MachineTypeId.of("zone", "type"), AttachedDisk.of(AttachedDisk.PersistentDiskConfiguration.of(DiskId.of("zone", "disk"))), NetworkInterface.of(NetworkId.of("project", "network"))); InstanceInfo instanceWithProject = InstanceInfo.of(INSTANCE_ID, MACHINE_TYPE, ATTACHED_DISK, NETWORK_INTERFACE); compareInstanceInfo(instanceWithProject, instance.setProjectId("project")); }
@Test public void testCreateInstance() { EasyMock.expect( computeRpcMock.createInstance( INSTANCE_ID.getZone(), INSTANCE.toPb(), EMPTY_RPC_OPTIONS)) .andReturn(zoneOperation.toPb()); EasyMock.replay(computeRpcMock); compute = options.getService(); InstanceInfo instance = InstanceInfo.of( InstanceId.of("zone", "instance"), MachineTypeId.of("zone", "type"), ATTACHED_DISK, NetworkInterface.of(NetworkId.of("network"))); Operation operation = compute.create(instance); assertEquals(zoneOperation, operation); }
MachineTypeId machineTypeId = MachineTypeId.of("us-central1-a", "n1-standard-1"); InstanceInfo instance = InstanceInfo.of(instanceId, machineTypeId, attachedDisk, networkInterface); operation = compute.create(instance);
@Test public void testToAndFromPb() { compareInstanceInfo(INSTANCE_INFO, InstanceInfo.fromPb(INSTANCE_INFO.toPb())); InstanceInfo instance = InstanceInfo.of(INSTANCE_ID, MACHINE_TYPE, ATTACHED_DISK, NETWORK_INTERFACE); compareInstanceInfo(instance, InstanceInfo.fromPb(instance.toPb())); }
public static void main(String... args) throws InterruptedException, TimeoutException { Compute compute = ComputeOptions.getDefaultInstance().getService(); 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.getErrors() == null) { // use instance Instance instance = compute.getInstance(instanceId); } } }
@Test public void testToBuilderIncomplete() { initializeExpectedInstance(5); InstanceInfo instanceInfo = InstanceInfo.of(INSTANCE_ID, MACHINE_TYPE, ATTACHED_DISK, NETWORK_INTERFACE); Instance instance = new Instance(serviceMockReturnsOptions, new InstanceInfo.BuilderImpl(instanceInfo)); compareInstance(instance, instance.toBuilder().build()); }
@Test public void testOf() { InstanceInfo instance = InstanceInfo.of(INSTANCE_ID, MACHINE_TYPE, ATTACHED_DISK, NETWORK_INTERFACE); assertNull(instance.getGeneratedId()); assertEquals(INSTANCE_ID, instance.getInstanceId()); assertNull(instance.getCreationTimestamp()); assertNull(instance.getDescription()); assertNull(instance.getStatus()); assertNull(instance.getStatusMessage()); assertNull(instance.getTags()); assertEquals(MACHINE_TYPE, instance.getMachineType()); assertNull(instance.canIpForward()); assertEquals(NETWORK_INTERFACES, instance.getNetworkInterfaces()); assertEquals(ATTACHED_DISKS, instance.getAttachedDisks()); assertNull(instance.getMetadata()); assertNull(instance.getServiceAccounts()); assertNull(instance.getSchedulingOptions()); assertNull(instance.getCpuPlatform()); }
assertSame(serviceMockReturnsOptions, expectedInstance.getCompute()); InstanceInfo instanceInfo = InstanceInfo.of(INSTANCE_ID, MACHINE_TYPE, ATTACHED_DISK, NETWORK_INTERFACE); Instance instance = new Instance(serviceMockReturnsOptions, new InstanceInfo.BuilderImpl(instanceInfo));