Skip to content

Commit

Permalink
Feat: Refactor for Project Role assignments for Team
Browse files Browse the repository at this point in the history
  • Loading branch information
TomerHeber committed Apr 6, 2024
1 parent dc3d675 commit 4f64d85
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 110 deletions.
4 changes: 2 additions & 2 deletions client/api_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ type ApiClientInterface interface {
TeamCreate(payload TeamCreatePayload) (Team, error)
TeamUpdate(id string, payload TeamUpdatePayload) (Team, error)
TeamDelete(id string) error
TeamProjectAssignmentCreateOrUpdate(payload TeamProjectAssignmentPayload) (TeamProjectAssignment, error)
TeamProjectAssignmentDelete(assignmentId string) error
TeamProjectAssignmentCreateOrUpdate(payload *TeamProjectAssignmentPayload) (*TeamProjectAssignment, error)
TeamProjectAssignmentDelete(projectId string, teamId string) error
TeamProjectAssignments(projectId string) ([]TeamProjectAssignment, error)
Environments() ([]Environment, error)
ProjectEnvironments(projectId string) ([]Environment, error)
Expand Down
12 changes: 6 additions & 6 deletions client/api_client_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 15 additions & 24 deletions client/team_project_assignment.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
package client

import (
"errors"
)

type ProjectRole string

const (
Expand All @@ -18,40 +14,35 @@ func IsBuiltinProjectRole(role string) bool {
}

type TeamProjectAssignmentPayload struct {
TeamId string `json:"teamId"`
ProjectId string `json:"projectId"`
ProjectRole string `json:"projectRole" tfschema:"-"`
TeamId string `json:"teamId"`
ProjectId string `json:"projectId"`
Role string `json:"role" tfschema:"-"`
}

type TeamProjectAssignment struct {
Id string `json:"id"`
TeamId string `json:"teamId"`
ProjectId string `json:"projectId"`
ProjectRole string `json:"projectRole" tfschema:"-"`
Id string `json:"id"`
TeamId string `json:"teamId"`
ProjectId string `json:"projectId"`
Role string `json:"role" tfschema:"-"`
}

func (client *ApiClient) TeamProjectAssignmentCreateOrUpdate(payload TeamProjectAssignmentPayload) (TeamProjectAssignment, error) {
func (client *ApiClient) TeamProjectAssignmentCreateOrUpdate(payload *TeamProjectAssignmentPayload) (*TeamProjectAssignment, error) {
var result TeamProjectAssignment

var err = client.http.Post("/teams/assignments", payload, &result)

if err != nil {
return TeamProjectAssignment{}, err
if err := client.http.Post("/roles/assignments/teams", payload, &result); err != nil {
return nil, err
}
return result, nil

return &result, nil
}

func (client *ApiClient) TeamProjectAssignmentDelete(assignmentId string) error {
if assignmentId == "" {
return errors.New("empty assignmentId")
}
return client.http.Delete("/teams/assignments/"+assignmentId, nil)
func (client *ApiClient) TeamProjectAssignmentDelete(projectId string, teamId string) error {
return client.http.Delete("/roles/assignments/teams", map[string]string{"projectId": projectId, "teamId": teamId})
}

func (client *ApiClient) TeamProjectAssignments(projectId string) ([]TeamProjectAssignment, error) {

var result []TeamProjectAssignment
err := client.http.Get("/teams/assignments", map[string]string{"projectId": projectId}, &result)
err := client.http.Get("/roles/assignments/teams", map[string]string{"projectId": projectId}, &result)

if err != nil {
return []TeamProjectAssignment{}, err
Expand Down
43 changes: 18 additions & 25 deletions client/team_project_assignment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,36 +14,34 @@ var _ = Describe("TeamProjectAssignment", func() {
const dummyTeamId = "dummyTeamId"

mockTeamProjectAssignment := TeamProjectAssignment{
Id: dummyProjectAssignmentId,
ProjectId: dummyProjectId,
ProjectRole: dummyProjectRole,
TeamId: dummyTeamId,
Id: dummyProjectAssignmentId,
ProjectId: dummyProjectId,
Role: dummyProjectRole,
TeamId: dummyTeamId,
}

Describe("CreateOrUpdate", func() {
Describe("Success", func() {
var teamProjectAssignment TeamProjectAssignment
var teamProjectAssignment *TeamProjectAssignment
BeforeEach(func() {
expectedPayload := TeamProjectAssignmentPayload{
TeamId: dummyTeamId,
ProjectId: dummyProjectId,
ProjectRole: dummyProjectRole,
TeamId: dummyTeamId,
ProjectId: dummyProjectId,
Role: dummyProjectRole,
}
httpCall = mockHttpClient.EXPECT().
Post("/teams/assignments", expectedPayload, gomock.Any()).
Post("/roles/assignments/teams", &expectedPayload, gomock.Any()).
Do(func(path string, request interface{}, response *TeamProjectAssignment) {
*response = mockTeamProjectAssignment
}).Times(1)
teamProjectAssignment, _ = apiClient.TeamProjectAssignmentCreateOrUpdate(expectedPayload)
teamProjectAssignment, _ = apiClient.TeamProjectAssignmentCreateOrUpdate(&expectedPayload)

})

It("Should send POST request with params", func() {
httpCall.Times(1)
})
It("Should send POST request with params", func() {})

It("Should return a new resource with id", func() {
Expect(teamProjectAssignment).To(Equal(mockTeamProjectAssignment))
Expect(*teamProjectAssignment).To(Equal(mockTeamProjectAssignment))
})
})
})
Expand All @@ -53,16 +51,14 @@ var _ = Describe("TeamProjectAssignment", func() {
var teamProjectAssignments []TeamProjectAssignment
BeforeEach(func() {
httpCall = mockHttpClient.EXPECT().
Get("/teams/assignments", map[string]string{"projectId": mockTeamProjectAssignment.ProjectId}, gomock.Any()).
Get("/roles/assignments/teams", map[string]string{"projectId": mockTeamProjectAssignment.ProjectId}, gomock.Any()).
Do(func(path string, request interface{}, response *[]TeamProjectAssignment) {
*response = mockTeamProjectAssignments
})
}).Times(1)
teamProjectAssignments, _ = apiClient.TeamProjectAssignments(mockTeamProjectAssignment.ProjectId)
})

It("Should send GET request", func() {
httpCall.Times(1)
})
It("Should send GET request", func() {})

It("Should return the projects assignments", func() {
Expect(teamProjectAssignments).To(Equal(mockTeamProjectAssignments))
Expand All @@ -71,15 +67,12 @@ var _ = Describe("TeamProjectAssignment", func() {
})

Describe("Delete", func() {

BeforeEach(func() {
httpCall = mockHttpClient.EXPECT().Delete("/teams/assignments/"+mockTeamProjectAssignment.Id, nil)
apiClient.TeamProjectAssignmentDelete(mockTeamProjectAssignment.Id)
httpCall = mockHttpClient.EXPECT().Delete("/roles/assignments/teams", map[string]string{"projectId": mockTeamProjectAssignment.ProjectId, "teamId": mockTeamProjectAssignment.TeamId}).Times(1)
apiClient.TeamProjectAssignmentDelete(mockTeamProjectAssignment.ProjectId, mockTeamProjectAssignment.TeamId)
})

It("Should send DELETE request with assignment id", func() {
httpCall.Times(1)
})
It("Should send DELETE request with assignment id", func() {})
})

})
23 changes: 13 additions & 10 deletions env0/resource_team_project_assignment.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,10 @@ func resourceTeamProjectAssignmentRead(ctx context.Context, d *schema.ResourceDa
return diag.Errorf("schema resource data serialization failed: %v", err)
}

if client.IsBuiltinProjectRole(assignment.ProjectRole) {
d.Set("role", assignment.ProjectRole)
if client.IsBuiltinProjectRole(assignment.Role) {
d.Set("role", assignment.Role)
} else {
d.Set("custom_role_id", assignment.ProjectRole)
d.Set("custom_role_id", assignment.Role)
}

return nil
Expand All @@ -96,22 +96,25 @@ func resourceTeamProjectAssignmentCreateOrUpdate(ctx context.Context, d *schema.
if !ok {
role = d.Get("custom_role_id")
}
payload.ProjectRole = role.(string)
payload.Role = role.(string)

response, err := apiClient.TeamProjectAssignmentCreateOrUpdate(payload)
assignment, err := apiClient.TeamProjectAssignmentCreateOrUpdate(&payload)
if err != nil {
return diag.Errorf("could not Create or Update TeamProjectAssignment: %v", err)
}

d.SetId(response.Id)
d.SetId(assignment.Id)

return nil
}

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

err := apiClient.TeamProjectAssignmentDelete(d.Id())
projectId := d.Get("project_id").(string)
teamId := d.Get("team_id").(string)

err := apiClient.TeamProjectAssignmentDelete(projectId, teamId)
if err != nil {
return diag.Errorf("could not delete TeamProjectAssignment: %v", err)
}
Expand Down Expand Up @@ -141,10 +144,10 @@ func resourceTeamProjectAssignmentImport(ctx context.Context, d *schema.Resource
return nil, fmt.Errorf("schema resource data serialization failed: %w", err)
}

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

0 comments on commit 4f64d85

Please sign in to comment.