From 26a5e6f2257e103ce617f61c25f109df77472a36 Mon Sep 17 00:00:00 2001 From: Xiaolu Dai Date: Mon, 28 Oct 2024 21:13:58 +0800 Subject: [PATCH] output resources from app init --- cli/azd/internal/repository/app_init.go | 40 +++++++++++++++++++++---- cli/azd/pkg/project/project.go | 7 +++++ cli/azd/pkg/project/resources.go | 19 ++++++++++++ 3 files changed, 61 insertions(+), 5 deletions(-) diff --git a/cli/azd/internal/repository/app_init.go b/cli/azd/internal/repository/app_init.go index ef5e2f5e9f6..a9477fd0120 100644 --- a/cli/azd/internal/repository/app_init.go +++ b/cli/azd/internal/repository/app_init.go @@ -261,7 +261,7 @@ func (i *Initializer) InitFromApp( tracing.SetUsageAttributes(fields.AppInitLastStep.String("generate")) i.console.Message(ctx, "\n"+output.WithBold("Generating files to run your app on Azure:")+"\n") - err = i.genProjectFile(ctx, azdCtx, detect) + err = i.genProjectFile(ctx, azdCtx, detect, spec) if err != nil { return err } @@ -323,14 +323,15 @@ func (i *Initializer) InitFromApp( func (i *Initializer) genProjectFile( ctx context.Context, azdCtx *azdcontext.AzdContext, - detect detectConfirm) error { + detect detectConfirm, + spec scaffold.InfraSpec) error { title := "Generating " + output.WithHighLightFormat("./"+azdcontext.ProjectFileName) i.console.ShowSpinner(ctx, title, input.Step) var err error defer i.console.StopSpinner(ctx, title, input.GetStepResultFormat(err)) - config, err := prjConfigFromDetect(azdCtx.ProjectDirectory(), detect) + config, err := prjConfigFromDetect(azdCtx.ProjectDirectory(), detect, spec) if err != nil { return fmt.Errorf("converting config: %w", err) } @@ -349,13 +350,15 @@ const InitGenTemplateId = "azd-init" func prjConfigFromDetect( root string, - detect detectConfirm) (project.ProjectConfig, error) { + detect detectConfirm, + spec scaffold.InfraSpec) (project.ProjectConfig, error) { config := project.ProjectConfig{ Name: azdcontext.ProjectName(root), Metadata: &project.ProjectMetadata{ Template: fmt.Sprintf("%s@%s", InitGenTemplateId, internal.VersionInfo().Version), }, - Services: map[string]*project.ServiceConfig{}, + Services: map[string]*project.ServiceConfig{}, + Resources: map[string]*project.ResourceConfig{}, } for _, prj := range detect.Services { rel, err := filepath.Rel(root, prj.Path) @@ -412,6 +415,33 @@ func prjConfigFromDetect( } } + for _, db := range prj.DatabaseDeps { + switch db { + case appdetect.DbMongo: + config.Resources["mongo"] = &project.ResourceConfig{ + Type: project.ResourceTypeDbMongo, + Name: spec.DbCosmosMongo.DatabaseName, + } + case appdetect.DbPostgres: + config.Resources["postgres"] = &project.ResourceConfig{ + Type: project.ResourceTypeDbPostgres, + Name: spec.DbPostgres.DatabaseName, + } + case appdetect.DbMySql: + config.Resources["mysql"] = &project.ResourceConfig{ + Type: project.ResourceTypeDbMySQL, + Props: project.MySQLProps{ + DatabaseName: spec.DbMySql.DatabaseName, + AuthType: "managedIdentity", + }, + } + case appdetect.DbRedis: + config.Resources["redis"] = &project.ResourceConfig{ + Type: project.ResourceTypeDbRedis, + } + } + } + name := filepath.Base(rel) if name == "." { name = config.Name diff --git a/cli/azd/pkg/project/project.go b/cli/azd/pkg/project/project.go index 73a50a15307..3aa988937bf 100644 --- a/cli/azd/pkg/project/project.go +++ b/cli/azd/pkg/project/project.go @@ -252,6 +252,13 @@ func Save(ctx context.Context, projectConfig *ProjectConfig, projectFilePath str copy.Services[name] = &svcCopy } + for name, resource := range projectConfig.Resources { + resourceCopy := *resource + resourceCopy.Project = © + + copy.Resources[name] = &resourceCopy + } + projectBytes, err := yaml.Marshal(copy) if err != nil { return fmt.Errorf("marshalling project yaml: %w", err) diff --git a/cli/azd/pkg/project/resources.go b/cli/azd/pkg/project/resources.go index 927aa682e65..9f4bca49765 100644 --- a/cli/azd/pkg/project/resources.go +++ b/cli/azd/pkg/project/resources.go @@ -14,6 +14,7 @@ type ResourceType string const ( ResourceTypeDbRedis ResourceType = "db.redis" ResourceTypeDbPostgres ResourceType = "db.postgres" + ResourceTypeDbMySQL ResourceType = "db.mysql" ResourceTypeDbMongo ResourceType = "db.mongo" ResourceTypeHostContainerApp ResourceType = "host.containerapp" ResourceTypeOpenAiModel ResourceType = "ai.openai.model" @@ -25,6 +26,8 @@ func (r ResourceType) String() string { return "Redis" case ResourceTypeDbPostgres: return "PostgreSQL" + case ResourceTypeDbMySQL: + return "MySQL" case ResourceTypeDbMongo: return "MongoDB" case ResourceTypeHostContainerApp: @@ -79,6 +82,11 @@ func (r *ResourceConfig) MarshalYAML() (interface{}, error) { if err != nil { return nil, err } + case ResourceTypeDbMySQL: + err := marshalRawProps(raw.Props.(MySQLProps)) + if err != nil { + return nil, err + } } return raw, nil @@ -118,6 +126,12 @@ func (r *ResourceConfig) UnmarshalYAML(value *yaml.Node) error { return err } raw.Props = cap + case ResourceTypeDbMySQL: + mp := MySQLProps{} + if err := unmarshalProps(&mp); err != nil { + return err + } + raw.Props = mp } *r = ResourceConfig(raw) @@ -145,3 +159,8 @@ type AIModelPropsModel struct { Name string `yaml:"name,omitempty"` Version string `yaml:"version,omitempty"` } + +type MySQLProps struct { + DatabaseName string `yaml:"databaseName,omitempty"` + AuthType string `yaml:"authType,omitempty"` +}