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

Output resources in azure.yaml after app init #4

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
40 changes: 35 additions & 5 deletions cli/azd/internal/repository/app_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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)
}
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions cli/azd/pkg/project/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

copy.Resources[name] = &resourceCopy
}

projectBytes, err := yaml.Marshal(copy)
if err != nil {
return fmt.Errorf("marshalling project yaml: %w", err)
Expand Down
19 changes: 19 additions & 0 deletions cli/azd/pkg/project/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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:
Expand Down Expand Up @@ -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
}
Copy link

Choose a reason for hiding this comment

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

Is it necessary to add other resource type like: ResourceTypeDbRedis, ResourceTypeDbPostgres, ResourceTypeDbMong?

Copy link

Choose a reason for hiding this comment

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

Could you please add unit test for MarshalYAML and UnmarshalYAML?

}

return raw, nil
Expand Down Expand Up @@ -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
Copy link

Choose a reason for hiding this comment

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

Same here: Is it necessary to add other resource type like: ResourceTypeDbRedis, ResourceTypeDbPostgres, ResourceTypeDbMong?

}

*r = ResourceConfig(raw)
Expand Down Expand Up @@ -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"`
}