• 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.RegionsStub;
28 import com.google.cloud.compute.v1.stub.RegionsStubSettings;
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 Regions 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 (RegionsClient regionsClient = RegionsClient.create()) {
49  *   String project = "project-309310695";
50  *   String region = "region-934795532";
51  *   Region response = regionsClient.get(project, region);
52  * }
53  * }</pre>
54  *
55  * <p>Note: close() needs to be called on the RegionsClient 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 RegionsSettings to create().
79  * For 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  * RegionsSettings regionsSettings =
90  *     RegionsSettings.newBuilder()
91  *         .setCredentialsProvider(FixedCredentialsProvider.create(myCredentials))
92  *         .build();
93  * RegionsClient regionsClient = RegionsClient.create(regionsSettings);
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  * RegionsSettings regionsSettings = RegionsSettings.newBuilder().setEndpoint(myEndpoint).build();
105  * RegionsClient regionsClient = RegionsClient.create(regionsSettings);
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 RegionsClient implements BackgroundResource {
112   private final RegionsSettings settings;
113   private final RegionsStub stub;
114 
115   /** Constructs an instance of RegionsClient with default settings. */
create()116   public static final RegionsClient create() throws IOException {
117     return create(RegionsSettings.newBuilder().build());
118   }
119 
120   /**
121    * Constructs an instance of RegionsClient, using the given settings. The channels are created
122    * based on the settings passed in, or defaults for any settings that are not set.
123    */
create(RegionsSettings settings)124   public static final RegionsClient create(RegionsSettings settings) throws IOException {
125     return new RegionsClient(settings);
126   }
127 
128   /**
129    * Constructs an instance of RegionsClient, using the given stub for making calls. This is for
130    * advanced usage - prefer using create(RegionsSettings).
131    */
create(RegionsStub stub)132   public static final RegionsClient create(RegionsStub stub) {
133     return new RegionsClient(stub);
134   }
135 
136   /**
137    * Constructs an instance of RegionsClient, 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    */
RegionsClient(RegionsSettings settings)140   protected RegionsClient(RegionsSettings settings) throws IOException {
141     this.settings = settings;
142     this.stub = ((RegionsStubSettings) settings.getStubSettings()).createStub();
143   }
144 
RegionsClient(RegionsStub stub)145   protected RegionsClient(RegionsStub stub) {
146     this.settings = null;
147     this.stub = stub;
148   }
149 
getSettings()150   public final RegionsSettings getSettings() {
151     return settings;
152   }
153 
getStub()154   public RegionsStub getStub() {
155     return stub;
156   }
157 
158   // AUTO-GENERATED DOCUMENTATION AND METHOD.
159   /**
160    * Returns the specified Region resource. To decrease latency for this method, you can optionally
161    * omit any unneeded information from the response by using a field mask. This practice is
162    * especially recommended for unused quota information (the `quotas` field). To exclude one or
163    * more fields, set your request's `fields` query parameter to only include the fields you need.
164    * For example, to only include the `id` and `selfLink` fields, add the query parameter
165    * `?fields=id,selfLink` to your request.
166    *
167    * <p>Sample code:
168    *
169    * <pre>{@code
170    * // This snippet has been automatically generated and should be regarded as a code template only.
171    * // It will require modifications to work:
172    * // - It may require correct/in-range values for request initialization.
173    * // - It may require specifying regional endpoints when creating the service client as shown in
174    * // https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library
175    * try (RegionsClient regionsClient = RegionsClient.create()) {
176    *   String project = "project-309310695";
177    *   String region = "region-934795532";
178    *   Region response = regionsClient.get(project, region);
179    * }
180    * }</pre>
181    *
182    * @param project Project ID for this request.
183    * @param region Name of the region resource to return.
184    * @throws com.google.api.gax.rpc.ApiException if the remote call fails
185    */
get(String project, String region)186   public final Region get(String project, String region) {
187     GetRegionRequest request =
188         GetRegionRequest.newBuilder().setProject(project).setRegion(region).build();
189     return get(request);
190   }
191 
192   // AUTO-GENERATED DOCUMENTATION AND METHOD.
193   /**
194    * Returns the specified Region resource. To decrease latency for this method, you can optionally
195    * omit any unneeded information from the response by using a field mask. This practice is
196    * especially recommended for unused quota information (the `quotas` field). To exclude one or
197    * more fields, set your request's `fields` query parameter to only include the fields you need.
198    * For example, to only include the `id` and `selfLink` fields, add the query parameter
199    * `?fields=id,selfLink` to your request.
200    *
201    * <p>Sample code:
202    *
203    * <pre>{@code
204    * // This snippet has been automatically generated and should be regarded as a code template only.
205    * // It will require modifications to work:
206    * // - It may require correct/in-range values for request initialization.
207    * // - It may require specifying regional endpoints when creating the service client as shown in
208    * // https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library
209    * try (RegionsClient regionsClient = RegionsClient.create()) {
210    *   GetRegionRequest request =
211    *       GetRegionRequest.newBuilder()
212    *           .setProject("project-309310695")
213    *           .setRegion("region-934795532")
214    *           .build();
215    *   Region response = regionsClient.get(request);
216    * }
217    * }</pre>
218    *
219    * @param request The request object containing all of the parameters for the API call.
220    * @throws com.google.api.gax.rpc.ApiException if the remote call fails
221    */
get(GetRegionRequest request)222   public final Region get(GetRegionRequest request) {
223     return getCallable().call(request);
224   }
225 
226   // AUTO-GENERATED DOCUMENTATION AND METHOD.
227   /**
228    * Returns the specified Region resource. To decrease latency for this method, you can optionally
229    * omit any unneeded information from the response by using a field mask. This practice is
230    * especially recommended for unused quota information (the `quotas` field). To exclude one or
231    * more fields, set your request's `fields` query parameter to only include the fields you need.
232    * For example, to only include the `id` and `selfLink` fields, add the query parameter
233    * `?fields=id,selfLink` to your request.
234    *
235    * <p>Sample code:
236    *
237    * <pre>{@code
238    * // This snippet has been automatically generated and should be regarded as a code template only.
239    * // It will require modifications to work:
240    * // - It may require correct/in-range values for request initialization.
241    * // - It may require specifying regional endpoints when creating the service client as shown in
242    * // https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library
243    * try (RegionsClient regionsClient = RegionsClient.create()) {
244    *   GetRegionRequest request =
245    *       GetRegionRequest.newBuilder()
246    *           .setProject("project-309310695")
247    *           .setRegion("region-934795532")
248    *           .build();
249    *   ApiFuture<Region> future = regionsClient.getCallable().futureCall(request);
250    *   // Do something.
251    *   Region response = future.get();
252    * }
253    * }</pre>
254    */
getCallable()255   public final UnaryCallable<GetRegionRequest, Region> getCallable() {
256     return stub.getCallable();
257   }
258 
259   // AUTO-GENERATED DOCUMENTATION AND METHOD.
260   /**
261    * Retrieves the list of region resources available to the specified project. To decrease latency
262    * for this method, you can optionally omit any unneeded information from the response by using a
263    * field mask. This practice is especially recommended for unused quota information (the
264    * `items.quotas` field). To exclude one or more fields, set your request's `fields` query
265    * parameter to only include the fields you need. For example, to only include the `id` and
266    * `selfLink` fields, add the query parameter `?fields=id,selfLink` to your request.
267    *
268    * <p>Sample code:
269    *
270    * <pre>{@code
271    * // This snippet has been automatically generated and should be regarded as a code template only.
272    * // It will require modifications to work:
273    * // - It may require correct/in-range values for request initialization.
274    * // - It may require specifying regional endpoints when creating the service client as shown in
275    * // https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library
276    * try (RegionsClient regionsClient = RegionsClient.create()) {
277    *   String project = "project-309310695";
278    *   for (Region element : regionsClient.list(project).iterateAll()) {
279    *     // doThingsWith(element);
280    *   }
281    * }
282    * }</pre>
283    *
284    * @param project Project ID for this request.
285    * @throws com.google.api.gax.rpc.ApiException if the remote call fails
286    */
list(String project)287   public final ListPagedResponse list(String project) {
288     ListRegionsRequest request = ListRegionsRequest.newBuilder().setProject(project).build();
289     return list(request);
290   }
291 
292   // AUTO-GENERATED DOCUMENTATION AND METHOD.
293   /**
294    * Retrieves the list of region resources available to the specified project. To decrease latency
295    * for this method, you can optionally omit any unneeded information from the response by using a
296    * field mask. This practice is especially recommended for unused quota information (the
297    * `items.quotas` field). To exclude one or more fields, set your request's `fields` query
298    * parameter to only include the fields you need. For example, to only include the `id` and
299    * `selfLink` fields, add the query parameter `?fields=id,selfLink` to your request.
300    *
301    * <p>Sample code:
302    *
303    * <pre>{@code
304    * // This snippet has been automatically generated and should be regarded as a code template only.
305    * // It will require modifications to work:
306    * // - It may require correct/in-range values for request initialization.
307    * // - It may require specifying regional endpoints when creating the service client as shown in
308    * // https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library
309    * try (RegionsClient regionsClient = RegionsClient.create()) {
310    *   ListRegionsRequest request =
311    *       ListRegionsRequest.newBuilder()
312    *           .setFilter("filter-1274492040")
313    *           .setMaxResults(1128457243)
314    *           .setOrderBy("orderBy-1207110587")
315    *           .setPageToken("pageToken873572522")
316    *           .setProject("project-309310695")
317    *           .setReturnPartialSuccess(true)
318    *           .build();
319    *   for (Region element : regionsClient.list(request).iterateAll()) {
320    *     // doThingsWith(element);
321    *   }
322    * }
323    * }</pre>
324    *
325    * @param request The request object containing all of the parameters for the API call.
326    * @throws com.google.api.gax.rpc.ApiException if the remote call fails
327    */
list(ListRegionsRequest request)328   public final ListPagedResponse list(ListRegionsRequest request) {
329     return listPagedCallable().call(request);
330   }
331 
332   // AUTO-GENERATED DOCUMENTATION AND METHOD.
333   /**
334    * Retrieves the list of region resources available to the specified project. To decrease latency
335    * for this method, you can optionally omit any unneeded information from the response by using a
336    * field mask. This practice is especially recommended for unused quota information (the
337    * `items.quotas` field). To exclude one or more fields, set your request's `fields` query
338    * parameter to only include the fields you need. For example, to only include the `id` and
339    * `selfLink` fields, add the query parameter `?fields=id,selfLink` to your request.
340    *
341    * <p>Sample code:
342    *
343    * <pre>{@code
344    * // This snippet has been automatically generated and should be regarded as a code template only.
345    * // It will require modifications to work:
346    * // - It may require correct/in-range values for request initialization.
347    * // - It may require specifying regional endpoints when creating the service client as shown in
348    * // https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library
349    * try (RegionsClient regionsClient = RegionsClient.create()) {
350    *   ListRegionsRequest request =
351    *       ListRegionsRequest.newBuilder()
352    *           .setFilter("filter-1274492040")
353    *           .setMaxResults(1128457243)
354    *           .setOrderBy("orderBy-1207110587")
355    *           .setPageToken("pageToken873572522")
356    *           .setProject("project-309310695")
357    *           .setReturnPartialSuccess(true)
358    *           .build();
359    *   ApiFuture<Region> future = regionsClient.listPagedCallable().futureCall(request);
360    *   // Do something.
361    *   for (Region element : future.get().iterateAll()) {
362    *     // doThingsWith(element);
363    *   }
364    * }
365    * }</pre>
366    */
listPagedCallable()367   public final UnaryCallable<ListRegionsRequest, ListPagedResponse> listPagedCallable() {
368     return stub.listPagedCallable();
369   }
370 
371   // AUTO-GENERATED DOCUMENTATION AND METHOD.
372   /**
373    * Retrieves the list of region resources available to the specified project. To decrease latency
374    * for this method, you can optionally omit any unneeded information from the response by using a
375    * field mask. This practice is especially recommended for unused quota information (the
376    * `items.quotas` field). To exclude one or more fields, set your request's `fields` query
377    * parameter to only include the fields you need. For example, to only include the `id` and
378    * `selfLink` fields, add the query parameter `?fields=id,selfLink` to your request.
379    *
380    * <p>Sample code:
381    *
382    * <pre>{@code
383    * // This snippet has been automatically generated and should be regarded as a code template only.
384    * // It will require modifications to work:
385    * // - It may require correct/in-range values for request initialization.
386    * // - It may require specifying regional endpoints when creating the service client as shown in
387    * // https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library
388    * try (RegionsClient regionsClient = RegionsClient.create()) {
389    *   ListRegionsRequest request =
390    *       ListRegionsRequest.newBuilder()
391    *           .setFilter("filter-1274492040")
392    *           .setMaxResults(1128457243)
393    *           .setOrderBy("orderBy-1207110587")
394    *           .setPageToken("pageToken873572522")
395    *           .setProject("project-309310695")
396    *           .setReturnPartialSuccess(true)
397    *           .build();
398    *   while (true) {
399    *     RegionList response = regionsClient.listCallable().call(request);
400    *     for (Region element : response.getItemsList()) {
401    *       // doThingsWith(element);
402    *     }
403    *     String nextPageToken = response.getNextPageToken();
404    *     if (!Strings.isNullOrEmpty(nextPageToken)) {
405    *       request = request.toBuilder().setPageToken(nextPageToken).build();
406    *     } else {
407    *       break;
408    *     }
409    *   }
410    * }
411    * }</pre>
412    */
listCallable()413   public final UnaryCallable<ListRegionsRequest, RegionList> listCallable() {
414     return stub.listCallable();
415   }
416 
417   @Override
close()418   public final void close() {
419     stub.close();
420   }
421 
422   @Override
shutdown()423   public void shutdown() {
424     stub.shutdown();
425   }
426 
427   @Override
isShutdown()428   public boolean isShutdown() {
429     return stub.isShutdown();
430   }
431 
432   @Override
isTerminated()433   public boolean isTerminated() {
434     return stub.isTerminated();
435   }
436 
437   @Override
shutdownNow()438   public void shutdownNow() {
439     stub.shutdownNow();
440   }
441 
442   @Override
awaitTermination(long duration, TimeUnit unit)443   public boolean awaitTermination(long duration, TimeUnit unit) throws InterruptedException {
444     return stub.awaitTermination(duration, unit);
445   }
446 
447   public static class ListPagedResponse
448       extends AbstractPagedListResponse<
449           ListRegionsRequest, RegionList, Region, ListPage, ListFixedSizeCollection> {
450 
createAsync( PageContext<ListRegionsRequest, RegionList, Region> context, ApiFuture<RegionList> futureResponse)451     public static ApiFuture<ListPagedResponse> createAsync(
452         PageContext<ListRegionsRequest, RegionList, Region> context,
453         ApiFuture<RegionList> futureResponse) {
454       ApiFuture<ListPage> futurePage =
455           ListPage.createEmptyPage().createPageAsync(context, futureResponse);
456       return ApiFutures.transform(
457           futurePage, input -> new ListPagedResponse(input), MoreExecutors.directExecutor());
458     }
459 
ListPagedResponse(ListPage page)460     private ListPagedResponse(ListPage page) {
461       super(page, ListFixedSizeCollection.createEmptyCollection());
462     }
463   }
464 
465   public static class ListPage
466       extends AbstractPage<ListRegionsRequest, RegionList, Region, ListPage> {
467 
ListPage( PageContext<ListRegionsRequest, RegionList, Region> context, RegionList response)468     private ListPage(
469         PageContext<ListRegionsRequest, RegionList, Region> context, RegionList response) {
470       super(context, response);
471     }
472 
createEmptyPage()473     private static ListPage createEmptyPage() {
474       return new ListPage(null, null);
475     }
476 
477     @Override
createPage( PageContext<ListRegionsRequest, RegionList, Region> context, RegionList response)478     protected ListPage createPage(
479         PageContext<ListRegionsRequest, RegionList, Region> context, RegionList response) {
480       return new ListPage(context, response);
481     }
482 
483     @Override
createPageAsync( PageContext<ListRegionsRequest, RegionList, Region> context, ApiFuture<RegionList> futureResponse)484     public ApiFuture<ListPage> createPageAsync(
485         PageContext<ListRegionsRequest, RegionList, Region> context,
486         ApiFuture<RegionList> futureResponse) {
487       return super.createPageAsync(context, futureResponse);
488     }
489   }
490 
491   public static class ListFixedSizeCollection
492       extends AbstractFixedSizeCollection<
493           ListRegionsRequest, RegionList, Region, ListPage, ListFixedSizeCollection> {
494 
ListFixedSizeCollection(List<ListPage> pages, int collectionSize)495     private ListFixedSizeCollection(List<ListPage> pages, int collectionSize) {
496       super(pages, collectionSize);
497     }
498 
createEmptyCollection()499     private static ListFixedSizeCollection createEmptyCollection() {
500       return new ListFixedSizeCollection(null, 0);
501     }
502 
503     @Override
createCollection(List<ListPage> pages, int collectionSize)504     protected ListFixedSizeCollection createCollection(List<ListPage> pages, int collectionSize) {
505       return new ListFixedSizeCollection(pages, collectionSize);
506     }
507   }
508 }
509