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

Allow env refresh without bicep #3796

Merged
merged 2 commits into from
Apr 26, 2024
Merged
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
18 changes: 16 additions & 2 deletions cli/azd/cmd/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,16 @@ 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/account"
"github.com/azure/azure-dev/cli/azd/pkg/environment"
"github.com/azure/azure-dev/cli/azd/pkg/environment/azdcontext"
"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/input"
"github.com/azure/azure-dev/cli/azd/pkg/output"
"github.com/azure/azure-dev/cli/azd/pkg/output/ux"
"github.com/azure/azure-dev/cli/azd/pkg/project"
"github.com/azure/azure-dev/cli/azd/pkg/prompt"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)
Expand Down Expand Up @@ -396,6 +399,7 @@ type envRefreshAction struct {
projectManager project.ProjectManager
env *environment.Environment
envManager environment.Manager
prompters prompt.Prompter
flags *envRefreshFlags
console input.Console
formatter output.Formatter
Expand All @@ -409,6 +413,7 @@ func newEnvRefreshAction(
projectManager project.ProjectManager,
env *environment.Environment,
envManager environment.Manager,
prompters prompt.Prompter,
flags *envRefreshFlags,
console input.Console,
formatter output.Formatter,
Expand All @@ -420,6 +425,7 @@ func newEnvRefreshAction(
projectManager: projectManager,
env: env,
envManager: envManager,
prompters: prompters,
console: console,
flags: flags,
formatter: formatter,
Expand All @@ -445,10 +451,18 @@ func (ef *envRefreshAction) Run(ctx context.Context) (*actions.ActionResult, err
}
defer func() { _ = infra.Cleanup() }()

if err := ef.provisionManager.Initialize(ctx, ef.projectConfig.Path, infra.Options); err != nil {
// env refresh supports "BYOI" infrastructure where bicep isn't available
err = ef.provisionManager.Initialize(ctx, ef.projectConfig.Path, infra.Options)
if errors.Is(err, bicep.ErrEnsureEnvPreReqBicepCompileFailed) {
// If bicep is not available, we continue to prompt for subscription and location unfiltered
err = provisioning.EnsureSubscriptionAndLocation(ctx, ef.envManager, ef.env, ef.prompters,
func(_ account.Location) bool { return true })
if err != nil {
return nil, err
}
} else if err != nil {
return nil, fmt.Errorf("initializing provisioning manager: %w", err)
}

// If resource group is defined within the project but not in the environment then
// add it to the environment to support BYOI lookup scenarios like ADE
// Infra providers do not currently have access to project configuration
Expand Down
72 changes: 72 additions & 0 deletions cli/azd/test/functional/env_refresh_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
package cli_test

import (
"os"
"path/filepath"
"testing"

"github.com/azure/azure-dev/cli/azd/pkg/environment/azdcontext"
"github.com/azure/azure-dev/cli/azd/test/azdcli"
"github.com/azure/azure-dev/cli/azd/test/recording"
"github.com/stretchr/testify/require"
)

// Validates that env refresh works even without bicep defined.
func Test_CLI_EnvRefresh_NoBicep(t *testing.T) {
// running this test in parallel is ok as it uses a t.TempDir()
t.Parallel()
ctx, cancel := newTestContext(t)
defer cancel()

dir := tempDirWithDiagnostics(t)
t.Logf("DIR: %s", dir)

session := recording.Start(t)

envName := randomOrStoredEnvName(session)
t.Logf("AZURE_ENV_NAME: %s", envName)

cli := azdcli.NewCLI(t, azdcli.WithSession(session))
cli.WorkingDirectory = dir
cli.Env = append(cli.Env, os.Environ()...)
cli.Env = append(cli.Env, "AZURE_LOCATION=eastus2")

err := copySample(dir, "storage")
require.NoError(t, err, "failed expanding sample")

_, err = cli.RunCommandWithStdIn(ctx, stdinForInit(envName), "init")
require.NoError(t, err)

_, err = cli.RunCommandWithStdIn(ctx, stdinForProvision(), "provision")
require.NoError(t, err)

// Remove .azure and infra
environment := filepath.Join(dir, azdcontext.EnvironmentDirectoryName)
require.NoError(t, os.RemoveAll(environment))

infraPath := filepath.Join(dir, "infra")
infraPathHidden := filepath.Join(dir, "infra-delete")
require.NoError(t, os.Rename(infraPath, infraPathHidden))

// Reuse same environment name
_, err = cli.RunCommandWithStdIn(ctx, envName+"\n", "env", "refresh")
require.NoError(t, err)

env, err := envFromAzdRoot(ctx, dir, envName)
require.NoError(t, err)

// Env refresh should populate these values
assertEnvValuesStored(t, env)

if session != nil {
session.Variables[recording.SubscriptionIdKey] = env.GetSubscriptionId()
}

// restore infra path for deletion
require.NoError(t, os.Rename(infraPathHidden, infraPath))

_, err = cli.RunCommand(ctx, "down", "--force", "--purge")
require.NoError(t, err)
}
Loading
Loading