Skip to content

Commit

Permalink
wip3
Browse files Browse the repository at this point in the history
  • Loading branch information
wbreza committed Aug 2, 2024
1 parent d7934db commit 2529c4f
Show file tree
Hide file tree
Showing 56 changed files with 1,541 additions and 849 deletions.
2 changes: 2 additions & 0 deletions cli/azd/.vscode/cspell-azd-dictionary.txt
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ notrail
omitempty
oneauth
oneline
onmicrosoft
opentelemetry
ostest
osutil
Expand Down Expand Up @@ -203,6 +204,7 @@ trafficmanager
Truef
typeflag
unhide
unmanage
unmarshaled
unmarshalled
unmarshalling
Expand Down
42 changes: 22 additions & 20 deletions cli/azd/cmd/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -597,45 +597,47 @@ func registerCommonDependencies(container *ioc.NestedContainer) {
)
})

// Tools
container.MustRegisterSingleton(docker.NewDocker)
container.MustRegisterSingleton(dotnet.NewDotNetCli)
container.MustRegisterSingleton(git.NewGitCli)
container.MustRegisterSingleton(github.NewGitHubCli)
container.MustRegisterSingleton(javac.NewCli)
container.MustRegisterSingleton(kubectl.NewKubectl)
container.MustRegisterSingleton(maven.NewMavenCli)
container.MustRegisterSingleton(kubelogin.NewCli)
container.MustRegisterSingleton(helm.NewCli)
container.MustRegisterSingleton(kustomize.NewCli)
container.MustRegisterSingleton(npm.NewNpmCli)
container.MustRegisterSingleton(python.NewPythonCli)
container.MustRegisterSingleton(swa.NewSwaCli)
container.MustRegisterScoped(ai.NewPythonBridge)
container.MustRegisterScoped(project.NewAiHelper)

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

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

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

return deployments, nil
})

container.MustRegisterSingleton(azapi.NewResourceService)
container.MustRegisterNamedSingleton(string(azapi.DeploymentTypeStandard), azapi.NewDeployments)
container.MustRegisterNamedSingleton(string(azapi.DeploymentTypeStacks), azapi.NewDeploymentStacks)

container.MustRegisterSingleton(azapi.NewDeploymentOperations)
container.MustRegisterSingleton(docker.NewDocker)
container.MustRegisterSingleton(dotnet.NewDotNetCli)
container.MustRegisterSingleton(git.NewGitCli)
container.MustRegisterSingleton(github.NewGitHubCli)
container.MustRegisterSingleton(javac.NewCli)
container.MustRegisterSingleton(kubectl.NewKubectl)
container.MustRegisterSingleton(maven.NewMavenCli)
container.MustRegisterSingleton(kubelogin.NewCli)
container.MustRegisterSingleton(helm.NewCli)
container.MustRegisterSingleton(kustomize.NewCli)
container.MustRegisterSingleton(npm.NewNpmCli)
container.MustRegisterSingleton(python.NewPythonCli)
container.MustRegisterSingleton(swa.NewSwaCli)
container.MustRegisterScoped(ai.NewPythonBridge)
container.MustRegisterScoped(project.NewAiHelper)

// Provisioning
container.MustRegisterSingleton(infra.NewDeploymentManager)
container.MustRegisterSingleton(infra.NewAzureResourceManager)
container.MustRegisterScoped(provisioning.NewManager)
container.MustRegisterScoped(provisioning.NewPrincipalIdProvider)
Expand Down
26 changes: 12 additions & 14 deletions cli/azd/cmd/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"github.com/azure/azure-dev/cli/azd/pkg/infra"
"github.com/azure/azure-dev/cli/azd/pkg/input"
"github.com/azure/azure-dev/cli/azd/pkg/output"
"github.com/azure/azure-dev/cli/azd/pkg/tools/azcli"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)
Expand Down Expand Up @@ -65,8 +64,8 @@ type monitorAction struct {
azdCtx *azdcontext.AzdContext
env *environment.Environment
subResolver account.SubscriptionTenantResolver
azCli azcli.AzCli
deploymentOperations azapi.DeploymentOperations
resourceManager infra.ResourceManager
resourceService *azapi.ResourceService
console input.Console
flags *monitorFlags
portalUrlBase string
Expand All @@ -77,8 +76,8 @@ func newMonitorAction(
azdCtx *azdcontext.AzdContext,
env *environment.Environment,
subResolver account.SubscriptionTenantResolver,
azCli azcli.AzCli,
deploymentOperations azapi.DeploymentOperations,
resourceManager infra.ResourceManager,
resourceService *azapi.ResourceService,
console input.Console,
flags *monitorFlags,
portalUrlBase cloud.PortalUrlBase,
Expand All @@ -87,8 +86,8 @@ func newMonitorAction(
return &monitorAction{
azdCtx: azdCtx,
env: env,
azCli: azCli,
deploymentOperations: deploymentOperations,
resourceManager: resourceManager,
resourceService: resourceService,
console: console,
flags: flags,
subResolver: subResolver,
Expand All @@ -114,28 +113,27 @@ func (m *monitorAction) Run(ctx context.Context) (*actions.ActionResult, error)
return nil, nil
}

resourceManager := infra.NewAzureResourceManager(m.azCli, m.deploymentOperations)
resourceGroups, err := resourceManager.GetResourceGroupsForEnvironment(
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 []azcli.AzCliResource
var portalResources []azcli.AzCliResource
var insightsResources []azapi.AzCliResource
var portalResources []azapi.AzCliResource

for _, resourceGroup := range resourceGroups {
resources, err := m.azCli.ListResourceGroupResources(
resources, err := m.resourceService.ListResourceGroupResources(
ctx, azure.SubscriptionFromRID(resourceGroup.Id), resourceGroup.Name, nil)
if err != nil {
return nil, fmt.Errorf("listing resources: %w", err)
}

for _, resource := range resources {
switch resource.Type {
case string(infra.AzureResourceTypePortalDashboard):
case string(azapi.AzureResourceTypePortalDashboard):
portalResources = append(portalResources, resource)
case string(infra.AzureResourceTypeAppInsightComponent):
case string(azapi.AzureResourceTypeAppInsightComponent):
insightsResources = append(insightsResources, resource)
}
}
Expand Down
16 changes: 9 additions & 7 deletions cli/azd/cmd/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ 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/azapi"
"github.com/azure/azure-dev/cli/azd/pkg/cloud"
"github.com/azure/azure-dev/cli/azd/pkg/contracts"
"github.com/azure/azure-dev/cli/azd/pkg/environment"
Expand Down Expand Up @@ -61,7 +60,7 @@ type showAction struct {
writer io.Writer
azCli azcli.AzCli
envManager environment.Manager
deploymentOperations azapi.DeploymentOperations
infraResourceManager infra.ResourceManager
azdCtx *azdcontext.AzdContext
flags *showFlags
lazyServiceManager *lazy.Lazy[project.ServiceManager]
Expand All @@ -75,7 +74,7 @@ func newShowAction(
writer io.Writer,
azCli azcli.AzCli,
envManager environment.Manager,
deploymentOperations azapi.DeploymentOperations,
infraResourceManager infra.ResourceManager,
projectConfig *project.ProjectConfig,
importManager *project.ImportManager,
azdCtx *azdcontext.AzdContext,
Expand All @@ -92,7 +91,7 @@ func newShowAction(
writer: writer,
azCli: azCli,
envManager: envManager,
deploymentOperations: deploymentOperations,
infraResourceManager: infraResourceManager,
azdCtx: azdCtx,
flags: flags,
lazyServiceManager: lazyServiceManager,
Expand Down Expand Up @@ -161,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.azCli, s.deploymentOperations)
resourceManager := project.NewResourceManager(env, s.azCli, 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
5 changes: 2 additions & 3 deletions cli/azd/internal/vsrpc/environment_service_refresh.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
"github.com/azure/azure-dev/cli/azd/pkg/infra/provisioning"
"github.com/azure/azure-dev/cli/azd/pkg/infra/provisioning/bicep"
"github.com/azure/azure-dev/cli/azd/pkg/project"
"github.com/azure/azure-dev/cli/azd/pkg/tools/azcli"
)

// RefreshEnvironmentAsync is the server implementation of:
Expand Down Expand Up @@ -55,7 +54,7 @@ func (s *environmentService) refreshEnvironmentAsync(
importManager *project.ImportManager `container:"type"`
bicep provisioning.Provider `container:"name"`
azureResourceManager *infra.AzureResourceManager `container:"type"`
azcli azcli.AzCli `container:"type"`
resourceService *azapi.ResourceService `container:"type"`
resourceManager project.ResourceManager `container:"type"`
serviceManager project.ServiceManager `container:"type"`
envManager environment.Manager `container:"type"`
Expand Down Expand Up @@ -147,7 +146,7 @@ func (s *environmentService) refreshEnvironmentAsync(
}
}

resources, err := c.azcli.ListResourceGroupResources(ctx, subId, rgName, nil)
resources, err := c.resourceService.ListResourceGroupResources(ctx, subId, rgName, nil)
if err == nil {
for _, res := range resources {
env.Resources = append(env.Resources, &Resource{
Expand Down
19 changes: 19 additions & 0 deletions cli/azd/pkg/async/progress.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,22 @@ func RunWithProgress[T comparable, R any](
<-done
return res, err
}

// RunWithProgressE runs a function with a background goroutine reporting and progress to an observer.
func RunWithProgressE[T comparable](
observer func(T),
f func(*Progress[T]) error,
) error {
progress := NewProgress[T]()
done := make(chan struct{})
go func() {
for p := range progress.Progress() {
observer(p)
}
close(done)
}()
err := f(progress)
progress.Done()
<-done
return err
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

package infra
package azapi

import "strings"

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

package infra
package azapi

import (
"fmt"
Expand Down
60 changes: 56 additions & 4 deletions cli/azd/pkg/azapi/deployments.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"github.com/Azure/azure-sdk-for-go/sdk/azcore"
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/resources/armresources"
"github.com/azure/azure-dev/cli/azd/pkg/async"
"github.com/azure/azure-dev/cli/azd/pkg/azure"
)

Expand Down Expand Up @@ -79,7 +80,23 @@ const (
DeploymentProvisioningStateWaiting DeploymentProvisioningState = "waiting"
)

type Deployments interface {
type DeploymentService interface {
GenerateDeploymentName(baseName string) string
CalculateTemplateHash(
ctx context.Context,
subscriptionId string,
template azure.RawArmTemplate) (string, error)
ListSubscriptionDeploymentResources(
ctx context.Context,
subscriptionId string,
deploymentName string,
) ([]*armresources.ResourceReference, error)
ListResourceGroupDeploymentResources(
ctx context.Context,
subscriptionId string,
resourceGroupName string,
deploymentName string,
) ([]*armresources.ResourceReference, error)
ListSubscriptionDeployments(
ctx context.Context,
subscriptionId string,
Expand Down Expand Up @@ -118,6 +135,17 @@ type Deployments interface {
parameters azure.ArmParameters,
tags map[string]*string,
) (*ResourceDeployment, error)
ListSubscriptionDeploymentOperations(
ctx context.Context,
subscriptionId string,
deploymentName string,
) ([]*armresources.DeploymentOperation, error)
ListResourceGroupDeploymentOperations(
ctx context.Context,
subscriptionId string,
resourceGroupName string,
deploymentName string,
) ([]*armresources.DeploymentOperation, error)
WhatIfDeployToSubscription(
ctx context.Context,
subscriptionId string,
Expand All @@ -134,13 +162,37 @@ type Deployments interface {
armTemplate azure.RawArmTemplate,
parameters azure.ArmParameters,
) (*armresources.WhatIfOperationResult, error)
GenerateDeploymentName(baseName string) string
CalculateTemplateHash(
DeleteSubscriptionDeployment(
ctx context.Context,
subscriptionId string,
template azure.RawArmTemplate) (armresources.DeploymentsClientCalculateTemplateHashResponse, error)
deploymentName string,
progress *async.Progress[DeleteDeploymentProgress],
) error
DeleteResourceGroupDeployment(
ctx context.Context,
subscriptionId,
resourceGroupName string,
deploymentName string,
progress *async.Progress[DeleteDeploymentProgress],
) error
}

type DeleteResourceState string

const (
DeleteResourceStateInProgress DeleteResourceState = "InProgress"
DeleteResourceStateSucceeded DeleteResourceState = "Succeeded"
DeleteResourceStateFailed DeleteResourceState = "Failed"
)

type DeleteDeploymentProgress struct {
Name string
Message string
State DeleteResourceState
}

type ReportDeleteProgress func(progress *DeleteDeploymentProgress)

var (
ErrDeploymentNotFound = errors.New("deployment not found")
)
Expand Down
Loading

0 comments on commit 2529c4f

Please sign in to comment.