Skip to content

Split spec_service_binding.go by app type: Spring Boot app, other app#105

Merged
rujche merged 24 commits into
azure-javaee:feature/sjadfrom
rujche:split-spec_service_binding
Jan 23, 2025
Merged

Split spec_service_binding.go by app type: Spring Boot app, other app#105
rujche merged 24 commits into
azure-javaee:feature/sjadfrom
rujche:split-spec_service_binding

Conversation

@rujche

@rujche rujche commented Jan 14, 2025

Copy link
Copy Markdown

Split spec_service_binding.go by app type: Spring Boot app, other app.

@rujche rujche requested review from Copilot and saragluna January 14, 2025 13:41

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot reviewed 6 out of 14 changed files in this pull request and generated 1 comment.

Files not reviewed (8)
  • cli/azd/resources/scaffold/templates/resources.bicept: Language not supported
  • cli/azd/internal/scaffold/spec_service_binding_test.go: Evaluated as low risk
  • cli/azd/internal/repository/app_init.go: Evaluated as low risk
  • cli/azd/internal/repository/infra_confirm.go: Evaluated as low risk
  • cli/azd/internal/repository/infra_confirm_test.go: Evaluated as low risk
  • cli/azd/internal/scaffold/spec.go: Evaluated as low risk
  • cli/azd/internal/scaffold/bicep_env_test.go: Evaluated as low risk
  • cli/azd/internal/scaffold/bicep_env.go: Evaluated as low risk
Comments suppressed due to low confidence (4)

cli/azd/internal/binding/binding_test.go:151

  • The test case 'invalid input' in 'TestToTargetAndInfoType' should have an actual invalid input to test the function's behavior with invalid data.
input: "${binding:azure.db.mysql::username}"

cli/azd/internal/binding/binding_common.go:22

  • The error message should use 'common source' instead of 'spring boot app' to match the context.
return nil, fmt.Errorf("unsupported target type when binding for spring boot app, target.Type = %s", target.Type)

cli/azd/pkg/project/scaffold_gen.go:207

  • Ensure that the changes involving the merging of environment variables using MergeMapWithDuplicationCheck are covered by tests. This includes scenarios with and without duplication.
serviceSpec.Envs, err = binding.MergeMapWithDuplicationCheck(serviceSpec.Envs, projectConfig.Services[resource.Name].Env)

cli/azd/pkg/project/scaffold_gen.go:255

  • Ensure that the changes involving the handling of different source types (e.g., Spring Boot apps) are covered by tests. This includes verifying that the correct environment variables are returned for each source type.
sourceType := toSourceType(userService.Language)

Comment thread cli/azd/internal/binding/binding_spring_boot.go Outdated
rujche and others added 2 commits January 14, 2025 21:44
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@rujche rujche requested review from Copilot and haoozhang January 14, 2025 13:45

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot reviewed 6 out of 14 changed files in this pull request and generated no comments.

Files not reviewed (8)
  • cli/azd/resources/scaffold/templates/resources.bicept: Language not supported
  • cli/azd/internal/scaffold/spec_service_binding_test.go: Evaluated as low risk
  • cli/azd/internal/repository/app_init.go: Evaluated as low risk
  • cli/azd/internal/repository/infra_confirm.go: Evaluated as low risk
  • cli/azd/internal/repository/infra_confirm_test.go: Evaluated as low risk
  • cli/azd/internal/scaffold/spec.go: Evaluated as low risk
  • cli/azd/internal/scaffold/bicep_env_test.go: Evaluated as low risk
  • cli/azd/internal/scaffold/bicep_env.go: Evaluated as low risk
Comments suppressed due to low confidence (2)

cli/azd/internal/binding/binding_spring_boot.go:1

  • The new functions for generating Spring Boot environment variables should be covered by tests to ensure they work as expected.
package binding

cli/azd/internal/binding/binding_common.go:22

  • The error message incorrectly mentions "spring boot app." It should be updated to "common source."
return nil, fmt.Errorf("unsupported target type when binding for spring boot app, target.Type = %s", target.Type)

func GetBindingEnvs(source Source, target Target) (map[string]string,
error) {
switch source.Type {
case Java, SpringBoot: // todo: support other Java types

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This todo will not be done in current PR.

@rujche rujche requested a review from Copilot January 14, 2025 15:12

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot reviewed 6 out of 14 changed files in this pull request and generated 1 comment.

Files not reviewed (8)
  • cli/azd/resources/scaffold/templates/resources.bicept: Language not supported
  • cli/azd/internal/scaffold/spec_service_binding_test.go: Evaluated as low risk
  • cli/azd/internal/repository/app_init.go: Evaluated as low risk
  • cli/azd/internal/repository/infra_confirm.go: Evaluated as low risk
  • cli/azd/internal/repository/infra_confirm_test.go: Evaluated as low risk
  • cli/azd/internal/scaffold/spec.go: Evaluated as low risk
  • cli/azd/internal/scaffold/bicep_env_test.go: Evaluated as low risk
  • cli/azd/internal/scaffold/bicep_env.go: Evaluated as low risk
Comments suppressed due to low confidence (4)

cli/azd/internal/binding/binding_spring_boot.go:93

  • The InfoType used for 'spring.data.mongodb.uri' should be 'InfoTypeConnectionString' instead of 'InfoTypeJdbcUrl'.
"spring.data.mongodb.uri": ToBindingEnv(target, InfoTypeJdbcUrl),

cli/azd/internal/binding/binding_common.go:22

  • The error message should be 'unsupported target type when binding for common source, target.Type = %s'.
return nil, fmt.Errorf("unsupported target type when binding for spring boot app, target.Type = %s", target.Type)

cli/azd/pkg/project/scaffold_gen.go:339

  • [nitpick] The variable name 'sourceType' is not very descriptive. It should be renamed to 'bindingSourceType'.
sourceType := toSourceType(userService.Language)

cli/azd/pkg/project/scaffold_gen.go:349

  • The changes to the 'printEnvListAboutUses' function introduce new behavior that is not covered by tests. Add tests to ensure this function works correctly with the new 'binding' package.
func printEnvListAboutUses(infraSpec *scaffold.InfraSpec, projectConfig *ProjectConfig, console input.Console, ctx context.Context) error {

Comment thread cli/azd/internal/binding/binding.go Outdated
ServiceTypeHostContainerApp: {
ServiceBindingInfoTypeHost: "https://{{BackendName}}.${containerAppsEnvironment.outputs.defaultDomain}",
binding.AzureContainerApp: {
binding.InfoTypeHost: "'https://{{BackendName}}.${containerAppsEnvironment.outputs.defaultDomain}'",

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI: Added ' because it's a string value.

run: |
cd ./cli/azd
go test $(go list ./... | grep -v github.com/azure/azure-dev/cli/azd/test/functional) -cover -v
go test $(go list ./... | grep -v github.com/azure/azure-dev/cli/azd/test/functional | grep -v github.com/azure/azure-dev/cli/azd/pkg/infra/provisioning/terraform) -cover -v

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ignore the test failure in current PR because it's not caused by current PR. The failure already exists in previous PR: #104

image

Maybe this failure can be fixed by merge upstream main, but I'm not sure. Anyway, skip related test in current PR.


func toSourceType(language appdetect.Language) binding.SourceType {
switch language {
case appdetect.Java:

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the type SpringBoot being used?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently, application will not be detected as SpringBoot, but when an application be detected as SpringBoot, it will take effect. Keep SpringBoot type is just for future usage.

Delete SpringBoot type will make it confusing: An application has type Java, but is called binding_spring_boot.go.

func BindToPostgres(sourceType binding.SourceType, serviceSpec *ServiceSpec, postgres *DatabasePostgres) error {
serviceSpec.DbPostgres = postgres
envs, err := GetServiceBindingEnvsForPostgres(*postgres)
envs, err := binding.GetBindingEnvs(binding.Source{Type: sourceType},

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So the workflow of these envs is like:

  • App detection to define the app types, aka language
  • Then in scaffold, we get the env list, based on the app type + the resource type
  • In the bicep files, we inject the envs

So the only thing to pass down is the app type?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

App detection to define the app types, aka language

Currently, app type is decided by language, in the future, it can be decided by other information like framework dependency.

Then in scaffold, we get the env list, based on the app type + the resource type

Not only app type, there is some other informations like jsJms isKafka, SpringBootVersion

In the bicep files, we inject the envs

Yes.

So the only thing to pass down is the app type?

Not only the app type, all these information are necessary:

  • Source
    • AppType
    • metadata like: isJms, isKafka, SpringBootVersion
  • Target
    • Target Type
    • AuthType

}

func BindToCosmosDb(serviceSpec *ServiceSpec, cosmos *DatabaseCosmosAccount) error {
func BindToCosmosDb(sourceType binding.SourceType, serviceSpec *ServiceSpec, cosmos *DatabaseCosmosAccount) error {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like these methods are very alike, can we reduce them?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. Updated.

Comment thread cli/azd/pkg/project/scaffold_gen.go Outdated
case ResourceTypeMessagingKafka:
variables, err = binding.GetBindingEnvs(binding.Source{
Type: sourceType,
IsSpringBootKafka: true,

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to refactor in this PR, but isSpringBootKafka is also some footprint we want to reduce.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All binding related logic has been moved into binding package, so there should be some code to represent the information about is spring boot kafka.

How about keeping current change in current PR, if we want to reduce it, we can do it in another PR.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated. Moved IsSpringBootKafka from Source to Source.Metadata.


func GetBindingEnvsForSpringBootToPostgresql(authType internal.AuthType) (map[string]string, error) {
target := Target{Type: AzureDatabaseForPostgresql}
switch authType {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here we get binding envs based on the app type first, then in each func, we return them based on the auth type.

I have a question that, the different dependencies used also have impact on the binding envs, should return different Env Vars. For example, for the app type: SpringBoot and the authType: ConnectionString of Kafka,

  • if the dependency is spring-cloud-starter-stream-kafka (spring-cloud stream style), it should configure the properties like spring.cloud.stream.kafka.binder.brokers.
  • if the dependency is spring-kafka (pure spring), it should configure the property like spring.kafka.bootstrap-servers.

So how to deal with this case?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point.

  1. This is a feature which is not implemented before current PR.
  2. After current PR merged, this can be implemented by adding metadata in binding.Source.Metadata:

image

Now we already have these metadata:

image

…er' , which is different from specified PrincipalType 'ServicePrincipal'.
}
{
principalId: principalId
principalType: 'ServicePrincipal'

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI: This is a bug before current PR.

Error message:

UnmatchedPrincipalType: The PrincipalId 'xxx' has type 'User' , which is different from specified PrincipalType 'ServicePrincipal'.

@rujche rujche marked this pull request as draft January 16, 2025 02:29
@rujche rujche marked this pull request as ready for review January 17, 2025 02:04

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot reviewed 6 out of 16 changed files in this pull request and generated no comments.

Files not reviewed (10)
  • cli/azd/resources/scaffold/templates/resources.bicept: Language not supported
  • cli/azd/internal/scaffold/spec_service_binding_test.go: Evaluated as low risk
  • cli/azd/internal/scaffold/bicep_env_test.go: Evaluated as low risk
  • cli/azd/internal/binding/binding_common.go: Evaluated as low risk
  • cli/azd/internal/repository/infra_confirm.go: Evaluated as low risk
  • cli/azd/internal/repository/infra_confirm_test.go: Evaluated as low risk
  • cli/azd/pkg/project/scaffold_gen.go: Evaluated as low risk
  • cli/azd/internal/scaffold/scaffold.go: Evaluated as low risk
  • cli/azd/internal/repository/app_init.go: Evaluated as low risk
  • cli/azd/internal/scaffold/spec.go: Evaluated as low risk
Comments suppressed due to low confidence (2)

cli/azd/internal/binding/binding_spring_boot.go:94

  • The method name should be GetBindingEnvsForSpringBootToMongoDB for consistency.
func GetBindingEnvsForSpringBootToMongoDb(authType internal.AuthType) (map[string]string, error) {

cli/azd/internal/scaffold/bicep_env.go:31

  • The ReplaceBindingEnv function should use the correct indices for the substring replacement. The current implementation may not handle cases where the prefix and suffix are not found correctly.
bicepEnvValue = binding.ReplaceBindingEnv(value, bicepEnvValue)

@rujche

rujche commented Jan 20, 2025

Copy link
Copy Markdown
Author

Hi, @saragluna , @haoozhang .
Please help to review this PR again.

@rujche rujche merged commit 1a5ffb5 into azure-javaee:feature/sjad Jan 23, 2025
@rujche rujche deleted the split-spec_service_binding branch January 23, 2025 08:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants