diff --git a/cli/azd/internal/repository/app_init_test.go b/cli/azd/internal/repository/app_init_test.go index 29be2930407..12b6fbabd40 100644 --- a/cli/azd/internal/repository/app_init_test.go +++ b/cli/azd/internal/repository/app_init_test.go @@ -217,6 +217,7 @@ func TestInitializer_prjConfigFromDetect(t *testing.T) { "my$special$db", "n", "postgres", // fill in db name + "Username and password", }, want: project.ProjectConfig{ Services: map[string]*project.ServiceConfig{ @@ -237,18 +238,25 @@ func TestInitializer_prjConfigFromDetect(t *testing.T) { Type: project.ResourceTypeDbRedis, Name: "redis", }, - "mongodb": { + "mongo": { Type: project.ResourceTypeDbMongo, - Name: "mongodb", + Name: "mongo", + Props: project.MongoDBProps{ + DatabaseName: "mongodb", + }, }, - "postgres": { + "postgresql": { Type: project.ResourceTypeDbPostgres, - Name: "postgres", + Name: "postgresql", + Props: project.PostgresProps{ + AuthType: internal.AuthTypePassword, + DatabaseName: "postgres", + }, }, "py": { Type: project.ResourceTypeHostContainerApp, Name: "py", - Uses: []string{"postgres", "mongodb", "redis"}, + Uses: []string{"postgresql", "mongo", "redis"}, Props: project.ContainerAppProps{ Port: 80, }, diff --git a/cli/azd/pkg/project/importer_test.go b/cli/azd/pkg/project/importer_test.go index cf9c671c4f4..03ab794d06f 100644 --- a/cli/azd/pkg/project/importer_test.go +++ b/cli/azd/pkg/project/importer_test.go @@ -411,6 +411,9 @@ func Test_ImportManager_ProjectInfrastructure_FromResources(t *testing.T) { prjConfig := &ProjectConfig{} err := yaml.Unmarshal([]byte(prjWithResources), prjConfig) + for key, res := range prjConfig.Resources { + res.Name = key + } require.NoError(t, err) infra, err := im.ProjectInfrastructure(context.Background(), prjConfig) @@ -443,6 +446,9 @@ func TestImportManager_SynthAllInfrastructure_FromResources(t *testing.T) { prjConfig := &ProjectConfig{} err := yaml.Unmarshal([]byte(prjWithResources), prjConfig) require.NoError(t, err) + for key, res := range prjConfig.Resources { + res.Name = key + } projectFs, err := im.SynthAllInfrastructure(context.Background(), prjConfig) require.NoError(t, err) diff --git a/cli/azd/pkg/project/scaffold_gen.go b/cli/azd/pkg/project/scaffold_gen.go index 53f24f14a58..479e9ab8c9c 100644 --- a/cli/azd/pkg/project/scaffold_gen.go +++ b/cli/azd/pkg/project/scaffold_gen.go @@ -310,9 +310,11 @@ func printHintsAboutUses(infraSpec *scaffold.InfraSpec, projectConfig *ProjectCo return fmt.Errorf("in azure.yaml, (%s) uses (%s), but (%s) doesn't", userResourceName, usedResourceName, usedResourceName) } - (*console).Message(*context, fmt.Sprintf("CAUTION: In azure.yaml, '%s' uses '%s'. "+ - "After deployed, the 'uses' is achieved by providing these environment variables: ", - userResourceName, usedResourceName)) + if *console != nil { + (*console).Message(*context, fmt.Sprintf("CAUTION: In azure.yaml, '%s' uses '%s'. "+ + "After deployed, the 'uses' is achieved by providing these environment variables: ", + userResourceName, usedResourceName)) + } switch usedResource.Type { case ResourceTypeDbPostgres: err := printHintsAboutUsePostgres(userSpec.DbPostgres.AuthType, console, context) @@ -356,7 +358,10 @@ func printHintsAboutUses(infraSpec *scaffold.InfraSpec, projectConfig *ProjectCo "which is doen't add necessary environment variable", userResource.Name, usedResource.Name, usedResource.Name, usedResource.Type) } - (*console).Message(*context, "Please make sure your application used the right environment variable name.\n") + if *console != nil { + (*console).Message(*context, "Please make sure your application used the right environment variable name.\n") + } + } } return nil @@ -485,7 +490,7 @@ func fulfillFrontendBackend( usedSpec := getServiceSpecByName(infraSpec, usedResource.Name) if usedSpec == nil { - return fmt.Errorf("'%s' uses '%s', but %s doesn't", userSpec.Name, usedResource.Name, usedResource.Name) + return fmt.Errorf("'%s' uses '%s', but %s doesn't exist", userSpec.Name, usedResource.Name, usedResource.Name) } if usedSpec.Backend == nil { usedSpec.Backend = &scaffold.Backend{} @@ -506,6 +511,9 @@ func getServiceSpecByName(infraSpec *scaffold.InfraSpec, name string) *scaffold. func printHintsAboutUsePostgres(authType internal.AuthType, console *input.Console, context *context.Context) error { + if *console == nil { + return nil + } (*console).Message(*context, "POSTGRES_HOST=xxx") (*console).Message(*context, "POSTGRES_DATABASE=xxx") (*console).Message(*context, "POSTGRES_PORT=xxx") @@ -534,6 +542,9 @@ func printHintsAboutUsePostgres(authType internal.AuthType, func printHintsAboutUseMySql(authType internal.AuthType, console *input.Console, context *context.Context) error { + if *console == nil { + return nil + } (*console).Message(*context, "MYSQL_HOST=xxx") (*console).Message(*context, "MYSQL_DATABASE=xxx") (*console).Message(*context, "MYSQL_PORT=xxx") @@ -560,6 +571,9 @@ func printHintsAboutUseMySql(authType internal.AuthType, } func printHintsAboutUseRedis(console *input.Console, context *context.Context) { + if *console == nil { + return + } (*console).Message(*context, "REDIS_HOST=xxx") (*console).Message(*context, "REDIS_PORT=xxx") (*console).Message(*context, "REDIS_URL=xxx") @@ -569,18 +583,27 @@ func printHintsAboutUseRedis(console *input.Console, context *context.Context) { } func printHintsAboutUseMongo(console *input.Console, context *context.Context) { + if *console == nil { + return + } (*console).Message(*context, "MONGODB_URL=xxx") (*console).Message(*context, "spring.data.mongodb.uri=xxx") (*console).Message(*context, "spring.data.mongodb.database=xxx") } func printHintsAboutUseCosmos(console *input.Console, context *context.Context) { + if *console == nil { + return + } (*console).Message(*context, "spring.cloud.azure.cosmos.endpoint=xxx") (*console).Message(*context, "spring.cloud.azure.cosmos.database=xxx") } func printHintsAboutUseServiceBus(isJms bool, authType internal.AuthType, console *input.Console, context *context.Context) error { + if *console == nil { + return nil + } if !isJms { (*console).Message(*context, "spring.cloud.azure.servicebus.namespace=xxx") } @@ -602,6 +625,9 @@ func printHintsAboutUseServiceBus(isJms bool, authType internal.AuthType, func printHintsAboutUseEventHubs(UseKafka bool, authType internal.AuthType, springBootVersion string, console *input.Console, context *context.Context) error { + if *console == nil { + return nil + } if !UseKafka { (*console).Message(*context, "spring.cloud.azure.eventhubs.namespace=xxx") } else { @@ -630,6 +656,9 @@ func printHintsAboutUseEventHubs(UseKafka bool, authType internal.AuthType, spri func printHintsAboutUseStorageAccount(authType internal.AuthType, console *input.Console, context *context.Context) error { + if *console == nil { + return nil + } (*console).Message(*context, "spring.cloud.azure.eventhubs.processor.checkpoint-store.account-name=xxx") if authType == internal.AuthTypeUserAssignedManagedIdentity { (*console).Message(*context, "spring.cloud.azure.eventhubs.processor.checkpoint-store.connection-string=''") @@ -649,6 +678,9 @@ func printHintsAboutUseStorageAccount(authType internal.AuthType, func printHintsAboutUseHostContainerApp(userResourceName string, usedResourceName string, console *input.Console, context *context.Context) { + if *console == nil { + return + } (*console).Message(*context, fmt.Sprintf("Environemnt variables in %s:", userResourceName)) (*console).Message(*context, fmt.Sprintf("%s_BASE_URL=xxx", strings.ToUpper(usedResourceName))) (*console).Message(*context, fmt.Sprintf("Environemnt variables in %s:", usedResourceName)) @@ -656,5 +688,8 @@ func printHintsAboutUseHostContainerApp(userResourceName string, usedResourceNam } func printHintsAboutUseOpenAiModel(console *input.Console, context *context.Context) { + if *console == nil { + return + } (*console).Message(*context, "AZURE_OPENAI_ENDPOINT") } diff --git a/cli/azd/test/functional/init_test.go b/cli/azd/test/functional/init_test.go index de961f0f1eb..3e4809947a5 100644 --- a/cli/azd/test/functional/init_test.go +++ b/cli/azd/test/functional/init_test.go @@ -203,7 +203,7 @@ func Test_CLI_Init_From_App_With_Infra(t *testing.T) { "Use code in the current directory\n"+ "Confirm and continue initializing my app\n"+ "appdb\n"+ - "Use user assigned managed identity\n"+ + "User assigned managed identity\n"+ "TESTENV\n", "init", )