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

compose: with existing infra #4541

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
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
2 changes: 1 addition & 1 deletion cli/azd/internal/vsrpc/environment_service_refresh.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func (s *environmentService) refreshEnvironmentAsync(

_ = observer.OnNext(ctx, newInfoProgressMessage("Loading latest deployment information"))

deployment, err := bicepProvider.LastDeployment(ctx)
deployment, err := bicepProvider.LastRootDeployment(ctx)
if err != nil {
log.Printf("failed to get latest deployment result: %v", err)
} else {
Expand Down
24 changes: 18 additions & 6 deletions cli/azd/pkg/azapi/resource_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ package azapi

import (
"context"
"errors"
"fmt"
"slices"

"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/resourcemanager/resources/armresources"
"github.com/azure/azure-dev/cli/azd/pkg/account"
Expand Down Expand Up @@ -194,6 +197,11 @@ func (rs *ResourceService) DeleteResourceGroup(ctx context.Context, subscription
}

poller, err := client.BeginDelete(ctx, resourceGroupName, nil)
var respErr *azcore.ResponseError
if errors.As(err, &respErr) && respErr.StatusCode == 404 { // Resource group is already deleted
return nil
}

if err != nil {
return fmt.Errorf("beginning resource group deletion: %w", err)
}
Expand Down Expand Up @@ -259,12 +267,16 @@ func GroupByResourceGroup(resources []*armresources.ResourceReference) (map[stri

resourceType := resourceId.ResourceType.String()
if resourceType != string(AzureResourceTypeResourceGroup) {
groupResources = append(groupResources, &Resource{
Id: *resource.ID,
Name: resourceId.Name,
Type: resourceType,
Location: resourceId.Location,
})
if !slices.ContainsFunc(groupResources, func(r *Resource) bool {
return r.Id == *resource.ID
}) {
groupResources = append(groupResources, &Resource{
Id: *resource.ID,
Name: resourceId.Name,
Type: resourceType,
Location: resourceId.Location,
})
}
}

resourceMap[resourceId.ResourceGroupName] = groupResources
Expand Down
5 changes: 5 additions & 0 deletions cli/azd/pkg/azure/tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ const (
// TagKeyAzdEnvName is the name of the key in the tags map of a resource
// used to store the azd environment a resource is associated with.
TagKeyAzdEnvName = "azd-env-name"

// TagKeyAzdModuleName is the name of the key in the tags map of a resource
// used to store the Bicep module a resource is associated with.
TagKeyAzdModuleName = "azd-module-name"

/* #nosec G101 - Potential hardcoded credentials - false positive */
// TagKeyAzdDeploymentStateParamHashName is the name of the key in the tags map of a deployment
// used to store the parameters hash.
Expand Down
5 changes: 3 additions & 2 deletions cli/azd/pkg/devcenter/provision_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,8 @@ func (p *ProvisionProvider) pollForProgress(ctx context.Context, deployment infr
}

// Report incremental progress
progressDisplay := p.deploymentManager.ProgressDisplay(deployment)
demoMode, _ := strconv.ParseBool(os.Getenv("AZD_DEMO_MODE"))
progressDisplay := p.deploymentManager.ProgressDisplay(demoMode, 1)

initialDelay := 3 * time.Second
regularDelay := 10 * time.Second
Expand All @@ -488,7 +489,7 @@ func (p *ProvisionProvider) pollForProgress(ctx context.Context, deployment infr
timer.Stop()
return
case <-timer.C:
if err := progressDisplay.ReportProgress(ctx, &queryStartTime); err != nil {
if err := progressDisplay.ReportProgress(ctx, deployment, &queryStartTime); err != nil {
// We don't want to fail the whole deployment if a progress reporting error occurs
log.Printf("error while reporting progress: %s", err.Error())
}
Expand Down
28 changes: 23 additions & 5 deletions cli/azd/pkg/infra/deployment_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"log"
"slices"
"strings"

Expand Down Expand Up @@ -47,8 +48,8 @@ func (dm *DeploymentManager) CalculateTemplateHash(
return dm.deploymentService.CalculateTemplateHash(ctx, subscriptionId, template)
}

func (dm *DeploymentManager) ProgressDisplay(deployment Deployment) *ProvisioningProgressDisplay {
return NewProvisioningProgressDisplay(dm.resourceManager, dm.console, deployment)
func (dm *DeploymentManager) ProgressDisplay(demoMode bool, totalDeployments int) *ProvisioningProgressDisplay {
return NewProvisioningProgressDisplay(dm.resourceManager, dm.console, demoMode, totalDeployments)
}

func (dm *DeploymentManager) SubscriptionScope(subscriptionId string, location string) *SubscriptionScope {
Expand Down Expand Up @@ -77,6 +78,7 @@ func (dm *DeploymentManager) CompletedDeployments(
ctx context.Context,
scope Scope,
envName string,
moduleName string,
hint string,
) ([]*azapi.ResourceDeployment, error) {
deployments, err := scope.ListDeployments(ctx)
Expand All @@ -94,7 +96,8 @@ func (dm *DeploymentManager) CompletedDeployments(
}

// Environment matching strategy
// 1. Deployment with azd tagged env name
// 1. Deployment with azd tagged env name + module name
// 1.1 Deployment with azd tagged env name (legacy)
// 2. Exact match on environment name to deployment name (old azd strategy)
// 3. Multiple matching names based on specified hint (show user prompt)
matchingDeployments := []*azapi.ResourceDeployment{}
Expand All @@ -106,8 +109,23 @@ func (dm *DeploymentManager) CompletedDeployments(
continue
}

// Match on current azd strategy (tags) or old azd strategy (deployment name)
if v, has := deployment.Tags[azure.TagKeyAzdEnvName]; has && *v == envName || deployment.Name == envName {
// Match on current azd strategy (tags)
if v, has := deployment.Tags[azure.TagKeyAzdEnvName]; has && *v == envName {
moduleVal, hasModuleTag := deployment.Tags[azure.TagKeyAzdModuleName]
if hasModuleTag && *moduleVal == moduleName {
log.Printf("completedDeployments: matched deployment '%s' using moduleName: %s", deployment.Name, moduleName)
return []*azapi.ResourceDeployment{deployment}, nil
}

// LEGACY: the deployment is of an old format (no module tag), return the deployment based only on env tag
if !hasModuleTag {
log.Printf("completedDeployments: matched deployment '%s' using envName", deployment.Name)
return []*azapi.ResourceDeployment{deployment}, nil
}
}

// LEGACY: match on deployment name
if deployment.Name == envName {
return []*azapi.ResourceDeployment{deployment}, nil
}

Expand Down
Loading
Loading