Skip to content

Commit

Permalink
use structure output for provision (#2887)
Browse files Browse the repository at this point in the history
* use structure output for provision

* use ux component

* test

* recordings update

* more recordings

* final fix

* fix test

* cl
  • Loading branch information
vhvb1989 committed Oct 25, 2023
1 parent 4e0d603 commit d165bd2
Show file tree
Hide file tree
Showing 15 changed files with 2,579 additions and 2,533 deletions.
8 changes: 3 additions & 5 deletions cli/azd/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
# Release History

## 1.5.0-beta.1 (Unreleased)
## 1.4.3 (2023-10-24)

### Features Added

- [[2787]](https://github.com/Azure/azure-dev/pull/2787) Added `azd config show` and deprecated `azd config list`.

### Breaking Changes

### Bugs Fixed

### Other Changes

- [[2887]](https://github.com/Azure/azure-dev/pull/2887) Update the subscription and location information during `azd provision`.

## 1.4.2 (2023-10-12)

### Features Added
Expand Down
48 changes: 25 additions & 23 deletions cli/azd/cmd/provision.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"io"
"log"
"time"

"github.com/azure/azure-dev/cli/azd/cmd/actions"
Expand Down Expand Up @@ -129,29 +130,6 @@ func (p *provisionAction) Run(ctx context.Context) (*actions.ActionResult, error
defaultTitleNote = "This is a preview. No changes will be applied to your Azure resources."
}

// Get Subscription to Display in Command Title Note
subscriptions, subErr := p.subManager.GetSubscriptions(ctx)
if subErr == nil {
// Find subscription name
for _, sub := range subscriptions {
if sub.Id == p.env.GetSubscriptionId() {
messageFormat := "Provisioning Azure resources in subscription (%s) %s and location (%s) can take some time"
if previewMode {
messageFormat = "This is a preview. No changes will be applied to your Azure resources in subscription (%s) %s " +
"and location (%s)."
}
// Formate the note
defaultTitleNote = fmt.Sprintf(
messageFormat,
sub.Name,
sub.Id,
p.env.GetLocation(),
)
break
}
}
}

p.console.MessageUxItem(ctx, &ux.MessageTitle{
Title: defaultTitle,
TitleNote: defaultTitleNote},
Expand All @@ -168,6 +146,30 @@ func (p *provisionAction) Run(ctx context.Context) (*actions.ActionResult, error
return nil, fmt.Errorf("initializing provisioning manager: %w", err)
}

// Get Subscription to Display in Command Title Note
// Subscription and Location are ONLY displayed when they are available (found from env), otherwise, this message
// is not displayed.
// This needs to happen after the provisionManager initializes to make sure the env is ready for the provisioning
// provider
subscription, subErr := p.subManager.GetSubscription(ctx, p.env.GetSubscriptionId())
if subErr == nil {
location, err := p.subManager.GetLocation(ctx, p.env.GetSubscriptionId(), p.env.GetLocation())
var locationDisplay string
if err != nil {
log.Printf("failed getting location: %v", err)
} else {
locationDisplay = location.DisplayName
}

p.console.MessageUxItem(ctx, &ux.EnvironmentDetails{
Subscription: fmt.Sprintf("%s (%s)", subscription.Name, subscription.Id),
Location: locationDisplay},
)

} else {
log.Printf("failed getting subscriptions. Skip displaying sub and location: %v", subErr)
}

var deployResult *provisioning.DeployResult
var deployPreviewResult *provisioning.DeployPreviewResult

Expand Down
2 changes: 1 addition & 1 deletion cli/azd/pkg/account/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ func Test_SetDefaultSubscription(t *testing.T) {
NewBypassSubscriptionsCache()))
require.NoError(t, err)

actualSubscription, err := manager.SetDefaultSubscription(context.Background(), expectedSubscription.Id)
actualSubscription, err := manager.SetDefaultSubscription(context.Background(), "invalid")

require.Error(t, err)
require.Nil(t, actualSubscription)
Expand Down
41 changes: 40 additions & 1 deletion cli/azd/pkg/account/subscriptions_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,20 @@ func (m *SubscriptionsManager) GetSubscriptions(ctx context.Context) ([]Subscrip
return subscriptions, nil
}

func (m *SubscriptionsManager) GetSubscription(ctx context.Context, subscriptionId string) (*Subscription, error) {
subscriptions, err := m.GetSubscriptions(ctx)
if err != nil {
return nil, err
}

for _, sub := range subscriptions {
if sub.Id == subscriptionId {
return &sub, nil
}
}
return m.getSubscription(ctx, subscriptionId)
}

type tenantSubsResult struct {
subs []Subscription
err error
Expand Down Expand Up @@ -278,6 +292,24 @@ func (m *SubscriptionsManager) ListSubscriptions(ctx context.Context) ([]Subscri
return allSubscriptions, nil
}

func (m *SubscriptionsManager) GetLocation(ctx context.Context, subscriptionId, locationName string) (Location, error) {
var err error
m.console.ShowSpinner(ctx, "Reading subscription and location from environment...", input.Step)
defer m.console.StopSpinner(ctx, "", input.GetStepResultFormat(err))

allLocations, err := m.listLocations(ctx, subscriptionId)
if err != nil {
return Location{}, err
}

for _, location := range allLocations {
if locationName == location.Name {
return location, nil
}
}
return Location{}, fmt.Errorf("location name %s not found", locationName)
}

func (m *SubscriptionsManager) ListLocations(
ctx context.Context,
subscriptionId string,
Expand All @@ -287,14 +319,21 @@ func (m *SubscriptionsManager) ListLocations(
m.console.ShowSpinner(ctx, msg, input.Step)
defer m.console.StopSpinner(ctx, msg, input.GetStepResultFormat(err))

return m.listLocations(ctx, subscriptionId)
}

func (m *SubscriptionsManager) listLocations(
ctx context.Context,
subscriptionId string,
) ([]Location, error) {
tenantId, err := m.LookupTenant(ctx, subscriptionId)
if err != nil {
return nil, err
}
return m.service.ListSubscriptionLocations(ctx, subscriptionId, tenantId)
}

func (m *SubscriptionsManager) GetSubscription(ctx context.Context, subscriptionId string) (*Subscription, error) {
func (m *SubscriptionsManager) getSubscription(ctx context.Context, subscriptionId string) (*Subscription, error) {
tenantId, err := m.LookupTenant(ctx, subscriptionId)
if err != nil {
return nil, err
Expand Down
34 changes: 34 additions & 0 deletions cli/azd/pkg/output/ux/environment_details.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

package ux

import (
"encoding/json"
"fmt"

"github.com/azure/azure-dev/cli/azd/pkg/output"
"github.com/fatih/color"
)

type EnvironmentDetails struct {
Subscription string
Location string
}

func (t *EnvironmentDetails) ToString(currentIndentation string) string {
var location string
if t.Location != "" {
location = fmt.Sprintf("\nLocation: %s", color.BlueString(t.Location))
}
return fmt.Sprintf(
"Subscription: %s%s\n",
color.BlueString(t.Subscription),
location,
)
}

func (t *EnvironmentDetails) MarshalJSON() ([]byte, error) {
// reusing the same envelope from console messages
return json.Marshal(output.EventForMessage(fmt.Sprintf("\n%s\n%s\n", t.Subscription, t.Location)))
}
20 changes: 20 additions & 0 deletions cli/azd/pkg/output/ux/environment_details_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

package ux

import (
"testing"

"github.com/azure/azure-dev/cli/azd/test/snapshot"
)

func TestEnvironmentDetails(t *testing.T) {
pp := &EnvironmentDetails{
Subscription: "Foo (bar)",
Location: "Somewhere cool",
}

output := pp.ToString(" ")
snapshot.SnapshotT(t, output)
}
3 changes: 3 additions & 0 deletions cli/azd/pkg/output/ux/testdata/TestEnvironmentDetails.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Subscription: Foo (bar)
Location: Somewhere cool

1,612 changes: 542 additions & 1,070 deletions cli/azd/test/functional/testdata/recordings/Test_CLI_InfraBicepParam.yaml

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,32 @@ interactions:
- --username
- 00000000-0000-0000-0000-000000000000
- --password-stdin
- crx57w4kclxqd5y.azurecr.io
- crzsryxds53ljkk.azurecr.io
exitCode: 0
stdout: |
Login Succeeded
stderr: ""
- id: 1
args:
- push
- crx57w4kclxqd5y.azurecr.io/containerapp/web-azdtest-l35f3c5:azd-deploy-1696620259
- crzsryxds53ljkk.azurecr.io/containerapp/web-azdtest-l93c276:azd-deploy-1698205558
exitCode: 0
stdout: |
The push refers to repository [crx57w4kclxqd5y.azurecr.io/containerapp/web-azdtest-l35f3c5]
8c9ec71d43c5: Preparing
The push refers to repository [crzsryxds53ljkk.azurecr.io/containerapp/web-azdtest-l93c276]
ab22857c740d: Preparing
01206a546ccb: Preparing
0b06ef9e62c7: Preparing
de65fc897446: Preparing
cfa45b411d67: Preparing
4170e926e0d8: Preparing
6e3b92711bf1: Preparing
6e3b92711bf1: Waiting
4170e926e0d8: Waiting
8c9ec71d43c5: Pushed
ab22857c740d: Pushed
de65fc897446: Pushed
01206a546ccb: Pushed
0b06ef9e62c7: Pushed
4170e926e0d8: Pushed
cfa45b411d67: Pushed
6e3b92711bf1: Pushed
azd-deploy-1696620259: digest: sha256:8a0c626ecd1955a5cd685b7f0d2f1980ce24d61c2a54bf37a72ea660b848d022 size: 1786
azd-deploy-1698205558: digest: sha256:d494a37502c47f3799238d062d8835814ed0fd22bacdbedee93e4c3e8a2c9d7a size: 1786
stderr: ""
1,043 changes: 560 additions & 483 deletions cli/azd/test/functional/testdata/recordings/Test_CLI_Up_Down_ContainerApp.yaml

Large diffs are not rendered by default.

663 changes: 391 additions & 272 deletions cli/azd/test/functional/testdata/recordings/Test_StorageBlobClient.yaml

Large diffs are not rendered by default.

0 comments on commit d165bd2

Please sign in to comment.