Skip to content

Commit

Permalink
synth: Take infra section of azure.yaml into account
Browse files Browse the repository at this point in the history
`azd infra synth` was not taking the path to the shared infrastructure
folder inor the name of the root module from the `infra` of
`azure.yaml` into account, and instead just always generated files
into the `infra` folder at the root of the project.

Since these were getting generated to the wrong place, they were
ignored during future `azd provision` runs.

We now take these values into account when generating filenames and
paths.
  • Loading branch information
ellismg committed May 6, 2024
1 parent a7a563a commit d5268ac
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 13 deletions.
6 changes: 3 additions & 3 deletions cli/azd/pkg/apphost/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ func ContainerAppManifestTemplateForProject(

// BicepTemplate returns a filesystem containing the generated bicep files for the given manifest. These files represent
// the shared infrastructure that would normally be under the `infra/` folder for the given manifest.
func BicepTemplate(manifest *Manifest, options AppHostOptions) (*memfs.FS, error) {
func BicepTemplate(name string, manifest *Manifest, options AppHostOptions) (*memfs.FS, error) {
generator := newInfraGenerator()

if err := generator.LoadManifest(manifest); err != nil {
Expand Down Expand Up @@ -271,7 +271,7 @@ func BicepTemplate(manifest *Manifest, options AppHostOptions) (*memfs.FS, error
WithMetadataParameters: parameters,
MainToResourcesParams: mapToResourceParams,
}
if err := executeToFS(fs, genTemplates, "main.bicep", "main.bicep", context); err != nil {
if err := executeToFS(fs, genTemplates, "main.bicep", name+".bicep", context); err != nil {
return nil, fmt.Errorf("generating infra/main.bicep: %w", err)
}

Expand All @@ -280,7 +280,7 @@ func BicepTemplate(manifest *Manifest, options AppHostOptions) (*memfs.FS, error
}

if err := executeToFS(
fs, genTemplates, "main.parameters.json", "main.parameters.json", generator.bicepContext); err != nil {
fs, genTemplates, "main.parameters.json", name+".parameters.json", generator.bicepContext); err != nil {
return nil, fmt.Errorf("generating infra/resources.bicep: %w", err)
}

Expand Down
10 changes: 5 additions & 5 deletions cli/azd/pkg/apphost/generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func TestAspireStorageGeneration(t *testing.T) {
m, err := ManifestFromAppHost(ctx, filepath.Join("testdata", "AspireDocker.AppHost.csproj"), mockCli, "")
require.NoError(t, err)

files, err := BicepTemplate(m, AppHostOptions{})
files, err := BicepTemplate("main", m, AppHostOptions{})
require.NoError(t, err)

err = fs.WalkDir(files, ".", func(path string, d fs.DirEntry, err error) error {
Expand Down Expand Up @@ -141,7 +141,7 @@ func TestAspireBicepGeneration(t *testing.T) {
m, err := ManifestFromAppHost(ctx, filepath.Join("testdata", "AspireDocker.AppHost.csproj"), mockCli, "")
require.NoError(t, err)

files, err := BicepTemplate(m, AppHostOptions{})
files, err := BicepTemplate("main", m, AppHostOptions{})
require.NoError(t, err)

err = fs.WalkDir(files, ".", func(path string, d fs.DirEntry, err error) error {
Expand Down Expand Up @@ -192,7 +192,7 @@ func TestAspireDockerGeneration(t *testing.T) {
})
}

files, err := BicepTemplate(m, AppHostOptions{})
files, err := BicepTemplate("main", m, AppHostOptions{})
require.NoError(t, err)

err = fs.WalkDir(files, ".", func(path string, d fs.DirEntry, err error) error {
Expand Down Expand Up @@ -227,7 +227,7 @@ func TestAspireDashboardGeneration(t *testing.T) {
m, err := ManifestFromAppHost(ctx, filepath.Join("testdata", "AspireDocker.AppHost.csproj"), mockCli, "")
require.NoError(t, err)

files, err := BicepTemplate(m, AppHostOptions{AspireDashboard: true})
files, err := BicepTemplate("main", m, AppHostOptions{AspireDashboard: true})
require.NoError(t, err)

err = fs.WalkDir(files, ".", func(path string, d fs.DirEntry, err error) error {
Expand Down Expand Up @@ -281,7 +281,7 @@ func TestAspireContainerGeneration(t *testing.T) {
m, err := ManifestFromAppHost(ctx, filepath.Join("testdata", "AspireDocker.AppHost.csproj"), mockCli, "")
require.NoError(t, err)

files, err := BicepTemplate(m, AppHostOptions{})
files, err := BicepTemplate("main", m, AppHostOptions{})
require.NoError(t, err)

err = fs.WalkDir(files, ".", func(path string, d fs.DirEntry, err error) error {
Expand Down
19 changes: 14 additions & 5 deletions cli/azd/pkg/project/dotnet_importer.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func (ai *DotNetImporter) ProjectInfrastructure(ctx context.Context, svcConfig *
return nil, fmt.Errorf("generating app host manifest: %w", err)
}

files, err := apphost.BicepTemplate(manifest, apphost.AppHostOptions{
files, err := apphost.BicepTemplate("main", manifest, apphost.AppHostOptions{
AspireDashboard: apphost.IsAspireDashboardEnabled(ai.alphaFeatureManager),
})
if err != nil {
Expand Down Expand Up @@ -289,13 +289,23 @@ func (ai *DotNetImporter) SynthAllInfrastructure(

generatedFS := memfs.New()

infraFS, err := apphost.BicepTemplate(manifest, apphost.AppHostOptions{
rootModuleName := DefaultModule
if p.Infra.Module != "" {
rootModuleName = p.Infra.Module
}

infraFS, err := apphost.BicepTemplate(rootModuleName, manifest, apphost.AppHostOptions{
AspireDashboard: apphost.IsAspireDashboardEnabled(ai.alphaFeatureManager),
})
if err != nil {
return nil, fmt.Errorf("generating infra/ folder: %w", err)
}

infraPathPrefix := DefaultPath
if p.Infra.Path != "" {
infraPathPrefix = p.Infra.Path
}

err = fs.WalkDir(infraFS, ".", func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
Expand All @@ -305,7 +315,7 @@ func (ai *DotNetImporter) SynthAllInfrastructure(
return nil
}

err = generatedFS.MkdirAll(filepath.Join("infra", filepath.Dir(path)), osutil.PermissionDirectoryOwnerOnly)
err = generatedFS.MkdirAll(filepath.Join(infraPathPrefix, filepath.Dir(path)), osutil.PermissionDirectoryOwnerOnly)
if err != nil {
return err
}
Expand All @@ -315,8 +325,7 @@ func (ai *DotNetImporter) SynthAllInfrastructure(
return err
}

return generatedFS.WriteFile(filepath.Join("infra", path), contents, d.Type().Perm())

return generatedFS.WriteFile(filepath.Join(infraPathPrefix, path), contents, d.Type().Perm())
})
if err != nil {
return nil, err
Expand Down

0 comments on commit d5268ac

Please sign in to comment.