Skip to content

Commit

Permalink
output resources from app init (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
saragluna authored Nov 5, 2024
1 parent 030ec5b commit 41331c0
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 5 deletions.
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 @@ -263,7 +263,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 @@ -325,14 +325,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 @@ -351,13 +352,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 @@ -414,6 +417,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
}
}

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
}

*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"`
}

0 comments on commit 41331c0

Please sign in to comment.