• 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.stub;
18 
19 import static com.google.cloud.compute.v1.ZonesClient.ListPagedResponse;
20 
21 import com.google.api.core.ApiFunction;
22 import com.google.api.core.ApiFuture;
23 import com.google.api.core.BetaApi;
24 import com.google.api.gax.core.GaxProperties;
25 import com.google.api.gax.core.GoogleCredentialsProvider;
26 import com.google.api.gax.core.InstantiatingExecutorProvider;
27 import com.google.api.gax.httpjson.GaxHttpJsonProperties;
28 import com.google.api.gax.httpjson.HttpJsonTransportChannel;
29 import com.google.api.gax.httpjson.InstantiatingHttpJsonChannelProvider;
30 import com.google.api.gax.retrying.RetrySettings;
31 import com.google.api.gax.rpc.ApiCallContext;
32 import com.google.api.gax.rpc.ApiClientHeaderProvider;
33 import com.google.api.gax.rpc.ClientContext;
34 import com.google.api.gax.rpc.PageContext;
35 import com.google.api.gax.rpc.PagedCallSettings;
36 import com.google.api.gax.rpc.PagedListDescriptor;
37 import com.google.api.gax.rpc.PagedListResponseFactory;
38 import com.google.api.gax.rpc.StatusCode;
39 import com.google.api.gax.rpc.StubSettings;
40 import com.google.api.gax.rpc.TransportChannelProvider;
41 import com.google.api.gax.rpc.UnaryCallSettings;
42 import com.google.api.gax.rpc.UnaryCallable;
43 import com.google.cloud.compute.v1.GetZoneRequest;
44 import com.google.cloud.compute.v1.ListZonesRequest;
45 import com.google.cloud.compute.v1.Zone;
46 import com.google.cloud.compute.v1.ZoneList;
47 import com.google.common.collect.ImmutableList;
48 import com.google.common.collect.ImmutableMap;
49 import com.google.common.collect.ImmutableSet;
50 import com.google.common.collect.Lists;
51 import java.io.IOException;
52 import java.util.List;
53 import javax.annotation.Generated;
54 import org.threeten.bp.Duration;
55 
56 // AUTO-GENERATED DOCUMENTATION AND CLASS.
57 /**
58  * Settings class to configure an instance of {@link ZonesStub}.
59  *
60  * <p>The default instance has everything set to sensible defaults:
61  *
62  * <ul>
63  *   <li>The default service address (compute.googleapis.com) and default port (443) are used.
64  *   <li>Credentials are acquired automatically through Application Default Credentials.
65  *   <li>Retries are configured for idempotent methods but not for non-idempotent methods.
66  * </ul>
67  *
68  * <p>The builder of this class is recursive, so contained classes are themselves builders. When
69  * build() is called, the tree of builders is called to create the complete settings object.
70  *
71  * <p>For example, to set the total timeout of get to 30 seconds:
72  *
73  * <pre>{@code
74  * // This snippet has been automatically generated and should be regarded as a code template only.
75  * // It will require modifications to work:
76  * // - It may require correct/in-range values for request initialization.
77  * // - It may require specifying regional endpoints when creating the service client as shown in
78  * // https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library
79  * ZonesStubSettings.Builder zonesSettingsBuilder = ZonesStubSettings.newBuilder();
80  * zonesSettingsBuilder
81  *     .getSettings()
82  *     .setRetrySettings(
83  *         zonesSettingsBuilder
84  *             .getSettings()
85  *             .getRetrySettings()
86  *             .toBuilder()
87  *             .setTotalTimeout(Duration.ofSeconds(30))
88  *             .build());
89  * ZonesStubSettings zonesSettings = zonesSettingsBuilder.build();
90  * }</pre>
91  */
92 @Generated("by gapic-generator-java")
93 public class ZonesStubSettings extends StubSettings<ZonesStubSettings> {
94   /** The default scopes of the service. */
95   private static final ImmutableList<String> DEFAULT_SERVICE_SCOPES =
96       ImmutableList.<String>builder()
97           .add("https://www.googleapis.com/auth/compute.readonly")
98           .add("https://www.googleapis.com/auth/compute")
99           .add("https://www.googleapis.com/auth/cloud-platform")
100           .build();
101 
102   private final UnaryCallSettings<GetZoneRequest, Zone> getSettings;
103   private final PagedCallSettings<ListZonesRequest, ZoneList, ListPagedResponse> listSettings;
104 
105   private static final PagedListDescriptor<ListZonesRequest, ZoneList, Zone> LIST_PAGE_STR_DESC =
106       new PagedListDescriptor<ListZonesRequest, ZoneList, Zone>() {
107         @Override
108         public String emptyToken() {
109           return "";
110         }
111 
112         @Override
113         public ListZonesRequest injectToken(ListZonesRequest payload, String token) {
114           return ListZonesRequest.newBuilder(payload).setPageToken(token).build();
115         }
116 
117         @Override
118         public ListZonesRequest injectPageSize(ListZonesRequest payload, int pageSize) {
119           return ListZonesRequest.newBuilder(payload).setMaxResults(pageSize).build();
120         }
121 
122         @Override
123         public Integer extractPageSize(ListZonesRequest payload) {
124           return payload.getMaxResults();
125         }
126 
127         @Override
128         public String extractNextToken(ZoneList payload) {
129           return payload.getNextPageToken();
130         }
131 
132         @Override
133         public Iterable<Zone> extractResources(ZoneList payload) {
134           return payload.getItemsList() == null ? ImmutableList.<Zone>of() : payload.getItemsList();
135         }
136       };
137 
138   private static final PagedListResponseFactory<ListZonesRequest, ZoneList, ListPagedResponse>
139       LIST_PAGE_STR_FACT =
140           new PagedListResponseFactory<ListZonesRequest, ZoneList, ListPagedResponse>() {
141             @Override
142             public ApiFuture<ListPagedResponse> getFuturePagedResponse(
143                 UnaryCallable<ListZonesRequest, ZoneList> callable,
144                 ListZonesRequest request,
145                 ApiCallContext context,
146                 ApiFuture<ZoneList> futureResponse) {
147               PageContext<ListZonesRequest, ZoneList, Zone> pageContext =
148                   PageContext.create(callable, LIST_PAGE_STR_DESC, request, context);
149               return ListPagedResponse.createAsync(pageContext, futureResponse);
150             }
151           };
152 
153   /** Returns the object with the settings used for calls to get. */
getSettings()154   public UnaryCallSettings<GetZoneRequest, Zone> getSettings() {
155     return getSettings;
156   }
157 
158   /** Returns the object with the settings used for calls to list. */
listSettings()159   public PagedCallSettings<ListZonesRequest, ZoneList, ListPagedResponse> listSettings() {
160     return listSettings;
161   }
162 
createStub()163   public ZonesStub createStub() throws IOException {
164     if (getTransportChannelProvider()
165         .getTransportName()
166         .equals(HttpJsonTransportChannel.getHttpJsonTransportName())) {
167       return HttpJsonZonesStub.create(this);
168     }
169     throw new UnsupportedOperationException(
170         String.format(
171             "Transport not supported: %s", getTransportChannelProvider().getTransportName()));
172   }
173 
174   /** Returns a builder for the default ExecutorProvider for this service. */
defaultExecutorProviderBuilder()175   public static InstantiatingExecutorProvider.Builder defaultExecutorProviderBuilder() {
176     return InstantiatingExecutorProvider.newBuilder();
177   }
178 
179   /** Returns the default service endpoint. */
getDefaultEndpoint()180   public static String getDefaultEndpoint() {
181     return "compute.googleapis.com:443";
182   }
183 
184   /** Returns the default mTLS service endpoint. */
getDefaultMtlsEndpoint()185   public static String getDefaultMtlsEndpoint() {
186     return "compute.mtls.googleapis.com:443";
187   }
188 
189   /** Returns the default service scopes. */
getDefaultServiceScopes()190   public static List<String> getDefaultServiceScopes() {
191     return DEFAULT_SERVICE_SCOPES;
192   }
193 
194   /** Returns a builder for the default credentials for this service. */
defaultCredentialsProviderBuilder()195   public static GoogleCredentialsProvider.Builder defaultCredentialsProviderBuilder() {
196     return GoogleCredentialsProvider.newBuilder()
197         .setScopesToApply(DEFAULT_SERVICE_SCOPES)
198         .setUseJwtAccessWithScope(true);
199   }
200 
201   /** Returns a builder for the default ChannelProvider for this service. */
202   public static InstantiatingHttpJsonChannelProvider.Builder
defaultHttpJsonTransportProviderBuilder()203       defaultHttpJsonTransportProviderBuilder() {
204     return InstantiatingHttpJsonChannelProvider.newBuilder();
205   }
206 
defaultTransportChannelProvider()207   public static TransportChannelProvider defaultTransportChannelProvider() {
208     return defaultHttpJsonTransportProviderBuilder().build();
209   }
210 
211   @BetaApi("The surface for customizing headers is not stable yet and may change in the future.")
defaultApiClientHeaderProviderBuilder()212   public static ApiClientHeaderProvider.Builder defaultApiClientHeaderProviderBuilder() {
213     return ApiClientHeaderProvider.newBuilder()
214         .setGeneratedLibToken("gapic", GaxProperties.getLibraryVersion(ZonesStubSettings.class))
215         .setTransportToken(
216             GaxHttpJsonProperties.getHttpJsonTokenName(),
217             GaxHttpJsonProperties.getHttpJsonVersion());
218   }
219 
220   /** Returns a new builder for this class. */
newBuilder()221   public static Builder newBuilder() {
222     return Builder.createDefault();
223   }
224 
225   /** Returns a new builder for this class. */
newBuilder(ClientContext clientContext)226   public static Builder newBuilder(ClientContext clientContext) {
227     return new Builder(clientContext);
228   }
229 
230   /** Returns a builder containing all the values of this settings class. */
toBuilder()231   public Builder toBuilder() {
232     return new Builder(this);
233   }
234 
ZonesStubSettings(Builder settingsBuilder)235   protected ZonesStubSettings(Builder settingsBuilder) throws IOException {
236     super(settingsBuilder);
237 
238     getSettings = settingsBuilder.getSettings().build();
239     listSettings = settingsBuilder.listSettings().build();
240   }
241 
242   /** Builder for ZonesStubSettings. */
243   public static class Builder extends StubSettings.Builder<ZonesStubSettings, Builder> {
244     private final ImmutableList<UnaryCallSettings.Builder<?, ?>> unaryMethodSettingsBuilders;
245     private final UnaryCallSettings.Builder<GetZoneRequest, Zone> getSettings;
246     private final PagedCallSettings.Builder<ListZonesRequest, ZoneList, ListPagedResponse>
247         listSettings;
248     private static final ImmutableMap<String, ImmutableSet<StatusCode.Code>>
249         RETRYABLE_CODE_DEFINITIONS;
250 
251     static {
252       ImmutableMap.Builder<String, ImmutableSet<StatusCode.Code>> definitions =
253           ImmutableMap.builder();
254       definitions.put(
255           "retry_policy_0_codes",
256           ImmutableSet.copyOf(
257               Lists.<StatusCode.Code>newArrayList(
258                   StatusCode.Code.DEADLINE_EXCEEDED, StatusCode.Code.UNAVAILABLE)));
259       RETRYABLE_CODE_DEFINITIONS = definitions.build();
260     }
261 
262     private static final ImmutableMap<String, RetrySettings> RETRY_PARAM_DEFINITIONS;
263 
264     static {
265       ImmutableMap.Builder<String, RetrySettings> definitions = ImmutableMap.builder();
266       RetrySettings settings = null;
267       settings =
268           RetrySettings.newBuilder()
269               .setInitialRetryDelay(Duration.ofMillis(100L))
270               .setRetryDelayMultiplier(1.3)
271               .setMaxRetryDelay(Duration.ofMillis(60000L))
272               .setInitialRpcTimeout(Duration.ofMillis(600000L))
273               .setRpcTimeoutMultiplier(1.0)
274               .setMaxRpcTimeout(Duration.ofMillis(600000L))
275               .setTotalTimeout(Duration.ofMillis(600000L))
276               .build();
277       definitions.put("retry_policy_0_params", settings);
278       RETRY_PARAM_DEFINITIONS = definitions.build();
279     }
280 
Builder()281     protected Builder() {
282       this(((ClientContext) null));
283     }
284 
Builder(ClientContext clientContext)285     protected Builder(ClientContext clientContext) {
286       super(clientContext);
287 
288       getSettings = UnaryCallSettings.newUnaryCallSettingsBuilder();
289       listSettings = PagedCallSettings.newBuilder(LIST_PAGE_STR_FACT);
290 
291       unaryMethodSettingsBuilders =
292           ImmutableList.<UnaryCallSettings.Builder<?, ?>>of(getSettings, listSettings);
293       initDefaults(this);
294     }
295 
Builder(ZonesStubSettings settings)296     protected Builder(ZonesStubSettings settings) {
297       super(settings);
298 
299       getSettings = settings.getSettings.toBuilder();
300       listSettings = settings.listSettings.toBuilder();
301 
302       unaryMethodSettingsBuilders =
303           ImmutableList.<UnaryCallSettings.Builder<?, ?>>of(getSettings, listSettings);
304     }
305 
createDefault()306     private static Builder createDefault() {
307       Builder builder = new Builder(((ClientContext) null));
308 
309       builder.setTransportChannelProvider(defaultTransportChannelProvider());
310       builder.setCredentialsProvider(defaultCredentialsProviderBuilder().build());
311       builder.setInternalHeaderProvider(defaultApiClientHeaderProviderBuilder().build());
312       builder.setEndpoint(getDefaultEndpoint());
313       builder.setMtlsEndpoint(getDefaultMtlsEndpoint());
314       builder.setSwitchToMtlsEndpointAllowed(true);
315 
316       return initDefaults(builder);
317     }
318 
initDefaults(Builder builder)319     private static Builder initDefaults(Builder builder) {
320       builder
321           .getSettings()
322           .setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("retry_policy_0_codes"))
323           .setRetrySettings(RETRY_PARAM_DEFINITIONS.get("retry_policy_0_params"));
324 
325       builder
326           .listSettings()
327           .setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("retry_policy_0_codes"))
328           .setRetrySettings(RETRY_PARAM_DEFINITIONS.get("retry_policy_0_params"));
329 
330       return builder;
331     }
332 
333     /**
334      * Applies the given settings updater function to all of the unary API methods in this service.
335      *
336      * <p>Note: This method does not support applying settings to streaming methods.
337      */
applyToAllUnaryMethods( ApiFunction<UnaryCallSettings.Builder<?, ?>, Void> settingsUpdater)338     public Builder applyToAllUnaryMethods(
339         ApiFunction<UnaryCallSettings.Builder<?, ?>, Void> settingsUpdater) {
340       super.applyToAllUnaryMethods(unaryMethodSettingsBuilders, settingsUpdater);
341       return this;
342     }
343 
unaryMethodSettingsBuilders()344     public ImmutableList<UnaryCallSettings.Builder<?, ?>> unaryMethodSettingsBuilders() {
345       return unaryMethodSettingsBuilders;
346     }
347 
348     /** Returns the builder for the settings used for calls to get. */
getSettings()349     public UnaryCallSettings.Builder<GetZoneRequest, Zone> getSettings() {
350       return getSettings;
351     }
352 
353     /** Returns the builder for the settings used for calls to list. */
listSettings()354     public PagedCallSettings.Builder<ListZonesRequest, ZoneList, ListPagedResponse> listSettings() {
355       return listSettings;
356     }
357 
358     @Override
build()359     public ZonesStubSettings build() throws IOException {
360       return new ZonesStubSettings(this);
361     }
362   }
363 }
364