From 9fe1fef73a9fbce35c1d0eb520d4abfdf9b8caf8 Mon Sep 17 00:00:00 2001 From: Tomer Heber Date: Sun, 21 Jul 2024 12:43:28 -0500 Subject: [PATCH] Fix: importing a non-existent environment show an 'Authentication error' in variable sets instead --- env0/resource_environment.go | 12 ++++++---- env0/resource_environment_test.go | 38 +++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 5 deletions(-) diff --git a/env0/resource_environment.go b/env0/resource_environment.go index 2446553c..cfd3d018 100644 --- a/env0/resource_environment.go +++ b/env0/resource_environment.go @@ -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 { @@ -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) @@ -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 } diff --git a/env0/resource_environment_test.go b/env0/resource_environment_test.go index e7ac6e1f..ef2a0b16 100644 --- a/env0/resource_environment_test.go +++ b/env0/resource_environment_test.go @@ -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{