Skip to content

Commit

Permalink
Merge branch 'main' of github.com:Azure/azure-dev into fix-387
Browse files Browse the repository at this point in the history
  • Loading branch information
vhvb1989 committed Dec 12, 2023
2 parents 275b5de + a029661 commit cccd1ed
Show file tree
Hide file tree
Showing 31 changed files with 122 additions and 82 deletions.
2 changes: 1 addition & 1 deletion cli/azd/cmd/auth_token.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func getTenantIdFromAzdEnv(
if err != nil {
return tenantId, fmt.Errorf(
"resolving the Azure Directory from azd environment (%s): %w",
azdEnv.GetEnvName(),
azdEnv.Name(),
err)
}

Expand Down
4 changes: 2 additions & 2 deletions cli/azd/cmd/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ func (en *envNewAction) Run(ctx context.Context) (*actions.ActionResult, error)
return nil, fmt.Errorf("creating new environment: %w", err)
}

if err := en.azdCtx.SetDefaultEnvironmentName(env.GetEnvName()); err != nil {
if err := en.azdCtx.SetDefaultEnvironmentName(env.Name()); err != nil {
return nil, fmt.Errorf("saving default environment: %w", err)
}

Expand Down Expand Up @@ -432,7 +432,7 @@ func newEnvRefreshAction(
func (ef *envRefreshAction) Run(ctx context.Context) (*actions.ActionResult, error) {
// Command title
ef.console.MessageUxItem(ctx, &ux.MessageTitle{
Title: fmt.Sprintf("Refreshing environment %s (azd env refresh)", ef.env.GetEnvName()),
Title: fmt.Sprintf("Refreshing environment %s (azd env refresh)", ef.env.Name()),
})

if err := ef.projectManager.Initialize(ctx, ef.projectConfig); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion cli/azd/cmd/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func (hra *hooksRunAction) Run(ctx context.Context) (*actions.ActionResult, erro
TitleNote: fmt.Sprintf(
"Finding and executing %s hooks for environment %s",
output.WithHighLightFormat(hookName),
output.WithHighLightFormat(hra.env.GetEnvName()),
output.WithHighLightFormat(hra.env.Name()),
),
})

Expand Down
2 changes: 1 addition & 1 deletion cli/azd/cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ func (i *initAction) initializeEnv(
return nil, fmt.Errorf("loading environment: %w", err)
}

if err := azdCtx.SetDefaultEnvironmentName(env.GetEnvName()); err != nil {
if err := azdCtx.SetDefaultEnvironmentName(env.Name()); err != nil {
return nil, fmt.Errorf("saving default environment: %w", err)
}

Expand Down
2 changes: 1 addition & 1 deletion cli/azd/cmd/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func (m *monitorAction) Run(ctx context.Context) (*actions.ActionResult, error)

resourceManager := infra.NewAzureResourceManager(m.azCli, m.deploymentOperations)
resourceGroups, err := resourceManager.GetResourceGroupsForEnvironment(
ctx, m.env.GetSubscriptionId(), m.env.GetEnvName())
ctx, m.env.GetSubscriptionId(), m.env.Name())
if err != nil {
return nil, fmt.Errorf("discovering resource groups from deployment: %w", err)
}
Expand Down
4 changes: 2 additions & 2 deletions cli/azd/cmd/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ func (s *showAction) Run(ctx context.Context) (*actions.ActionResult, error) {
} else {
azureResourceManager := infra.NewAzureResourceManager(s.azCli, s.deploymentOperations)
resourceManager := project.NewResourceManager(env, s.azCli, s.deploymentOperations)
envName := env.GetEnvName()
envName := env.Name()

rgName, err = azureResourceManager.FindResourceGroupForEnvironment(ctx, subId, envName)
if err == nil {
Expand All @@ -185,7 +185,7 @@ func (s *showAction) Run(ctx context.Context) (*actions.ActionResult, error) {
} else {
log.Printf(
"ignoring error determining resource group for environment %s, resource ids will not be available: %v",
env.GetEnvName(),
env.Name(),
err)
}
}
Expand Down
8 changes: 6 additions & 2 deletions cli/azd/internal/repository/detect_confirm_apphost.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,12 @@ func (d *detectConfirmAppHost) render(ctx context.Context) error {
d.console.Message(ctx, " "+color.BlueString(projectDisplayName(d.AppHost)))
d.console.Message(ctx, " "+"Detected in: "+output.WithHighLightFormat(relSafe(d.root, d.AppHost.Path)))
d.console.Message(ctx, "")
d.console.Message(ctx,
"azd will generate the files necessary to host your app on Azure using "+color.MagentaString("Azure Container Apps")+".\n")
d.console.Message(
ctx,
"azd will generate the files necessary to host your app on Azure using "+color.MagentaString(
"Azure Container Apps",
)+".\n",
)

return nil
}
2 changes: 1 addition & 1 deletion cli/azd/pkg/azdo/build_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func CreateBuildPolicy(

policySettings := map[string]interface{}{
"buildDefinitionId": buildDefinition.Id,
"displayName": fmt.Sprintf("Azure Dev Deploy PR - %s", env.GetEnvName()),
"displayName": fmt.Sprintf("Azure Dev Deploy PR - %s", env.Name()),
"manualQueueOnly": false,
"queueOnSourceUpdateOnly": true,
"validDuration": 720,
Expand Down
2 changes: 1 addition & 1 deletion cli/azd/pkg/azdo/pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ func getDefinitionVariables(
additionalVariables map[string]string) (*map[string]build.BuildDefinitionVariable, error) {
variables := map[string]build.BuildDefinitionVariable{
"AZURE_LOCATION": createBuildDefinitionVariable(env.GetLocation(), false, false),
"AZURE_ENV_NAME": createBuildDefinitionVariable(env.GetEnvName(), false, false),
"AZURE_ENV_NAME": createBuildDefinitionVariable(env.Name(), false, false),
"AZURE_SERVICE_CONNECTION": createBuildDefinitionVariable(ServiceConnectionName, false, false),
"AZURE_SUBSCRIPTION_ID": createBuildDefinitionVariable(credentials.SubscriptionId, false, false),
}
Expand Down
6 changes: 3 additions & 3 deletions cli/azd/pkg/devcenter/environment_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func NewEnvironmentStore(

// EnvPath returns the path for the environment
func (s *EnvironmentStore) EnvPath(env *environment.Environment) string {
return fmt.Sprintf("projects/%s/users/me/environments/%s", s.config.Project, env.GetEnvName())
return fmt.Sprintf("projects/%s/users/me/environments/%s", s.config.Project, env.Name())
}

// ConfigPath returns the path for the environment configuration
Expand Down Expand Up @@ -106,7 +106,7 @@ func (s *EnvironmentStore) Get(ctx context.Context, name string) (*environment.E
// Reload reloads the environment from the remote data store
func (s *EnvironmentStore) Reload(ctx context.Context, env *environment.Environment) error {
filter := func(e *devcentersdk.Environment) bool {
return s.envDefFilter(e) && strings.EqualFold(e.Name, env.GetEnvName())
return s.envDefFilter(e) && strings.EqualFold(e.Name, env.Name())
}

envList, err := s.matchingEnvironments(ctx, filter)
Expand All @@ -122,7 +122,7 @@ func (s *EnvironmentStore) Reload(ctx context.Context, env *environment.Environm
DevCenterByName(s.config.Name).
ProjectByName(s.config.Project).
EnvironmentsByUser(envList[0].User).
EnvironmentByName(env.GetEnvName()).
EnvironmentByName(env.Name()).
Get(ctx)

if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions cli/azd/pkg/devcenter/environment_store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ func Test_EnvironmentStore_Get(t *testing.T) {
env, err := store.Get(*mockContext.Context, mockEnvironments[0].Name)
require.NoError(t, err)
require.NotNil(t, env)
require.Equal(t, mockEnvironments[0].Name, env.GetEnvName())
require.Equal(t, mockEnvironments[0].Name, env.Name())
require.Equal(t, "value1", env.Getenv("KEY1"))
require.Equal(t, "value2", env.Getenv("KEY2"))

Expand Down Expand Up @@ -206,7 +206,7 @@ func Test_EnvironmentStore_GetEnvPath(t *testing.T) {
store := newEnvironmentStoreForTest(t, mockContext, config, nil)
env := environment.New(mockEnvironments[0].Name)
path := store.EnvPath(env)
require.Equal(t, fmt.Sprintf("projects/%s/users/me/environments/%s", config.Project, env.GetEnvName()), path)
require.Equal(t, fmt.Sprintf("projects/%s/users/me/environments/%s", config.Project, env.Name()), path)
}

func Test_EnvironmentStore_Save(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion cli/azd/pkg/devcenter/prompter.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ func (p *Prompter) PromptParameters(
// Only prompt for parameter values when it has not already been set in the environment configuration
if !exists {
if param.Name == "environmentName" {
paramValues[param.Id] = env.GetEnvName()
paramValues[param.Id] = env.Name()
continue
}

Expand Down
6 changes: 3 additions & 3 deletions cli/azd/pkg/devcenter/provision_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func (p *ProvisionProvider) State(
return nil, fmt.Errorf("invalid devcenter configuration, %w", err)
}

envName := p.env.GetEnvName()
envName := p.env.Name()
environment, err := p.devCenterClient.
DevCenterByName(p.config.Name).
ProjectByName(p.config.Project).
Expand Down Expand Up @@ -159,7 +159,7 @@ func (p *ProvisionProvider) Deploy(ctx context.Context) (*provisioning.DeployRes
return nil, fmt.Errorf("failed saving environment: %w", err)
}

envName := p.env.GetEnvName()
envName := p.env.Name()

// Check to see if an existing devcenter environment already exists
existingEnv, _ := p.devCenterClient.
Expand Down Expand Up @@ -256,7 +256,7 @@ func (p *ProvisionProvider) Destroy(
return nil, fmt.Errorf("invalid devcenter configuration, %w", err)
}

envName := p.env.GetEnvName()
envName := p.env.Name()
spinnerMessage := fmt.Sprintf("Deleting devcenter environment %s", output.WithHighLightFormat(envName))

if !options.Force() {
Expand Down
26 changes: 13 additions & 13 deletions cli/azd/pkg/devcenter/provision_provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,12 @@ func Test_ProvisionProvider_Deploy(t *testing.T) {
config.EnvironmentDefinition,
mockEnvDefinitions[0],
)
mockdevcentersdk.MockGetEnvironment(mockContext, config.Project, config.User, env.GetEnvName(), mockEnvironments[0])
mockdevcentersdk.MockGetEnvironment(mockContext, config.Project, config.User, env.Name(), mockEnvironments[0])
mockdevcentersdk.MockPutEnvironment(
mockContext,
config.Project,
config.User,
env.GetEnvName(),
env.Name(),
&devcentersdk.OperationStatus{
Id: "id",
Name: mockEnvironments[0].Name,
Expand Down Expand Up @@ -177,12 +177,12 @@ func Test_ProvisionProvider_Deploy(t *testing.T) {
config.EnvironmentDefinition,
mockEnvDefinitions[3],
)
mockdevcentersdk.MockGetEnvironment(mockContext, config.Project, config.User, env.GetEnvName(), mockEnvironments[0])
mockdevcentersdk.MockGetEnvironment(mockContext, config.Project, config.User, env.Name(), mockEnvironments[0])
mockdevcentersdk.MockPutEnvironment(
mockContext,
config.Project,
config.User,
env.GetEnvName(),
env.Name(),
&devcentersdk.OperationStatus{
Id: "id",
Name: mockEnvironments[0].Name,
Expand Down Expand Up @@ -229,12 +229,12 @@ func Test_ProvisionProvider_Deploy(t *testing.T) {
config.EnvironmentDefinition,
mockEnvDefinitions[0],
)
mockdevcentersdk.MockGetEnvironment(mockContext, config.Project, config.User, env.GetEnvName(), nil)
mockdevcentersdk.MockGetEnvironment(mockContext, config.Project, config.User, env.Name(), nil)
mockdevcentersdk.MockPutEnvironment(
mockContext,
config.Project,
config.User,
env.GetEnvName(),
env.Name(),
&devcentersdk.OperationStatus{
Id: "id",
Name: mockEnvironments[0].Name,
Expand Down Expand Up @@ -276,7 +276,7 @@ func Test_ProvisionProvider_State(t *testing.T) {
Return(outputParams, nil)

mockdevcentersdk.MockDevCenterGraphQuery(mockContext, mockDevCenterList)
mockdevcentersdk.MockGetEnvironment(mockContext, config.Project, config.User, env.GetEnvName(), mockEnvironments[0])
mockdevcentersdk.MockGetEnvironment(mockContext, config.Project, config.User, env.Name(), mockEnvironments[0])

provider := newProvisionProviderForTest(t, mockContext, config, env, manager)
result, err := provider.State(*mockContext.Context, &provisioning.StateOptions{})
Expand All @@ -298,7 +298,7 @@ func Test_ProvisionProvider_State(t *testing.T) {
env := environment.New("test")

mockdevcentersdk.MockDevCenterGraphQuery(mockContext, mockDevCenterList)
mockdevcentersdk.MockGetEnvironment(mockContext, config.Project, config.User, env.GetEnvName(), nil)
mockdevcentersdk.MockGetEnvironment(mockContext, config.Project, config.User, env.Name(), nil)

provider := newProvisionProviderForTest(t, mockContext, config, env, nil)
result, err := provider.State(*mockContext.Context, &provisioning.StateOptions{})
Expand All @@ -324,7 +324,7 @@ func Test_ProvisionProvider_State(t *testing.T) {
Return(nil, errors.New("no outputs"))

mockdevcentersdk.MockDevCenterGraphQuery(mockContext, mockDevCenterList)
mockdevcentersdk.MockGetEnvironment(mockContext, config.Project, config.User, env.GetEnvName(), mockEnvironments[0])
mockdevcentersdk.MockGetEnvironment(mockContext, config.Project, config.User, env.Name(), mockEnvironments[0])

provider := newProvisionProviderForTest(t, mockContext, config, env, manager)
result, err := provider.State(*mockContext.Context, &provisioning.StateOptions{})
Expand All @@ -347,12 +347,12 @@ func Test_ProvisionProvider_Destroy(t *testing.T) {
env := environment.New("test")

mockdevcentersdk.MockDevCenterGraphQuery(mockContext, mockDevCenterList)
mockdevcentersdk.MockGetEnvironment(mockContext, config.Project, config.User, env.GetEnvName(), mockEnvironments[0])
mockdevcentersdk.MockGetEnvironment(mockContext, config.Project, config.User, env.Name(), mockEnvironments[0])
mockdevcentersdk.MockDeleteEnvironment(
mockContext,
config.Project,
config.User,
env.GetEnvName(),
env.Name(),
&devcentersdk.OperationStatus{
Id: "id",
Name: mockEnvironments[0].Name,
Expand Down Expand Up @@ -398,8 +398,8 @@ func Test_ProvisionProvider_Destroy(t *testing.T) {
env := environment.New("test")

mockdevcentersdk.MockDevCenterGraphQuery(mockContext, mockDevCenterList)
mockdevcentersdk.MockGetEnvironment(mockContext, config.Project, config.User, env.GetEnvName(), nil)
mockdevcentersdk.MockDeleteEnvironment(mockContext, config.Project, config.User, env.GetEnvName(), nil)
mockdevcentersdk.MockGetEnvironment(mockContext, config.Project, config.User, env.Name(), nil)
mockdevcentersdk.MockDeleteEnvironment(mockContext, config.Project, config.User, env.Name(), nil)

provider := newProvisionProviderForTest(t, mockContext, config, env, nil)
destroyOptions := provisioning.NewDestroyOptions(true, true)
Expand Down
18 changes: 9 additions & 9 deletions cli/azd/pkg/environment/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ func New(name string) *Environment {
Config: getInitialConfig(),
}

env.SetEnvName(name)
env.DotenvSet(EnvNameEnvVarName, name)

return env
}

Expand Down Expand Up @@ -100,7 +101,6 @@ func NewWithValues(name string, values map[string]string) *Environment {
env.dotenv = values
}

env.SetEnvName(name)
return env
}

Expand Down Expand Up @@ -179,14 +179,14 @@ func (e *Environment) DotenvSet(key string, value string) {
delete(e.deletedKeys, key)
}

// GetEnvName is shorthand for Getenv(EnvNameEnvVarName)
func (e *Environment) GetEnvName() string {
return e.Getenv(EnvNameEnvVarName)
}
// Name gets the name of the environment
// If empty will fallback to the value of the AZURE_ENV_NAME environment variable
func (e *Environment) Name() string {
if e.name == "" {
e.name = e.Getenv(EnvNameEnvVarName)
}

// SetEnvName is shorthand for DotenvSet(EnvNameEnvVarName, envname)
func (e *Environment) SetEnvName(envname string) {
e.DotenvSet(EnvNameEnvVarName, envname)
return e.name
}

// GetSubscriptionId is shorthand for Getenv(SubscriptionIdEnvVarName)
Expand Down
6 changes: 3 additions & 3 deletions cli/azd/pkg/environment/local_file_data_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,8 @@ func (fs *LocalFileDataStore) Reload(ctx context.Context, env *Environment) erro
env.Config = cfg
}

if env.GetEnvName() != "" {
tracing.SetUsageAttributes(fields.StringHashed(fields.EnvNameKey, env.GetEnvName()))
if env.Name() != "" {
tracing.SetUsageAttributes(fields.StringHashed(fields.EnvNameKey, env.Name()))
}

if _, err := uuid.Parse(env.GetSubscriptionId()); err == nil {
Expand Down Expand Up @@ -172,6 +172,6 @@ func (fs *LocalFileDataStore) Save(ctx context.Context, env *Environment) error
return fmt.Errorf("saving .env: %w", err)
}

tracing.SetUsageAttributes(fields.StringHashed(fields.EnvNameKey, env.GetEnvName()))
tracing.SetUsageAttributes(fields.StringHashed(fields.EnvNameKey, env.Name()))
return nil
}
15 changes: 10 additions & 5 deletions cli/azd/pkg/environment/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,15 +209,11 @@ func (m *manager) LoadOrCreateInteractive(ctx context.Context, environmentName s
}

if isNew {
if env.GetEnvName() == "" {
env.SetEnvName(environmentName)
}

if err := m.Save(ctx, env); err != nil {
return nil, err
}

if err := m.azdContext.SetDefaultEnvironmentName(env.GetEnvName()); err != nil {
if err := m.azdContext.SetDefaultEnvironmentName(env.Name()); err != nil {
return nil, fmt.Errorf("saving default environment: %w", err)
}
}
Expand Down Expand Up @@ -310,6 +306,15 @@ func (m *manager) Get(ctx context.Context, name string) (*Environment, error) {
localEnv = remoteEnv
}

// Ensures local environment variable name is synced with the environment name
envName, ok := localEnv.LookupEnv(EnvNameEnvVarName)
if !ok || envName != name {
localEnv.DotenvSet(EnvNameEnvVarName, name)
if err := m.Save(ctx, localEnv); err != nil {
return nil, err
}
}

return localEnv, nil
}

Expand Down
Loading

0 comments on commit cccd1ed

Please sign in to comment.