Skip to content

Commit

Permalink
Adds support for Azure Deployment stacks (#4165)
Browse files Browse the repository at this point in the history
  • Loading branch information
wbreza authored Aug 28, 2024
1 parent e879c96 commit 72bc498
Show file tree
Hide file tree
Showing 72 changed files with 12,114 additions and 8,013 deletions.
3 changes: 3 additions & 0 deletions cli/azd/.vscode/cspell-azd-dictionary.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ armappconfiguration
armappplatform
armcognitiveservices
armcosmos
armdeploymentstacks
armmachinelearning
armresourcegraph
armsql
Expand Down Expand Up @@ -145,6 +146,7 @@ notrail
omitempty
oneauth
oneline
onmicrosoft
opentelemetry
ostest
osutil
Expand Down Expand Up @@ -209,6 +211,7 @@ trafficmanager
Truef
typeflag
unhide
unmanage
unmarshaled
unmarshalled
unmarshalling
Expand Down
3 changes: 3 additions & 0 deletions cli/azd/.vscode/cspell.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ overrides:
- filename: test/functional/testdata/samples/funcapp/getting_started.md
words:
- funcignore
- filename: internal/vsrpc/handler.go
words:
- arity
ignorePaths:
- "**/*_test.go"
- "**/mock*.go"
41 changes: 39 additions & 2 deletions cli/azd/cmd/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -583,8 +583,8 @@ func registerCommonDependencies(container *ioc.NestedContainer) {

// Tools
container.MustRegisterSingleton(azcli.NewAzCli)
container.MustRegisterSingleton(azapi.NewDeployments)
container.MustRegisterSingleton(azapi.NewDeploymentOperations)

// Tools
container.MustRegisterSingleton(azapi.NewResourceService)
container.MustRegisterSingleton(docker.NewCli)
container.MustRegisterSingleton(dotnet.NewCli)
Expand All @@ -603,6 +603,43 @@ func registerCommonDependencies(container *ioc.NestedContainer) {
container.MustRegisterScoped(project.NewAiHelper)

// Provisioning
container.MustRegisterSingleton(func(
serviceLocator ioc.ServiceLocator,
featureManager *alpha.FeatureManager,
) (azapi.DeploymentService, error) {
deploymentsType := azapi.DeploymentTypeStandard

if featureManager.IsEnabled(azapi.FeatureDeploymentStacks) {
deploymentsType = azapi.DeploymentTypeStacks
}

var deployments azapi.DeploymentService
if err := serviceLocator.ResolveNamed(string(deploymentsType), &deployments); err != nil {
return nil, err
}

return deployments, nil
})

container.MustRegisterSingleton(azapi.NewResourceService)

// Register Deployment Services
deploymentServiceTypes := map[azapi.DeploymentType]any{
azapi.DeploymentTypeStandard: func(deploymentService *azapi.StandardDeployments) azapi.DeploymentService {
return deploymentService
},
azapi.DeploymentTypeStacks: func(deploymentService *azapi.StackDeployments) azapi.DeploymentService {
return deploymentService
},
}

for deploymentType, constructor := range deploymentServiceTypes {
container.MustRegisterNamedSingleton(string(deploymentType), constructor)
}

container.MustRegisterSingleton(azapi.NewStandardDeployments)
container.MustRegisterSingleton(azapi.NewStackDeployments)
container.MustRegisterSingleton(infra.NewDeploymentManager)
container.MustRegisterSingleton(infra.NewAzureResourceManager)
container.MustRegisterScoped(provisioning.NewManager)
container.MustRegisterScoped(provisioning.NewPrincipalIdProvider)
Expand Down
31 changes: 19 additions & 12 deletions cli/azd/cmd/down.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/azure/azure-dev/cli/azd/cmd/actions"
"github.com/azure/azure-dev/cli/azd/internal"
"github.com/azure/azure-dev/cli/azd/pkg/alpha"
"github.com/azure/azure-dev/cli/azd/pkg/azapi"
"github.com/azure/azure-dev/cli/azd/pkg/environment"
"github.com/azure/azure-dev/cli/azd/pkg/infra/provisioning"
"github.com/azure/azure-dev/cli/azd/pkg/input"
Expand Down Expand Up @@ -53,12 +54,13 @@ func newDownCmd() *cobra.Command {
}

type downAction struct {
flags *downFlags
provisionManager *provisioning.Manager
importManager *project.ImportManager
env *environment.Environment
console input.Console
projectConfig *project.ProjectConfig
flags *downFlags
provisionManager *provisioning.Manager
importManager *project.ImportManager
env *environment.Environment
console input.Console
projectConfig *project.ProjectConfig
alphaFeatureManager *alpha.FeatureManager
}

func newDownAction(
Expand All @@ -71,12 +73,13 @@ func newDownAction(
importManager *project.ImportManager,
) actions.Action {
return &downAction{
flags: flags,
provisionManager: provisionManager,
env: env,
console: console,
projectConfig: projectConfig,
importManager: importManager,
flags: flags,
provisionManager: provisionManager,
env: env,
console: console,
projectConfig: projectConfig,
importManager: importManager,
alphaFeatureManager: alphaFeatureManager,
}
}

Expand All @@ -99,6 +102,10 @@ func (a *downAction) Run(ctx context.Context) (*actions.ActionResult, error) {
return nil, fmt.Errorf("initializing provisioning manager: %w", err)
}

if a.alphaFeatureManager.IsEnabled(azapi.FeatureDeploymentStacks) {
a.console.WarnForFeature(ctx, azapi.FeatureDeploymentStacks)
}

destroyOptions := provisioning.NewDestroyOptions(a.flags.forceDelete, a.flags.purgeDelete)
if _, err := a.provisionManager.Destroy(ctx, destroyOptions); err != nil {
return nil, fmt.Errorf("deleting infrastructure: %w", err)
Expand Down
14 changes: 6 additions & 8 deletions cli/azd/cmd/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ type monitorAction struct {
azdCtx *azdcontext.AzdContext
env *environment.Environment
subResolver account.SubscriptionTenantResolver
resourceManager infra.ResourceManager
resourceService *azapi.ResourceService
deploymentOperations azapi.DeploymentOperations
console input.Console
flags *monitorFlags
portalUrlBase string
Expand All @@ -76,8 +76,8 @@ func newMonitorAction(
azdCtx *azdcontext.AzdContext,
env *environment.Environment,
subResolver account.SubscriptionTenantResolver,
resourceManager infra.ResourceManager,
resourceService *azapi.ResourceService,
deploymentOperations azapi.DeploymentOperations,
console input.Console,
flags *monitorFlags,
cloud *cloud.Cloud,
Expand All @@ -86,8 +86,8 @@ func newMonitorAction(
return &monitorAction{
azdCtx: azdCtx,
env: env,
resourceManager: resourceManager,
resourceService: resourceService,
deploymentOperations: deploymentOperations,
console: console,
flags: flags,
subResolver: subResolver,
Expand All @@ -113,15 +113,13 @@ func (m *monitorAction) Run(ctx context.Context) (*actions.ActionResult, error)
return nil, nil
}

resourceManager := infra.NewAzureResourceManager(m.resourceService, m.deploymentOperations)
resourceGroups, err := resourceManager.GetResourceGroupsForEnvironment(
ctx, m.env.GetSubscriptionId(), m.env.Name())
resourceGroups, err := m.resourceManager.GetResourceGroupsForEnvironment(ctx, m.env.GetSubscriptionId(), m.env.Name())
if err != nil {
return nil, fmt.Errorf("discovering resource groups from deployment: %w", err)
}

var insightsResources []azapi.Resource
var portalResources []azapi.Resource
var insightsResources []*azapi.Resource
var portalResources []*azapi.Resource

for _, resourceGroup := range resourceGroups {
resources, err := m.resourceService.ListResourceGroupResources(
Expand Down
15 changes: 9 additions & 6 deletions cli/azd/cmd/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ type showAction struct {
writer io.Writer
resourceService *azapi.ResourceService
envManager environment.Manager
deploymentOperations azapi.DeploymentOperations
infraResourceManager infra.ResourceManager
azdCtx *azdcontext.AzdContext
flags *showFlags
lazyServiceManager *lazy.Lazy[project.ServiceManager]
Expand All @@ -74,7 +74,7 @@ func newShowAction(
writer io.Writer,
resourceService *azapi.ResourceService,
envManager environment.Manager,
deploymentOperations azapi.DeploymentOperations,
infraResourceManager infra.ResourceManager,
projectConfig *project.ProjectConfig,
importManager *project.ImportManager,
azdCtx *azdcontext.AzdContext,
Expand All @@ -91,7 +91,7 @@ func newShowAction(
writer: writer,
resourceService: resourceService,
envManager: envManager,
deploymentOperations: deploymentOperations,
infraResourceManager: infraResourceManager,
azdCtx: azdCtx,
flags: flags,
lazyServiceManager: lazyServiceManager,
Expand Down Expand Up @@ -160,11 +160,14 @@ func (s *showAction) Run(ctx context.Context) (*actions.ActionResult, error) {
if subId = env.GetSubscriptionId(); subId == "" {
log.Printf("provision has not been run, resource ids will not be available")
} else {
azureResourceManager := infra.NewAzureResourceManager(s.resourceService, s.deploymentOperations)
resourceManager := project.NewResourceManager(env, s.resourceService, s.deploymentOperations)
resourceManager, err := s.lazyResourceManager.GetValue()
if err != nil {
return nil, err
}

envName := env.Name()

rgName, err = azureResourceManager.FindResourceGroupForEnvironment(ctx, subId, envName)
rgName, err = s.infraResourceManager.FindResourceGroupForEnvironment(ctx, subId, envName)
if err == nil {
for _, serviceConfig := range stableServices {
svcName := serviceConfig.Name
Expand Down
61 changes: 35 additions & 26 deletions cli/azd/internal/cmd/provision.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
"github.com/azure/azure-dev/cli/azd/cmd/actions"
"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/alpha"
"github.com/azure/azure-dev/cli/azd/pkg/azapi"
"github.com/azure/azure-dev/cli/azd/pkg/cloud"
"github.com/azure/azure-dev/cli/azd/pkg/environment"
"github.com/azure/azure-dev/cli/azd/pkg/infra/provisioning"
Expand Down Expand Up @@ -93,19 +95,20 @@ func NewProvisionCmd() *cobra.Command {
}

type ProvisionAction struct {
flags *ProvisionFlags
provisionManager *provisioning.Manager
projectManager project.ProjectManager
resourceManager project.ResourceManager
env *environment.Environment
envManager environment.Manager
formatter output.Formatter
projectConfig *project.ProjectConfig
writer io.Writer
console input.Console
subManager *account.SubscriptionsManager
importManager *project.ImportManager
portalUrlBase string
flags *ProvisionFlags
provisionManager *provisioning.Manager
projectManager project.ProjectManager
resourceManager project.ResourceManager
env *environment.Environment
envManager environment.Manager
formatter output.Formatter
projectConfig *project.ProjectConfig
writer io.Writer
console input.Console
subManager *account.SubscriptionsManager
importManager *project.ImportManager
alphaFeatureManager *alpha.FeatureManager
portalUrlBase string
}

func NewProvisionAction(
Expand All @@ -121,22 +124,24 @@ func NewProvisionAction(
formatter output.Formatter,
writer io.Writer,
subManager *account.SubscriptionsManager,
alphaFeatureManager *alpha.FeatureManager,
cloud *cloud.Cloud,
) actions.Action {
return &ProvisionAction{
flags: flags,
provisionManager: provisionManager,
projectManager: projectManager,
resourceManager: resourceManager,
env: env,
envManager: envManager,
formatter: formatter,
projectConfig: projectConfig,
writer: writer,
console: console,
subManager: subManager,
importManager: importManager,
portalUrlBase: cloud.PortalUrlBase,
flags: flags,
provisionManager: provisionManager,
projectManager: projectManager,
resourceManager: resourceManager,
env: env,
envManager: envManager,
formatter: formatter,
projectConfig: projectConfig,
writer: writer,
console: console,
subManager: subManager,
importManager: importManager,
alphaFeatureManager: alphaFeatureManager,
portalUrlBase: cloud.PortalUrlBase,
}
}

Expand Down Expand Up @@ -233,6 +238,10 @@ func (p *ProvisionAction) Run(ctx context.Context) (*actions.ActionResult, error
},
}

if p.alphaFeatureManager.IsEnabled(azapi.FeatureDeploymentStacks) {
p.console.WarnForFeature(ctx, azapi.FeatureDeploymentStacks)
}

err = p.projectConfig.Invoke(ctx, project.ProjectEventProvision, projectEventArgs, func() error {
var err error
if previewMode {
Expand Down
9 changes: 5 additions & 4 deletions cli/azd/internal/vsrpc/environment_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/azure/azure-dev/cli/azd/internal"
"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"
"github.com/azure/azure-dev/cli/azd/pkg/infra/provisioning"
"github.com/azure/azure-dev/cli/azd/pkg/project"
)
Expand Down Expand Up @@ -161,20 +162,20 @@ func (s *environmentService) DeleteEnvironmentAsync(
if mode&DeleteModeAzureResources > 0 {
_ = observer.OnNext(ctx, newImportantProgressMessage("Removing Azure resources"))

infra, err := c.importManager.ProjectInfrastructure(ctx, c.projectConfig)
projectInfra, err := c.importManager.ProjectInfrastructure(ctx, c.projectConfig)
if err != nil {
return false, err
}
defer func() { _ = infra.Cleanup() }()
defer func() { _ = projectInfra.Cleanup() }()

if err := c.provisionManager.Initialize(ctx, c.projectConfig.Path, infra.Options); err != nil {
if err := c.provisionManager.Initialize(ctx, c.projectConfig.Path, projectInfra.Options); err != nil {
return false, fmt.Errorf("initializing provisioning manager: %w", err)
}

// Enable force and purge options
destroyOptions := provisioning.NewDestroyOptions(true, true)
_, err = c.provisionManager.Destroy(ctx, destroyOptions)
if errors.Is(err, provisioning.ErrDeploymentsNotFound) {
if errors.Is(err, infra.ErrDeploymentsNotFound) {
_ = observer.OnNext(ctx, newInfoProgressMessage("No Azure resources were found"))
} else if err != nil {
return false, fmt.Errorf("deleting infrastructure: %w", err)
Expand Down
7 changes: 3 additions & 4 deletions cli/azd/internal/vsrpc/environment_service_refresh.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"log"

"github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/resources/armresources"
"github.com/azure/azure-dev/cli/azd/internal"
"github.com/azure/azure-dev/cli/azd/pkg/azapi"
"github.com/azure/azure-dev/cli/azd/pkg/environment"
Expand Down Expand Up @@ -94,9 +93,9 @@ func (s *environmentService) refreshEnvironmentAsync(
log.Printf("failed to get latest deployment result: %v", err)
} else {
env.LastDeployment = &DeploymentResult{
DeploymentId: *deployment.ID,
Success: *deployment.Properties.ProvisioningState == armresources.ProvisioningStateSucceeded,
Time: *deployment.Properties.Timestamp,
DeploymentId: deployment.Id,
Success: deployment.ProvisioningState == azapi.DeploymentProvisioningStateSucceeded,
Time: deployment.Timestamp,
}
}

Expand Down
Loading

0 comments on commit 72bc498

Please sign in to comment.