static DiskId parseDiskId(String[] args) { return DiskId.of(args[0], args[1]); } }
@Override DiskId parse(String... args) throws Exception { String message; if (args.length == 2) { return DiskId.of(args[0], args[1]); } else if (args.length > 2) { message = "Too many arguments."; } else { message = "Missing required zone and disk id."; } throw new IllegalArgumentException(message); }
static DiskId fromUrl(String url) { Matcher matcher = PATTERN.matcher(url); if (!matcher.matches()) { throw new IllegalArgumentException(url + " is not a valid disk URL"); } return DiskId.of(matcher.group(1), matcher.group(2), matcher.group(3)); } }
@Override DiskId setProjectId(String projectId) { if (getProject() != null) { return this; } return DiskId.of(projectId, zone, disk); }
@Override SnapshotInfo parse(String... args) throws Exception { String message; if (args.length == 3) { String snapshot = args[0]; String zone = args[1]; String disk = args[2]; return SnapshotInfo.of(SnapshotId.of(snapshot), DiskId.of(zone, disk)); } else if (args.length > 3) { message = "Too many arguments."; } else { message = "Missing required arguments."; } throw new IllegalArgumentException(message); }
@Override ImageInfo parse(String... args) throws Exception { String message; if (args.length == 3) { String image = args[0]; String zone = args[1]; String disk = args[2]; return ImageInfo.of(ImageId.of(image), DiskImageConfiguration.of(DiskId.of(zone, disk))); } 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); }
public static void main(String... args) throws InterruptedException, TimeoutException { Compute compute = ComputeOptions.getDefaultInstance().getService(); DiskId diskId = DiskId.of("us-central1-a", "disk-name"); Disk disk = compute.getDisk(diskId, Compute.DiskOption.fields()); if (disk != null) { String snapshotName = "disk-name-snapshot"; Operation operation = disk.createSnapshot(snapshotName); operation = operation.waitFor(); if (operation.getErrors() == null) { // use snapshot Snapshot snapshot = compute.getSnapshot(snapshotName); } } } }
@Test public void testSetProjectId() { DiskImageConfiguration configuration = CONFIGURATION.toBuilder().setSourceDisk(DiskId.of("zone", "disk")).build(); compareDiskImageConfiguration(CONFIGURATION, configuration.setProjectId("project")); }
@Test public void testMatchesUrl() { assertTrue(DiskId.matchesUrl(DiskId.of(PROJECT, ZONE, NAME).getSelfLink())); assertFalse(DiskId.matchesUrl("notMatchingUrl")); }
@Test public void testToAndFromUrl() { DiskId diskId = DiskId.of(PROJECT, ZONE, NAME); compareDiskId(diskId, DiskId.fromUrl(diskId.getSelfLink())); thrown.expect(IllegalArgumentException.class); thrown.expectMessage("notMatchingUrl is not a valid disk URL"); DiskId.fromUrl("notMatchingUrl"); }
@Test public void testCreateDisk() { EasyMock.expect(computeRpcMock.createDisk(DISK_ID.getZone(), DISK.toPb(), EMPTY_RPC_OPTIONS)) .andReturn(zoneOperation.toPb()); EasyMock.replay(computeRpcMock); compute = options.getService(); DiskId diskId = DiskId.of("zone", "disk"); DiskTypeId diskTypeId = DiskTypeId.of("zone", "diskType"); DiskInfo disk = DISK.toBuilder() .setDiskId(diskId) .setConfiguration(StandardDiskConfiguration.of(diskTypeId)) .build(); Operation operation = compute.create(disk); assertEquals(zoneOperation, operation); }
@Test public void testSetProjectId() { SnapshotInfo snapshotInfo = SNAPSHOT_INFO .toBuilder() .setSnapshotId(SnapshotId.of("snapshot")) .setSourceDisk(DiskId.of("zone", "disk")) .build(); compareSnapshotInfo(SNAPSHOT_INFO, snapshotInfo.setProjectId("project")); }
@Test public void testOf() { DiskId diskId = DiskId.of(PROJECT, ZONE, NAME); assertEquals(PROJECT, diskId.getProject()); assertEquals(ZONE, diskId.getZone()); assertEquals(NAME, diskId.getDisk()); assertEquals(URL, diskId.getSelfLink()); diskId = DiskId.of(ZONE, NAME); assertNull(diskId.getProject()); assertEquals(ZONE, diskId.getZone()); assertEquals(NAME, diskId.getDisk()); diskId = DiskId.of(ZoneId.of(ZONE), NAME); assertNull(diskId.getProject()); assertEquals(ZONE, diskId.getZone()); assertEquals(NAME, diskId.getDisk()); }
@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 testSetProjectId() { ImageInfo imageInfo = DISK_IMAGE .toBuilder() .setImageId(ImageId.of("image")) .setConfiguration( DISK_CONFIGURATION.toBuilder().setSourceDisk(DiskId.of("zone", "disk")).build()) .build(); compareImageInfo(DISK_IMAGE, imageInfo.setProjectId("project")); }
@Test public void testConfigurationSetProjectId() { PersistentDiskConfiguration persistentConfiguration = PersistentDiskConfiguration.of(DiskId.of("zone", "disk")); comparePersistentDiskConfiguration( PersistentDiskConfiguration.of(DiskId.of("project", "zone", "disk")), persistentConfiguration.setProjectId("project")); ScratchDiskConfiguration scratchDiskConfiguration = ScratchDiskConfiguration.of(DiskTypeId.of("zone", "diskType")); compareScratchDiskConfiguration( ScratchDiskConfiguration.of(DiskTypeId.of("project", "zone", "diskType")), scratchDiskConfiguration.setProjectId("project")); CreateDiskConfiguration createDiskConfiguration = CREATE_DISK_CONFIGURATION .toBuilder() .setDiskType(DiskTypeId.of("zone", "diskType")) .setSourceImage(ImageId.of("image")) .build(); compareCreateDiskConfiguration( CREATE_DISK_CONFIGURATION, createDiskConfiguration.setProjectId("project")); }
@Test public void testToBuilder() { compareDiskImageConfiguration(CONFIGURATION, CONFIGURATION.toBuilder().build()); DiskId newDisk = DiskId.of("newProject", "newZone", "newDisk"); String newDiskId = "newDiskId"; DiskImageConfiguration configuration = CONFIGURATION.toBuilder().setSourceDisk(newDisk).setSourceDiskId(newDiskId).build(); assertEquals(newDisk, configuration.getSourceDisk()); assertEquals(newDiskId, configuration.getSourceDiskId()); configuration = configuration .toBuilder() .setSourceDiskId(SOURCE_DISK_ID) .setSourceDisk(SOURCE_DISK) .build(); compareDiskImageConfiguration(CONFIGURATION, configuration); }