Skip to content

Commit

Permalink
Feat: env0_team_project_assignment doesn't support import (#679)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomerHeber authored Jul 17, 2023
1 parent 7548497 commit c6efab2
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 0 deletions.
38 changes: 38 additions & 0 deletions env0/resource_team_project_assignment.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package env0

import (
"context"
"fmt"
"log"
"strings"

"github.com/env0/terraform-provider-env0/client"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
Expand All @@ -16,6 +18,8 @@ func resourceTeamProjectAssignment() *schema.Resource {
UpdateContext: resourceTeamProjectAssignmentCreateOrUpdate,
DeleteContext: resourceTeamProjectAssignmentDelete,

Importer: &schema.ResourceImporter{StateContext: resourceTeamProjectAssignmentImport},

Schema: map[string]*schema.Schema{
"team_id": {
Type: schema.TypeString,
Expand Down Expand Up @@ -114,3 +118,37 @@ func resourceTeamProjectAssignmentDelete(ctx context.Context, d *schema.Resource

return nil
}

func resourceTeamProjectAssignmentImport(ctx context.Context, d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
splitTeamProject := strings.Split(d.Id(), "_")
if len(splitTeamProject) != 2 {
return nil, fmt.Errorf("the id %v is invalid must be <team_id>_<project_id>", d.Id())
}

teamId := splitTeamProject[0]
projectId := splitTeamProject[1]

apiClient := meta.(client.ApiClientInterface)

assignments, err := apiClient.TeamProjectAssignments(projectId)
if err != nil {
return nil, err
}

for _, assignment := range assignments {
if assignment.TeamId == teamId {
if err := writeResourceData(&assignment, d); err != nil {
return nil, fmt.Errorf("schema resource data serialization failed: %w", err)
}

if client.IsBuiltinProjectRole(assignment.ProjectRole) {
d.Set("role", assignment.ProjectRole)
} else {
d.Set("custom_role_id", assignment.ProjectRole)
}
return []*schema.ResourceData{d}, nil
}
}

return nil, fmt.Errorf("assignment with id %v not found", d.Id())
}
81 changes: 81 additions & 0 deletions env0/resource_team_project_assignment_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package env0

import (
"regexp"
"testing"

"github.com/env0/terraform-provider-env0/client"
Expand All @@ -11,6 +12,7 @@ import (
func TestUnitTeamProjectAssignmentResource(t *testing.T) {
resourceType := "env0_team_project_assignment"
resourceName := "test"
resourceNameImport := resourceType + "." + resourceName

accessor := resourceAccessor(resourceType, resourceName)

Expand Down Expand Up @@ -168,4 +170,83 @@ func TestUnitTeamProjectAssignmentResource(t *testing.T) {
})
})

t.Run("import - built-in role", func(t *testing.T) {
testCase := resource.TestCase{
Steps: []resource.TestStep{
{
Config: resourceConfigCreate(resourceType, resourceName, map[string]interface{}{
"team_id": assignment.TeamId,
"project_id": assignment.ProjectId,
"role": assignment.ProjectRole,
}),
},
{
ResourceName: resourceNameImport,
ImportState: true,
ImportStateId: assignment.TeamId + "_" + assignment.ProjectId,
ImportStateVerify: true,
},
},
}

runUnitTest(t, testCase, func(mock *client.MockApiClientInterface) {
mock.EXPECT().TeamProjectAssignmentCreateOrUpdate(client.TeamProjectAssignmentPayload{TeamId: assignment.TeamId, ProjectId: assignment.ProjectId, ProjectRole: assignment.ProjectRole}).Times(1).Return(assignment, nil)
mock.EXPECT().TeamProjectAssignments(assignment.ProjectId).Times(3).Return([]client.TeamProjectAssignment{assignment}, nil)
mock.EXPECT().TeamProjectAssignmentDelete(assignment.Id).Times(1).Return(nil)
})
})

t.Run("import - custom role", func(t *testing.T) {
testCase := resource.TestCase{
Steps: []resource.TestStep{
{
Config: resourceConfigCreate(resourceType, resourceName, map[string]interface{}{
"team_id": assignmentCustom.TeamId,
"project_id": assignmentCustom.ProjectId,
"custom_role_id": assignmentCustom.ProjectRole,
}),
},
{
ResourceName: resourceNameImport,
ImportState: true,
ImportStateId: assignmentCustom.TeamId + "_" + assignmentCustom.ProjectId,
ImportStateVerify: true,
},
},
}

runUnitTest(t, testCase, func(mock *client.MockApiClientInterface) {
mock.EXPECT().TeamProjectAssignmentCreateOrUpdate(client.TeamProjectAssignmentPayload{TeamId: assignmentCustom.TeamId, ProjectId: assignmentCustom.ProjectId, ProjectRole: assignmentCustom.ProjectRole}).Times(1).Return(assignmentCustom, nil)
mock.EXPECT().TeamProjectAssignments(assignmentCustom.ProjectId).Times(3).Return([]client.TeamProjectAssignment{assignmentCustom}, nil)
mock.EXPECT().TeamProjectAssignmentDelete(assignmentCustom.Id).Times(1).Return(nil)
})
})

t.Run("Import role - not found", func(t *testing.T) {
testCase := resource.TestCase{
Steps: []resource.TestStep{
{
Config: resourceConfigCreate(resourceType, resourceName, map[string]interface{}{
"team_id": assignment.TeamId,
"project_id": assignment.ProjectId,
"role": assignment.ProjectRole,
}),
},
{
ResourceName: resourceNameImport,
ImportState: true,
ImportStateId: assignment.TeamId + "_" + assignment.ProjectId,
ImportStateVerify: true,
ExpectError: regexp.MustCompile("not found"),
},
},
}

runUnitTest(t, testCase, func(mock *client.MockApiClientInterface) {
mock.EXPECT().TeamProjectAssignmentCreateOrUpdate(client.TeamProjectAssignmentPayload{TeamId: assignment.TeamId, ProjectId: assignment.ProjectId, ProjectRole: assignment.ProjectRole}).Times(1).Return(assignment, nil)
mock.EXPECT().TeamProjectAssignments(assignment.ProjectId).Times(1).Return([]client.TeamProjectAssignment{assignment}, nil)
mock.EXPECT().TeamProjectAssignments(assignment.ProjectId).Times(1).Return([]client.TeamProjectAssignment{}, nil)
mock.EXPECT().TeamProjectAssignmentDelete(assignment.Id).Times(1)
})
})
}
1 change: 1 addition & 0 deletions examples/resources/env0_team_project_assignment/import.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
terraform import env0_team_project_assignment.example team-id_project-id

0 comments on commit c6efab2

Please sign in to comment.