Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(spring): Replace global properties with CredentialsProvider bean #1123

Merged
merged 6 commits into from
Dec 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ static String buildPomString(GapicContext context) {
+ "\n"
+ " <dependency>\n"
+ " <groupId>com.google.cloud</groupId>\n"
+ " <artifactId>spring-cloud-gcp-core</artifactId>\n"
+ " <artifactId>spring-cloud-gcp-autoconfigure</artifactId>\n"
+ " </dependency>\n"
+ "</dependencies>\n"
+ "\n"
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import com.google.api.generator.gapic.model.Service;
import com.google.api.generator.gapic.model.Transport;
import com.google.api.generator.spring.composer.comment.SpringPropertiesCommentComposer;
import com.google.api.generator.spring.utils.ComposerUtils;
import com.google.api.generator.spring.utils.Utils;
import com.google.cloud.spring.core.Credentials;
import com.google.cloud.spring.core.CredentialsSupplier;
Expand Down Expand Up @@ -106,35 +107,6 @@ public GapicClass generate(GapicContext context, Service service) {
// return null;
}

private static ExprStatement createMemberVarStatement(
String varName,
TypeNode varType,
boolean isFinal,
Expr defaultVal,
List<AnnotationNode> annotationNodes) {
Variable memberVar = Variable.builder().setName(varName).setType(varType).build();
VariableExpr memberVarExpr =
VariableExpr.builder()
.setVariable(memberVar)
.setScope(ScopeNode.PRIVATE)
.setAnnotations(annotationNodes == null ? Collections.emptyList() : annotationNodes)
.setIsDecl(true)
.setIsFinal(isFinal)
.build();

if (defaultVal == null) {
return ExprStatement.withExpr(memberVarExpr);
}
AssignmentExpr assignmentExpr =
AssignmentExpr.builder()
.setVariableExpr(memberVarExpr.toBuilder().setIsDecl(true).build())
.setValueExpr(defaultVal)
.build();
ExprStatement memberVarStatement = ExprStatement.withExpr(assignmentExpr);

return memberVarStatement;
}

private static List<Statement> createMemberVariables(
Service service,
String packageName,
Expand All @@ -161,7 +133,7 @@ private static List<Statement> createMemberVariables(
List<AnnotationNode> credentialsAnnotations =
Arrays.asList(AnnotationNode.withType(STATIC_TYPES.get("NestedConfigurationProperty")));
ExprStatement credentialsStatement =
createMemberVarStatement(
ComposerUtils.createMemberVarStatement(
"credentials",
STATIC_TYPES.get("Credentials"),
true,
Expand All @@ -170,15 +142,17 @@ private static List<Statement> createMemberVariables(
statements.add(credentialsStatement);
// private String quotaProjectId;
ExprStatement quotaProjectIdVarStatement =
createMemberVarStatement("quotaProjectId", TypeNode.STRING, false, null, null);
ComposerUtils.createMemberVarStatement(
"quotaProjectId", TypeNode.STRING, false, null, null);
statements.add(quotaProjectIdVarStatement);
// private Integer executorThreadCount;
ExprStatement executorThreadCountVarStatement =
createMemberVarStatement("executorThreadCount", TypeNode.INT_OBJECT, false, null, null);
ComposerUtils.createMemberVarStatement(
"executorThreadCount", TypeNode.INT_OBJECT, false, null, null);
statements.add(executorThreadCountVarStatement);
if (hasRestOption) {
ExprStatement useRestVarStatement =
createMemberVarStatement(
ComposerUtils.createMemberVarStatement(
"useRest",
TypeNode.BOOLEAN,
false,
Expand Down Expand Up @@ -207,7 +181,8 @@ private static List<Statement> createMemberVariables(
}
String propertyName = Joiner.on("").join(methodAndPropertyName);
ExprStatement retrySettingsStatement =
createMemberVarStatement(propertyName, propertyType, false, null, null);
ComposerUtils.createMemberVarStatement(
propertyName, propertyType, false, null, null);
getterAndSetter.add(retrySettingsStatement);
return getterAndSetter;
},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Copyright 2022 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.api.generator.spring.utils;

import com.google.api.generator.engine.ast.AnnotationNode;
import com.google.api.generator.engine.ast.AssignmentExpr;
import com.google.api.generator.engine.ast.Expr;
import com.google.api.generator.engine.ast.ExprStatement;
import com.google.api.generator.engine.ast.ScopeNode;
import com.google.api.generator.engine.ast.TypeNode;
import com.google.api.generator.engine.ast.Variable;
import com.google.api.generator.engine.ast.VariableExpr;
import java.util.Collections;
import java.util.List;

public class ComposerUtils {

public static ExprStatement createMemberVarStatement(
String varName,
TypeNode varType,
boolean isFinal,
Expr defaultVal,
List<AnnotationNode> annotationNodes) {
Variable memberVar = Variable.builder().setName(varName).setType(varType).build();
VariableExpr memberVarExpr =
VariableExpr.builder()
.setVariable(memberVar)
.setScope(ScopeNode.PRIVATE)
.setAnnotations(annotationNodes == null ? Collections.emptyList() : annotationNodes)
.setIsDecl(true)
.setIsFinal(isFinal)
.build();

if (defaultVal == null) {
return ExprStatement.withExpr(memberVarExpr);
}
AssignmentExpr assignmentExpr =
AssignmentExpr.builder()
.setVariableExpr(memberVarExpr.toBuilder().setIsDecl(true).build())
.setValueExpr(defaultVal)
.build();

return ExprStatement.withExpr(assignmentExpr);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import com.google.api.gax.rpc.HeaderProvider;
import com.google.api.gax.rpc.TransportChannelProvider;
import com.google.cloud.spring.core.Credentials;
import com.google.cloud.spring.core.DefaultCredentialsProvider;
import com.google.cloud.spring.global.GlobalProperties;
import com.google.showcase.v1beta1.EchoClient;
import com.google.showcase.v1beta1.EchoSettings;
import java.io.IOException;
Expand Down Expand Up @@ -62,35 +61,25 @@ import org.threeten.bp.Duration;
@ConditionalOnProperty(
value = "com.google.showcase.v1beta1.spring.auto.echo.enabled",
matchIfMissing = true)
@EnableConfigurationProperties({EchoSpringProperties.class, GlobalProperties.class})
@EnableConfigurationProperties(EchoSpringProperties.class)
public class EchoSpringAutoConfiguration {
private final EchoSpringProperties clientProperties;
private final GlobalProperties globalProperties;
private final CredentialsProvider credentialsProvider;
private static final Log LOGGER = LogFactory.getLog(EchoSpringAutoConfiguration.class);

protected EchoSpringAutoConfiguration(
EchoSpringProperties clientProperties, GlobalProperties globalProperties) {
EchoSpringProperties clientProperties, CredentialsProvider credentialsProvider)
throws IOException {
this.clientProperties = clientProperties;
this.globalProperties = globalProperties;
}

/**
* Obtains the default credentials provider. The used key will be obtained from Spring Boot
* configuration data files.
*/
@Bean
@ConditionalOnMissingBean
public CredentialsProvider echoCredentials() throws IOException {
if (this.clientProperties.getCredentials().hasKey()) {
if (LOGGER.isTraceEnabled()) {
LOGGER.trace("Using credentials from Echo-specific configuration");
}
return ((CredentialsProvider) new DefaultCredentialsProvider(this.clientProperties));
}
if (LOGGER.isTraceEnabled()) {
LOGGER.trace("Using credentials from global configuration");
this.credentialsProvider =
((CredentialsProvider) new DefaultCredentialsProvider(this.clientProperties));
} else {
this.credentialsProvider = credentialsProvider;
}
return ((CredentialsProvider) new DefaultCredentialsProvider(this.globalProperties));
}

/**
Expand All @@ -116,13 +105,12 @@ public class EchoSpringAutoConfiguration {
@Bean
@ConditionalOnMissingBean
public EchoSettings echoSettings(
@Qualifier("echoCredentials") CredentialsProvider credentialsProvider,
@Qualifier("defaultEchoTransportChannelProvider")
TransportChannelProvider defaultTransportChannelProvider)
throws IOException {
EchoSettings.Builder clientSettingsBuilder =
EchoSettings.newBuilder()
.setCredentialsProvider(credentialsProvider)
.setCredentialsProvider(this.credentialsProvider)
.setTransportChannelProvider(defaultTransportChannelProvider)
.setHeaderProvider(this.userAgentHeaderProvider());
if (this.clientProperties.getQuotaProjectId() != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import com.google.api.gax.rpc.HeaderProvider;
import com.google.api.gax.rpc.TransportChannelProvider;
import com.google.cloud.spring.core.Credentials;
import com.google.cloud.spring.core.DefaultCredentialsProvider;
import com.google.cloud.spring.global.GlobalProperties;
import com.google.showcase.v1beta1.EchoClient;
import com.google.showcase.v1beta1.EchoSettings;
import java.io.IOException;
Expand Down Expand Up @@ -42,35 +41,25 @@ import org.threeten.bp.Duration;
@ConditionalOnProperty(
value = "com.google.showcase.v1beta1.spring.auto.echo.enabled",
matchIfMissing = true)
@EnableConfigurationProperties({EchoSpringProperties.class, GlobalProperties.class})
@EnableConfigurationProperties(EchoSpringProperties.class)
public class EchoSpringAutoConfiguration {
private final EchoSpringProperties clientProperties;
private final GlobalProperties globalProperties;
private final CredentialsProvider credentialsProvider;
private static final Log LOGGER = LogFactory.getLog(EchoSpringAutoConfiguration.class);

protected EchoSpringAutoConfiguration(
EchoSpringProperties clientProperties, GlobalProperties globalProperties) {
EchoSpringProperties clientProperties, CredentialsProvider credentialsProvider)
throws IOException {
this.clientProperties = clientProperties;
this.globalProperties = globalProperties;
}

/**
* Obtains the default credentials provider. The used key will be obtained from Spring Boot
* configuration data files.
*/
@Bean
@ConditionalOnMissingBean
public CredentialsProvider echoCredentials() throws IOException {
if (this.clientProperties.getCredentials().hasKey()) {
if (LOGGER.isTraceEnabled()) {
LOGGER.trace("Using credentials from Echo-specific configuration");
}
return ((CredentialsProvider) new DefaultCredentialsProvider(this.clientProperties));
}
if (LOGGER.isTraceEnabled()) {
LOGGER.trace("Using credentials from global configuration");
this.credentialsProvider =
((CredentialsProvider) new DefaultCredentialsProvider(this.clientProperties));
} else {
this.credentialsProvider = credentialsProvider;
}
return ((CredentialsProvider) new DefaultCredentialsProvider(this.globalProperties));
}

/**
Expand All @@ -96,13 +85,12 @@ public class EchoSpringAutoConfiguration {
@Bean
@ConditionalOnMissingBean
public EchoSettings echoSettings(
@Qualifier("echoCredentials") CredentialsProvider credentialsProvider,
@Qualifier("defaultEchoTransportChannelProvider")
TransportChannelProvider defaultTransportChannelProvider)
throws IOException {
EchoSettings.Builder clientSettingsBuilder =
EchoSettings.newBuilder()
.setCredentialsProvider(credentialsProvider)
.setCredentialsProvider(this.credentialsProvider)
.setTransportChannelProvider(defaultTransportChannelProvider)
.setHeaderProvider(this.userAgentHeaderProvider());
if (this.clientProperties.getQuotaProjectId() != null) {
Expand Down
Loading