Skip to content

Commit b933ca0

Browse files
authored
samples: update deprecated snippets for compute client (googleapis#7278)
1 parent 8f33da6 commit b933ca0

3 files changed

Lines changed: 149 additions & 113 deletions

File tree

google-cloud-examples/src/main/java/com/google/cloud/examples/compute/snippets/CreateAddressDiskAndInstance.java

Lines changed: 80 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -16,86 +16,97 @@
1616

1717
package com.google.cloud.examples.compute.snippets;
1818

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;
3835

3936
/**
4037
* A snippet for Google Cloud Compute Engine showing how to create a disk and an address. The
4138
* snippet also shows how to create a virtual machine instance using the created disk and address.
4239
*/
4340
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";
4448

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+
}
6060
}
61-
6261
// 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+
}
7581
}
76-
7782
// 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+
}
99110
}
100111
}
101112
}

google-cloud-examples/src/main/java/com/google/cloud/examples/compute/snippets/CreateInstance.java

Lines changed: 42 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -16,36 +16,52 @@
1616

1717
package com.google.cloud.examples.compute.snippets;
1818

19-
import com.google.cloud.compute.deprecated.AttachedDisk;
20-
import com.google.cloud.compute.deprecated.Compute;
21-
import com.google.cloud.compute.deprecated.ComputeOptions;
22-
import com.google.cloud.compute.deprecated.ImageId;
23-
import com.google.cloud.compute.deprecated.Instance;
24-
import com.google.cloud.compute.deprecated.InstanceId;
25-
import com.google.cloud.compute.deprecated.InstanceInfo;
26-
import com.google.cloud.compute.deprecated.MachineTypeId;
27-
import com.google.cloud.compute.deprecated.NetworkId;
28-
import com.google.cloud.compute.deprecated.NetworkInterface;
29-
import com.google.cloud.compute.deprecated.Operation;
30-
import java.util.concurrent.TimeoutException;
19+
import com.google.cloud.ServiceOptions;
20+
import com.google.cloud.compute.v1.AttachedDisk;
21+
import com.google.cloud.compute.v1.AttachedDiskInitializeParams;
22+
import com.google.cloud.compute.v1.Instance;
23+
import com.google.cloud.compute.v1.InstanceClient;
24+
import com.google.cloud.compute.v1.NetworkInterface;
25+
import com.google.cloud.compute.v1.Operation;
26+
import com.google.cloud.compute.v1.ProjectZoneMachineTypeName;
27+
import com.google.cloud.compute.v1.ProjectZoneName;
28+
import java.io.IOException;
3129

3230
/** A snippet for Google Cloud Compute Engine showing how to create a virtual machine instance. */
3331
public class CreateInstance {
32+
private static final String ZONE = "us-central1-a";
33+
private static final String DEFAULT_IMAGE =
34+
"https://www.googleapis.com/compute/v1/projects/debian-cloud/global/images/debian-7-wheezy-v20150710";
35+
private static final String DEFAULT_PROJECT = ServiceOptions.getDefaultProjectId();
3436

35-
public static void main(String... args) throws InterruptedException, TimeoutException {
36-
Compute compute = ComputeOptions.getDefaultInstance().getService();
37-
ImageId imageId = ImageId.of("debian-cloud", "debian-8-jessie-v20160329");
38-
NetworkId networkId = NetworkId.of("default");
39-
AttachedDisk attachedDisk = AttachedDisk.of(AttachedDisk.CreateDiskConfiguration.of(imageId));
40-
NetworkInterface networkInterface = NetworkInterface.of(networkId);
41-
InstanceId instanceId = InstanceId.of("us-central1-a", "instance-name");
42-
MachineTypeId machineTypeId = MachineTypeId.of("us-central1-a", "n1-standard-1");
43-
Operation operation =
44-
compute.create(InstanceInfo.of(instanceId, machineTypeId, attachedDisk, networkInterface));
45-
operation = operation.waitFor();
46-
if (operation.getErrors() == null) {
47-
// use instance
48-
Instance instance = compute.getInstance(instanceId);
37+
public static void main(String... args) throws IOException {
38+
try (InstanceClient instanceClient = InstanceClient.create()) {
39+
ProjectZoneName zone = ProjectZoneName.of(DEFAULT_PROJECT, ZONE);
40+
String machineType =
41+
ProjectZoneMachineTypeName.of("n1-standard-1", DEFAULT_PROJECT, ZONE).toString();
42+
AttachedDisk disk =
43+
AttachedDisk.newBuilder()
44+
.setBoot(true)
45+
.setAutoDelete(true)
46+
.setType("PERSISTENT")
47+
.setInitializeParams(
48+
AttachedDiskInitializeParams.newBuilder().setSourceImage(DEFAULT_IMAGE).build())
49+
.build();
50+
NetworkInterface networkInterface = NetworkInterface.newBuilder().setName("default").build();
51+
Instance instanceResource =
52+
Instance.newBuilder()
53+
.setName("instance-name")
54+
.setMachineType(machineType)
55+
.addDisks(disk)
56+
.addNetworkInterfaces(networkInterface)
57+
.build();
58+
Operation response = instanceClient.insertInstance(zone.toString(), instanceResource);
59+
if (response.getError() == null) {
60+
System.out.println("Instance was created successfully");
61+
} else {
62+
// inspect operation.getErrors()
63+
throw new RuntimeException("Instance creation failed");
64+
}
4965
}
5066
}
5167
}

google-cloud-examples/src/main/java/com/google/cloud/examples/compute/snippets/CreateSnapshot.java

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,31 +16,40 @@
1616

1717
package com.google.cloud.examples.compute.snippets;
1818

19-
import com.google.cloud.compute.deprecated.Compute;
20-
import com.google.cloud.compute.deprecated.ComputeOptions;
21-
import com.google.cloud.compute.deprecated.Disk;
22-
import com.google.cloud.compute.deprecated.DiskId;
23-
import com.google.cloud.compute.deprecated.Operation;
24-
import com.google.cloud.compute.deprecated.Snapshot;
25-
import java.util.concurrent.TimeoutException;
19+
import com.google.cloud.ServiceOptions;
20+
import com.google.cloud.compute.v1.CreateSnapshotDiskHttpRequest;
21+
import com.google.cloud.compute.v1.DiskClient;
22+
import com.google.cloud.compute.v1.Operation;
23+
import com.google.cloud.compute.v1.ProjectZoneDiskName;
24+
import com.google.cloud.compute.v1.Snapshot;
25+
import java.io.IOException;
2626

2727
/**
2828
* A snippet for Google Cloud Compute Engine showing how to create a snapshot of a disk if the disk
2929
* exists.
3030
*/
3131
public class CreateSnapshot {
32+
private static final String DISK_NAME = "test-disk";
33+
private static final String DEFAULT_PROJECT = ServiceOptions.getDefaultProjectId();
34+
private static final String ZONE = "us-central1-a";
35+
private static final ProjectZoneDiskName PROJECT_ZONE_DISK_NAME =
36+
ProjectZoneDiskName.of(DISK_NAME, DEFAULT_PROJECT, ZONE);
3237

33-
public static void main(String... args) throws InterruptedException, TimeoutException {
34-
Compute compute = ComputeOptions.getDefaultInstance().getService();
35-
DiskId diskId = DiskId.of("us-central1-a", "disk-name");
36-
Disk disk = compute.getDisk(diskId, Compute.DiskOption.fields());
37-
if (disk != null) {
38-
String snapshotName = "disk-name-snapshot";
39-
Operation operation = disk.createSnapshot(snapshotName);
40-
operation = operation.waitFor();
41-
if (operation.getErrors() == null) {
42-
// use snapshot
43-
Snapshot snapshot = compute.getSnapshot(snapshotName);
38+
public static void main(String... args) throws IOException {
39+
Snapshot snapshotResource = Snapshot.newBuilder().setName("test-snapshot").build();
40+
CreateSnapshotDiskHttpRequest diskHttpRequest =
41+
CreateSnapshotDiskHttpRequest.newBuilder()
42+
.setDisk(PROJECT_ZONE_DISK_NAME.toString())
43+
.setGuestFlush(Boolean.FALSE)
44+
.setSnapshotResource(snapshotResource)
45+
.build();
46+
try (DiskClient diskClient = DiskClient.create()) {
47+
Operation snapshotDisk = diskClient.createSnapshotDisk(diskHttpRequest);
48+
if (snapshotDisk.getError() == null) {
49+
System.out.println("Snapshot was successfully created");
50+
} else {
51+
// inspect operation.getErrors()
52+
throw new RuntimeException("Snapshot creation failed");
4453
}
4554
}
4655
}

0 commit comments

Comments
 (0)