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

First cut at refactoring DefaultClientOptionsBuilder #3270

Closed
wants to merge 12 commits into from
36 changes: 27 additions & 9 deletions cli/azd/cmd/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"strings"

"github.com/Azure/azure-sdk-for-go/sdk/azcore"
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/resourcegraph/armresourcegraph"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/arm"
"github.com/MakeNowJust/heredoc/v2"
"github.com/azure/azure-dev/cli/azd/cmd/actions"
"github.com/azure/azure-dev/cli/azd/cmd/middleware"
Expand Down Expand Up @@ -375,16 +375,34 @@ func registerCommonDependencies(container *ioc.NestedContainer) {
},
)

// Default client options
container.MustRegisterSingleton(func(
ctx context.Context,
credential azcore.TokenCredential,
httpClient httputil.HttpClient,
) (*armresourcegraph.Client, error) {
options := azsdk.
DefaultClientOptionsBuilder(ctx, httpClient, "azd").
clientOptionsBuilderFactory azsdk.ClientOptionsBuilderFactory,
userAgent httputil.UserAgent,
) *azcore.ClientOptions {
return clientOptionsBuilderFactory.
ClientOptionsBuilder().
WithPerCallPolicy(azsdk.NewMsCorrelationPolicy()).
WithPerCallPolicy(azsdk.NewUserAgentPolicy(string(userAgent))).
BuildCoreClientOptions()
})

container.MustRegisterSingleton(func(
clientOptionsBuilderFactory azsdk.ClientOptionsBuilderFactory,
userAgent httputil.UserAgent,
) *arm.ClientOptions {
return clientOptionsBuilderFactory.
ClientOptionsBuilder().
WithPerCallPolicy(azsdk.NewMsCorrelationPolicy()).
WithPerCallPolicy(azsdk.NewUserAgentPolicy(string(userAgent))).
BuildArmClientOptions()
})

return armresourcegraph.NewClient(credential, options)
container.MustRegisterSingleton(func(
httpClient httputil.HttpClient,
userAgent httputil.UserAgent,
) *azsdk.ClientOptionsBuilderFactory {
return azsdk.NewClientOptionsBuilderFactory(httpClient, string(userAgent))
})

container.MustRegisterSingleton(templates.NewTemplateManager)
Expand Down Expand Up @@ -444,7 +462,7 @@ func registerCommonDependencies(container *ioc.NestedContainer) {
})

// Tools
container.MustRegisterSingleton(func(
container.MustRegisterScoped(func(
rootOptions *internal.GlobalCommandOptions,
credentialProvider account.SubscriptionCredentialProvider,
httpClient httputil.HttpClient,
Expand Down
138 changes: 23 additions & 115 deletions cli/azd/pkg/azapi/deployments.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,37 +13,28 @@ import (
"github.com/Azure/azure-sdk-for-go/sdk/azcore"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/resources/armresources"
azdinternal "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/azsdk"
"github.com/azure/azure-dev/cli/azd/pkg/azure"
"github.com/azure/azure-dev/cli/azd/pkg/httputil"
)

type Deployments interface {
ListSubscriptionDeployments(
ctx context.Context,
subscriptionId string,
) ([]*armresources.DeploymentExtended, error)
GetSubscriptionDeployment(
ctx context.Context,
subscriptionId string,
deploymentName string,
) (*armresources.DeploymentExtended, error)
ListResourceGroupDeployments(
ctx context.Context,
subscriptionId string,
resourceGroupName string,
) ([]*armresources.DeploymentExtended, error)
GetResourceGroupDeployment(
ctx context.Context,
subscriptionId string,
resourceGroupName string,
deploymentName string,
) (*armresources.DeploymentExtended, error)
DeployToSubscription(
ctx context.Context,
subscriptionId string,
location string,
deploymentName string,
armTemplate azure.RawArmTemplate,
Expand All @@ -52,7 +43,6 @@ type Deployments interface {
) (*armresources.DeploymentExtended, error)
DeployToResourceGroup(
ctx context.Context,
subscriptionId,
resourceGroup,
deploymentName string,
armTemplate azure.RawArmTemplate,
Expand All @@ -61,72 +51,55 @@ type Deployments interface {
) (*armresources.DeploymentExtended, error)
WhatIfDeployToSubscription(
ctx context.Context,
subscriptionId string,
location string,
deploymentName string,
armTemplate azure.RawArmTemplate,
parameters azure.ArmParameters,
) (*armresources.WhatIfOperationResult, error)
WhatIfDeployToResourceGroup(
ctx context.Context,
subscriptionId,
resourceGroup,
deploymentName string,
armTemplate azure.RawArmTemplate,
parameters azure.ArmParameters,
) (*armresources.WhatIfOperationResult, error)
DeleteSubscriptionDeployment(ctx context.Context, subscriptionId string, deploymentName string) error
DeleteSubscriptionDeployment(ctx context.Context, deploymentName string) error
CalculateTemplateHash(
ctx context.Context,
subscriptionId string,
template azure.RawArmTemplate) (armresources.DeploymentsClientCalculateTemplateHashResponse, error)
template azure.RawArmTemplate,
) (armresources.DeploymentsClientCalculateTemplateHashResponse, error)
}

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

type deployments struct {
credentialProvider account.SubscriptionCredentialProvider
httpClient httputil.HttpClient
userAgent string
deploymentsClient *armresources.DeploymentsClient
}

func NewDeployments(
credentialProvider account.SubscriptionCredentialProvider,
httpClient httputil.HttpClient,
deploymentsClient *armresources.DeploymentsClient,
) Deployments {
return &deployments{
credentialProvider: credentialProvider,
httpClient: httpClient,
userAgent: azdinternal.UserAgent(),
deploymentsClient: deploymentsClient,
}
}

func (ds *deployments) CalculateTemplateHash(
ctx context.Context,
subscriptionId string,
template azure.RawArmTemplate) (result armresources.DeploymentsClientCalculateTemplateHashResponse, err error) {
deploymentClient, err := ds.createDeploymentsClient(ctx, subscriptionId)
if err != nil {
return result, fmt.Errorf("creating deployments client: %w", err)
}

return deploymentClient.CalculateTemplateHash(ctx, template, nil)
template azure.RawArmTemplate,
) (result armresources.DeploymentsClientCalculateTemplateHashResponse, err error) {
return ds.deploymentsClient.CalculateTemplateHash(ctx, template, nil)
}

func (ds *deployments) ListSubscriptionDeployments(
ctx context.Context,
subscriptionId string,
) ([]*armresources.DeploymentExtended, error) {
deploymentClient, err := ds.createDeploymentsClient(ctx, subscriptionId)
if err != nil {
return nil, fmt.Errorf("creating deployments client: %w", err)
}

results := []*armresources.DeploymentExtended{}

pager := deploymentClient.NewListAtSubscriptionScopePager(nil)
pager := ds.deploymentsClient.NewListAtSubscriptionScopePager(nil)
for pager.More() {
page, err := pager.NextPage(ctx)
if err != nil {
Expand All @@ -141,15 +114,10 @@ func (ds *deployments) ListSubscriptionDeployments(

func (ds *deployments) GetSubscriptionDeployment(
ctx context.Context,
subscriptionId string,
deploymentName string,
) (*armresources.DeploymentExtended, error) {
deploymentClient, err := ds.createDeploymentsClient(ctx, subscriptionId)
if err != nil {
return nil, fmt.Errorf("creating deployments client: %w", err)
}

deployment, err := deploymentClient.GetAtSubscriptionScope(ctx, deploymentName, nil)
deployment, err := ds.deploymentsClient.GetAtSubscriptionScope(ctx, deploymentName, nil)
if err != nil {
var errDetails *azcore.ResponseError
if errors.As(err, &errDetails) && errDetails.StatusCode == 404 {
Expand All @@ -163,17 +131,12 @@ func (ds *deployments) GetSubscriptionDeployment(

func (ds *deployments) ListResourceGroupDeployments(
ctx context.Context,
subscriptionId string,
resourceGroupName string,
) ([]*armresources.DeploymentExtended, error) {
deploymentClient, err := ds.createDeploymentsClient(ctx, subscriptionId)
if err != nil {
return nil, fmt.Errorf("creating deployments client: %w", err)
}

results := []*armresources.DeploymentExtended{}

pager := deploymentClient.NewListByResourceGroupPager(resourceGroupName, nil)
pager := ds.deploymentsClient.NewListByResourceGroupPager(resourceGroupName, nil)
for pager.More() {
page, err := pager.NextPage(ctx)
if err != nil {
Expand All @@ -188,16 +151,11 @@ func (ds *deployments) ListResourceGroupDeployments(

func (ds *deployments) GetResourceGroupDeployment(
ctx context.Context,
subscriptionId string,
resourceGroupName string,
deploymentName string,
) (*armresources.DeploymentExtended, error) {
deploymentClient, err := ds.createDeploymentsClient(ctx, subscriptionId)
if err != nil {
return nil, fmt.Errorf("creating deployments client: %w", err)
}

deployment, err := deploymentClient.Get(ctx, resourceGroupName, deploymentName, nil)
deployment, err := ds.deploymentsClient.Get(ctx, resourceGroupName, deploymentName, nil)
if err != nil {
var errDetails *azcore.ResponseError
if errors.As(err, &errDetails) && errDetails.StatusCode == 404 {
Expand All @@ -209,39 +167,15 @@ func (ds *deployments) GetResourceGroupDeployment(
return &deployment.DeploymentExtended, nil
}

func (ds *deployments) createDeploymentsClient(
ctx context.Context,
subscriptionId string,
) (*armresources.DeploymentsClient, error) {
credential, err := ds.credentialProvider.CredentialForSubscription(ctx, subscriptionId)
if err != nil {
return nil, err
}

options := ds.clientOptionsBuilder(ctx).BuildArmClientOptions()
client, err := armresources.NewDeploymentsClient(subscriptionId, credential, options)
if err != nil {
return nil, fmt.Errorf("creating deployments client: %w", err)
}

return client, nil
}

func (ds *deployments) DeployToSubscription(
ctx context.Context,
subscriptionId string,
location string,
deploymentName string,
armTemplate azure.RawArmTemplate,
parameters azure.ArmParameters,
tags map[string]*string,
) (*armresources.DeploymentExtended, error) {
deploymentClient, err := ds.createDeploymentsClient(ctx, subscriptionId)
if err != nil {
return nil, fmt.Errorf("creating deployments client: %w", err)
}

createFromTemplateOperation, err := deploymentClient.BeginCreateOrUpdateAtSubscriptionScope(
createFromTemplateOperation, err := ds.deploymentsClient.BeginCreateOrUpdateAtSubscriptionScope(
ctx, deploymentName,
armresources.Deployment{
Properties: &armresources.DeploymentProperties{
Expand Down Expand Up @@ -271,17 +205,12 @@ func (ds *deployments) DeployToSubscription(

func (ds *deployments) DeployToResourceGroup(
ctx context.Context,
subscriptionId, resourceGroup, deploymentName string,
resourceGroup, deploymentName string,
armTemplate azure.RawArmTemplate,
parameters azure.ArmParameters,
tags map[string]*string,
) (*armresources.DeploymentExtended, error) {
deploymentClient, err := ds.createDeploymentsClient(ctx, subscriptionId)
if err != nil {
return nil, fmt.Errorf("creating deployments client: %w", err)
}

createFromTemplateOperation, err := deploymentClient.BeginCreateOrUpdate(
createFromTemplateOperation, err := ds.deploymentsClient.BeginCreateOrUpdate(
ctx, resourceGroup, deploymentName,
armresources.Deployment{
Properties: &armresources.DeploymentProperties{
Expand Down Expand Up @@ -310,18 +239,12 @@ func (ds *deployments) DeployToResourceGroup(

func (ds *deployments) WhatIfDeployToSubscription(
ctx context.Context,
subscriptionId string,
location string,
deploymentName string,
armTemplate azure.RawArmTemplate,
parameters azure.ArmParameters,
) (*armresources.WhatIfOperationResult, error) {
deploymentClient, err := ds.createDeploymentsClient(ctx, subscriptionId)
if err != nil {
return nil, fmt.Errorf("creating deployments client: %w", err)
}

createFromTemplateOperation, err := deploymentClient.BeginWhatIfAtSubscriptionScope(
createFromTemplateOperation, err := ds.deploymentsClient.BeginWhatIfAtSubscriptionScope(
ctx, deploymentName,
armresources.DeploymentWhatIf{
Properties: &armresources.DeploymentWhatIfProperties{
Expand Down Expand Up @@ -351,16 +274,11 @@ func (ds *deployments) WhatIfDeployToSubscription(

func (ds *deployments) WhatIfDeployToResourceGroup(
ctx context.Context,
subscriptionId, resourceGroup, deploymentName string,
resourceGroup, deploymentName string,
armTemplate azure.RawArmTemplate,
parameters azure.ArmParameters,
) (*armresources.WhatIfOperationResult, error) {
deploymentClient, err := ds.createDeploymentsClient(ctx, subscriptionId)
if err != nil {
return nil, fmt.Errorf("creating deployments client: %w", err)
}

createFromTemplateOperation, err := deploymentClient.BeginWhatIf(
createFromTemplateOperation, err := ds.deploymentsClient.BeginWhatIf(
ctx, resourceGroup, deploymentName,
armresources.DeploymentWhatIf{
Properties: &armresources.DeploymentWhatIfProperties{
Expand All @@ -387,13 +305,10 @@ func (ds *deployments) WhatIfDeployToResourceGroup(
}

func (ds *deployments) DeleteSubscriptionDeployment(
ctx context.Context, subscriptionId string, deploymentName string) error {
deploymentClient, err := ds.createDeploymentsClient(ctx, subscriptionId)
if err != nil {
return fmt.Errorf("deleting deployment: %w", err)
}

deleteDeploymentOperation, err := deploymentClient.BeginDeleteAtSubscriptionScope(ctx, deploymentName, nil)
ctx context.Context,
deploymentName string,
) error {
deleteDeploymentOperation, err := ds.deploymentsClient.BeginDeleteAtSubscriptionScope(ctx, deploymentName, nil)
if err != nil {
return fmt.Errorf("starting to delete deployment: %w", err)
}
Expand Down Expand Up @@ -527,10 +442,3 @@ func createDeploymentError(err error) error {

return err
}

func (ds *deployments) clientOptionsBuilder(ctx context.Context) *azsdk.ClientOptionsBuilder {
return azsdk.NewClientOptionsBuilder().
WithTransport(ds.httpClient).
WithPerCallPolicy(azsdk.NewUserAgentPolicy(ds.userAgent)).
WithPerCallPolicy(azsdk.NewMsCorrelationPolicy(ctx))
}
Loading
Loading