@@ -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.
1615Check https://cloud.google.com/compute/docs/api/libraries for the recommended Java client library to use for
1716accessing Compute.
1817
@@ -89,10 +88,24 @@ These credentials are automatically inferred from your environment, so you only
8988code 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
98111For 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.
106119Add 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
114127Then add the following code to create an address. Most Compute Engine calls return an ` Operation `
115128object that can be used to wait for operation completion and to check whether operation failed or
116129succeeded:
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.
138156Add 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
147167Then 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
213188In
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 )
215190we put together all the code shown above into one program. The program assumes that you are
216191running on Compute Engine or from your own desktop. To run the example on App Engine, simply move
217192the code from the main method to your application's servlet class and change the print statements to
218193display 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-
229195Troubleshooting
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
0 commit comments