Skip to content

Commit 0325488

Browse files
authored
Update READMEs for Compute (#3388)
Update READMEs and URL links. Add ComputeExample.java which contains working client code for the Compute client.
1 parent 60471ba commit 0325488

File tree

5 files changed

+203
-92
lines changed

5 files changed

+203
-92
lines changed

README.md

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ This library supports the following Google Cloud Platform services with clients
3737

3838
This library supports the following Google Cloud Platform services with clients at an [Alpha](#versioning) quality level:
3939

40+
- [Cloud Compute](google-cloud-clients/google-cloud-compute) (Alpha)
4041
- [Cloud Dataproc](google-cloud-clients/google-cloud-dataproc) (Alpha)
4142
- [Cloud DNS](google-cloud-clients/google-cloud-dns) (Alpha)
4243
- [Cloud OS Login](google-cloud-clients/google-cloud-os-login) (Alpha)
@@ -46,10 +47,6 @@ This library supports the following Google Cloud Platform services with clients
4647
- [Dialogflow](google-cloud-clients/google-cloud-dialogflow) (Alpha)
4748
- [Web Security Scanner](google-cloud-clients/google-cloud-websecurityscanner) (Alpha)
4849

49-
These libraries are deprecated and no longer receive updates:
50-
51-
- [Cloud Compute](google-cloud-clients/google-cloud-compute) (Deprecated)
52-
5350
Quickstart
5451
----------
5552

google-cloud-clients/google-cloud-compute/README.md

Lines changed: 52 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ Java idiomatic client for [Google Cloud Compute][cloud-compute].
1212
- [Product Documentation][compute-product-docs]
1313
- [Client Library Documentation][compute-client-lib-docs]
1414

15-
> Note: This client is no longer receiving updates; new features in the Compute API will not be added to this client.
1615
Check https://cloud.google.com/compute/docs/api/libraries for the recommended Java client library to use for
1716
accessing Compute.
1817

@@ -89,10 +88,24 @@ These credentials are automatically inferred from your environment, so you only
8988
code to create your service object:
9089

9190
```java
92-
import com.google.cloud.compute.deprecated.Compute;
93-
import com.google.cloud.compute.deprecated.ComputeOptions;
94-
95-
Compute compute = ComputeOptions.getDefaultInstance().getService();
91+
import com.google.api.gax.core.FixedCredentialsProvider;
92+
import com.google.auth.Credentials;
93+
import com.google.auth.oauth2.GoogleCredentials;
94+
import com.google.cloud.compute.v1.AddressClient;
95+
import com.google.cloud.compute.v1.AddressSettings;
96+
97+
Credentials myCredentials = GoogleCredentials.getApplicationDefault();
98+
String myEndpoint = AddressSettings.getDefaultEndpoint();
99+
100+
AddressSettings addressSettings =
101+
AddressSettings.newBuilder()
102+
.setCredentialsProvider(FixedCredentialsProvider.create(myCredentials))
103+
.setTransportChannelProvider(
104+
AddressSettings.defaultHttpJsonTransportProviderBuilder()
105+
.setEndpoint(myEndpoint)
106+
.build())
107+
.build();
108+
return AddressClient.create(addressSettings);
96109
```
97110

98111
For other authentication options, see the [Authentication](https://github.com/GoogleCloudPlatform/google-cloud-java#authentication)
@@ -106,24 +119,29 @@ Engine. In this code snippet, we will create a new external region address.
106119
Add the following imports at the top of your file:
107120

108121
```java
109-
import com.google.cloud.compute.deprecated.AddressInfo;
110-
import com.google.cloud.compute.deprecated.Operation;
111-
import com.google.cloud.compute.deprecated.RegionAddressId;
122+
import com.google.cloud.compute.v1.InsertAddressHttpRequest;
123+
import com.google.cloud.compute.v1.Operation;
124+
import com.google.cloud.compute.v1.ProjectRegionAddressName;
112125
```
113126

114127
Then add the following code to create an address. Most Compute Engine calls return an `Operation`
115128
object that can be used to wait for operation completion and to check whether operation failed or
116129
succeeded:
117130

118131
```java
119-
RegionAddressId addressId = RegionAddressId.of("us-central1", "test-address");
120-
Operation operation = compute.create(AddressInfo.of(addressId));
121-
// Wait for operation to complete
122-
operation = operation.waitFor();
123-
if (operation.getErrors() == null) {
132+
ProjectRegionName region = ProjectRegionName.of(PROJECT_NAME, REGION);
133+
Address address = Address.newBuilder().build();
134+
InsertAddressHttpRequest request =
135+
InsertAddressHttpRequest.newBuilder()
136+
.setRegion(region.toString())
137+
.setAddressResource(address)
138+
.build();
139+
140+
Operation response = client.insertAddress(request);
141+
if (operation.getError() == null) {
124142
System.out.println("Address " + addressId + " was successfully created");
125143
} else {
126-
// inspect operation.getErrors()
144+
// inspect operation.getError()
127145
throw new RuntimeException("Address creation failed");
128146
}
129147
```
@@ -138,94 +156,42 @@ a publicly-available image.
138156
Add the following imports at the top of your file:
139157

140158
```java
141-
import com.google.cloud.compute.deprecated.DiskInfo;
142-
import com.google.cloud.compute.deprecated.DiskId;
143-
import com.google.cloud.compute.deprecated.ImageDiskConfiguration;
144-
import com.google.cloud.compute.deprecated.ImageId;
159+
import com.google.api.core.ApiFuture;
160+
import com.google.cloud.compute.v1.Disk;
161+
import com.google.cloud.compute.v1.DiskClient;
162+
import com.google.cloud.compute.v1.InsertDiskHttpRequest;
163+
import com.google.cloud.compute.v1.Operation;
164+
import com.google.cloud.compute.v1.ProjectZoneName;
145165
```
146166

147167
Then add the following code to create a disk and wait for disk creation to terminate.
148168

149169
```java
150-
ImageId imageId = ImageId.of("debian-cloud", "debian-8-jessie-v20160329");
151-
DiskId diskId = DiskId.of("us-central1-a", "test-disk");
152-
ImageDiskConfiguration diskConfiguration = ImageDiskConfiguration.of(imageId);
153-
DiskInfo disk = DiskInfo.of(diskId, diskConfiguration);
154-
Operation operation = compute.create(disk);
155-
// Wait for operation to complete
156-
operation = operation.waitFor();
157-
if (operation.getErrors() == null) {
158-
System.out.println("Disk " + diskId + " was successfully created");
159-
} else {
160-
// inspect operation.getErrors()
170+
ProjectZoneName zone = ProjectZoneName.of("[PROJECT]", "[ZONE]");
171+
Disk diskResource = Disk.newBuilder().build();
172+
InsertDiskHttpRequest request = InsertDiskHttpRequest.newBuilder()
173+
.setZone(zone.toString())
174+
.setDiskResource(diskResource)
175+
.build();
176+
ApiFuture<Operation> future = client.insertDiskCallable().futureCall(request);
177+
Operation response;
178+
try {
179+
response = future.get();
180+
} catch (InterruptedException | ExecutionException e) {
181+
// inspect operation.getError()
161182
throw new RuntimeException("Disk creation failed");
162183
}
163184
```
164185

165-
#### Creating a virtual machine instance
166-
A Google Compute Engine instance is a virtual machine (VM) hosted on Google's infrastructure. An
167-
instance can be created given its identity, a machine type, one boot disk and a network interface.
168-
In this code snippet, we will create a virtual machine instance in the default network using as a
169-
boot disk the disk we have just created and assigning to it the just created IP address.
170-
171-
Add the following imports at the top of your file:
172-
173-
```java
174-
import com.google.cloud.compute.deprecated.AttachedDisk;
175-
import com.google.cloud.compute.deprecated.AttachedDisk.PersistentDiskConfiguration;
176-
import com.google.cloud.compute.deprecated.InstanceId;
177-
import com.google.cloud.compute.deprecated.InstanceInfo;
178-
import com.google.cloud.compute.deprecated.MachineTypeId;
179-
import com.google.cloud.compute.deprecated.NetworkConfiguration;
180-
import com.google.cloud.compute.deprecated.NetworkConfiguration.AccessConfig;
181-
import com.google.cloud.compute.deprecated.NetworkId;
182-
import com.google.cloud.compute.deprecated.NetworkInterface;
183-
```
184-
185-
Then add the following code to create an instance and wait for instance creation to terminate.
186-
187-
```java
188-
Address externalIp = compute.getAddress(addressId);
189-
InstanceId instanceId = InstanceId.of("us-central1-a", "test-instance");
190-
NetworkId networkId = NetworkId.of("default");
191-
PersistentDiskConfiguration attachConfiguration =
192-
PersistentDiskConfiguration.newBuilder(diskId).setBoot(true).build();
193-
AttachedDisk attachedDisk = AttachedDisk.of("dev0", attachConfiguration);
194-
NetworkInterface networkInterface = NetworkInterface.newBuilder(networkId)
195-
.setAccessConfigurations(AccessConfig.of(externalIp.getAddress()))
196-
.build();
197-
MachineTypeId machineTypeId = MachineTypeId.of("us-central1-a", "n1-standard-1");
198-
InstanceInfo instance =
199-
InstanceInfo.of(instanceId, machineTypeId, attachedDisk, networkInterface);
200-
Operation operation = compute.create(instance);
201-
// Wait for operation to complete
202-
operation = operation.waitFor();
203-
if (operation.getErrors() == null) {
204-
System.out.println("Instance " + instanceId + " was successfully created");
205-
} else {
206-
// inspect operation.getErrors()
207-
throw new RuntimeException("Instance creation failed");
208-
}
209-
```
210-
211186
#### Complete source code
212187

213188
In
214-
[CreateAddressDiskAndInstance.java](../../google-cloud-examples/src/main/java/com/google/cloud/examples/compute/snippets/CreateAddressDiskAndInstance.java)
189+
[ComputeExample.java](../../google-cloud-examples/src/main/java/com/google/cloud/examples/compute/v1/ComputeExample.java)
215190
we put together all the code shown above into one program. The program assumes that you are
216191
running on Compute Engine or from your own desktop. To run the example on App Engine, simply move
217192
the code from the main method to your application's servlet class and change the print statements to
218193
display on your webpage.
219194

220-
#### Other examples
221-
222-
Other examples are available too:
223-
224-
- [CreateSnapshot.java](../../google-cloud-examples/src/main/java/com/google/cloud/examples/compute/snippets/CreateSnapshot.java) shows
225-
how to create a snapshot from an existing disk
226-
- [CreateInstance.java](../../google-cloud-examples/src/main/java/com/google/cloud/examples/compute/snippets/CreateInstance.java) shows
227-
how to create a virtual machine instance (shorter sample than the one above)
228-
229195
Troubleshooting
230196
---------------
231197

@@ -276,4 +242,4 @@ Apache 2.0 - See [LICENSE] for more information.
276242

277243
[cloud-compute]: https://cloud.google.com/compute/
278244
[compute-product-docs]: https://cloud.google.com/compute/docs/
279-
[compute-client-lib-docs]: https://googlecloudplatform.github.io/google-cloud-java/google-cloud-clients/apidocs/index.html?com/google/cloud/compute/deprecated/package-summary.html
245+
[compute-client-lib-docs]: https://googlecloudplatform.github.io/google-cloud-java/google-cloud-clients/apidocs/index.html?com/google/cloud/compute/v1/package-summary.html

google-cloud-examples/pom.xml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,11 @@
134134
<name>BigQueryExample</name>
135135
</program>
136136
<program>
137-
<mainClass>com.google.cloud.examples.compute.ComputeExample</mainClass>
137+
<mainClass>com.google.cloud.examples.compute.deprecated.ComputeExample</mainClass>
138+
<name>ComputeExample</name>
139+
</program>
140+
<program>
141+
<mainClass>com.google.cloud.examples.compute.v1.ComputeExample</mainClass>
138142
<name>ComputeExample</name>
139143
</program>
140144
<program>

google-cloud-examples/src/main/java/com/google/cloud/examples/compute/ComputeExample.java renamed to google-cloud-examples/src/main/java/com/google/cloud/examples/compute/deprecated/ComputeExample.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
package com.google.cloud.examples.compute;
17+
package com.google.cloud.examples.compute.deprecated;
1818

1919
import com.google.api.gax.paging.Page;
2020
import com.google.cloud.Tuple;
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
package com.google.cloud.examples.compute.v1;
2+
3+
import com.google.api.core.ApiFuture;
4+
import com.google.api.gax.core.FixedCredentialsProvider;
5+
import com.google.auth.Credentials;
6+
import com.google.auth.oauth2.GoogleCredentials;
7+
import com.google.cloud.compute.v1.Address;
8+
import com.google.cloud.compute.v1.AddressClient;
9+
import com.google.cloud.compute.v1.AddressSettings;
10+
import com.google.cloud.compute.v1.InsertAddressHttpRequest;
11+
import com.google.cloud.compute.v1.ListAddressesHttpRequest;
12+
import com.google.cloud.compute.v1.Operation;
13+
import com.google.cloud.compute.v1.ProjectRegionAddressName;
14+
import com.google.cloud.compute.v1.ProjectRegionName;
15+
import java.io.IOException;
16+
import java.util.concurrent.ExecutionException;
17+
import java.util.concurrent.TimeUnit;
18+
19+
/** Working example code to make live calls on Addresses resources in a GCP Compute project. */
20+
public class ComputeExample {
21+
22+
// Replace the following String values with your Project and Region ids.
23+
private static String PROJECT_NAME = "my-project-id";
24+
private static String REGION = "us-central1";
25+
26+
/**
27+
* List addresses, Insert an address, and then delete the address. Use ResourceNames in the
28+
* request objects.
29+
*/
30+
public static void main(String[] args) throws IOException {
31+
AddressClient client = createCredentialedClient();
32+
33+
System.out.println("Running with Gapic Client.");
34+
AddressClient.ListAddressesPagedResponse listResponse = listAddresses(client);
35+
verifyListAddressWithGets(client, listResponse);
36+
37+
System.out.println("Running with Gapic Client and Resource Name.");
38+
String newAddressName = "new_address_name";
39+
System.out.println("Inserting address:");
40+
41+
insertNewAddressJustClient(client, newAddressName);
42+
43+
listAddresses(client);
44+
45+
System.out.println("Deleting address:");
46+
Operation deleteResponse =
47+
client.deleteAddress(ProjectRegionAddressName.of(newAddressName, PROJECT_NAME, REGION));
48+
System.out.format("Result of delete: %s\n", deleteResponse.toString());
49+
int sleepTimeInSeconds = 3;
50+
System.out.format("Waiting %d seconds for server to update...\n", sleepTimeInSeconds);
51+
// Wait for the delete operation to finish on the server.
52+
try {
53+
TimeUnit.SECONDS.sleep(sleepTimeInSeconds);
54+
} catch (InterruptedException e) {
55+
e.printStackTrace();
56+
}
57+
listAddresses(client);
58+
}
59+
60+
private static AddressClient createCredentialedClient() throws IOException {
61+
Credentials myCredentials = GoogleCredentials.getApplicationDefault();
62+
String myEndpoint = AddressSettings.getDefaultEndpoint();
63+
64+
AddressSettings addressSettings =
65+
AddressSettings.newBuilder()
66+
.setCredentialsProvider(FixedCredentialsProvider.create(myCredentials))
67+
.setTransportChannelProvider(
68+
AddressSettings.defaultHttpJsonTransportProviderBuilder()
69+
.setEndpoint(myEndpoint)
70+
.build())
71+
.build();
72+
return AddressClient.create(addressSettings);
73+
}
74+
75+
private static void insertNewAddressJustClient(AddressClient client, String newAddressName) {
76+
// Begin samplegen code for insertAddress().
77+
Address newAddress = Address.newBuilder().setName(newAddressName).build();
78+
ProjectRegionName region = ProjectRegionName.of(PROJECT_NAME, REGION);
79+
Operation response = client.insertAddress(region, newAddress);
80+
// End samplegen code for insertAddress().
81+
System.out.format("Result of insert: %s\n", response.toString());
82+
}
83+
84+
/** Use an InsertAddressHttpRequest object to make an addresses.insert method call. */
85+
private static void insertNewAddressUsingRequest(AddressClient client, String newAddressName)
86+
throws InterruptedException, ExecutionException {
87+
// Begin samplegen code for insertAddress().
88+
ProjectRegionName region = ProjectRegionName.of(PROJECT_NAME, REGION);
89+
Address address = Address.newBuilder().build();
90+
InsertAddressHttpRequest request =
91+
InsertAddressHttpRequest.newBuilder()
92+
.setRegion(region.toString())
93+
.setAddressResource(address)
94+
.build();
95+
// Do something
96+
Operation response = client.insertAddress(request);
97+
98+
// End samplegen code for insertAddress().
99+
System.out.format("Result of insert: %s\n", response.toString());
100+
}
101+
102+
/** Use an callable object to make an addresses.insert method call. */
103+
private static void insertAddressUsingCallable(AddressClient client, String newAddressName)
104+
throws InterruptedException, ExecutionException {
105+
// Begin samplegen code for insertAddress().
106+
ProjectRegionName region = ProjectRegionName.of(PROJECT_NAME, REGION);
107+
Address address = Address.newBuilder().build();
108+
InsertAddressHttpRequest request =
109+
InsertAddressHttpRequest.newBuilder()
110+
.setRegion(region.toString())
111+
.setAddressResource(address)
112+
.build();
113+
ApiFuture<Operation> future = client.insertAddressCallable().futureCall(request);
114+
// Do something
115+
Operation response = future.get();
116+
117+
// End samplegen code for insertAddress().
118+
System.out.format("Result of insert: %s\n", response.toString());
119+
}
120+
121+
/** List Addresses in the under the project PROJECT_NAME and region REGION. */
122+
private static AddressClient.ListAddressesPagedResponse listAddresses(AddressClient client) {
123+
System.out.println("Listing addresses:");
124+
ProjectRegionName regionName =
125+
ProjectRegionName.newBuilder().setRegion(REGION).setProject(PROJECT_NAME).build();
126+
ListAddressesHttpRequest listRequest =
127+
ListAddressesHttpRequest.newBuilder().setRegion(regionName.toString()).build();
128+
AddressClient.ListAddressesPagedResponse response = client.listAddresses(listRequest);
129+
for (Address address : response.iterateAll()) {
130+
System.out.println("\t - " + address.toString());
131+
}
132+
return response;
133+
}
134+
135+
private static void verifyListAddressWithGets(
136+
AddressClient client, AddressClient.ListAddressesPagedResponse listResponse) {
137+
for (Address address : listResponse.iterateAll()) {
138+
System.out.format("Making get request for address \"%s\"...\n", address.getName());
139+
Address fetchedAddress =
140+
client.getAddress(ProjectRegionAddressName.of(address.getName(), PROJECT_NAME, REGION));
141+
System.out.format("addresses.get returns \n\t%s\n\n", fetchedAddress.toString());
142+
}
143+
}
144+
}

0 commit comments

Comments
 (0)