Skip to content

Commit

Permalink
Fix: importing a non-existent environment show an 'Authentication err…
Browse files Browse the repository at this point in the history
…or' in variable sets instead (#901)
  • Loading branch information
TomerHeber authored Jul 22, 2024
1 parent 588f12b commit 5eeae08
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 5 deletions.
12 changes: 7 additions & 5 deletions env0/resource_environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -1340,6 +1340,7 @@ func getEnvironmentById(environmentId string, meta interface{}) (client.Environm
func resourceEnvironmentImporter(ctx context.Context, d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
id := d.Id()
var getErr diag.Diagnostics

var environment client.Environment
_, err := uuid.Parse(id)
if err == nil {
Expand All @@ -1350,6 +1351,11 @@ func resourceEnvironmentImporter(ctx context.Context, d *schema.ResourceData, me

environment, getErr = getEnvironmentByName(meta, id, "", false)
}

if getErr != nil {
return nil, errors.New(getErr[0].Summary)
}

apiClient := meta.(client.ApiClientInterface)
d.SetId(environment.Id)

Expand Down Expand Up @@ -1406,9 +1412,5 @@ func resourceEnvironmentImporter(ctx context.Context, d *schema.ResourceData, me

d.Set("vcs_pr_comments_enabled", environment.VcsCommandsAlias != "" || environment.VcsPrCommentsEnabled)

if getErr != nil {
return nil, errors.New(getErr[0].Summary)
} else {
return []*schema.ResourceData{d}, nil
}
return []*schema.ResourceData{d}, nil
}
38 changes: 38 additions & 0 deletions env0/resource_environment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -744,6 +744,44 @@ func TestUnitEnvironmentResource(t *testing.T) {
})
})

t.Run("Import By Id - not found", func(t *testing.T) {
testCase := resource.TestCase{
Steps: []resource.TestStep{
{
Config: createEnvironmentResourceConfig(environment),
},
{
ResourceName: resourceNameImport,
ImportState: true,
ImportStateId: environment.Id,
ExpectError: regexp.MustCompile("Could not find environment: error"),
},
},
}

runUnitTest(t, testCase, func(mock *client.MockApiClientInterface) {
mock.EXPECT().Template(environment.LatestDeploymentLog.BlueprintId).Times(1).Return(template, nil)
mock.EXPECT().EnvironmentCreate(client.EnvironmentCreate{
Name: environment.Name,
ProjectId: environment.ProjectId,
WorkspaceName: environment.WorkspaceName,
AutoDeployByCustomGlob: autoDeployByCustomGlobDefault,
TerragruntWorkingDirectory: environment.TerragruntWorkingDirectory,
VcsCommandsAlias: environment.VcsCommandsAlias,
DeployRequest: &client.DeployRequest{
BlueprintId: templateId,
},
IsRemoteBackend: &isRemoteBackendFalse,
K8sNamespace: environment.K8sNamespace,
}).Times(1).Return(environment, nil)
mock.EXPECT().Environment(environment.Id).Times(1).Return(environment, nil)
mock.EXPECT().ConfigurationVariablesByScope(client.ScopeEnvironment, environment.Id).Times(1).Return(client.ConfigurationChanges{}, nil)
mock.EXPECT().ConfigurationSetsAssignments("ENVIRONMENT", environment.Id).Times(1).Return(nil, nil)
mock.EXPECT().Environment(environment.Id).Times(1).Return(client.Environment{}, errors.New("error"))
mock.EXPECT().EnvironmentDestroy(environment.Id).Times(1)
})
})

t.Run("Success create and remove drift cron", func(t *testing.T) {
testCase := resource.TestCase{
Steps: []resource.TestStep{
Expand Down

0 comments on commit 5eeae08

Please sign in to comment.