Skip to content

Commit

Permalink
Merge pull request #91 from env0/fix-resources-drift-detect
Browse files Browse the repository at this point in the history
Fix: project resource attributes drift detection
  • Loading branch information
yaronya authored Jun 2, 2021
2 parents d0fc6c8 + d3a43e6 commit c95fbb4
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 7 deletions.
1 change: 1 addition & 0 deletions env0/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ func runUnitTest(t *testing.T, testCase resource.TestCase, mockFunc func(mockFun
mockFunc(apiClientMock)

testCase.ProviderFactories = testUnitProviders
testCase.PreventPostDestroyRefresh = true
resource.UnitTest(&testReporter, testCase)
}

Expand Down
26 changes: 21 additions & 5 deletions env0/resource_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ func resourceProject() *schema.Resource {
}
}

func setProjectSchema(d *schema.ResourceData, project client.Project) {
d.Set("name", project.Name)
d.Set("description", project.Description)
}

func resourceProjectCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
apiClient := meta.(client.ApiClientInterface)

Expand All @@ -50,18 +55,21 @@ func resourceProjectCreate(ctx context.Context, d *schema.ResourceData, meta int
}

d.SetId(project.Id)
setProjectSchema(d, project)

return nil
}

func resourceProjectRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
apiClient := meta.(client.ApiClientInterface)

id := d.Id()
_, err := apiClient.Project(id)
project, err := apiClient.Project(d.Id())
if err != nil {
return diag.Errorf("could not get project: %v", err)
}

setProjectSchema(d, project)

return nil
}

Expand All @@ -73,10 +81,14 @@ func resourceProjectUpdate(ctx context.Context, d *schema.ResourceData, meta int
Name: d.Get("name").(string),
Description: d.Get("description").(string),
}
_, err := apiClient.ProjectUpdate(id, payload)

project, err := apiClient.ProjectUpdate(id, payload)
if err != nil {
return diag.Errorf("could not update project: %v", err)
}

setProjectSchema(d, project)

return nil
}

Expand All @@ -94,17 +106,21 @@ func resourceProjectDelete(ctx context.Context, d *schema.ResourceData, meta int
func resourceProjectImport(ctx context.Context, d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
id := d.Id()
var getErr diag.Diagnostics
_, uuidErr := uuid.Parse(id)
if uuidErr == nil {
_, err := uuid.Parse(id)
if err == nil {
log.Println("[INFO] Resolving Project by id: ", id)
_, getErr = getProjectById(id, meta)
} else {
log.Println("[DEBUG] ID is not a valid env0 id ", id)
log.Println("[INFO] Resolving Project by name: ", id)

var project client.Project
project, getErr = getProjectByName(id, meta)

d.SetId(project.Id)
setProjectSchema(d, project)
}

if getErr != nil {
return nil, errors.New(getErr[0].Summary)
} else {
Expand Down
8 changes: 6 additions & 2 deletions env0/resource_project_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package env0
import (
"fmt"
"github.com/env0/terraform-provider-env0/client"
"github.com/golang/mock/gomock"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"testing"
)
Expand All @@ -23,7 +24,6 @@ func TestUnitProjectResource(t *testing.T) {
}

testCase := resource.TestCase{
ProviderFactories: testUnitProviders,
Steps: []resource.TestStep{
{
Config: testEnv0ProjectResourceConfig(project.Name, project.Description),
Expand Down Expand Up @@ -51,7 +51,11 @@ func TestUnitProjectResource(t *testing.T) {
Description: updatedProject.Description,
}).Times(1).Return(updatedProject, nil)

mock.EXPECT().Project(project.Id).AnyTimes()
gomock.InOrder(
mock.EXPECT().Project(gomock.Any()).Times(2).Return(project, nil), // 1 after create, 1 before update
mock.EXPECT().Project(gomock.Any()).Times(1).Return(updatedProject, nil), // 1 after update
)

mock.EXPECT().ProjectDelete(project.Id).Times(1)
})
}
Expand Down

0 comments on commit c95fbb4

Please sign in to comment.