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

Feat: env0_team_project_assignment doesn't support import #679

Merged
merged 1 commit into from
Jul 17, 2023
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
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())
}
Comment on lines +123 to +126
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add documentation for it?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Example is added:
examples/resources/env0_team_project_assignment/import.sh


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