Skip to content
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
5 changes: 2 additions & 3 deletions cli/azd/internal/repository/initializer.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (

"github.com/azure/azure-dev/cli/azd/pkg/environment"
"github.com/azure/azure-dev/cli/azd/pkg/environment/azdcontext"
"github.com/azure/azure-dev/cli/azd/pkg/infra/provisioning/bicep"
"github.com/azure/azure-dev/cli/azd/pkg/input"
"github.com/azure/azure-dev/cli/azd/pkg/lazy"
"github.com/azure/azure-dev/cli/azd/pkg/osutil"
Expand Down Expand Up @@ -341,7 +340,7 @@ func (i *Initializer) InitializeMinimal(ctx context.Context, azdCtx *azdcontext.
// Default infra path if not specified
infraPath := projectConfig.Infra.Path
if infraPath == "" {
infraPath = bicep.Defaults.Path
infraPath = project.DefaultPath
}

err = os.MkdirAll(infraPath, osutil.PermissionDirectory)
Expand All @@ -351,7 +350,7 @@ func (i *Initializer) InitializeMinimal(ctx context.Context, azdCtx *azdcontext.

module := projectConfig.Infra.Module
if projectConfig.Infra.Module == "" {
module = bicep.Defaults.Module
module = project.DefaultModule
}

mainPath := filepath.Join(infraPath, module)
Expand Down
19 changes: 10 additions & 9 deletions cli/azd/pkg/infra/provisioning/bicep/bicep_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"strings"
"time"

"dario.cat/mergo"
"github.com/Azure/azure-sdk-for-go/sdk/azcore"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/arm"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
Expand All @@ -46,10 +45,10 @@ import (
"golang.org/x/exp/maps"
)

var Defaults = Options{
Module: "main",
Path: "infra",
}
const (
Comment thread
vhvb1989 marked this conversation as resolved.
defaultModule = "main"
defaultPath = "infra"
)

type deploymentDetails struct {
CompiledBicep *compileBicepResult
Expand Down Expand Up @@ -91,12 +90,14 @@ func (p *BicepProvider) RequiredExternalTools() []tools.ExternalTool {
}

func (p *BicepProvider) Initialize(ctx context.Context, projectPath string, options Options) error {
if err := mergo.Merge(&options, Defaults); err != nil {
return fmt.Errorf("merging bicep defaults: %w", err)
}
Comment thread
vhvb1989 marked this conversation as resolved.

p.projectPath = projectPath
p.options = options
if p.options.Module == "" {
p.options.Module = defaultModule
}
if p.options.Path == "" {
p.options.Path = defaultPath
}

requiredTools := p.RequiredExternalTools()
if err := tools.EnsureInstalled(ctx, requiredTools...); err != nil {
Expand Down
14 changes: 14 additions & 0 deletions cli/azd/pkg/infra/provisioning/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,21 @@ type Manager struct {
options *Options
}

// defaultOptions for this package.
const (
defaultModule = "main"
defaultPath = "infra"
)

func (m *Manager) Initialize(ctx context.Context, projectPath string, options Options) error {
// applied defaults if missing
if options.Module == "" {
options.Module = defaultModule
}
if options.Path == "" {
options.Path = defaultPath
}

Comment thread
vhvb1989 marked this conversation as resolved.
m.projectPath = projectPath
m.options = &options

Expand Down
19 changes: 10 additions & 9 deletions cli/azd/pkg/infra/provisioning/terraform/terraform_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"path/filepath"
"strings"

"dario.cat/mergo"
"github.com/azure/azure-dev/cli/azd/internal"
"github.com/azure/azure-dev/cli/azd/pkg/account"
"github.com/azure/azure-dev/cli/azd/pkg/environment"
Expand All @@ -24,10 +23,10 @@ import (
"golang.org/x/exp/maps"
)

var Defaults = Options{
Module: "main",
Path: "infra",
}
const (
defaultModule = "main"
defaultPath = "infra"
)

// TerraformProvider exposes infrastructure provisioning using Azure Terraform templates
type TerraformProvider struct {
Expand Down Expand Up @@ -78,12 +77,14 @@ func NewTerraformProvider(
}

func (t *TerraformProvider) Initialize(ctx context.Context, projectPath string, options Options) error {
if err := mergo.Merge(&options, Defaults); err != nil {
return fmt.Errorf("merging terraform defaults: %w", err)
}

t.projectPath = projectPath
t.options = options
if t.options.Module == "" {
t.options.Module = defaultModule
}
if t.options.Path == "" {
t.options.Path = defaultPath
}

requiredTools := t.RequiredExternalTools()
if err := tools.EnsureInstalled(ctx, requiredTools...); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion cli/azd/pkg/project/dotnet_importer.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ func (ai *DotNetImporter) ProjectInfrastructure(ctx context.Context, svcConfig *
Options: provisioning.Options{
Provider: provisioning.Bicep,
Path: tmpDir,
Module: "main",
Module: DefaultModule,
},
Inputs: inputs,
cleanupDir: tmpDir,
Expand Down
44 changes: 40 additions & 4 deletions cli/azd/pkg/project/importer.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

package project

import (
Expand Down Expand Up @@ -97,15 +100,31 @@ func (im *ImportManager) ServiceStable(ctx context.Context, projectConfig *Proje
return allServicesSlice, nil
}

// defaultOptions for infra settings. These values are applied across provisioning providers.
const (
DefaultModule = "main"
DefaultPath = "infra"
)

// ProjectInfrastructure parses the project configuration and returns the infrastructure configuration.
// The configuration can be explicitly defined on azure.yaml using path and module, or in case these values
// are not explicitly defined, the project importer uses default values to find the infrastructure.
func (im *ImportManager) ProjectInfrastructure(ctx context.Context, projectConfig *ProjectConfig) (*Infra, error) {
// Use default project values for Infra when not specified in azure.yaml
if projectConfig.Infra.Module == "" {
Comment thread
vhvb1989 marked this conversation as resolved.
projectConfig.Infra.Module = DefaultModule
}
if projectConfig.Infra.Path == "" {
projectConfig.Infra.Path = DefaultPath
}
Comment thread
vhvb1989 marked this conversation as resolved.

infraRoot := projectConfig.Infra.Path
if !filepath.IsAbs(infraRoot) {
infraRoot = filepath.Join(projectConfig.Path, infraRoot)
}

// Allow overriding the infrastructure by placing an `infra` folder in the location that would be expected based
// on azure.yaml
if _, err := os.Stat(infraRoot); err == nil {
// Allow overriding the infrastructure only when path and module exists.
if moduleExists, err := pathHasModule(infraRoot, projectConfig.Infra.Module); err == nil && moduleExists {
log.Printf("using infrastructure from %s directory", infraRoot)
return &Infra{
Options: projectConfig.Infra,
Expand All @@ -131,7 +150,24 @@ func (im *ImportManager) ProjectInfrastructure(ctx context.Context, projectConfi
}

return nil, fmt.Errorf(
"this project does not contain any infrastructure, have you created an '%s' folder?", filepath.Base(infraRoot))
"this project does not contain expected infrastructure, folder: '%s' and module: '%s'",
infraRoot,
projectConfig.Infra.Module)
}

// pathHasModule returns true if there is a file named "<module>" or "<module.bicep>" in path.
func pathHasModule(path, module string) (bool, error) {
Comment thread
vhvb1989 marked this conversation as resolved.
Comment thread
vhvb1989 marked this conversation as resolved.
files, err := os.ReadDir(path)
if err != nil {
return false, fmt.Errorf("error while iterating directory: %w", err)
}

return slices.ContainsFunc(files, func(file fs.DirEntry) bool {
fileName := file.Name()
fileNameNoExt := strings.TrimSuffix(fileName, filepath.Ext(fileName))
return !file.IsDir() && fileNameNoExt == module
}), nil

}

func (im *ImportManager) SynthAllInfrastructure(ctx context.Context, projectConfig *ProjectConfig) (fs.FS, error) {
Expand Down
Loading