-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(spring): extend hasRestOption fix to properties composer and add …
…golden tests (#1348) This PR is a follow-up on #1343: - Extends fix to SpringPropertiesClassComposer, so that for services without REST-enabled rpcs, the unused useRest property is also not generated - Adds golden tests for the updated hasRestOption scenario using the wicked proto - Updates SpringAutoconfigCommentComposer for javadoc comments alluding to the useRest option
- Loading branch information
Showing
11 changed files
with
354 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
164 changes: 164 additions & 0 deletions
164
...ogle/api/generator/spring/composer/goldens/WickedSpringAutoConfigurationNoRestRpcs.golden
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
package com.google.showcase.v1beta1.spring; | ||
|
||
import com.google.api.gax.core.CredentialsProvider; | ||
import com.google.api.gax.core.ExecutorProvider; | ||
import com.google.api.gax.retrying.RetrySettings; | ||
import com.google.api.gax.rpc.HeaderProvider; | ||
import com.google.api.gax.rpc.TransportChannelProvider; | ||
import com.google.cloud.spring.autoconfigure.core.GcpContextAutoConfiguration; | ||
import com.google.cloud.spring.core.Credentials; | ||
import com.google.cloud.spring.core.DefaultCredentialsProvider; | ||
import com.google.cloud.spring.core.Retry; | ||
import com.google.cloud.spring.core.util.RetryUtil; | ||
import com.google.showcase.v1beta1.WickedClient; | ||
import com.google.showcase.v1beta1.WickedSettings; | ||
import java.io.IOException; | ||
import java.util.Collections; | ||
import org.apache.commons.logging.Log; | ||
import org.apache.commons.logging.LogFactory; | ||
import org.springframework.beans.factory.annotation.Qualifier; | ||
import org.springframework.boot.autoconfigure.AutoConfiguration; | ||
import org.springframework.boot.autoconfigure.AutoConfigureAfter; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; | ||
import org.springframework.boot.context.properties.EnableConfigurationProperties; | ||
import org.springframework.context.annotation.Bean; | ||
|
||
// AUTO-GENERATED DOCUMENTATION AND CLASS. | ||
/** | ||
* Auto-configuration for {@link WickedClient}. | ||
* | ||
* <p>Provides auto-configuration for Spring Boot | ||
* | ||
* <p>The default instance has everything set to sensible defaults: | ||
* | ||
* <ul> | ||
* <li>The default transport provider is used. | ||
* <li>Credentials are acquired automatically through Application Default Credentials. | ||
* <li>Retries are configured for idempotent methods but not for non-idempotent methods. | ||
* </ul> | ||
*/ | ||
@AutoConfiguration | ||
@AutoConfigureAfter(GcpContextAutoConfiguration.class) | ||
@ConditionalOnClass(WickedClient.class) | ||
@ConditionalOnProperty(value = "com.google.showcase.v1beta1.wicked.enabled", matchIfMissing = true) | ||
@EnableConfigurationProperties(WickedSpringProperties.class) | ||
public class WickedSpringAutoConfiguration { | ||
private final WickedSpringProperties clientProperties; | ||
private final CredentialsProvider credentialsProvider; | ||
private static final Log LOGGER = LogFactory.getLog(WickedSpringAutoConfiguration.class); | ||
|
||
protected WickedSpringAutoConfiguration( | ||
WickedSpringProperties clientProperties, CredentialsProvider credentialsProvider) | ||
throws IOException { | ||
this.clientProperties = clientProperties; | ||
if (this.clientProperties.getCredentials().hasKey()) { | ||
if (LOGGER.isTraceEnabled()) { | ||
LOGGER.trace("Using credentials from Wicked-specific configuration"); | ||
} | ||
this.credentialsProvider = | ||
((CredentialsProvider) new DefaultCredentialsProvider(this.clientProperties)); | ||
} else { | ||
this.credentialsProvider = credentialsProvider; | ||
} | ||
} | ||
|
||
/** | ||
* Provides a default transport channel provider bean. The default is gRPC and will default to it | ||
* unless the useRest option is supported and provided to use HTTP transport instead | ||
* | ||
* @return a default transport channel provider. | ||
*/ | ||
@Bean | ||
@ConditionalOnMissingBean(name = "defaultWickedTransportChannelProvider") | ||
public TransportChannelProvider defaultWickedTransportChannelProvider() { | ||
return WickedSettings.defaultTransportChannelProvider(); | ||
} | ||
|
||
/** | ||
* Provides a WickedSettings bean configured to use a DefaultCredentialsProvider and the client | ||
* library's default transport channel provider (defaultWickedTransportChannelProvider()). It also | ||
* configures the quota project ID and executor thread count, if provided through properties. | ||
* | ||
* <p>Retry settings are also configured from service-level and method-level properties specified | ||
* in WickedSpringProperties. Method-level properties will take precedence over service-level | ||
* properties if available, and client library defaults will be used if neither are specified. | ||
* | ||
* @param defaultTransportChannelProvider TransportChannelProvider to use in the settings. | ||
* @return a {@link WickedSettings} bean configured with {@link TransportChannelProvider} bean. | ||
*/ | ||
@Bean | ||
@ConditionalOnMissingBean | ||
public WickedSettings wickedSettings( | ||
@Qualifier("defaultWickedTransportChannelProvider") | ||
TransportChannelProvider defaultTransportChannelProvider) | ||
throws IOException { | ||
WickedSettings.Builder clientSettingsBuilder = WickedSettings.newBuilder(); | ||
clientSettingsBuilder | ||
.setCredentialsProvider(this.credentialsProvider) | ||
.setTransportChannelProvider(defaultTransportChannelProvider) | ||
.setHeaderProvider(this.userAgentHeaderProvider()); | ||
if (this.clientProperties.getQuotaProjectId() != null) { | ||
clientSettingsBuilder.setQuotaProjectId(this.clientProperties.getQuotaProjectId()); | ||
if (LOGGER.isTraceEnabled()) { | ||
LOGGER.trace( | ||
"Quota project id set to " | ||
+ this.clientProperties.getQuotaProjectId() | ||
+ ", this overrides project id from credentials."); | ||
} | ||
} | ||
if (this.clientProperties.getExecutorThreadCount() != null) { | ||
ExecutorProvider executorProvider = | ||
WickedSettings.defaultExecutorProviderBuilder() | ||
.setExecutorThreadCount(this.clientProperties.getExecutorThreadCount()) | ||
.build(); | ||
clientSettingsBuilder.setBackgroundExecutorProvider(executorProvider); | ||
if (LOGGER.isTraceEnabled()) { | ||
LOGGER.trace( | ||
"Background executor thread count is " | ||
+ this.clientProperties.getExecutorThreadCount()); | ||
} | ||
} | ||
Retry serviceRetry = clientProperties.getRetry(); | ||
if (serviceRetry != null) { | ||
RetrySettings craftEvilPlanRetrySettings = | ||
RetryUtil.updateRetrySettings( | ||
clientSettingsBuilder.craftEvilPlanSettings().getRetrySettings(), serviceRetry); | ||
clientSettingsBuilder.craftEvilPlanSettings().setRetrySettings(craftEvilPlanRetrySettings); | ||
|
||
if (LOGGER.isTraceEnabled()) { | ||
LOGGER.trace("Configured service-level retry settings from properties."); | ||
} | ||
} | ||
Retry craftEvilPlanRetry = clientProperties.getCraftEvilPlanRetry(); | ||
if (craftEvilPlanRetry != null) { | ||
RetrySettings craftEvilPlanRetrySettings = | ||
RetryUtil.updateRetrySettings( | ||
clientSettingsBuilder.craftEvilPlanSettings().getRetrySettings(), craftEvilPlanRetry); | ||
clientSettingsBuilder.craftEvilPlanSettings().setRetrySettings(craftEvilPlanRetrySettings); | ||
if (LOGGER.isTraceEnabled()) { | ||
LOGGER.trace("Configured method-level retry settings for craftEvilPlan from properties."); | ||
} | ||
} | ||
return clientSettingsBuilder.build(); | ||
} | ||
|
||
/** | ||
* Provides a WickedClient bean configured with WickedSettings. | ||
* | ||
* @param wickedSettings settings to configure an instance of client bean. | ||
* @return a {@link WickedClient} bean configured with {@link WickedSettings} | ||
*/ | ||
@Bean | ||
@ConditionalOnMissingBean | ||
public WickedClient wickedClient(WickedSettings wickedSettings) throws IOException { | ||
return WickedClient.create(wickedSettings); | ||
} | ||
|
||
private HeaderProvider userAgentHeaderProvider() { | ||
String springLibrary = "spring-autogen-wicked"; | ||
String version = this.getClass().getPackage().getImplementationVersion(); | ||
return () -> Collections.singletonMap("user-agent", springLibrary + "/" + version); | ||
} | ||
} |
Oops, something went wrong.