|
| 1 | +/* |
| 2 | + * Copyright 2025 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.spring; |
| 18 | + |
| 19 | +import com.google.api.core.BetaApi; |
| 20 | +import com.google.api.gax.core.CredentialsProvider; |
| 21 | +import com.google.api.gax.core.ExecutorProvider; |
| 22 | +import com.google.api.gax.retrying.RetrySettings; |
| 23 | +import com.google.api.gax.rpc.HeaderProvider; |
| 24 | +import com.google.api.gax.rpc.TransportChannelProvider; |
| 25 | +import com.google.cloud.compute.v1.AdviceClient; |
| 26 | +import com.google.cloud.compute.v1.AdviceSettings; |
| 27 | +import com.google.cloud.spring.autoconfigure.core.GcpContextAutoConfiguration; |
| 28 | +import com.google.cloud.spring.core.DefaultCredentialsProvider; |
| 29 | +import com.google.cloud.spring.core.Retry; |
| 30 | +import com.google.cloud.spring.core.util.RetryUtil; |
| 31 | +import java.io.IOException; |
| 32 | +import java.util.Collections; |
| 33 | +import javax.annotation.Generated; |
| 34 | +import org.apache.commons.logging.Log; |
| 35 | +import org.apache.commons.logging.LogFactory; |
| 36 | +import org.springframework.beans.factory.annotation.Qualifier; |
| 37 | +import org.springframework.boot.autoconfigure.AutoConfiguration; |
| 38 | +import org.springframework.boot.autoconfigure.AutoConfigureAfter; |
| 39 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; |
| 40 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; |
| 41 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; |
| 42 | +import org.springframework.boot.context.properties.EnableConfigurationProperties; |
| 43 | +import org.springframework.context.annotation.Bean; |
| 44 | + |
| 45 | +// AUTO-GENERATED DOCUMENTATION AND CLASS. |
| 46 | +/** |
| 47 | + * Auto-configuration for {@link AdviceClient}. |
| 48 | + * |
| 49 | + * <p>Provides auto-configuration for Spring Boot |
| 50 | + * |
| 51 | + * <p>The default instance has everything set to sensible defaults: |
| 52 | + * |
| 53 | + * <ul> |
| 54 | + * <li>The default transport provider is used. |
| 55 | + * <li>Credentials are acquired automatically through Application Default Credentials. |
| 56 | + * <li>Retries are configured for idempotent methods but not for non-idempotent methods. |
| 57 | + * </ul> |
| 58 | + */ |
| 59 | +@Generated("by google-cloud-spring-generator") |
| 60 | +@BetaApi("Autogenerated Spring autoconfiguration is not yet stable") |
| 61 | +@AutoConfiguration |
| 62 | +@AutoConfigureAfter(GcpContextAutoConfiguration.class) |
| 63 | +@ConditionalOnClass(AdviceClient.class) |
| 64 | +@ConditionalOnProperty(value = "com.google.cloud.compute.v1.advice.enabled", matchIfMissing = true) |
| 65 | +@EnableConfigurationProperties(AdviceSpringProperties.class) |
| 66 | +public class AdviceSpringAutoConfiguration { |
| 67 | + private final AdviceSpringProperties clientProperties; |
| 68 | + private final CredentialsProvider credentialsProvider; |
| 69 | + private static final Log LOGGER = LogFactory.getLog(AdviceSpringAutoConfiguration.class); |
| 70 | + |
| 71 | + protected AdviceSpringAutoConfiguration( |
| 72 | + AdviceSpringProperties clientProperties, CredentialsProvider credentialsProvider) |
| 73 | + throws IOException { |
| 74 | + this.clientProperties = clientProperties; |
| 75 | + if (this.clientProperties.getCredentials().hasKey()) { |
| 76 | + if (LOGGER.isTraceEnabled()) { |
| 77 | + LOGGER.trace("Using credentials from Advice-specific configuration"); |
| 78 | + } |
| 79 | + this.credentialsProvider = |
| 80 | + ((CredentialsProvider) new DefaultCredentialsProvider(this.clientProperties)); |
| 81 | + } else { |
| 82 | + this.credentialsProvider = credentialsProvider; |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Provides a default transport channel provider bean, corresponding to the client library's |
| 88 | + * default transport channel provider. If the library supports both GRPC and REST transport, and |
| 89 | + * the useRest property is configured, the HTTP/JSON transport provider will be used instead of |
| 90 | + * GRPC. |
| 91 | + * |
| 92 | + * @return a default transport channel provider. |
| 93 | + */ |
| 94 | + @Bean |
| 95 | + @ConditionalOnMissingBean(name = "defaultAdviceTransportChannelProvider") |
| 96 | + public TransportChannelProvider defaultAdviceTransportChannelProvider() { |
| 97 | + return AdviceSettings.defaultTransportChannelProvider(); |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Provides a AdviceSettings bean configured to use a DefaultCredentialsProvider and the client |
| 102 | + * library's default transport channel provider (defaultAdviceTransportChannelProvider()). It also |
| 103 | + * configures the quota project ID and executor thread count, if provided through properties. |
| 104 | + * |
| 105 | + * <p>Retry settings are also configured from service-level and method-level properties specified |
| 106 | + * in AdviceSpringProperties. Method-level properties will take precedence over service-level |
| 107 | + * properties if available, and client library defaults will be used if neither are specified. |
| 108 | + * |
| 109 | + * @param defaultTransportChannelProvider TransportChannelProvider to use in the settings. |
| 110 | + * @return a {@link AdviceSettings} bean configured with {@link TransportChannelProvider} bean. |
| 111 | + */ |
| 112 | + @Bean |
| 113 | + @ConditionalOnMissingBean |
| 114 | + public AdviceSettings adviceSettings( |
| 115 | + @Qualifier("defaultAdviceTransportChannelProvider") |
| 116 | + TransportChannelProvider defaultTransportChannelProvider) |
| 117 | + throws IOException { |
| 118 | + AdviceSettings.Builder clientSettingsBuilder = AdviceSettings.newBuilder(); |
| 119 | + clientSettingsBuilder |
| 120 | + .setCredentialsProvider(this.credentialsProvider) |
| 121 | + .setTransportChannelProvider(defaultTransportChannelProvider) |
| 122 | + .setEndpoint(AdviceSettings.getDefaultEndpoint()) |
| 123 | + .setHeaderProvider(this.userAgentHeaderProvider()); |
| 124 | + if (this.clientProperties.getQuotaProjectId() != null) { |
| 125 | + clientSettingsBuilder.setQuotaProjectId(this.clientProperties.getQuotaProjectId()); |
| 126 | + if (LOGGER.isTraceEnabled()) { |
| 127 | + LOGGER.trace( |
| 128 | + "Quota project id set to " |
| 129 | + + this.clientProperties.getQuotaProjectId() |
| 130 | + + ", this overrides project id from credentials."); |
| 131 | + } |
| 132 | + } |
| 133 | + if (this.clientProperties.getExecutorThreadCount() != null) { |
| 134 | + ExecutorProvider executorProvider = |
| 135 | + AdviceSettings.defaultExecutorProviderBuilder() |
| 136 | + .setExecutorThreadCount(this.clientProperties.getExecutorThreadCount()) |
| 137 | + .build(); |
| 138 | + clientSettingsBuilder.setBackgroundExecutorProvider(executorProvider); |
| 139 | + if (LOGGER.isTraceEnabled()) { |
| 140 | + LOGGER.trace( |
| 141 | + "Background executor thread count is " |
| 142 | + + this.clientProperties.getExecutorThreadCount()); |
| 143 | + } |
| 144 | + } |
| 145 | + Retry serviceRetry = clientProperties.getRetry(); |
| 146 | + if (serviceRetry != null) { |
| 147 | + RetrySettings calendarModeRetrySettings = |
| 148 | + RetryUtil.updateRetrySettings( |
| 149 | + clientSettingsBuilder.calendarModeSettings().getRetrySettings(), serviceRetry); |
| 150 | + clientSettingsBuilder.calendarModeSettings().setRetrySettings(calendarModeRetrySettings); |
| 151 | + |
| 152 | + if (LOGGER.isTraceEnabled()) { |
| 153 | + LOGGER.trace("Configured service-level retry settings from properties."); |
| 154 | + } |
| 155 | + } |
| 156 | + Retry calendarModeRetry = clientProperties.getCalendarModeRetry(); |
| 157 | + if (calendarModeRetry != null) { |
| 158 | + RetrySettings calendarModeRetrySettings = |
| 159 | + RetryUtil.updateRetrySettings( |
| 160 | + clientSettingsBuilder.calendarModeSettings().getRetrySettings(), calendarModeRetry); |
| 161 | + clientSettingsBuilder.calendarModeSettings().setRetrySettings(calendarModeRetrySettings); |
| 162 | + if (LOGGER.isTraceEnabled()) { |
| 163 | + LOGGER.trace("Configured method-level retry settings for calendarMode from properties."); |
| 164 | + } |
| 165 | + } |
| 166 | + return clientSettingsBuilder.build(); |
| 167 | + } |
| 168 | + |
| 169 | + /** |
| 170 | + * Provides a AdviceClient bean configured with AdviceSettings. |
| 171 | + * |
| 172 | + * @param adviceSettings settings to configure an instance of client bean. |
| 173 | + * @return a {@link AdviceClient} bean configured with {@link AdviceSettings} |
| 174 | + */ |
| 175 | + @Bean |
| 176 | + @ConditionalOnMissingBean |
| 177 | + public AdviceClient adviceClient(AdviceSettings adviceSettings) throws IOException { |
| 178 | + return AdviceClient.create(adviceSettings); |
| 179 | + } |
| 180 | + |
| 181 | + private HeaderProvider userAgentHeaderProvider() { |
| 182 | + String springLibrary = "spring-autogen-advice"; |
| 183 | + String version = this.getClass().getPackage().getImplementationVersion(); |
| 184 | + return () -> Collections.singletonMap("user-agent", springLibrary + "/" + version); |
| 185 | + } |
| 186 | +} |
0 commit comments