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

Fix: importing a non-existent environment show an 'Authentication err… #901

Merged
merged 1 commit into from
Jul 22, 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
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
Loading