• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2022 Google LLC
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      https://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.google.cloud.compute.v1;
18 
19 import com.google.api.core.ApiFuture;
20 import com.google.api.core.ApiFutures;
21 import com.google.api.gax.core.BackgroundResource;
22 import com.google.api.gax.paging.AbstractFixedSizeCollection;
23 import com.google.api.gax.paging.AbstractPage;
24 import com.google.api.gax.paging.AbstractPagedListResponse;
25 import com.google.api.gax.rpc.PageContext;
26 import com.google.api.gax.rpc.UnaryCallable;
27 import com.google.cloud.compute.v1.stub.ZonesStub;
28 import com.google.cloud.compute.v1.stub.ZonesStubSettings;
29 import com.google.common.util.concurrent.MoreExecutors;
30 import java.io.IOException;
31 import java.util.List;
32 import java.util.concurrent.TimeUnit;
33 import javax.annotation.Generated;
34 
35 // AUTO-GENERATED DOCUMENTATION AND CLASS.
36 /**
37  * Service Description: The Zones API.
38  *
39  * <p>This class provides the ability to make remote calls to the backing service through method
40  * calls that map to API methods. Sample code to get started:
41  *
42  * <pre>{@code
43  * // This snippet has been automatically generated and should be regarded as a code template only.
44  * // It will require modifications to work:
45  * // - It may require correct/in-range values for request initialization.
46  * // - It may require specifying regional endpoints when creating the service client as shown in
47  * // https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library
48  * try (ZonesClient zonesClient = ZonesClient.create()) {
49  *   String project = "project-309310695";
50  *   String zone = "zone3744684";
51  *   Zone response = zonesClient.get(project, zone);
52  * }
53  * }</pre>
54  *
55  * <p>Note: close() needs to be called on the ZonesClient object to clean up resources such as
56  * threads. In the example above, try-with-resources is used, which automatically calls close().
57  *
58  * <p>The surface of this class includes several types of Java methods for each of the API's
59  * methods:
60  *
61  * <ol>
62  *   <li>A "flattened" method. With this type of method, the fields of the request type have been
63  *       converted into function parameters. It may be the case that not all fields are available as
64  *       parameters, and not every API method will have a flattened method entry point.
65  *   <li>A "request object" method. This type of method only takes one parameter, a request object,
66  *       which must be constructed before the call. Not every API method will have a request object
67  *       method.
68  *   <li>A "callable" method. This type of method takes no parameters and returns an immutable API
69  *       callable object, which can be used to initiate calls to the service.
70  * </ol>
71  *
72  * <p>See the individual methods for example code.
73  *
74  * <p>Many parameters require resource names to be formatted in a particular way. To assist with
75  * these names, this class includes a format method for each type of name, and additionally a parse
76  * method to extract the individual identifiers contained within names that are returned.
77  *
78  * <p>This class can be customized by passing in a custom instance of ZonesSettings to create(). For
79  * example:
80  *
81  * <p>To customize credentials:
82  *
83  * <pre>{@code
84  * // This snippet has been automatically generated and should be regarded as a code template only.
85  * // It will require modifications to work:
86  * // - It may require correct/in-range values for request initialization.
87  * // - It may require specifying regional endpoints when creating the service client as shown in
88  * // https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library
89  * ZonesSettings zonesSettings =
90  *     ZonesSettings.newBuilder()
91  *         .setCredentialsProvider(FixedCredentialsProvider.create(myCredentials))
92  *         .build();
93  * ZonesClient zonesClient = ZonesClient.create(zonesSettings);
94  * }</pre>
95  *
96  * <p>To customize the endpoint:
97  *
98  * <pre>{@code
99  * // This snippet has been automatically generated and should be regarded as a code template only.
100  * // It will require modifications to work:
101  * // - It may require correct/in-range values for request initialization.
102  * // - It may require specifying regional endpoints when creating the service client as shown in
103  * // https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library
104  * ZonesSettings zonesSettings = ZonesSettings.newBuilder().setEndpoint(myEndpoint).build();
105  * ZonesClient zonesClient = ZonesClient.create(zonesSettings);
106  * }</pre>
107  *
108  * <p>Please refer to the GitHub repository's samples for more quickstart code snippets.
109  */
110 @Generated("by gapic-generator-java")
111 public class ZonesClient implements BackgroundResource {
112   private final ZonesSettings settings;
113   private final ZonesStub stub;
114 
115   /** Constructs an instance of ZonesClient with default settings. */
create()116   public static final ZonesClient create() throws IOException {
117     return create(ZonesSettings.newBuilder().build());
118   }
119 
120   /**
121    * Constructs an instance of ZonesClient, using the given settings. The channels are created based
122    * on the settings passed in, or defaults for any settings that are not set.
123    */
create(ZonesSettings settings)124   public static final ZonesClient create(ZonesSettings settings) throws IOException {
125     return new ZonesClient(settings);
126   }
127 
128   /**
129    * Constructs an instance of ZonesClient, using the given stub for making calls. This is for
130    * advanced usage - prefer using create(ZonesSettings).
131    */
create(ZonesStub stub)132   public static final ZonesClient create(ZonesStub stub) {
133     return new ZonesClient(stub);
134   }
135 
136   /**
137    * Constructs an instance of ZonesClient, using the given settings. This is protected so that it
138    * is easy to make a subclass, but otherwise, the static factory methods should be preferred.
139    */
ZonesClient(ZonesSettings settings)140   protected ZonesClient(ZonesSettings settings) throws IOException {
141     this.settings = settings;
142     this.stub = ((ZonesStubSettings) settings.getStubSettings()).createStub();
143   }
144 
ZonesClient(ZonesStub stub)145   protected ZonesClient(ZonesStub stub) {
146     this.settings = null;
147     this.stub = stub;
148   }
149 
getSettings()150   public final ZonesSettings getSettings() {
151     return settings;
152   }
153 
getStub()154   public ZonesStub getStub() {
155     return stub;
156   }
157 
158   // AUTO-GENERATED DOCUMENTATION AND METHOD.
159   /**
160    * Returns the specified Zone resource.
161    *
162    * <p>Sample code:
163    *
164    * <pre>{@code
165    * // This snippet has been automatically generated and should be regarded as a code template only.
166    * // It will require modifications to work:
167    * // - It may require correct/in-range values for request initialization.
168    * // - It may require specifying regional endpoints when creating the service client as shown in
169    * // https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library
170    * try (ZonesClient zonesClient = ZonesClient.create()) {
171    *   String project = "project-309310695";
172    *   String zone = "zone3744684";
173    *   Zone response = zonesClient.get(project, zone);
174    * }
175    * }</pre>
176    *
177    * @param project Project ID for this request.
178    * @param zone Name of the zone resource to return.
179    * @throws com.google.api.gax.rpc.ApiException if the remote call fails
180    */
get(String project, String zone)181   public final Zone get(String project, String zone) {
182     GetZoneRequest request = GetZoneRequest.newBuilder().setProject(project).setZone(zone).build();
183     return get(request);
184   }
185 
186   // AUTO-GENERATED DOCUMENTATION AND METHOD.
187   /**
188    * Returns the specified Zone resource.
189    *
190    * <p>Sample code:
191    *
192    * <pre>{@code
193    * // This snippet has been automatically generated and should be regarded as a code template only.
194    * // It will require modifications to work:
195    * // - It may require correct/in-range values for request initialization.
196    * // - It may require specifying regional endpoints when creating the service client as shown in
197    * // https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library
198    * try (ZonesClient zonesClient = ZonesClient.create()) {
199    *   GetZoneRequest request =
200    *       GetZoneRequest.newBuilder()
201    *           .setProject("project-309310695")
202    *           .setZone("zone3744684")
203    *           .build();
204    *   Zone response = zonesClient.get(request);
205    * }
206    * }</pre>
207    *
208    * @param request The request object containing all of the parameters for the API call.
209    * @throws com.google.api.gax.rpc.ApiException if the remote call fails
210    */
get(GetZoneRequest request)211   public final Zone get(GetZoneRequest request) {
212     return getCallable().call(request);
213   }
214 
215   // AUTO-GENERATED DOCUMENTATION AND METHOD.
216   /**
217    * Returns the specified Zone resource.
218    *
219    * <p>Sample code:
220    *
221    * <pre>{@code
222    * // This snippet has been automatically generated and should be regarded as a code template only.
223    * // It will require modifications to work:
224    * // - It may require correct/in-range values for request initialization.
225    * // - It may require specifying regional endpoints when creating the service client as shown in
226    * // https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library
227    * try (ZonesClient zonesClient = ZonesClient.create()) {
228    *   GetZoneRequest request =
229    *       GetZoneRequest.newBuilder()
230    *           .setProject("project-309310695")
231    *           .setZone("zone3744684")
232    *           .build();
233    *   ApiFuture<Zone> future = zonesClient.getCallable().futureCall(request);
234    *   // Do something.
235    *   Zone response = future.get();
236    * }
237    * }</pre>
238    */
getCallable()239   public final UnaryCallable<GetZoneRequest, Zone> getCallable() {
240     return stub.getCallable();
241   }
242 
243   // AUTO-GENERATED DOCUMENTATION AND METHOD.
244   /**
245    * Retrieves the list of Zone resources available to the specified project.
246    *
247    * <p>Sample code:
248    *
249    * <pre>{@code
250    * // This snippet has been automatically generated and should be regarded as a code template only.
251    * // It will require modifications to work:
252    * // - It may require correct/in-range values for request initialization.
253    * // - It may require specifying regional endpoints when creating the service client as shown in
254    * // https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library
255    * try (ZonesClient zonesClient = ZonesClient.create()) {
256    *   String project = "project-309310695";
257    *   for (Zone element : zonesClient.list(project).iterateAll()) {
258    *     // doThingsWith(element);
259    *   }
260    * }
261    * }</pre>
262    *
263    * @param project Project ID for this request.
264    * @throws com.google.api.gax.rpc.ApiException if the remote call fails
265    */
list(String project)266   public final ListPagedResponse list(String project) {
267     ListZonesRequest request = ListZonesRequest.newBuilder().setProject(project).build();
268     return list(request);
269   }
270 
271   // AUTO-GENERATED DOCUMENTATION AND METHOD.
272   /**
273    * Retrieves the list of Zone resources available to the specified project.
274    *
275    * <p>Sample code:
276    *
277    * <pre>{@code
278    * // This snippet has been automatically generated and should be regarded as a code template only.
279    * // It will require modifications to work:
280    * // - It may require correct/in-range values for request initialization.
281    * // - It may require specifying regional endpoints when creating the service client as shown in
282    * // https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library
283    * try (ZonesClient zonesClient = ZonesClient.create()) {
284    *   ListZonesRequest request =
285    *       ListZonesRequest.newBuilder()
286    *           .setFilter("filter-1274492040")
287    *           .setMaxResults(1128457243)
288    *           .setOrderBy("orderBy-1207110587")
289    *           .setPageToken("pageToken873572522")
290    *           .setProject("project-309310695")
291    *           .setReturnPartialSuccess(true)
292    *           .build();
293    *   for (Zone element : zonesClient.list(request).iterateAll()) {
294    *     // doThingsWith(element);
295    *   }
296    * }
297    * }</pre>
298    *
299    * @param request The request object containing all of the parameters for the API call.
300    * @throws com.google.api.gax.rpc.ApiException if the remote call fails
301    */
list(ListZonesRequest request)302   public final ListPagedResponse list(ListZonesRequest request) {
303     return listPagedCallable().call(request);
304   }
305 
306   // AUTO-GENERATED DOCUMENTATION AND METHOD.
307   /**
308    * Retrieves the list of Zone resources available to the specified project.
309    *
310    * <p>Sample code:
311    *
312    * <pre>{@code
313    * // This snippet has been automatically generated and should be regarded as a code template only.
314    * // It will require modifications to work:
315    * // - It may require correct/in-range values for request initialization.
316    * // - It may require specifying regional endpoints when creating the service client as shown in
317    * // https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library
318    * try (ZonesClient zonesClient = ZonesClient.create()) {
319    *   ListZonesRequest request =
320    *       ListZonesRequest.newBuilder()
321    *           .setFilter("filter-1274492040")
322    *           .setMaxResults(1128457243)
323    *           .setOrderBy("orderBy-1207110587")
324    *           .setPageToken("pageToken873572522")
325    *           .setProject("project-309310695")
326    *           .setReturnPartialSuccess(true)
327    *           .build();
328    *   ApiFuture<Zone> future = zonesClient.listPagedCallable().futureCall(request);
329    *   // Do something.
330    *   for (Zone element : future.get().iterateAll()) {
331    *     // doThingsWith(element);
332    *   }
333    * }
334    * }</pre>
335    */
listPagedCallable()336   public final UnaryCallable<ListZonesRequest, ListPagedResponse> listPagedCallable() {
337     return stub.listPagedCallable();
338   }
339 
340   // AUTO-GENERATED DOCUMENTATION AND METHOD.
341   /**
342    * Retrieves the list of Zone resources available to the specified project.
343    *
344    * <p>Sample code:
345    *
346    * <pre>{@code
347    * // This snippet has been automatically generated and should be regarded as a code template only.
348    * // It will require modifications to work:
349    * // - It may require correct/in-range values for request initialization.
350    * // - It may require specifying regional endpoints when creating the service client as shown in
351    * // https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library
352    * try (ZonesClient zonesClient = ZonesClient.create()) {
353    *   ListZonesRequest request =
354    *       ListZonesRequest.newBuilder()
355    *           .setFilter("filter-1274492040")
356    *           .setMaxResults(1128457243)
357    *           .setOrderBy("orderBy-1207110587")
358    *           .setPageToken("pageToken873572522")
359    *           .setProject("project-309310695")
360    *           .setReturnPartialSuccess(true)
361    *           .build();
362    *   while (true) {
363    *     ZoneList response = zonesClient.listCallable().call(request);
364    *     for (Zone element : response.getItemsList()) {
365    *       // doThingsWith(element);
366    *     }
367    *     String nextPageToken = response.getNextPageToken();
368    *     if (!Strings.isNullOrEmpty(nextPageToken)) {
369    *       request = request.toBuilder().setPageToken(nextPageToken).build();
370    *     } else {
371    *       break;
372    *     }
373    *   }
374    * }
375    * }</pre>
376    */
listCallable()377   public final UnaryCallable<ListZonesRequest, ZoneList> listCallable() {
378     return stub.listCallable();
379   }
380 
381   @Override
close()382   public final void close() {
383     stub.close();
384   }
385 
386   @Override
shutdown()387   public void shutdown() {
388     stub.shutdown();
389   }
390 
391   @Override
isShutdown()392   public boolean isShutdown() {
393     return stub.isShutdown();
394   }
395 
396   @Override
isTerminated()397   public boolean isTerminated() {
398     return stub.isTerminated();
399   }
400 
401   @Override
shutdownNow()402   public void shutdownNow() {
403     stub.shutdownNow();
404   }
405 
406   @Override
awaitTermination(long duration, TimeUnit unit)407   public boolean awaitTermination(long duration, TimeUnit unit) throws InterruptedException {
408     return stub.awaitTermination(duration, unit);
409   }
410 
411   public static class ListPagedResponse
412       extends AbstractPagedListResponse<
413           ListZonesRequest, ZoneList, Zone, ListPage, ListFixedSizeCollection> {
414 
createAsync( PageContext<ListZonesRequest, ZoneList, Zone> context, ApiFuture<ZoneList> futureResponse)415     public static ApiFuture<ListPagedResponse> createAsync(
416         PageContext<ListZonesRequest, ZoneList, Zone> context, ApiFuture<ZoneList> futureResponse) {
417       ApiFuture<ListPage> futurePage =
418           ListPage.createEmptyPage().createPageAsync(context, futureResponse);
419       return ApiFutures.transform(
420           futurePage, input -> new ListPagedResponse(input), MoreExecutors.directExecutor());
421     }
422 
ListPagedResponse(ListPage page)423     private ListPagedResponse(ListPage page) {
424       super(page, ListFixedSizeCollection.createEmptyCollection());
425     }
426   }
427 
428   public static class ListPage extends AbstractPage<ListZonesRequest, ZoneList, Zone, ListPage> {
429 
ListPage(PageContext<ListZonesRequest, ZoneList, Zone> context, ZoneList response)430     private ListPage(PageContext<ListZonesRequest, ZoneList, Zone> context, ZoneList response) {
431       super(context, response);
432     }
433 
createEmptyPage()434     private static ListPage createEmptyPage() {
435       return new ListPage(null, null);
436     }
437 
438     @Override
createPage( PageContext<ListZonesRequest, ZoneList, Zone> context, ZoneList response)439     protected ListPage createPage(
440         PageContext<ListZonesRequest, ZoneList, Zone> context, ZoneList response) {
441       return new ListPage(context, response);
442     }
443 
444     @Override
createPageAsync( PageContext<ListZonesRequest, ZoneList, Zone> context, ApiFuture<ZoneList> futureResponse)445     public ApiFuture<ListPage> createPageAsync(
446         PageContext<ListZonesRequest, ZoneList, Zone> context, ApiFuture<ZoneList> futureResponse) {
447       return super.createPageAsync(context, futureResponse);
448     }
449   }
450 
451   public static class ListFixedSizeCollection
452       extends AbstractFixedSizeCollection<
453           ListZonesRequest, ZoneList, Zone, ListPage, ListFixedSizeCollection> {
454 
ListFixedSizeCollection(List<ListPage> pages, int collectionSize)455     private ListFixedSizeCollection(List<ListPage> pages, int collectionSize) {
456       super(pages, collectionSize);
457     }
458 
createEmptyCollection()459     private static ListFixedSizeCollection createEmptyCollection() {
460       return new ListFixedSizeCollection(null, 0);
461     }
462 
463     @Override
createCollection(List<ListPage> pages, int collectionSize)464     protected ListFixedSizeCollection createCollection(List<ListPage> pages, int collectionSize) {
465       return new ListFixedSizeCollection(pages, collectionSize);
466     }
467   }
468 }
469