Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
janboll committed Dec 19, 2024
1 parent 54d5b52 commit 5175ae9
Showing 1 changed file with 22 additions and 12 deletions.
34 changes: 22 additions & 12 deletions tooling/templatize/pkg/pipeline/arm.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/Azure/ARO-HCP/tooling/templatize/pkg/config"

"github.com/Azure/azure-sdk-for-go/sdk/azcore/runtime"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/resources/armresources"
Expand Down Expand Up @@ -96,6 +97,22 @@ func printChangeReport(changes []*armresources.WhatIfChange) {
printChanges(armresources.ChangeTypeUnsupported, changes)
}

func pollAndPrint[T any](ctx context.Context, p *runtime.Poller[T]) error {
resp, err := p.PollUntilDone(ctx, nil)
if err != nil {
return fmt.Errorf("failed to wait for deployment completion: %w", err)
}
switch m := any(resp).(type) {
case armresources.DeploymentsClientWhatIfResponse:
printChangeReport(m.Properties.Changes)
case armresources.DeploymentsClientWhatIfAtSubscriptionScopeResponse:
printChangeReport(m.Properties.Changes)
default:
return fmt.Errorf("Unknown type %T", m)
}
return nil
}

func doDryRun(ctx context.Context, client *armresources.DeploymentsClient, rgName string, step *Step, vars config.Variables, input map[string]output) (output, error) {
logger := logr.FromContextOrDiscard(ctx)

Expand All @@ -112,36 +129,29 @@ func doDryRun(ctx context.Context, client *armresources.DeploymentsClient, rgNam
// Create the deployment
deployment := armresources.DeploymentWhatIf{
Properties: deploymentProperties,
Location: to.Ptr("westus3"),
}

if step.DeploymentLevel == "Subscription" {
poller, err := client.BeginWhatIfAtSubscriptionScope(ctx, step.Name, deployment, nil)

if err != nil {
return nil, fmt.Errorf("failed to create WhatIf Deployment: %w", err)
}
logger.Info("WhatIf Deployment started", "deployment", step.Name)

resp, err := poller.PollUntilDone(ctx, nil)
err = pollAndPrint(ctx, poller)
if err != nil {
return nil, fmt.Errorf("failed to wait for deployment completion: %w", err)
return nil, fmt.Errorf("failed to poll and print: %w", err)
}
logger.Info("WhatIf Deployment finished successfully", "deployment", step.Name)
printChangeReport(resp.Properties.Changes)
} else {
poller, err := client.BeginWhatIf(ctx, rgName, step.Name, deployment, nil)

if err != nil {
return nil, fmt.Errorf("failed to create WhatIf Deployment: %w", err)
}
logger.Info("WhatIf Deployment started", "deployment", step.Name)

resp, err := poller.PollUntilDone(ctx, nil)
err = pollAndPrint(ctx, poller)
if err != nil {
return nil, fmt.Errorf("failed to wait for deployment completion: %w", err)
return nil, fmt.Errorf("failed to poll and print: %w", err)
}
logger.Info("WhatIf Deployment finished successfully", "deployment", step.Name)
printChangeReport(resp.Properties.Changes)
}

return nil, nil
Expand Down

0 comments on commit 5175ae9

Please sign in to comment.