|
16 | 16 |
|
17 | 17 | package com.google.cloud.examples.compute.snippets; |
18 | 18 |
|
19 | | -import com.google.cloud.compute.deprecated.Address; |
20 | | -import com.google.cloud.compute.deprecated.AddressInfo; |
21 | | -import com.google.cloud.compute.deprecated.AttachedDisk; |
22 | | -import com.google.cloud.compute.deprecated.AttachedDisk.PersistentDiskConfiguration; |
23 | | -import com.google.cloud.compute.deprecated.Compute; |
24 | | -import com.google.cloud.compute.deprecated.ComputeOptions; |
25 | | -import com.google.cloud.compute.deprecated.DiskId; |
26 | | -import com.google.cloud.compute.deprecated.DiskInfo; |
27 | | -import com.google.cloud.compute.deprecated.ImageDiskConfiguration; |
28 | | -import com.google.cloud.compute.deprecated.ImageId; |
29 | | -import com.google.cloud.compute.deprecated.InstanceId; |
30 | | -import com.google.cloud.compute.deprecated.InstanceInfo; |
31 | | -import com.google.cloud.compute.deprecated.MachineTypeId; |
32 | | -import com.google.cloud.compute.deprecated.NetworkId; |
33 | | -import com.google.cloud.compute.deprecated.NetworkInterface; |
34 | | -import com.google.cloud.compute.deprecated.NetworkInterface.AccessConfig; |
35 | | -import com.google.cloud.compute.deprecated.Operation; |
36 | | -import com.google.cloud.compute.deprecated.RegionAddressId; |
37 | | -import java.util.concurrent.TimeoutException; |
| 19 | +import com.google.cloud.ServiceOptions; |
| 20 | +import com.google.cloud.compute.v1.Address; |
| 21 | +import com.google.cloud.compute.v1.AddressClient; |
| 22 | +import com.google.cloud.compute.v1.AttachedDisk; |
| 23 | +import com.google.cloud.compute.v1.AttachedDiskInitializeParams; |
| 24 | +import com.google.cloud.compute.v1.Disk; |
| 25 | +import com.google.cloud.compute.v1.DiskClient; |
| 26 | +import com.google.cloud.compute.v1.InsertDiskHttpRequest; |
| 27 | +import com.google.cloud.compute.v1.Instance; |
| 28 | +import com.google.cloud.compute.v1.InstanceClient; |
| 29 | +import com.google.cloud.compute.v1.NetworkInterface; |
| 30 | +import com.google.cloud.compute.v1.Operation; |
| 31 | +import com.google.cloud.compute.v1.ProjectRegionName; |
| 32 | +import com.google.cloud.compute.v1.ProjectZoneMachineTypeName; |
| 33 | +import com.google.cloud.compute.v1.ProjectZoneName; |
| 34 | +import java.io.IOException; |
38 | 35 |
|
39 | 36 | /** |
40 | 37 | * A snippet for Google Cloud Compute Engine showing how to create a disk and an address. The |
41 | 38 | * snippet also shows how to create a virtual machine instance using the created disk and address. |
42 | 39 | */ |
43 | 40 | public class CreateAddressDiskAndInstance { |
| 41 | + private static final String DEFAULT_PROJECT = ServiceOptions.getDefaultProjectId(); |
| 42 | + private static final String ZONE = "us-central1-a"; |
| 43 | + private static final String ADDRESS_NAME = "test-address"; |
| 44 | + private static final String DEFAULT_IMAGE = |
| 45 | + "https://www.googleapis.com/compute/v1/projects/debian-cloud/global/images/debian-7-wheezy-v20150710"; |
| 46 | + private static final String FORMATTED_ZONE = ProjectZoneName.format(DEFAULT_PROJECT, ZONE); |
| 47 | + private static final String REGION = "us-central1"; |
44 | 48 |
|
45 | | - public static void main(String... args) throws InterruptedException, TimeoutException { |
46 | | - // Create a service object |
47 | | - // Credentials are inferred from the environment. |
48 | | - Compute compute = ComputeOptions.getDefaultInstance().getService(); |
49 | | - |
50 | | - // Create an external region address |
51 | | - RegionAddressId addressId = RegionAddressId.of("us-central1", "test-address"); |
52 | | - Operation operation = compute.create(AddressInfo.of(addressId)); |
53 | | - // Wait for operation to complete |
54 | | - operation = operation.waitFor(); |
55 | | - if (operation.getErrors() == null) { |
56 | | - System.out.println("Address " + addressId + " was successfully created"); |
57 | | - } else { |
58 | | - // inspect operation.getErrors() |
59 | | - throw new RuntimeException("Address creation failed"); |
| 49 | + public static void main(String... args) throws IOException { |
| 50 | + try (AddressClient addressClient = AddressClient.create()) { |
| 51 | + ProjectRegionName projectRegionName = ProjectRegionName.of(DEFAULT_PROJECT, REGION); |
| 52 | + Address address = Address.newBuilder().setName(ADDRESS_NAME).build(); |
| 53 | + Operation operation = addressClient.insertAddress(projectRegionName, address); |
| 54 | + if (operation.getError() == null) { |
| 55 | + System.out.println("Address " + ADDRESS_NAME + " was successfully created"); |
| 56 | + } else { |
| 57 | + // inspect operation.getErrors() |
| 58 | + throw new RuntimeException("Address creation failed"); |
| 59 | + } |
60 | 60 | } |
61 | | - |
62 | 61 | // Create a persistent disk |
63 | | - ImageId imageId = ImageId.of("debian-cloud", "debian-8-jessie-v20160329"); |
64 | | - DiskId diskId = DiskId.of("us-central1-a", "test-disk"); |
65 | | - ImageDiskConfiguration diskConfiguration = ImageDiskConfiguration.of(imageId); |
66 | | - DiskInfo disk = DiskInfo.of(diskId, diskConfiguration); |
67 | | - operation = compute.create(disk); |
68 | | - // Wait for operation to complete |
69 | | - operation = operation.waitFor(); |
70 | | - if (operation.getErrors() == null) { |
71 | | - System.out.println("Disk " + diskId + " was successfully created"); |
72 | | - } else { |
73 | | - // inspect operation.getErrors() |
74 | | - throw new RuntimeException("Disk creation failed"); |
| 62 | + try (DiskClient diskClient = DiskClient.create()) { |
| 63 | + Disk diskResource = |
| 64 | + Disk.newBuilder() |
| 65 | + .setName("test-disk") |
| 66 | + .setSourceImageId("debian-8-jessie-v20160329") |
| 67 | + .setSizeGb("10") |
| 68 | + .build(); |
| 69 | + InsertDiskHttpRequest request = |
| 70 | + InsertDiskHttpRequest.newBuilder() |
| 71 | + .setZone(FORMATTED_ZONE) |
| 72 | + .setDiskResource(diskResource) |
| 73 | + .build(); |
| 74 | + Operation response = diskClient.insertDisk(request); |
| 75 | + if (response.getError() == null) { |
| 76 | + System.out.println("Disk " + diskResource.getName() + " was successfully created"); |
| 77 | + } else { |
| 78 | + // inspect operation.getErrors() |
| 79 | + throw new RuntimeException("Disk creation failed"); |
| 80 | + } |
75 | 81 | } |
76 | | - |
77 | 82 | // Create a virtual machine instance |
78 | | - Address externalIp = compute.getAddress(addressId); |
79 | | - InstanceId instanceId = InstanceId.of("us-central1-a", "test-instance"); |
80 | | - NetworkId networkId = NetworkId.of("default"); |
81 | | - PersistentDiskConfiguration attachConfiguration = |
82 | | - PersistentDiskConfiguration.newBuilder(diskId).setBoot(true).build(); |
83 | | - AttachedDisk attachedDisk = AttachedDisk.of("dev0", attachConfiguration); |
84 | | - NetworkInterface networkInterface = |
85 | | - NetworkInterface.newBuilder(networkId) |
86 | | - .setAccessConfigurations(AccessConfig.of(externalIp.getAddress())) |
87 | | - .build(); |
88 | | - MachineTypeId machineTypeId = MachineTypeId.of("us-central1-a", "n1-standard-1"); |
89 | | - InstanceInfo instance = |
90 | | - InstanceInfo.of(instanceId, machineTypeId, attachedDisk, networkInterface); |
91 | | - operation = compute.create(instance); |
92 | | - // Wait for operation to complete |
93 | | - operation = operation.waitFor(); |
94 | | - if (operation.getErrors() == null) { |
95 | | - System.out.println("Instance " + instanceId + " was successfully created"); |
96 | | - } else { |
97 | | - // inspect operation.getErrors() |
98 | | - throw new RuntimeException("Instance creation failed"); |
| 83 | + try (InstanceClient instanceClient = InstanceClient.create()) { |
| 84 | + String machineType = |
| 85 | + ProjectZoneMachineTypeName.of("n1-standard-1", DEFAULT_PROJECT, ZONE).toString(); |
| 86 | + AttachedDisk disk = |
| 87 | + AttachedDisk.newBuilder() |
| 88 | + .setBoot(true) |
| 89 | + .setAutoDelete(true) |
| 90 | + .setType("PERSISTENT") |
| 91 | + .setDiskSizeGb("10") |
| 92 | + .setInitializeParams( |
| 93 | + AttachedDiskInitializeParams.newBuilder().setSourceImage(DEFAULT_IMAGE).build()) |
| 94 | + .build(); |
| 95 | + NetworkInterface networkInterface = NetworkInterface.newBuilder().setName("default").build(); |
| 96 | + Instance instanceResource = |
| 97 | + Instance.newBuilder() |
| 98 | + .setName("test-instance") |
| 99 | + .setMachineType(machineType) |
| 100 | + .addDisks(disk) |
| 101 | + .addNetworkInterfaces(networkInterface) |
| 102 | + .build(); |
| 103 | + Operation response = instanceClient.insertInstance(FORMATTED_ZONE, instanceResource); |
| 104 | + if (response.getError() == null) { |
| 105 | + System.out.println("Instance " + instanceResource.getName() + " was successfully created"); |
| 106 | + } else { |
| 107 | + // inspect operation.getErrors() |
| 108 | + throw new RuntimeException("Instance creation failed"); |
| 109 | + } |
99 | 110 | } |
100 | 111 | } |
101 | 112 | } |
0 commit comments