/** * Creates a subnetwork for this network given its identity and the range of IPv4 addresses in * CIDR format. Subnetwork creation is only supported for networks in "custom subnet mode" (i.e. * {@link #getConfiguration()} returns a {@link SubnetNetworkConfiguration}) with automatic * creation of subnetworks disabled (i.e. {@link * SubnetNetworkConfiguration#autoCreateSubnetworks()} returns {@code false}). * * @return an operation object if creation request was successfully sent * @throws ComputeException upon failure * @see <a href="https://wikipedia.org/wiki/Classless_Inter-Domain_Routing">CIDR</a> */ public Operation createSubnetwork( SubnetworkId subnetworkId, String ipRange, OperationOption... options) { return compute.create(SubnetworkInfo.of(subnetworkId, getNetworkId(), ipRange), options); }
/** * Creates a snapshot for this disk given the snapshot's name. * * @return a zone operation for snapshot creation * @throws ComputeException upon failure */ public Operation createSnapshot(String snapshot, OperationOption... options) { return compute.create(SnapshotInfo.of(SnapshotId.of(snapshot), getDiskId()), options); }
@Override public void run(Compute compute, SnapshotInfo snapshot) throws InterruptedException { Operation operation = compute.create(snapshot); while (!operation.isDone()) { System.out.printf( "Waiting for operation %s to complete%n", operation.getOperationId().getOperation()); Thread.sleep(1000L); } operation = operation.reload(); if (operation.getErrors() == null) { System.out.printf("Snapshot %s was created%n", snapshot.getSnapshotId()); } else { System.out.printf("Creation of snapshot %s failed%n", snapshot.getSnapshotId()); System.out.printf("Error: %s%n", operation.getErrors()); } }
@Override public void run(Compute compute, ImageInfo image) throws InterruptedException { Operation operation = compute.create(image); while (!operation.isDone()) { System.out.printf( "Waiting for operation %s to complete%n", operation.getOperationId().getOperation()); Thread.sleep(1000L); } operation = operation.reload(); if (operation.getErrors() == null) { System.out.printf("Image %s was created%n", image.getImageId()); } else { System.out.printf("Creation of image %s failed%n", image.getImageId()); System.out.printf("Error: %s%n", operation.getErrors()); } }
@Override public void run(Compute compute, DiskInfo disk) throws InterruptedException { Operation operation = compute.create(disk); while (!operation.isDone()) { System.out.printf( "Waiting for operation %s to complete%n", operation.getOperationId().getOperation()); Thread.sleep(1000L); } operation = operation.reload(); if (operation.getErrors() == null) { System.out.printf("Disk %s was created%n", disk.getDiskId()); } else { System.out.printf("Creation of disk %s failed%n", disk.getDiskId()); System.out.printf("Error: %s%n", operation.getErrors()); } }
@Override public void run(Compute compute, InstanceInfo instance) throws InterruptedException { Operation operation = compute.create(instance); while (!operation.isDone()) { System.out.printf( "Waiting for operation %s to complete%n", operation.getOperationId().getOperation()); Thread.sleep(1000L); } operation = operation.reload(); if (operation.getErrors() == null) { System.out.printf("Instance %s was created%n", instance.getInstanceId()); } else { System.out.printf("Creation of instance %s failed%n", instance.getInstanceId()); System.out.printf("Error: %s%n", operation.getErrors()); } }
/** * Creates an image for this disk given the image's name. * * @return a global operation if the image creation was successfully requested * @throws ComputeException upon failure */ public Operation createImage(String image, OperationOption... options) { ImageInfo imageInfo = ImageInfo.of(ImageId.of(image), DiskImageConfiguration.of(getDiskId())); return compute.create(imageInfo, options); }
/** * Creates a snapshot for this disk given the snapshot's name and description. * * @return a zone operation for snapshot creation * @throws ComputeException upon failure */ public Operation createSnapshot(String snapshot, String description, OperationOption... options) { SnapshotInfo snapshotInfo = SnapshotInfo.newBuilder(SnapshotId.of(snapshot), getDiskId()) .setDescription(description) .build(); return compute.create(snapshotInfo, options); }
/** * Creates an image for this disk given the image's name and description. * * @return a global operation if the image creation was successfully requested * @throws ComputeException upon failure */ public Operation createImage(String image, String description, OperationOption... options) { ImageInfo imageInfo = ImageInfo.newBuilder(ImageId.of(image), DiskImageConfiguration.of(getDiskId())) .setDescription(description) .build(); return compute.create(imageInfo, options); }
@Test public void testCreateImage() { EasyMock.expect(computeRpcMock.createImage(IMAGE.toPb(), EMPTY_RPC_OPTIONS)) .andReturn(globalOperation.toPb()); EasyMock.replay(computeRpcMock); compute = options.getService(); Operation operation = compute.create(IMAGE); assertEquals(globalOperation, operation); }
@Test public void testCreateNetwork() { EasyMock.expect(computeRpcMock.createNetwork(NETWORK.toPb(), EMPTY_RPC_OPTIONS)) .andReturn(globalOperation.toPb()); EasyMock.replay(computeRpcMock); compute = options.getService(); NetworkInfo network = NetworkInfo.of(NetworkId.of("network"), StandardNetworkConfiguration.of("192.168.0.0/16")); Operation operation = compute.create(network); assertEquals(globalOperation, operation); }
@Test public void testCreateSubnetwork() throws Exception { initializeExpectedNetwork(3); Operation operation = new Operation.Builder(serviceMockReturnsOptions) .setOperationId(RegionOperationId.of(SUBNETWORK1.getRegionId(), "op")) .build(); expect(compute.getOptions()).andReturn(mockOptions); expect(compute.create(SubnetworkInfo.of(SUBNETWORK1, NETWORK_ID, IP_RANGE))) .andReturn(operation); replay(compute); initializeNetwork(); assertSame(operation, network.createSubnetwork(SUBNETWORK1, IP_RANGE)); verify(compute); }
@Test public void testCreateSnapshot() { EasyMock.expect( computeRpcMock.createSnapshot( DISK_ID.getZone(), DISK_ID.getDisk(), SNAPSHOT_ID.getSnapshot(), null, EMPTY_RPC_OPTIONS)) .andReturn(zoneOperation.toPb()); EasyMock.replay(computeRpcMock); compute = options.getService(); Operation operation = compute.create(SNAPSHOT); assertEquals(zoneOperation, operation); }
@Test public void testCreateGlobalAddress() { EasyMock.expect(computeRpcMock.createGlobalAddress(GLOBAL_ADDRESS.toPb(), EMPTY_RPC_OPTIONS)) .andReturn(globalOperation.toPb()); EasyMock.replay(computeRpcMock); compute = options.getService(); AddressId incompleteId = GlobalAddressId.of("address"); Operation operation = compute.create(GLOBAL_ADDRESS.toBuilder().setAddressId(incompleteId).build()); assertEquals(globalOperation, operation); }
@Test public void testCreateSubnetwork() { EasyMock.expect( computeRpcMock.createSubnetwork( SUBNETWORK_ID.getRegion(), SUBNETWORK.toPb(), EMPTY_RPC_OPTIONS)) .andReturn(regionOperation.toPb()); EasyMock.replay(computeRpcMock); compute = options.getService(); SubnetworkId subnetworkId = SubnetworkId.of("region", "network"); NetworkId networkId = NetworkId.of("network"); SubnetworkInfo subnetwork = SubnetworkInfo.of(subnetworkId, networkId, "192.168.0.0/16"); Operation operation = compute.create(subnetwork); assertEquals(regionOperation, operation); }
@Test public void testCreateRegionAddress() { EasyMock.expect( computeRpcMock.createRegionAddress( REGION_ADDRESS_ID.getRegion(), REGION_ADDRESS.toPb(), EMPTY_RPC_OPTIONS)) .andReturn(regionOperation.toPb()); EasyMock.replay(computeRpcMock); compute = options.getService(); AddressId incompleteId = RegionAddressId.of("region", "address"); Operation operation = compute.create(REGION_ADDRESS.toBuilder().setAddressId(incompleteId).build()); assertEquals(regionOperation, operation); }
@Test public void testCreateSnapshot() { initializeExpectedDisk(4); expect(compute.getOptions()).andReturn(mockOptions); Operation operation = new Operation.Builder(serviceMockReturnsOptions) .setOperationId(ZoneOperationId.of("project", "zone", "op")) .build(); SnapshotId snapshotId = SnapshotId.of(SNAPSHOT.getSnapshot()); SnapshotInfo snapshot = SnapshotInfo.newBuilder(snapshotId, DISK_ID).build(); expect(compute.create(snapshot)).andReturn(operation); replay(compute); initializeDisk(); assertSame(operation, disk.createSnapshot(SNAPSHOT.getSnapshot())); }
@Test public void testCreateImage() { initializeExpectedDisk(4); expect(compute.getOptions()).andReturn(mockOptions); Operation operation = new Operation.Builder(serviceMockReturnsOptions) .setOperationId(GlobalOperationId.of("project", "op")) .build(); ImageId imageId = ImageId.of(IMAGE.getImage()); ImageInfo image = ImageInfo.of(imageId, DiskImageConfiguration.of(DISK_ID)); expect(compute.create(image)).andReturn(operation); replay(compute); initializeDisk(); assertSame(operation, disk.createImage(IMAGE.getImage())); }
@Test public void testCreateSnapshotWithDescription() { initializeExpectedDisk(4); expect(compute.getOptions()).andReturn(mockOptions); Operation operation = new Operation.Builder(serviceMockReturnsOptions) .setOperationId(ZoneOperationId.of("project", "zone", "op")) .build(); SnapshotId snapshotId = SnapshotId.of(SNAPSHOT.getSnapshot()); SnapshotInfo snapshot = SnapshotInfo.newBuilder(snapshotId, DISK_ID).setDescription("description").build(); expect(compute.create(snapshot)).andReturn(operation); replay(compute); initializeDisk(); assertSame(operation, disk.createSnapshot(SNAPSHOT.getSnapshot(), "description")); }
@Test public void testCreateImageWithOptions() { initializeExpectedDisk(4); expect(compute.getOptions()).andReturn(mockOptions); Operation operation = new Operation.Builder(serviceMockReturnsOptions) .setOperationId(GlobalOperationId.of("project", "op")) .build(); ImageId imageId = ImageId.of(IMAGE.getImage()); ImageInfo image = ImageInfo.of(imageId, DiskImageConfiguration.of(DISK_ID)); expect(compute.create(image, Compute.OperationOption.fields())).andReturn(operation); replay(compute); initializeDisk(); assertSame(operation, disk.createImage(IMAGE.getImage(), Compute.OperationOption.fields())); }