@Override InstanceId parse(String... args) throws Exception { String message; if (args.length == 2) { return InstanceId.of(args[0], args[1]); } else if (args.length > 2) { message = "Too many arguments."; } else { message = "Missing required zone and instance."; } throw new IllegalArgumentException(message); }
static InstanceId fromUrl(String url) { Matcher matcher = PATTERN.matcher(url); if (!matcher.matches()) { throw new IllegalArgumentException(url + " is not a valid instance URL"); } return InstanceId.of(matcher.group(1), matcher.group(2), matcher.group(3)); } }
@Override Tuple<InstanceId, List<String>> parse(String... args) throws Exception { if (args.length >= 2) { InstanceId instanceId = InstanceId.of(args[0], args[1]); List<String> tags = Lists.newArrayListWithCapacity(args.length - 2); tags.addAll(Arrays.asList(args).subList(2, args.length)); return Tuple.of(instanceId, tags); } else { throw new IllegalArgumentException("Missing required arguments."); } }
@Override InstanceId setProjectId(String projectId) { if (getProject() != null) { return this; } return InstanceId.of(projectId, zone, instance); }
@Override Triple<InstanceId, String, String> parse(String... args) throws Exception { String message; if (args.length == 4) { InstanceId instance = InstanceId.of(args[0], args[1]); String networkInterface = args[2]; String accessConfig = args[3]; return Triple.of(instance, networkInterface, accessConfig); } else if (args.length > 4) { message = "Too many arguments."; } else { message = "Missing required arguments."; } throw new IllegalArgumentException(message); }
@Override Tuple<InstanceId, Map<String, String>> parse(String... args) throws Exception { if (args.length >= 2) { if ((args.length & 0x1) == 0x1) { throw new IllegalArgumentException("Metadata must be a list of key-value pairs."); } InstanceId instanceId = InstanceId.of(args[0], args[1]); Map<String, String> metadata = Maps.newHashMapWithExpectedSize((args.length / 2) - 1); for (int i = 2; i < args.length; i += 2) { metadata.put(args[i], args[i + 1]); } return Tuple.of(instanceId, metadata); } else { throw new IllegalArgumentException("Missing required arguments."); } }
@Override Tuple<InstanceId, String> parse(String... args) throws Exception { String message; if (args.length == 3) { String zone = args[0]; String instance = args[1]; String deviceName = args[2]; return Tuple.of(InstanceId.of(zone, instance), deviceName); } else if (args.length > 4) { message = "Too many arguments."; } else { message = "Missing required arguments."; } throw new IllegalArgumentException(message); }
@Override Tuple<InstanceId, Integer> parse(String... args) throws Exception { if (args.length >= 2) { InstanceId instanceId = InstanceId.of(args[0], args[1]); Integer port = null; if (args.length == 3) { try { port = Integer.parseInt(args[2]); } catch (NumberFormatException ex) { throw new IllegalArgumentException( "Error parsing portNumber parameter (must be a number)"); } } else if (args.length > 3) { throw new IllegalArgumentException("Too many arguments."); } return Tuple.of(instanceId, port); } else { throw new IllegalArgumentException("Missing required arguments."); } }
@Override Triple<InstanceId, String, Boolean> parse(String... args) throws Exception { String message; if (args.length == 4) { InstanceId instance = InstanceId.of(args[0], args[1]); String deviceName = args[2]; boolean autoDelete; switch (args[3]) { case "true": autoDelete = true; break; case "false": autoDelete = false; break; default: throw new IllegalArgumentException( "Couldn't parse autoDelete argument (must be either true or false)."); } return Triple.of(instance, deviceName, autoDelete); } else if (args.length > 3) { message = "Too many arguments."; } else { message = "Missing required arguments."; } throw new IllegalArgumentException(message); }
@Override Tuple<InstanceId, MachineTypeId> parse(String... args) throws Exception { String message; if (args.length == 3) { String zone = args[0]; String instance = args[1]; String machineType = args[2]; return Tuple.of(InstanceId.of(zone, instance), MachineTypeId.of(zone, machineType)); } else if (args.length > 3) { message = "Too many arguments."; } else { message = "Missing required arguments."; } throw new IllegalArgumentException(message); }
@Override Triple<InstanceId, String, PersistentDiskConfiguration> parse(String... args) throws Exception { String message; if (args.length == 4) { String zone = args[0]; String instance = args[1]; String deviceName = args[2]; String disk = args[3]; return Triple.of( InstanceId.of(zone, instance), deviceName, PersistentDiskConfiguration.of(DiskId.of(zone, disk))); } else if (args.length > 4) { message = "Too many arguments."; } else { message = "Missing required arguments."; } throw new IllegalArgumentException(message); }
@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); }
@Override Triple<InstanceId, String, AccessConfig> parse(String... args) throws Exception { String message; if (args.length >= 4) { InstanceId instance = InstanceId.of(args[0], args[1]); String networkInterface = args[2]; String accessConfig = args[3]; if (args.length == 4) { return Triple.of( instance, networkInterface, AccessConfig.newBuilder().setName(accessConfig).build()); } else if (args.length == 5) { return Triple.of( instance, networkInterface, AccessConfig.newBuilder().setName(accessConfig).setNatIp(args[4]).build()); } else { message = "Too many arguments."; } } else { message = "Missing required arguments."; } throw new IllegalArgumentException(message); }
@Test public void testMatchesUrl() { assertTrue(InstanceId.matchesUrl(InstanceId.of(PROJECT, ZONE, NAME).getSelfLink())); assertFalse(InstanceId.matchesUrl("notMatchingUrl")); }
@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); }
@Test public void testToAndFromUrl() { InstanceId instanceId = InstanceId.of(PROJECT, ZONE, NAME); compareInstanceId(instanceId, InstanceId.fromUrl(instanceId.getSelfLink())); thrown.expect(IllegalArgumentException.class); thrown.expectMessage("notMatchingUrl is not a valid instance URL"); InstanceId.fromUrl("notMatchingUrl"); }
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 testOf() { InstanceId instanceId = InstanceId.of(PROJECT, ZONE, NAME); assertEquals(PROJECT, instanceId.getProject()); assertEquals(ZONE, instanceId.getZone()); assertEquals(NAME, instanceId.getInstance()); assertEquals(URL, instanceId.getSelfLink()); instanceId = InstanceId.of(ZoneId.of(PROJECT, ZONE), NAME); assertEquals(PROJECT, instanceId.getProject()); assertEquals(ZONE, instanceId.getZone()); assertEquals(NAME, instanceId.getInstance()); assertEquals(URL, instanceId.getSelfLink()); instanceId = InstanceId.of(ZONE, NAME); assertNull(instanceId.getProject()); assertEquals(ZONE, instanceId.getZone()); assertEquals(NAME, instanceId.getInstance()); }
@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")); }