diff --git a/client/api_client.go b/client/api_client.go index d8aa83d3..b5667d3c 100644 --- a/client/api_client.go +++ b/client/api_client.go @@ -61,9 +61,6 @@ 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(projectId string, teamId string) error - TeamProjectAssignments(projectId string) ([]TeamProjectAssignment, error) Environments() ([]Environment, error) ProjectEnvironments(projectId string) ([]Environment, error) Environment(id string) (Environment, error) @@ -138,12 +135,6 @@ type ApiClientInterface interface { AssignUserRoleToEnvironment(payload *AssignUserRoleToEnvironmentPayload) (*UserRoleEnvironmentAssignment, error) RemoveUserRoleFromEnvironment(environmentId string, userId string) error UserRoleEnvironmentAssignments(environmentId string) ([]UserRoleEnvironmentAssignment, error) - AssignTeamRoleToEnvironment(payload *AssignTeamRoleToEnvironmentPayload) (*TeamRoleEnvironmentAssignment, error) - RemoveTeamRoleFromEnvironment(environmentId string, teamId string) error - TeamRoleEnvironmentAssignments(environmentId string) ([]TeamRoleEnvironmentAssignment, error) - AssignOrganizationRoleToTeam(payload *AssignOrganizationRoleToTeamPayload) (*OrganizationRoleTeamAssignment, error) - RemoveOrganizationRoleFromTeam(teamId string) error - OrganizationRoleTeamAssignments() ([]OrganizationRoleTeamAssignment, error) ApprovalPolicies(name string) ([]ApprovalPolicy, error) ApprovalPolicyAssign(assignment *ApprovalPolicyAssignment) (*ApprovalPolicyAssignment, error) ApprovalPolicyUnassign(scope string, scopeId string) error @@ -156,6 +147,9 @@ type ApiClientInterface interface { PutEnvironmentDiscovery(projectId string, payload *EnvironmentDiscoveryPutPayload) (*EnvironmentDiscoveryPayload, error) GetEnvironmentDiscovery(projectId string) (*EnvironmentDiscoveryPayload, error) DeleteEnvironmentDiscovery(projectId string) error + TeamRoleAssignmentCreateOrUpdate(payload *TeamRoleAssignmentCreateOrUpdatePayload) (*TeamRoleAssignmentPayload, error) + TeamRoleAssignmentDelete(payload *TeamRoleAssignmentDeletePayload) error + TeamRoleAssignments(payload *TeamRoleAssignmentListPayload) ([]TeamRoleAssignmentPayload, error) } func NewApiClient(client http.HttpClientInterface, defaultOrganizationId string) ApiClientInterface { diff --git a/client/api_client_mock.go b/client/api_client_mock.go index 8a8ec0cf..f30fabc7 100644 --- a/client/api_client_mock.go +++ b/client/api_client_mock.go @@ -245,36 +245,6 @@ func (mr *MockApiClientInterfaceMockRecorder) AssignCostCredentialsToProject(arg return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "AssignCostCredentialsToProject", reflect.TypeOf((*MockApiClientInterface)(nil).AssignCostCredentialsToProject), arg0, arg1) } -// AssignOrganizationRoleToTeam mocks base method. -func (m *MockApiClientInterface) AssignOrganizationRoleToTeam(arg0 *AssignOrganizationRoleToTeamPayload) (*OrganizationRoleTeamAssignment, error) { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "AssignOrganizationRoleToTeam", arg0) - ret0, _ := ret[0].(*OrganizationRoleTeamAssignment) - ret1, _ := ret[1].(error) - return ret0, ret1 -} - -// AssignOrganizationRoleToTeam indicates an expected call of AssignOrganizationRoleToTeam. -func (mr *MockApiClientInterfaceMockRecorder) AssignOrganizationRoleToTeam(arg0 any) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "AssignOrganizationRoleToTeam", reflect.TypeOf((*MockApiClientInterface)(nil).AssignOrganizationRoleToTeam), arg0) -} - -// AssignTeamRoleToEnvironment mocks base method. -func (m *MockApiClientInterface) AssignTeamRoleToEnvironment(arg0 *AssignTeamRoleToEnvironmentPayload) (*TeamRoleEnvironmentAssignment, error) { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "AssignTeamRoleToEnvironment", arg0) - ret0, _ := ret[0].(*TeamRoleEnvironmentAssignment) - ret1, _ := ret[1].(error) - return ret0, ret1 -} - -// AssignTeamRoleToEnvironment indicates an expected call of AssignTeamRoleToEnvironment. -func (mr *MockApiClientInterfaceMockRecorder) AssignTeamRoleToEnvironment(arg0 any) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "AssignTeamRoleToEnvironment", reflect.TypeOf((*MockApiClientInterface)(nil).AssignTeamRoleToEnvironment), arg0) -} - // AssignTemplateToProject mocks base method. func (m *MockApiClientInterface) AssignTemplateToProject(arg0 string, arg1 TemplateAssignmentToProjectPayload) (Template, error) { m.ctrl.T.Helper() @@ -1207,21 +1177,6 @@ func (mr *MockApiClientInterfaceMockRecorder) OrganizationPolicyUpdate(arg0 any) return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "OrganizationPolicyUpdate", reflect.TypeOf((*MockApiClientInterface)(nil).OrganizationPolicyUpdate), arg0) } -// OrganizationRoleTeamAssignments mocks base method. -func (m *MockApiClientInterface) OrganizationRoleTeamAssignments() ([]OrganizationRoleTeamAssignment, error) { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "OrganizationRoleTeamAssignments") - ret0, _ := ret[0].([]OrganizationRoleTeamAssignment) - ret1, _ := ret[1].(error) - return ret0, ret1 -} - -// OrganizationRoleTeamAssignments indicates an expected call of OrganizationRoleTeamAssignments. -func (mr *MockApiClientInterfaceMockRecorder) OrganizationRoleTeamAssignments() *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "OrganizationRoleTeamAssignments", reflect.TypeOf((*MockApiClientInterface)(nil).OrganizationRoleTeamAssignments)) -} - // OrganizationUserUpdateRole mocks base method. func (m *MockApiClientInterface) OrganizationUserUpdateRole(arg0, arg1 string) error { m.ctrl.T.Helper() @@ -1589,34 +1544,6 @@ func (mr *MockApiClientInterfaceMockRecorder) RemoveCostCredentialsFromProject(a return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "RemoveCostCredentialsFromProject", reflect.TypeOf((*MockApiClientInterface)(nil).RemoveCostCredentialsFromProject), arg0, arg1) } -// RemoveOrganizationRoleFromTeam mocks base method. -func (m *MockApiClientInterface) RemoveOrganizationRoleFromTeam(arg0 string) error { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "RemoveOrganizationRoleFromTeam", arg0) - ret0, _ := ret[0].(error) - return ret0 -} - -// RemoveOrganizationRoleFromTeam indicates an expected call of RemoveOrganizationRoleFromTeam. -func (mr *MockApiClientInterfaceMockRecorder) RemoveOrganizationRoleFromTeam(arg0 any) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "RemoveOrganizationRoleFromTeam", reflect.TypeOf((*MockApiClientInterface)(nil).RemoveOrganizationRoleFromTeam), arg0) -} - -// RemoveTeamRoleFromEnvironment mocks base method. -func (m *MockApiClientInterface) RemoveTeamRoleFromEnvironment(arg0, arg1 string) error { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "RemoveTeamRoleFromEnvironment", arg0, arg1) - ret0, _ := ret[0].(error) - return ret0 -} - -// RemoveTeamRoleFromEnvironment indicates an expected call of RemoveTeamRoleFromEnvironment. -func (mr *MockApiClientInterfaceMockRecorder) RemoveTeamRoleFromEnvironment(arg0, arg1 any) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "RemoveTeamRoleFromEnvironment", reflect.TypeOf((*MockApiClientInterface)(nil).RemoveTeamRoleFromEnvironment), arg0, arg1) -} - // RemoveTemplateFromProject mocks base method. func (m *MockApiClientInterface) RemoveTemplateFromProject(arg0, arg1 string) error { m.ctrl.T.Helper() @@ -1850,63 +1777,48 @@ func (mr *MockApiClientInterfaceMockRecorder) TeamDelete(arg0 any) *gomock.Call return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "TeamDelete", reflect.TypeOf((*MockApiClientInterface)(nil).TeamDelete), arg0) } -// TeamProjectAssignmentCreateOrUpdate mocks base method. -func (m *MockApiClientInterface) TeamProjectAssignmentCreateOrUpdate(arg0 *TeamProjectAssignmentPayload) (*TeamProjectAssignment, error) { +// TeamRoleAssignmentCreateOrUpdate mocks base method. +func (m *MockApiClientInterface) TeamRoleAssignmentCreateOrUpdate(arg0 *TeamRoleAssignmentCreateOrUpdatePayload) (*TeamRoleAssignmentPayload, error) { m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "TeamProjectAssignmentCreateOrUpdate", arg0) - ret0, _ := ret[0].(*TeamProjectAssignment) + ret := m.ctrl.Call(m, "TeamRoleAssignmentCreateOrUpdate", arg0) + ret0, _ := ret[0].(*TeamRoleAssignmentPayload) ret1, _ := ret[1].(error) return ret0, ret1 } -// TeamProjectAssignmentCreateOrUpdate indicates an expected call of TeamProjectAssignmentCreateOrUpdate. -func (mr *MockApiClientInterfaceMockRecorder) TeamProjectAssignmentCreateOrUpdate(arg0 any) *gomock.Call { +// TeamRoleAssignmentCreateOrUpdate indicates an expected call of TeamRoleAssignmentCreateOrUpdate. +func (mr *MockApiClientInterfaceMockRecorder) TeamRoleAssignmentCreateOrUpdate(arg0 any) *gomock.Call { mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "TeamProjectAssignmentCreateOrUpdate", reflect.TypeOf((*MockApiClientInterface)(nil).TeamProjectAssignmentCreateOrUpdate), arg0) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "TeamRoleAssignmentCreateOrUpdate", reflect.TypeOf((*MockApiClientInterface)(nil).TeamRoleAssignmentCreateOrUpdate), arg0) } -// TeamProjectAssignmentDelete mocks base method. -func (m *MockApiClientInterface) TeamProjectAssignmentDelete(arg0, arg1 string) error { +// TeamRoleAssignmentDelete mocks base method. +func (m *MockApiClientInterface) TeamRoleAssignmentDelete(arg0 *TeamRoleAssignmentDeletePayload) error { m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "TeamProjectAssignmentDelete", arg0, arg1) + ret := m.ctrl.Call(m, "TeamRoleAssignmentDelete", arg0) ret0, _ := ret[0].(error) return ret0 } -// TeamProjectAssignmentDelete indicates an expected call of TeamProjectAssignmentDelete. -func (mr *MockApiClientInterfaceMockRecorder) TeamProjectAssignmentDelete(arg0, arg1 any) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "TeamProjectAssignmentDelete", reflect.TypeOf((*MockApiClientInterface)(nil).TeamProjectAssignmentDelete), arg0, arg1) -} - -// TeamProjectAssignments mocks base method. -func (m *MockApiClientInterface) TeamProjectAssignments(arg0 string) ([]TeamProjectAssignment, error) { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "TeamProjectAssignments", arg0) - ret0, _ := ret[0].([]TeamProjectAssignment) - ret1, _ := ret[1].(error) - return ret0, ret1 -} - -// TeamProjectAssignments indicates an expected call of TeamProjectAssignments. -func (mr *MockApiClientInterfaceMockRecorder) TeamProjectAssignments(arg0 any) *gomock.Call { +// TeamRoleAssignmentDelete indicates an expected call of TeamRoleAssignmentDelete. +func (mr *MockApiClientInterfaceMockRecorder) TeamRoleAssignmentDelete(arg0 any) *gomock.Call { mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "TeamProjectAssignments", reflect.TypeOf((*MockApiClientInterface)(nil).TeamProjectAssignments), arg0) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "TeamRoleAssignmentDelete", reflect.TypeOf((*MockApiClientInterface)(nil).TeamRoleAssignmentDelete), arg0) } -// TeamRoleEnvironmentAssignments mocks base method. -func (m *MockApiClientInterface) TeamRoleEnvironmentAssignments(arg0 string) ([]TeamRoleEnvironmentAssignment, error) { +// TeamRoleAssignments mocks base method. +func (m *MockApiClientInterface) TeamRoleAssignments(arg0 *TeamRoleAssignmentListPayload) ([]TeamRoleAssignmentPayload, error) { m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "TeamRoleEnvironmentAssignments", arg0) - ret0, _ := ret[0].([]TeamRoleEnvironmentAssignment) + ret := m.ctrl.Call(m, "TeamRoleAssignments", arg0) + ret0, _ := ret[0].([]TeamRoleAssignmentPayload) ret1, _ := ret[1].(error) return ret0, ret1 } -// TeamRoleEnvironmentAssignments indicates an expected call of TeamRoleEnvironmentAssignments. -func (mr *MockApiClientInterfaceMockRecorder) TeamRoleEnvironmentAssignments(arg0 any) *gomock.Call { +// TeamRoleAssignments indicates an expected call of TeamRoleAssignments. +func (mr *MockApiClientInterfaceMockRecorder) TeamRoleAssignments(arg0 any) *gomock.Call { mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "TeamRoleEnvironmentAssignments", reflect.TypeOf((*MockApiClientInterface)(nil).TeamRoleEnvironmentAssignments), arg0) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "TeamRoleAssignments", reflect.TypeOf((*MockApiClientInterface)(nil).TeamRoleAssignments), arg0) } // TeamUpdate mocks base method. diff --git a/client/builtin_roles.go b/client/builtin_roles.go new file mode 100644 index 00000000..45761e4c --- /dev/null +++ b/client/builtin_roles.go @@ -0,0 +1,12 @@ +package client + +const ( + Admin string = "Admin" + Deployer string = "Deployer" + Planner string = "Planner" + Viewer string = "Viewer" +) + +func IsBuiltinProjectRole(role string) bool { + return role == Admin || role == Deployer || role == Planner || role == Viewer +} diff --git a/client/team_environment_assignment.go b/client/team_environment_assignment.go deleted file mode 100644 index dc4b23bd..00000000 --- a/client/team_environment_assignment.go +++ /dev/null @@ -1,37 +0,0 @@ -package client - -type AssignTeamRoleToEnvironmentPayload struct { - TeamId string `json:"teamId"` - Role string `json:"role" tfschema:"role_id"` - EnvironmentId string `json:"environmentId"` -} - -type TeamRoleEnvironmentAssignment struct { - TeamId string `json:"teamId"` - Role string `json:"role" tfschema:"role_id"` - Id string `json:"id"` -} - -func (client *ApiClient) AssignTeamRoleToEnvironment(payload *AssignTeamRoleToEnvironmentPayload) (*TeamRoleEnvironmentAssignment, error) { - var result TeamRoleEnvironmentAssignment - - if err := client.http.Put("/roles/assignments/teams", payload, &result); err != nil { - return nil, err - } - - return &result, nil -} - -func (client *ApiClient) RemoveTeamRoleFromEnvironment(environmentId string, teamId string) error { - return client.http.Delete("/roles/assignments/teams", map[string]string{"environmentId": environmentId, "teamId": teamId}) -} - -func (client *ApiClient) TeamRoleEnvironmentAssignments(environmentId string) ([]TeamRoleEnvironmentAssignment, error) { - var result []TeamRoleEnvironmentAssignment - - if err := client.http.Get("/roles/assignments/teams", map[string]string{"environmentId": environmentId}, &result); err != nil { - return nil, err - } - - return result, nil -} diff --git a/client/team_environment_assignment_test.go b/client/team_environment_assignment_test.go deleted file mode 100644 index 19ea2db8..00000000 --- a/client/team_environment_assignment_test.go +++ /dev/null @@ -1,136 +0,0 @@ -package client_test - -import ( - "errors" - - . "github.com/env0/terraform-provider-env0/client" - . "github.com/onsi/ginkgo" - . "github.com/onsi/gomega" - "go.uber.org/mock/gomock" -) - -var _ = Describe("Team Envrionment Assignment", func() { - environmentId := "environmentId" - teamId := "teamId" - - assignPayload := &AssignTeamRoleToEnvironmentPayload{ - TeamId: teamId, - EnvironmentId: environmentId, - Role: "role1", - } - - expectedResponse := &TeamRoleEnvironmentAssignment{ - Id: "id", - } - - errorMock := errors.New("error") - - Describe("AssignTeamToEnvironment", func() { - - Describe("Successful", func() { - var actualResult *TeamRoleEnvironmentAssignment - var err error - - BeforeEach(func() { - httpCall = mockHttpClient.EXPECT(). - Put("/roles/assignments/teams", assignPayload, gomock.Any()). - Do(func(path string, request interface{}, response *TeamRoleEnvironmentAssignment) { - *response = *expectedResponse - }).Times(1) - actualResult, err = apiClient.AssignTeamRoleToEnvironment(assignPayload) - - }) - - It("Should send POST request with params", func() { - httpCall.Times(1) - }) - - It("should return the PUT result", func() { - Expect(*actualResult).To(Equal(*expectedResponse)) - }) - - It("Should not return error", func() { - Expect(err).To(BeNil()) - }) - }) - - Describe("Failure", func() { - var actualResult *TeamRoleEnvironmentAssignment - var err error - - BeforeEach(func() { - httpCall = mockHttpClient.EXPECT(). - Put("/roles/assignments/teams", gomock.Any(), gomock.Any()).Return(errorMock) - actualResult, err = apiClient.AssignTeamRoleToEnvironment(assignPayload) - }) - - It("Should fail if API call fails", func() { - Expect(err).To(Equal(errorMock)) - }) - - It("Should not return results", func() { - Expect(actualResult).To(BeNil()) - }) - }) - }) - - Describe("RemoveTeamFromEnvironment", func() { - BeforeEach(func() { - httpCall = mockHttpClient.EXPECT().Delete("/roles/assignments/teams", map[string]string{"environmentId": environmentId, "teamId": teamId}) - apiClient.RemoveTeamRoleFromEnvironment(environmentId, teamId) - }) - - It("Should send DELETE request with assignment id", func() { - httpCall.Times(1) - }) - }) - - Describe("TeamEnvironmentAssignments", func() { - - Describe("Successful", func() { - var actualResult []TeamRoleEnvironmentAssignment - var err error - - BeforeEach(func() { - httpCall = mockHttpClient.EXPECT(). - Get("/roles/assignments/teams", map[string]string{"environmentId": environmentId}, gomock.Any()). - Do(func(path string, request interface{}, response *[]TeamRoleEnvironmentAssignment) { - *response = []TeamRoleEnvironmentAssignment{*expectedResponse} - }).Times(1) - actualResult, err = apiClient.TeamRoleEnvironmentAssignments(environmentId) - - }) - - It("Should send GET request with params", func() { - httpCall.Times(1) - }) - - It("should return the GET result", func() { - Expect(actualResult).To(Equal([]TeamRoleEnvironmentAssignment{*expectedResponse})) - }) - - It("Should not return error", func() { - Expect(err).To(BeNil()) - }) - }) - - Describe("Failure", func() { - var actualResult []TeamRoleEnvironmentAssignment - var err error - - BeforeEach(func() { - httpCall = mockHttpClient.EXPECT(). - Get("/roles/assignments/teams", map[string]string{"environmentId": environmentId}, gomock.Any()).Return(errorMock) - actualResult, err = apiClient.TeamRoleEnvironmentAssignments(environmentId) - }) - - It("Should fail if API call fails", func() { - Expect(err).To(Equal(errorMock)) - }) - - It("Should not return results", func() { - Expect(actualResult).To(BeNil()) - }) - }) - }) -}) diff --git a/client/team_organization_assignment.go b/client/team_organization_assignment.go deleted file mode 100644 index 1ab62252..00000000 --- a/client/team_organization_assignment.go +++ /dev/null @@ -1,59 +0,0 @@ -package client - -type AssignOrganizationRoleToTeamPayload struct { - TeamId string `json:"teamId"` - Role string `json:"role" tfschema:"role_id"` -} - -type OrganizationRoleTeamAssignment struct { - TeamId string `json:"teamId"` - Role string `json:"role" tfschema:"role_id"` - Id string `json:"id"` -} - -func (client *ApiClient) AssignOrganizationRoleToTeam(payload *AssignOrganizationRoleToTeamPayload) (*OrganizationRoleTeamAssignment, error) { - var result OrganizationRoleTeamAssignment - - organizationId, err := client.OrganizationId() - if err != nil { - return nil, err - } - - payloadWithOrganizationId := struct { - *AssignOrganizationRoleToTeamPayload - OrganizationId string `json:"organizationId"` - }{ - payload, - organizationId, - } - - if err := client.http.Put("/roles/assignments/teams", payloadWithOrganizationId, &result); err != nil { - return nil, err - } - - return &result, nil -} - -func (client *ApiClient) RemoveOrganizationRoleFromTeam(teamId string) error { - organizationId, err := client.OrganizationId() - if err != nil { - return err - } - - return client.http.Delete("/roles/assignments/teams", map[string]string{"organizationId": organizationId, "teamId": teamId}) -} - -func (client *ApiClient) OrganizationRoleTeamAssignments() ([]OrganizationRoleTeamAssignment, error) { - organizationId, err := client.OrganizationId() - if err != nil { - return nil, err - } - - var result []OrganizationRoleTeamAssignment - - if err := client.http.Get("/roles/assignments/teams", map[string]string{"organizationId": organizationId}, &result); err != nil { - return nil, err - } - - return result, nil -} diff --git a/client/team_organization_assignment_test.go b/client/team_organization_assignment_test.go deleted file mode 100644 index 6795c626..00000000 --- a/client/team_organization_assignment_test.go +++ /dev/null @@ -1,142 +0,0 @@ -package client_test - -import ( - "errors" - - . "github.com/env0/terraform-provider-env0/client" - . "github.com/onsi/ginkgo" - . "github.com/onsi/gomega" - "go.uber.org/mock/gomock" -) - -var _ = Describe("Team Orgnization Assignment", func() { - organizationId := "organizationId" - teamId := "teamId" - - assignPayload := &AssignOrganizationRoleToTeamPayload{ - TeamId: teamId, - Role: "role1", - } - - assignPayloadWithOrganizationId := struct { - *AssignOrganizationRoleToTeamPayload - OrganizationId string `json:"organizationId"` - }{ - assignPayload, - organizationId, - } - - expectedResponse := &OrganizationRoleTeamAssignment{ - Id: "id", - } - - errorMock := errors.New("error") - - Describe("AssignTeamToOrganization", func() { - - Describe("Successful", func() { - var actualResult *OrganizationRoleTeamAssignment - var err error - - BeforeEach(func() { - mockOrganizationIdCall(organizationId).Times(1) - httpCall = mockHttpClient.EXPECT(). - Put("/roles/assignments/teams", assignPayloadWithOrganizationId, gomock.Any()). - Do(func(path string, request interface{}, response *OrganizationRoleTeamAssignment) { - *response = *expectedResponse - }).Times(1) - actualResult, err = apiClient.AssignOrganizationRoleToTeam(assignPayload) - - }) - - It("Should send POST request with params", func() {}) - - It("should return the PUT result", func() { - Expect(*actualResult).To(Equal(*expectedResponse)) - }) - - It("Should not return error", func() { - Expect(err).To(BeNil()) - }) - }) - - Describe("Failure", func() { - var actualResult *OrganizationRoleTeamAssignment - var err error - - BeforeEach(func() { - mockOrganizationIdCall(organizationId).Times(1) - httpCall = mockHttpClient.EXPECT(). - Put("/roles/assignments/teams", gomock.Any(), gomock.Any()).Return(errorMock).Times(1) - actualResult, err = apiClient.AssignOrganizationRoleToTeam(assignPayload) - }) - - It("Should fail if API call fails", func() { - Expect(err).To(Equal(errorMock)) - }) - - It("Should not return results", func() { - Expect(actualResult).To(BeNil()) - }) - }) - }) - - Describe("RemoveTeamFromOrganization", func() { - BeforeEach(func() { - mockOrganizationIdCall(organizationId).Times(1) - httpCall = mockHttpClient.EXPECT().Delete("/roles/assignments/teams", map[string]string{"organizationId": organizationId, "teamId": teamId}).Times(1) - apiClient.RemoveOrganizationRoleFromTeam(teamId) - }) - - It("Should send DELETE request with assignment id", func() {}) - }) - - Describe("TeamOrganizationAssignments", func() { - - Describe("Successful", func() { - var actualResult []OrganizationRoleTeamAssignment - var err error - - BeforeEach(func() { - mockOrganizationIdCall(organizationId).Times(1) - httpCall = mockHttpClient.EXPECT(). - Get("/roles/assignments/teams", map[string]string{"organizationId": organizationId}, gomock.Any()). - Do(func(path string, request interface{}, response *[]OrganizationRoleTeamAssignment) { - *response = []OrganizationRoleTeamAssignment{*expectedResponse} - }).Times(1) - actualResult, err = apiClient.OrganizationRoleTeamAssignments() - - }) - - It("Should send GET request with params", func() {}) - - It("should return the GET result", func() { - Expect(actualResult).To(Equal([]OrganizationRoleTeamAssignment{*expectedResponse})) - }) - - It("Should not return error", func() { - Expect(err).To(BeNil()) - }) - }) - - Describe("Failure", func() { - var actualResult []OrganizationRoleTeamAssignment - var err error - - BeforeEach(func() { - mockOrganizationIdCall(organizationId).Times(1) - httpCall = mockHttpClient.EXPECT(). - Get("/roles/assignments/teams", map[string]string{"organizationId": organizationId}, gomock.Any()).Return(errorMock).Times(1) - actualResult, err = apiClient.OrganizationRoleTeamAssignments() - }) - - It("Should fail if API call fails", func() { - Expect(err).To(Equal(errorMock)) - }) - - It("Should not return results", func() { - Expect(actualResult).To(BeNil()) - }) - }) - }) -}) diff --git a/client/team_project_assignment.go b/client/team_project_assignment.go deleted file mode 100644 index dbbda011..00000000 --- a/client/team_project_assignment.go +++ /dev/null @@ -1,51 +0,0 @@ -package client - -type ProjectRole string - -const ( - Admin ProjectRole = "Admin" - Deployer ProjectRole = "Deployer" - Planner ProjectRole = "Planner" - Viewer ProjectRole = "Viewer" -) - -func IsBuiltinProjectRole(role string) bool { - return role == string(Admin) || role == string(Deployer) || role == string(Planner) || role == string(Viewer) -} - -type TeamProjectAssignmentPayload struct { - 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"` - Role string `json:"role" tfschema:"-"` -} - -func (client *ApiClient) TeamProjectAssignmentCreateOrUpdate(payload *TeamProjectAssignmentPayload) (*TeamProjectAssignment, error) { - var result TeamProjectAssignment - - if err := client.http.Put("/roles/assignments/teams", payload, &result); err != nil { - return nil, err - } - - return &result, 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("/roles/assignments/teams", map[string]string{"projectId": projectId}, &result) - - if err != nil { - return []TeamProjectAssignment{}, err - } - return result, nil -} diff --git a/client/team_project_assignment_test.go b/client/team_project_assignment_test.go deleted file mode 100644 index 271a2eed..00000000 --- a/client/team_project_assignment_test.go +++ /dev/null @@ -1,78 +0,0 @@ -package client_test - -import ( - . "github.com/env0/terraform-provider-env0/client" - . "github.com/onsi/ginkgo" - . "github.com/onsi/gomega" - "go.uber.org/mock/gomock" -) - -var _ = Describe("TeamProjectAssignment", func() { - const dummyProjectAssignmentId = "dummyId" - const dummyProjectId = "dummyProjectId" - const dummyProjectRole = "Admin" - const dummyTeamId = "dummyTeamId" - - mockTeamProjectAssignment := TeamProjectAssignment{ - Id: dummyProjectAssignmentId, - ProjectId: dummyProjectId, - Role: dummyProjectRole, - TeamId: dummyTeamId, - } - - Describe("CreateOrUpdate", func() { - Describe("Success", func() { - var teamProjectAssignment *TeamProjectAssignment - BeforeEach(func() { - expectedPayload := TeamProjectAssignmentPayload{ - TeamId: dummyTeamId, - ProjectId: dummyProjectId, - Role: dummyProjectRole, - } - httpCall = mockHttpClient.EXPECT(). - Put("/roles/assignments/teams", &expectedPayload, gomock.Any()). - Do(func(path string, request interface{}, response *TeamProjectAssignment) { - *response = mockTeamProjectAssignment - }).Times(1) - teamProjectAssignment, _ = apiClient.TeamProjectAssignmentCreateOrUpdate(&expectedPayload) - - }) - - It("Should send PUT request with params", func() {}) - - It("Should return a new resource with id", func() { - Expect(*teamProjectAssignment).To(Equal(mockTeamProjectAssignment)) - }) - }) - }) - - Describe("Get", func() { - mockTeamProjectAssignments := []TeamProjectAssignment{mockTeamProjectAssignment} - var teamProjectAssignments []TeamProjectAssignment - BeforeEach(func() { - httpCall = mockHttpClient.EXPECT(). - 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() {}) - - It("Should return the projects assignments", func() { - Expect(teamProjectAssignments).To(Equal(mockTeamProjectAssignments)) - }) - - }) - - Describe("Delete", func() { - BeforeEach(func() { - 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() {}) - }) - -}) diff --git a/client/team_role_assignment.go b/client/team_role_assignment.go new file mode 100644 index 00000000..eeaa87a8 --- /dev/null +++ b/client/team_role_assignment.go @@ -0,0 +1,82 @@ +package client + +type TeamRoleAssignmentPayload struct { + TeamId string `json:"teamId"` + Role string `json:"role" tfschema:"role_id"` + Id string `json:"id"` +} + +type TeamRoleAssignmentCreateOrUpdatePayload struct { + TeamId string `json:"teamId"` + Role string `json:"role" tfschema:"role_id"` + EnvironmentId string `json:"environmentId,omitempty"` + OrganizationId string `json:"organizationId,omitempty"` + ProjectId string `json:"projectId,omitempty"` +} + +type TeamRoleAssignmentDeletePayload struct { + TeamId string + EnvironmentId string + OrganizationId string + ProjectId string +} + +type TeamRoleAssignmentListPayload struct { + EnvironmentId string + OrganizationId string + ProjectId string +} + +func (client *ApiClient) TeamRoleAssignmentCreateOrUpdate(payload *TeamRoleAssignmentCreateOrUpdatePayload) (*TeamRoleAssignmentPayload, error) { + var result TeamRoleAssignmentPayload + + if err := client.http.Put("/roles/assignments/teams", payload, &result); err != nil { + return nil, err + } + + return &result, nil +} + +func (client *ApiClient) TeamRoleAssignmentDelete(payload *TeamRoleAssignmentDeletePayload) error { + params := make(map[string]string) + + params["teamId"] = payload.TeamId + + if payload.EnvironmentId != "" { + params["environmentId"] = payload.EnvironmentId + } + + if payload.OrganizationId != "" { + params["organizationId"] = payload.OrganizationId + } + + if payload.ProjectId != "" { + params["projectId"] = payload.ProjectId + } + + return client.http.Delete("/roles/assignments/teams", params) +} + +func (client *ApiClient) TeamRoleAssignments(payload *TeamRoleAssignmentListPayload) ([]TeamRoleAssignmentPayload, error) { + params := make(map[string]string) + + if payload.EnvironmentId != "" { + params["environmentId"] = payload.EnvironmentId + } + + if payload.OrganizationId != "" { + params["organizationId"] = payload.OrganizationId + } + + if payload.ProjectId != "" { + params["projectId"] = payload.ProjectId + } + + var result []TeamRoleAssignmentPayload + + if err := client.http.Get("/roles/assignments/teams", params, &result); err != nil { + return nil, err + } + + return result, nil +} diff --git a/client/team_role_assignment_test.go b/client/team_role_assignment_test.go new file mode 100644 index 00000000..5b3033e0 --- /dev/null +++ b/client/team_role_assignment_test.go @@ -0,0 +1,180 @@ +package client_test + +import ( + . "github.com/env0/terraform-provider-env0/client" + . "github.com/onsi/ginkgo" + . "github.com/onsi/gomega" + "go.uber.org/mock/gomock" +) + +var _ = Describe("TeamRoleAssignment", func() { + const dummyProjectAssignmentId = "dummyId" + const dummyProjectId = "dummyProjectId" + const dummyEnvironmentId = "dummyEnvironmentId" + const dummyOrganizationId = "dummyOrganizationId" + const dummyProjectRole = "Admin" + const dummyTeamId = "dummyTeamId" + + mockTeamRoleAssignment := TeamRoleAssignmentPayload{ + Id: dummyProjectAssignmentId, + Role: dummyProjectRole, + TeamId: dummyTeamId, + } + + mockTeamRoleAssignments := []TeamRoleAssignmentPayload{mockTeamRoleAssignment} + + Describe("CreateOrUpdate", func() { + Describe("ProjectId", func() { + var assignment *TeamRoleAssignmentPayload + BeforeEach(func() { + createPayload := TeamRoleAssignmentCreateOrUpdatePayload{ + TeamId: dummyTeamId, + ProjectId: dummyProjectId, + Role: dummyProjectRole, + } + httpCall = mockHttpClient.EXPECT(). + Put("/roles/assignments/teams", &createPayload, gomock.Any()). + Do(func(path string, request interface{}, response *TeamRoleAssignmentPayload) { + *response = mockTeamRoleAssignment + }).Times(1) + assignment, _ = apiClient.TeamRoleAssignmentCreateOrUpdate(&createPayload) + + }) + + It("Should send PUT request with params", func() {}) + + It("Should return a new resource with id", func() { + Expect(*assignment).To(Equal(mockTeamRoleAssignment)) + }) + }) + + Describe("EnvironmentId", func() { + var assignment *TeamRoleAssignmentPayload + BeforeEach(func() { + createPayload := TeamRoleAssignmentCreateOrUpdatePayload{ + TeamId: dummyTeamId, + EnvironmentId: dummyEnvironmentId, + Role: dummyProjectRole, + } + httpCall = mockHttpClient.EXPECT(). + Put("/roles/assignments/teams", &createPayload, gomock.Any()). + Do(func(path string, request interface{}, response *TeamRoleAssignmentPayload) { + *response = mockTeamRoleAssignment + }).Times(1) + assignment, _ = apiClient.TeamRoleAssignmentCreateOrUpdate(&createPayload) + + }) + + It("Should send PUT request with params", func() {}) + + It("Should return a new resource with id", func() { + Expect(*assignment).To(Equal(mockTeamRoleAssignment)) + }) + }) + + Describe("OrganizationId", func() { + var assignment *TeamRoleAssignmentPayload + BeforeEach(func() { + createPayload := TeamRoleAssignmentCreateOrUpdatePayload{ + TeamId: dummyTeamId, + OrganizationId: dummyOrganizationId, + Role: dummyProjectRole, + } + httpCall = mockHttpClient.EXPECT(). + Put("/roles/assignments/teams", &createPayload, gomock.Any()). + Do(func(path string, request interface{}, response *TeamRoleAssignmentPayload) { + *response = mockTeamRoleAssignment + }).Times(1) + assignment, _ = apiClient.TeamRoleAssignmentCreateOrUpdate(&createPayload) + }) + + It("Should send PUT request with params", func() {}) + + It("Should return a new resource with id", func() { + Expect(*assignment).To(Equal(mockTeamRoleAssignment)) + }) + }) + }) + + Describe("Get Project Assignments", func() { + var assignments []TeamRoleAssignmentPayload + BeforeEach(func() { + httpCall = mockHttpClient.EXPECT(). + Get("/roles/assignments/teams", map[string]string{"projectId": dummyProjectId}, gomock.Any()). + Do(func(path string, request interface{}, response *[]TeamRoleAssignmentPayload) { + *response = mockTeamRoleAssignments + }).Times(1) + assignments, _ = apiClient.TeamRoleAssignments(&TeamRoleAssignmentListPayload{ProjectId: dummyProjectId}) + }) + + It("Should send GET request", func() {}) + + It("Should return the projects assignments", func() { + Expect(assignments).To(Equal(mockTeamRoleAssignments)) + }) + }) + + Describe("Get Environment Assignments", func() { + var assignments []TeamRoleAssignmentPayload + BeforeEach(func() { + httpCall = mockHttpClient.EXPECT(). + Get("/roles/assignments/teams", map[string]string{"environmentId": dummyEnvironmentId}, gomock.Any()). + Do(func(path string, request interface{}, response *[]TeamRoleAssignmentPayload) { + *response = mockTeamRoleAssignments + }).Times(1) + assignments, _ = apiClient.TeamRoleAssignments(&TeamRoleAssignmentListPayload{EnvironmentId: dummyEnvironmentId}) + }) + + It("Should send GET request", func() {}) + + It("Should return the environment assignments", func() { + Expect(assignments).To(Equal(mockTeamRoleAssignments)) + }) + }) + + Describe("Get Organization Assignments", func() { + var assignments []TeamRoleAssignmentPayload + BeforeEach(func() { + httpCall = mockHttpClient.EXPECT(). + Get("/roles/assignments/teams", map[string]string{"organizationId": dummyOrganizationId}, gomock.Any()). + Do(func(path string, request interface{}, response *[]TeamRoleAssignmentPayload) { + *response = mockTeamRoleAssignments + }).Times(1) + assignments, _ = apiClient.TeamRoleAssignments(&TeamRoleAssignmentListPayload{OrganizationId: dummyOrganizationId}) + }) + + It("Should send GET request", func() {}) + + It("Should return the organization assignments", func() { + Expect(assignments).To(Equal(mockTeamRoleAssignments)) + }) + }) + + Describe("Delete Project Assignment", func() { + BeforeEach(func() { + httpCall = mockHttpClient.EXPECT().Delete("/roles/assignments/teams", map[string]string{"projectId": dummyProjectId, "teamId": dummyTeamId}).Times(1) + apiClient.TeamRoleAssignmentDelete(&TeamRoleAssignmentDeletePayload{TeamId: dummyTeamId, ProjectId: dummyProjectId}) + }) + + It("Should send Delete request with correct params", func() {}) + }) + + Describe("Delete Environment Assignment", func() { + BeforeEach(func() { + httpCall = mockHttpClient.EXPECT().Delete("/roles/assignments/teams", map[string]string{"environmentId": dummyEnvironmentId, "teamId": dummyTeamId}).Times(1) + apiClient.TeamRoleAssignmentDelete(&TeamRoleAssignmentDeletePayload{TeamId: dummyTeamId, EnvironmentId: dummyEnvironmentId}) + }) + + It("Should send Delete request with correct params", func() {}) + }) + + Describe("Delete Organization Assignment", func() { + BeforeEach(func() { + httpCall = mockHttpClient.EXPECT().Delete("/roles/assignments/teams", map[string]string{"organizationId": dummyOrganizationId, "teamId": dummyTeamId}).Times(1) + apiClient.TeamRoleAssignmentDelete(&TeamRoleAssignmentDeletePayload{TeamId: dummyTeamId, OrganizationId: dummyOrganizationId}) + }) + + It("Should send Delete request with correct params", func() {}) + }) + +}) diff --git a/env0/resource_team_environment_assignment.go b/env0/resource_team_environment_assignment.go index b417786d..58469840 100644 --- a/env0/resource_team_environment_assignment.go +++ b/env0/resource_team_environment_assignment.go @@ -11,8 +11,8 @@ import ( func resourceTeamEnvironmentAssignment() *schema.Resource { return &schema.Resource{ - CreateContext: resourceTeamEnvironmentAssignmentCreate, - UpdateContext: resourceTeamEnvironmentAssignmentUpdate, + CreateContext: resourceTeamEnvironmentAssignmentCreateOrUpdate, + UpdateContext: resourceTeamEnvironmentAssignmentCreateOrUpdate, ReadContext: resourceTeamEnvironmentAssignmentRead, DeleteContext: resourceTeamEnvironmentAssignmentDelete, @@ -39,15 +39,15 @@ func resourceTeamEnvironmentAssignment() *schema.Resource { } } -func resourceTeamEnvironmentAssignmentCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { - var newAssignment client.AssignTeamRoleToEnvironmentPayload - if err := readResourceData(&newAssignment, d); err != nil { +func resourceTeamEnvironmentAssignmentCreateOrUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { + apiClient := meta.(client.ApiClientInterface) + + var payload client.TeamRoleAssignmentCreateOrUpdatePayload + if err := readResourceData(&payload, d); err != nil { return diag.Errorf("schema resource data deserialization failed: %v", err) } - client := meta.(client.ApiClientInterface) - - assignment, err := client.AssignTeamRoleToEnvironment(&newAssignment) + assignment, err := apiClient.TeamRoleAssignmentCreateOrUpdate(&payload) if err != nil { return diag.Errorf("could not create assignment: %v", err) } @@ -58,12 +58,14 @@ func resourceTeamEnvironmentAssignmentCreate(ctx context.Context, d *schema.Reso } func resourceTeamEnvironmentAssignmentRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { - client := meta.(client.ApiClientInterface) + apiClient := meta.(client.ApiClientInterface) + + var payload client.TeamRoleAssignmentListPayload id := d.Id() - environmentId := d.Get("environment_id").(string) + payload.EnvironmentId = d.Get("environment_id").(string) - assignments, err := client.TeamRoleEnvironmentAssignments(environmentId) + assignments, err := apiClient.TeamRoleAssignments(&payload) if err != nil { return diag.Errorf("could not get assignments: %v", err) } @@ -86,31 +88,15 @@ func resourceTeamEnvironmentAssignmentRead(ctx context.Context, d *schema.Resour return nil } -func resourceTeamEnvironmentAssignmentUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { - var payload client.AssignTeamRoleToEnvironmentPayload - if err := readResourceData(&payload, d); err != nil { - return diag.Errorf("schema resource data deserialization failed: %v", err) - } - - client := meta.(client.ApiClientInterface) - - assignment, err := client.AssignTeamRoleToEnvironment(&payload) - if err != nil { - return diag.Errorf("could not update assignment: %v", err) - } - - d.SetId(assignment.Id) - - return nil -} - func resourceTeamEnvironmentAssignmentDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { - environmentId := d.Get("environment_id").(string) - teamId := d.Get("team_id").(string) + apiClient := meta.(client.ApiClientInterface) + + var payload client.TeamRoleAssignmentDeletePayload - client := meta.(client.ApiClientInterface) + payload.EnvironmentId = d.Get("environment_id").(string) + payload.TeamId = d.Get("team_id").(string) - if err := client.RemoveTeamRoleFromEnvironment(environmentId, teamId); err != nil { + if err := apiClient.TeamRoleAssignmentDelete(&payload); err != nil { return diag.Errorf("could not delete assignment: %v", err) } diff --git a/env0/resource_team_environment_assignment_test.go b/env0/resource_team_environment_assignment_test.go index c005d87a..49035225 100644 --- a/env0/resource_team_environment_assignment_test.go +++ b/env0/resource_team_environment_assignment_test.go @@ -17,31 +17,31 @@ func TestUnitTeamEnvironmntAssignmentResource(t *testing.T) { role := "rid1" updatedRole := "rid2" - createPayload := client.AssignTeamRoleToEnvironmentPayload{ + createPayload := client.TeamRoleAssignmentCreateOrUpdatePayload{ TeamId: teamId, Role: role, EnvironmentId: environmentId, } - updatePayload := client.AssignTeamRoleToEnvironmentPayload{ + updatePayload := client.TeamRoleAssignmentCreateOrUpdatePayload{ TeamId: teamId, Role: updatedRole, EnvironmentId: environmentId, } - createResponse := client.TeamRoleEnvironmentAssignment{ + createResponse := client.TeamRoleAssignmentPayload{ Id: id, TeamId: teamId, Role: role, } - updateResponse := client.TeamRoleEnvironmentAssignment{ + updateResponse := client.TeamRoleAssignmentPayload{ Id: id, TeamId: teamId, Role: updatedRole, } - otherResponse := client.TeamRoleEnvironmentAssignment{ + otherResponse := client.TeamRoleAssignmentPayload{ Id: "id2", TeamId: "teamId2", Role: "dasdasd", @@ -85,11 +85,11 @@ func TestUnitTeamEnvironmntAssignmentResource(t *testing.T) { runUnitTest(t, testCase, func(mock *client.MockApiClientInterface) { gomock.InOrder( - mock.EXPECT().AssignTeamRoleToEnvironment(&createPayload).Times(1).Return(&createResponse, nil), - mock.EXPECT().TeamRoleEnvironmentAssignments(environmentId).Times(2).Return([]client.TeamRoleEnvironmentAssignment{otherResponse, createResponse}, nil), - mock.EXPECT().AssignTeamRoleToEnvironment(&updatePayload).Times(1).Return(&updateResponse, nil), - mock.EXPECT().TeamRoleEnvironmentAssignments(environmentId).Times(1).Return([]client.TeamRoleEnvironmentAssignment{otherResponse, updateResponse}, nil), - mock.EXPECT().RemoveTeamRoleFromEnvironment(environmentId, teamId).Times(1).Return(nil), + mock.EXPECT().TeamRoleAssignmentCreateOrUpdate(&createPayload).Times(1).Return(&createResponse, nil), + mock.EXPECT().TeamRoleAssignments(&client.TeamRoleAssignmentListPayload{EnvironmentId: environmentId}).Times(2).Return([]client.TeamRoleAssignmentPayload{otherResponse, createResponse}, nil), + mock.EXPECT().TeamRoleAssignmentCreateOrUpdate(&updatePayload).Times(1).Return(&updateResponse, nil), + mock.EXPECT().TeamRoleAssignments(&client.TeamRoleAssignmentListPayload{EnvironmentId: environmentId}).Times(1).Return([]client.TeamRoleAssignmentPayload{otherResponse, updateResponse}, nil), + mock.EXPECT().TeamRoleAssignmentDelete(&client.TeamRoleAssignmentDeletePayload{TeamId: teamId, EnvironmentId: environmentId}).Times(1).Return(nil), ) }) }) @@ -131,8 +131,8 @@ func TestUnitTeamEnvironmntAssignmentResource(t *testing.T) { runUnitTest(t, testCase, func(mock *client.MockApiClientInterface) { gomock.InOrder( - mock.EXPECT().AssignTeamRoleToEnvironment(&createPayload).Times(1).Return(&createResponse, nil), - mock.EXPECT().TeamRoleEnvironmentAssignments(environmentId).Times(1).Return([]client.TeamRoleEnvironmentAssignment{otherResponse}, nil), + mock.EXPECT().TeamRoleAssignmentCreateOrUpdate(&createPayload).Times(1).Return(&createResponse, nil), + mock.EXPECT().TeamRoleAssignments(&client.TeamRoleAssignmentListPayload{EnvironmentId: environmentId}).Times(1).Return([]client.TeamRoleAssignmentPayload{otherResponse}, nil), ) }) }) @@ -153,7 +153,7 @@ func TestUnitTeamEnvironmntAssignmentResource(t *testing.T) { runUnitTest(t, testCase, func(mock *client.MockApiClientInterface) { gomock.InOrder( - mock.EXPECT().AssignTeamRoleToEnvironment(&createPayload).Times(1).Return(nil, errors.New("error")), + mock.EXPECT().TeamRoleAssignmentCreateOrUpdate(&createPayload).Times(1).Return(nil, errors.New("error")), ) }) }) @@ -174,9 +174,9 @@ func TestUnitTeamEnvironmntAssignmentResource(t *testing.T) { runUnitTest(t, testCase, func(mock *client.MockApiClientInterface) { gomock.InOrder( - mock.EXPECT().AssignTeamRoleToEnvironment(&createPayload).Times(1).Return(&createResponse, nil), - mock.EXPECT().TeamRoleEnvironmentAssignments(environmentId).Times(1).Return(nil, errors.New("error")), - mock.EXPECT().RemoveTeamRoleFromEnvironment(environmentId, teamId).Times(1).Return(nil), + mock.EXPECT().TeamRoleAssignmentCreateOrUpdate(&createPayload).Times(1).Return(&createResponse, nil), + mock.EXPECT().TeamRoleAssignments(&client.TeamRoleAssignmentListPayload{EnvironmentId: environmentId}).Times(1).Return(nil, errors.New("error")), + mock.EXPECT().TeamRoleAssignmentDelete(&client.TeamRoleAssignmentDeletePayload{TeamId: teamId, EnvironmentId: environmentId}).Times(1).Return(nil), ) }) }) @@ -203,17 +203,17 @@ func TestUnitTeamEnvironmntAssignmentResource(t *testing.T) { "environment_id": environmentId, "role_id": updatedRole, }), - ExpectError: regexp.MustCompile("could not update assignment: error"), + ExpectError: regexp.MustCompile("could not create assignment: error"), }, }, } runUnitTest(t, testCase, func(mock *client.MockApiClientInterface) { gomock.InOrder( - mock.EXPECT().AssignTeamRoleToEnvironment(&createPayload).Times(1).Return(&createResponse, nil), - mock.EXPECT().TeamRoleEnvironmentAssignments(environmentId).Times(2).Return([]client.TeamRoleEnvironmentAssignment{otherResponse, createResponse}, nil), - mock.EXPECT().AssignTeamRoleToEnvironment(&updatePayload).Times(1).Return(nil, errors.New("error")), - mock.EXPECT().RemoveTeamRoleFromEnvironment(environmentId, teamId).Times(1).Return(nil), + mock.EXPECT().TeamRoleAssignmentCreateOrUpdate(&createPayload).Times(1).Return(&createResponse, nil), + mock.EXPECT().TeamRoleAssignments(&client.TeamRoleAssignmentListPayload{EnvironmentId: environmentId}).Times(2).Return([]client.TeamRoleAssignmentPayload{otherResponse, createResponse}, nil), + mock.EXPECT().TeamRoleAssignmentCreateOrUpdate(&updatePayload).Times(1).Return(nil, errors.New("error")), + mock.EXPECT().TeamRoleAssignmentDelete(&client.TeamRoleAssignmentDeletePayload{TeamId: teamId, EnvironmentId: environmentId}).Times(1).Return(nil), ) }) }) diff --git a/env0/resource_team_organization_assignment.go b/env0/resource_team_organization_assignment.go index 27ff2606..192ef703 100644 --- a/env0/resource_team_organization_assignment.go +++ b/env0/resource_team_organization_assignment.go @@ -11,8 +11,8 @@ import ( func resourceTeamOrganizationAssignment() *schema.Resource { return &schema.Resource{ - CreateContext: resourceTeamOrganizationAssignmentCreate, - UpdateContext: resourceTeamOrganizationAssignmentUpdate, + CreateContext: resourceTeamOrganizationAssignmentCreateOrUpdate, + UpdateContext: resourceTeamOrganizationAssignmentCreateOrUpdate, ReadContext: resourceTeamOrganizationAssignmentRead, DeleteContext: resourceTeamOrganizationAssignmentDelete, @@ -35,15 +35,22 @@ func resourceTeamOrganizationAssignment() *schema.Resource { } } -func resourceTeamOrganizationAssignmentCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { - api_client := meta.(client.ApiClientInterface) +func resourceTeamOrganizationAssignmentCreateOrUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { + apiClient := meta.(client.ApiClientInterface) - var newAssignment client.AssignOrganizationRoleToTeamPayload - if err := readResourceData(&newAssignment, d); err != nil { + organizationId, err := apiClient.OrganizationId() + if err != nil { + return diag.Errorf("could not get organization id: %v", err) + + } + + var payload client.TeamRoleAssignmentCreateOrUpdatePayload + if err := readResourceData(&payload, d); err != nil { return diag.Errorf("schema resource data deserialization failed: %v", err) } + payload.OrganizationId = organizationId - assignment, err := api_client.AssignOrganizationRoleToTeam(&newAssignment) + assignment, err := apiClient.TeamRoleAssignmentCreateOrUpdate(&payload) if err != nil { return diag.Errorf("could not create assignment: %v", err) } @@ -54,15 +61,24 @@ func resourceTeamOrganizationAssignmentCreate(ctx context.Context, d *schema.Res } func resourceTeamOrganizationAssignmentRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { - api_client := meta.(client.ApiClientInterface) + apiClient := meta.(client.ApiClientInterface) - assignments, err := api_client.OrganizationRoleTeamAssignments() + organizationId, err := apiClient.OrganizationId() if err != nil { - return diag.Errorf("could not get assignments: %v", err) + return diag.Errorf("could not get organization id: %v", err) + } + var payload client.TeamRoleAssignmentListPayload + payload.OrganizationId = organizationId + id := d.Id() + assignments, err := apiClient.TeamRoleAssignments(&payload) + if err != nil { + return diag.Errorf("could not get assignments: %v", err) + } + for _, assignment := range assignments { if assignment.Id == id { if err := writeResourceData(&assignment, d); err != nil { @@ -81,30 +97,21 @@ func resourceTeamOrganizationAssignmentRead(ctx context.Context, d *schema.Resou return nil } -func resourceTeamOrganizationAssignmentUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { - api_client := meta.(client.ApiClientInterface) +func resourceTeamOrganizationAssignmentDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { + apiClient := meta.(client.ApiClientInterface) - var payload client.AssignOrganizationRoleToTeamPayload - if err := readResourceData(&payload, d); err != nil { - return diag.Errorf("schema resource data deserialization failed: %v", err) - } + var payload client.TeamRoleAssignmentDeletePayload - assignment, err := api_client.AssignOrganizationRoleToTeam(&payload) + organizationId, err := apiClient.OrganizationId() if err != nil { - return diag.Errorf("could not update assignment: %v", err) - } - - d.SetId(assignment.Id) - - return nil -} + return diag.Errorf("could not get organization id: %v", err) -func resourceTeamOrganizationAssignmentDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { - api_client := meta.(client.ApiClientInterface) + } - teamId := d.Get("team_id").(string) + payload.OrganizationId = organizationId + payload.TeamId = d.Get("team_id").(string) - if err := api_client.RemoveOrganizationRoleFromTeam(teamId); err != nil { + if err := apiClient.TeamRoleAssignmentDelete(&payload); err != nil { return diag.Errorf("could not delete assignment: %v", err) } diff --git a/env0/resource_team_organization_assignment_test.go b/env0/resource_team_organization_assignment_test.go index 63dce5ed..1d9d32ae 100644 --- a/env0/resource_team_organization_assignment_test.go +++ b/env0/resource_team_organization_assignment_test.go @@ -12,34 +12,37 @@ import ( func TestUnitTeamOrganizationAssignmentResource(t *testing.T) { teamId := "tid" + organizationId := "oid" id := "id" role := "rid1" updatedRole := "rid2" - createPayload := client.AssignOrganizationRoleToTeamPayload{ - TeamId: teamId, - Role: role, + createPayload := client.TeamRoleAssignmentCreateOrUpdatePayload{ + TeamId: teamId, + Role: role, + OrganizationId: organizationId, } - updatePayload := client.AssignOrganizationRoleToTeamPayload{ - TeamId: teamId, - Role: updatedRole, + updatePayload := client.TeamRoleAssignmentCreateOrUpdatePayload{ + TeamId: teamId, + Role: updatedRole, + OrganizationId: organizationId, } - createResponse := client.OrganizationRoleTeamAssignment{ + createResponse := client.TeamRoleAssignmentPayload{ Id: id, TeamId: teamId, Role: role, } - updateResponse := client.OrganizationRoleTeamAssignment{ + updateResponse := client.TeamRoleAssignmentPayload{ Id: id, TeamId: teamId, Role: updatedRole, } - otherResponse := client.OrganizationRoleTeamAssignment{ + otherResponse := client.TeamRoleAssignmentPayload{ Id: "id2", TeamId: "teamId2", Role: "dasdasd", @@ -78,12 +81,13 @@ func TestUnitTeamOrganizationAssignmentResource(t *testing.T) { } runUnitTest(t, testCase, func(mock *client.MockApiClientInterface) { + mock.EXPECT().OrganizationId().AnyTimes().Return(organizationId, nil) gomock.InOrder( - mock.EXPECT().AssignOrganizationRoleToTeam(&createPayload).Times(1).Return(&createResponse, nil), - mock.EXPECT().OrganizationRoleTeamAssignments().Times(2).Return([]client.OrganizationRoleTeamAssignment{otherResponse, createResponse}, nil), - mock.EXPECT().AssignOrganizationRoleToTeam(&updatePayload).Times(1).Return(&updateResponse, nil), - mock.EXPECT().OrganizationRoleTeamAssignments().Times(1).Return([]client.OrganizationRoleTeamAssignment{otherResponse, updateResponse}, nil), - mock.EXPECT().RemoveOrganizationRoleFromTeam(teamId).Times(1).Return(nil), + mock.EXPECT().TeamRoleAssignmentCreateOrUpdate(&createPayload).Times(1).Return(&createResponse, nil), + mock.EXPECT().TeamRoleAssignments(&client.TeamRoleAssignmentListPayload{OrganizationId: organizationId}).Times(2).Return([]client.TeamRoleAssignmentPayload{otherResponse, createResponse}, nil), + mock.EXPECT().TeamRoleAssignmentCreateOrUpdate(&updatePayload).Times(1).Return(&updateResponse, nil), + mock.EXPECT().TeamRoleAssignments(&client.TeamRoleAssignmentListPayload{OrganizationId: organizationId}).Times(1).Return([]client.TeamRoleAssignmentPayload{otherResponse, updateResponse}, nil), + mock.EXPECT().TeamRoleAssignmentDelete(&client.TeamRoleAssignmentDeletePayload{TeamId: teamId, OrganizationId: organizationId}).Times(1).Return(nil), ) }) }) @@ -120,9 +124,10 @@ func TestUnitTeamOrganizationAssignmentResource(t *testing.T) { } runUnitTest(t, testCase, func(mock *client.MockApiClientInterface) { + mock.EXPECT().OrganizationId().AnyTimes().Return(organizationId, nil) gomock.InOrder( - mock.EXPECT().AssignOrganizationRoleToTeam(&createPayload).Times(1).Return(&createResponse, nil), - mock.EXPECT().OrganizationRoleTeamAssignments().Times(1).Return([]client.OrganizationRoleTeamAssignment{otherResponse}, nil), + mock.EXPECT().TeamRoleAssignmentCreateOrUpdate(&createPayload).Times(1).Return(&createResponse, nil), + mock.EXPECT().TeamRoleAssignments(&client.TeamRoleAssignmentListPayload{OrganizationId: organizationId}).Times(1).Return([]client.TeamRoleAssignmentPayload{otherResponse}, nil), ) }) }) @@ -141,8 +146,9 @@ func TestUnitTeamOrganizationAssignmentResource(t *testing.T) { } runUnitTest(t, testCase, func(mock *client.MockApiClientInterface) { + mock.EXPECT().OrganizationId().AnyTimes().Return(organizationId, nil) gomock.InOrder( - mock.EXPECT().AssignOrganizationRoleToTeam(&createPayload).Times(1).Return(nil, errors.New("error")), + mock.EXPECT().TeamRoleAssignmentCreateOrUpdate(&createPayload).Times(1).Return(nil, errors.New("error")), ) }) }) @@ -161,10 +167,11 @@ func TestUnitTeamOrganizationAssignmentResource(t *testing.T) { } runUnitTest(t, testCase, func(mock *client.MockApiClientInterface) { + mock.EXPECT().OrganizationId().AnyTimes().Return(organizationId, nil) gomock.InOrder( - mock.EXPECT().AssignOrganizationRoleToTeam(&createPayload).Times(1).Return(&createResponse, nil), - mock.EXPECT().OrganizationRoleTeamAssignments().Times(1).Return(nil, errors.New("error")), - mock.EXPECT().RemoveOrganizationRoleFromTeam(teamId).Times(1).Return(nil), + mock.EXPECT().TeamRoleAssignmentCreateOrUpdate(&createPayload).Times(1).Return(&createResponse, nil), + mock.EXPECT().TeamRoleAssignments(&client.TeamRoleAssignmentListPayload{OrganizationId: organizationId}).Times(1).Return(nil, errors.New("error")), + mock.EXPECT().TeamRoleAssignmentDelete(&client.TeamRoleAssignmentDeletePayload{TeamId: teamId, OrganizationId: organizationId}).Times(1).Return(nil), ) }) }) @@ -188,17 +195,18 @@ func TestUnitTeamOrganizationAssignmentResource(t *testing.T) { "team_id": teamId, "role_id": updatedRole, }), - ExpectError: regexp.MustCompile("could not update assignment: error"), + ExpectError: regexp.MustCompile("could not create assignment: error"), }, }, } runUnitTest(t, testCase, func(mock *client.MockApiClientInterface) { + mock.EXPECT().OrganizationId().AnyTimes().Return(organizationId, nil) gomock.InOrder( - mock.EXPECT().AssignOrganizationRoleToTeam(&createPayload).Times(1).Return(&createResponse, nil), - mock.EXPECT().OrganizationRoleTeamAssignments().Times(2).Return([]client.OrganizationRoleTeamAssignment{otherResponse, createResponse}, nil), - mock.EXPECT().AssignOrganizationRoleToTeam(&updatePayload).Times(1).Return(nil, errors.New("error")), - mock.EXPECT().RemoveOrganizationRoleFromTeam(teamId).Times(1).Return(nil), + mock.EXPECT().TeamRoleAssignmentCreateOrUpdate(&createPayload).Times(1).Return(&createResponse, nil), + mock.EXPECT().TeamRoleAssignments(&client.TeamRoleAssignmentListPayload{OrganizationId: organizationId}).Times(2).Return([]client.TeamRoleAssignmentPayload{otherResponse, createResponse}, nil), + mock.EXPECT().TeamRoleAssignmentCreateOrUpdate(&updatePayload).Times(1).Return(nil, errors.New("error")), + mock.EXPECT().TeamRoleAssignmentDelete(&client.TeamRoleAssignmentDeletePayload{TeamId: teamId, OrganizationId: organizationId}).Times(1).Return(nil), ) }) }) diff --git a/env0/resource_team_project_assignment.go b/env0/resource_team_project_assignment.go index 8bbb2b3e..214737b3 100644 --- a/env0/resource_team_project_assignment.go +++ b/env0/resource_team_project_assignment.go @@ -51,15 +51,41 @@ func resourceTeamProjectAssignment() *schema.Resource { } } +func resourceTeamProjectAssignmentCreateOrUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { + apiClient := meta.(client.ApiClientInterface) + + var payload client.TeamRoleAssignmentCreateOrUpdatePayload + if err := readResourceData(&payload, d); err != nil { + return diag.Errorf("schema resource data deserialization failed: %v", err) + } + + role, ok := d.GetOk("role") + if !ok { + role = d.Get("custom_role_id") + } + payload.Role = role.(string) + + assignment, err := apiClient.TeamRoleAssignmentCreateOrUpdate(&payload) + if err != nil { + return diag.Errorf("could not create assignment: %v", err) + } + + d.SetId(assignment.Id) + + return nil +} + func resourceTeamProjectAssignmentRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { apiClient := meta.(client.ApiClientInterface) + var payload client.TeamRoleAssignmentListPayload + id := d.Id() - projectId := d.Get("project_id").(string) - assignments, err := apiClient.TeamProjectAssignments(projectId) + payload.ProjectId = d.Get("project_id").(string) + assignments, err := apiClient.TeamRoleAssignments(&payload) if err != nil { - return diag.Errorf("could not get TeamProjectAssignment: %v", err) + return diag.Errorf("could not get assignments: %v", err) } for _, assignment := range assignments { @@ -84,45 +110,24 @@ func resourceTeamProjectAssignmentRead(ctx context.Context, d *schema.ResourceDa return nil } -func resourceTeamProjectAssignmentCreateOrUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { +func resourceTeamProjectAssignmentDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { apiClient := meta.(client.ApiClientInterface) - var payload client.TeamProjectAssignmentPayload - if err := readResourceData(&payload, d); err != nil { - return diag.Errorf("schema resource data deserialization failed: %v", err) - } + var payload client.TeamRoleAssignmentDeletePayload - role, ok := d.GetOk("role") - if !ok { - role = d.Get("custom_role_id") - } - payload.Role = role.(string) + payload.ProjectId = d.Get("project_id").(string) + payload.TeamId = d.Get("team_id").(string) - assignment, err := apiClient.TeamProjectAssignmentCreateOrUpdate(&payload) - if err != nil { - return diag.Errorf("could not Create or Update TeamProjectAssignment: %v", err) + if err := apiClient.TeamRoleAssignmentDelete(&payload); err != nil { + return diag.Errorf("could not delete assignment: %v", err) } - d.SetId(assignment.Id) - return nil } -func resourceTeamProjectAssignmentDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { +func resourceTeamProjectAssignmentImport(ctx context.Context, d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { apiClient := meta.(client.ApiClientInterface) - 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) - } - - 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 _", d.Id()) @@ -131,9 +136,10 @@ func resourceTeamProjectAssignmentImport(ctx context.Context, d *schema.Resource teamId := splitTeamProject[0] projectId := splitTeamProject[1] - apiClient := meta.(client.ApiClientInterface) + var payload client.TeamRoleAssignmentListPayload + payload.ProjectId = projectId - assignments, err := apiClient.TeamProjectAssignments(projectId) + assignments, err := apiClient.TeamRoleAssignments(&payload) if err != nil { return nil, err } @@ -149,6 +155,9 @@ func resourceTeamProjectAssignmentImport(ctx context.Context, d *schema.Resource } else { d.Set("custom_role_id", assignment.Role) } + + d.Set("project_id", projectId) + return []*schema.ResourceData{d}, nil } } diff --git a/env0/resource_team_project_assignment_test.go b/env0/resource_team_project_assignment_test.go index 9b3306ba..62359161 100644 --- a/env0/resource_team_project_assignment_test.go +++ b/env0/resource_team_project_assignment_test.go @@ -19,32 +19,30 @@ func TestUnitTeamProjectAssignmentResource(t *testing.T) { customRole := "id1" updatedCustomRole := "id2" - assignment := client.TeamProjectAssignment{ - Id: "assignmentId", - TeamId: "teamId0", - ProjectId: "projectId0", - Role: string(client.Admin), + projectId := "projectId0" + + assignment := client.TeamRoleAssignmentPayload{ + Id: "assignmentId", + TeamId: "teamId0", + Role: string(client.Admin), } - updateAssignment := client.TeamProjectAssignment{ - Id: "assignmentIdupdate", - TeamId: "teamIdUupdate", - ProjectId: "projectId0", - Role: string(client.Admin), + updateAssignment := client.TeamRoleAssignmentPayload{ + Id: "assignmentIdupdate", + TeamId: "teamIdUupdate", + Role: string(client.Admin), } - assignmentCustom := client.TeamProjectAssignment{ - Id: "assignmentId", - TeamId: "teamId0", - ProjectId: "projectId0", - Role: customRole, + assignmentCustom := client.TeamRoleAssignmentPayload{ + Id: "assignmentId", + TeamId: "teamId0", + Role: customRole, } - updateAssignmentCustom := client.TeamProjectAssignment{ - Id: "assignmentIdupdate", - TeamId: "teamIdUupdate", - ProjectId: "projectId0", - Role: updatedCustomRole, + updateAssignmentCustom := client.TeamRoleAssignmentPayload{ + Id: "assignmentIdupdate", + TeamId: "teamIdUupdate", + Role: updatedCustomRole, } t.Run("create", func(t *testing.T) { @@ -53,12 +51,12 @@ func TestUnitTeamProjectAssignmentResource(t *testing.T) { { Config: resourceConfigCreate(resourceType, resourceName, map[string]interface{}{ "team_id": assignment.TeamId, - "project_id": assignment.ProjectId, + "project_id": projectId, "role": assignment.Role, }), Check: resource.ComposeAggregateTestCheckFunc( resource.TestCheckResourceAttr(accessor, "team_id", assignment.TeamId), - resource.TestCheckResourceAttr(accessor, "project_id", assignment.ProjectId), + resource.TestCheckResourceAttr(accessor, "project_id", projectId), resource.TestCheckResourceAttr(accessor, "role", assignment.Role), ), }, @@ -66,9 +64,9 @@ func TestUnitTeamProjectAssignmentResource(t *testing.T) { } runUnitTest(t, testCase, func(mock *client.MockApiClientInterface) { - mock.EXPECT().TeamProjectAssignmentCreateOrUpdate(&client.TeamProjectAssignmentPayload{TeamId: assignment.TeamId, ProjectId: assignment.ProjectId, Role: assignment.Role}).Times(1).Return(&assignment, nil) - mock.EXPECT().TeamProjectAssignments(assignment.ProjectId).Times(1).Return([]client.TeamProjectAssignment{assignment}, nil) - mock.EXPECT().TeamProjectAssignmentDelete(assignment.ProjectId, assignment.TeamId).Times(1).Return(nil) + mock.EXPECT().TeamRoleAssignmentCreateOrUpdate(&client.TeamRoleAssignmentCreateOrUpdatePayload{TeamId: assignment.TeamId, ProjectId: projectId, Role: assignment.Role}).Times(1).Return(&assignment, nil) + mock.EXPECT().TeamRoleAssignments(&client.TeamRoleAssignmentListPayload{ProjectId: projectId}).Times(1).Return([]client.TeamRoleAssignmentPayload{assignment}, nil) + mock.EXPECT().TeamRoleAssignmentDelete(&client.TeamRoleAssignmentDeletePayload{TeamId: assignment.TeamId, ProjectId: projectId}).Times(1).Return(nil) }) }) @@ -78,24 +76,24 @@ func TestUnitTeamProjectAssignmentResource(t *testing.T) { { Config: resourceConfigCreate(resourceType, resourceName, map[string]interface{}{ "team_id": assignment.TeamId, - "project_id": assignment.ProjectId, + "project_id": projectId, "role": assignment.Role, }), Check: resource.ComposeAggregateTestCheckFunc( resource.TestCheckResourceAttr(accessor, "team_id", assignment.TeamId), - resource.TestCheckResourceAttr(accessor, "project_id", assignment.ProjectId), + resource.TestCheckResourceAttr(accessor, "project_id", projectId), resource.TestCheckResourceAttr(accessor, "role", assignment.Role), ), }, { Config: resourceConfigCreate(resourceType, resourceName, map[string]interface{}{ "team_id": updateAssignment.TeamId, - "project_id": assignment.ProjectId, + "project_id": projectId, "role": assignment.Role, }), Check: resource.ComposeAggregateTestCheckFunc( resource.TestCheckResourceAttr(accessor, "team_id", updateAssignment.TeamId), - resource.TestCheckResourceAttr(accessor, "project_id", assignment.ProjectId), + resource.TestCheckResourceAttr(accessor, "project_id", projectId), resource.TestCheckResourceAttr(accessor, "role", string(assignment.Role)), ), }, @@ -103,14 +101,14 @@ func TestUnitTeamProjectAssignmentResource(t *testing.T) { } runUnitTest(t, driftTestCase, func(mock *client.MockApiClientInterface) { - mock.EXPECT().TeamProjectAssignmentCreateOrUpdate(&client.TeamProjectAssignmentPayload{TeamId: assignment.TeamId, ProjectId: assignment.ProjectId, Role: assignment.Role}).Times(1).Return(&assignment, nil) - mock.EXPECT().TeamProjectAssignmentCreateOrUpdate(&client.TeamProjectAssignmentPayload{TeamId: updateAssignment.TeamId, ProjectId: assignment.ProjectId, Role: assignment.Role}).Times(1).Return(&updateAssignment, nil) + mock.EXPECT().TeamRoleAssignmentCreateOrUpdate(&client.TeamRoleAssignmentCreateOrUpdatePayload{TeamId: assignment.TeamId, ProjectId: projectId, Role: assignment.Role}).Times(1).Return(&assignment, nil) + mock.EXPECT().TeamRoleAssignmentCreateOrUpdate(&client.TeamRoleAssignmentCreateOrUpdatePayload{TeamId: updateAssignment.TeamId, ProjectId: projectId, Role: assignment.Role}).Times(1).Return(&updateAssignment, nil) gomock.InOrder( - mock.EXPECT().TeamProjectAssignments(assignment.ProjectId).Times(1).Return([]client.TeamProjectAssignment{assignment}, nil), - mock.EXPECT().TeamProjectAssignments(assignment.ProjectId).Times(1).Return([]client.TeamProjectAssignment{updateAssignment}, nil), - mock.EXPECT().TeamProjectAssignments(assignment.ProjectId).Times(1).Return([]client.TeamProjectAssignment{updateAssignment}, nil), + mock.EXPECT().TeamRoleAssignments(&client.TeamRoleAssignmentListPayload{ProjectId: projectId}).Times(1).Return([]client.TeamRoleAssignmentPayload{assignment}, nil), + mock.EXPECT().TeamRoleAssignments(&client.TeamRoleAssignmentListPayload{ProjectId: projectId}).Times(1).Return([]client.TeamRoleAssignmentPayload{updateAssignment}, nil), + mock.EXPECT().TeamRoleAssignments(&client.TeamRoleAssignmentListPayload{ProjectId: projectId}).Times(1).Return([]client.TeamRoleAssignmentPayload{updateAssignment}, nil), ) - mock.EXPECT().TeamProjectAssignmentDelete(updateAssignment.ProjectId, updateAssignment.TeamId).Times(1).Return(nil) + mock.EXPECT().TeamRoleAssignmentDelete(&client.TeamRoleAssignmentDeletePayload{TeamId: updateAssignment.TeamId, ProjectId: projectId}).Times(1).Return(nil) }) }) @@ -120,36 +118,36 @@ func TestUnitTeamProjectAssignmentResource(t *testing.T) { { Config: resourceConfigCreate(resourceType, resourceName, map[string]interface{}{ "team_id": assignmentCustom.TeamId, - "project_id": assignmentCustom.ProjectId, + "project_id": projectId, "custom_role_id": customRole, }), Check: resource.ComposeAggregateTestCheckFunc( resource.TestCheckResourceAttr(accessor, "team_id", assignmentCustom.TeamId), - resource.TestCheckResourceAttr(accessor, "project_id", assignmentCustom.ProjectId), + resource.TestCheckResourceAttr(accessor, "project_id", projectId), resource.TestCheckResourceAttr(accessor, "custom_role_id", customRole), ), }, { Config: resourceConfigCreate(resourceType, resourceName, map[string]interface{}{ "team_id": updateAssignmentCustom.TeamId, - "project_id": updateAssignmentCustom.ProjectId, + "project_id": projectId, "custom_role_id": updatedCustomRole, }), Check: resource.ComposeAggregateTestCheckFunc( resource.TestCheckResourceAttr(accessor, "team_id", updateAssignmentCustom.TeamId), - resource.TestCheckResourceAttr(accessor, "project_id", updateAssignmentCustom.ProjectId), + resource.TestCheckResourceAttr(accessor, "project_id", projectId), resource.TestCheckResourceAttr(accessor, "custom_role_id", updatedCustomRole), ), }, { Config: resourceConfigCreate(resourceType, resourceName, map[string]interface{}{ "team_id": updateAssignmentCustom.TeamId, - "project_id": updateAssignmentCustom.ProjectId, + "project_id": projectId, "role": updateAssignment.Role, }), Check: resource.ComposeAggregateTestCheckFunc( resource.TestCheckResourceAttr(accessor, "team_id", updateAssignmentCustom.TeamId), - resource.TestCheckResourceAttr(accessor, "project_id", updateAssignmentCustom.ProjectId), + resource.TestCheckResourceAttr(accessor, "project_id", projectId), resource.TestCheckResourceAttr(accessor, "role", updateAssignment.Role), ), }, @@ -158,14 +156,14 @@ func TestUnitTeamProjectAssignmentResource(t *testing.T) { runUnitTest(t, testCase, func(mock *client.MockApiClientInterface) { gomock.InOrder( - mock.EXPECT().TeamProjectAssignmentCreateOrUpdate(&client.TeamProjectAssignmentPayload{TeamId: assignmentCustom.TeamId, ProjectId: assignmentCustom.ProjectId, Role: assignmentCustom.Role}).Times(1).Return(&assignmentCustom, nil), - mock.EXPECT().TeamProjectAssignments(assignmentCustom.ProjectId).Times(2).Return([]client.TeamProjectAssignment{assignmentCustom}, nil), - mock.EXPECT().TeamProjectAssignmentDelete(assignmentCustom.ProjectId, assignmentCustom.TeamId).Times(1).Return(nil), - mock.EXPECT().TeamProjectAssignmentCreateOrUpdate(&client.TeamProjectAssignmentPayload{TeamId: updateAssignmentCustom.TeamId, ProjectId: updateAssignmentCustom.ProjectId, Role: updateAssignmentCustom.Role}).Times(1).Return(&updateAssignmentCustom, nil), - mock.EXPECT().TeamProjectAssignments(assignmentCustom.ProjectId).Times(2).Return([]client.TeamProjectAssignment{updateAssignmentCustom}, nil), - mock.EXPECT().TeamProjectAssignmentCreateOrUpdate(&client.TeamProjectAssignmentPayload{TeamId: updateAssignmentCustom.TeamId, ProjectId: updateAssignmentCustom.ProjectId, Role: updateAssignment.Role}).Times(1).Return(&updateAssignment, nil), - mock.EXPECT().TeamProjectAssignments(assignmentCustom.ProjectId).Times(1).Return([]client.TeamProjectAssignment{updateAssignment}, nil), - mock.EXPECT().TeamProjectAssignmentDelete(updateAssignment.ProjectId, updateAssignment.TeamId).Times(1).Return(nil), + mock.EXPECT().TeamRoleAssignmentCreateOrUpdate(&client.TeamRoleAssignmentCreateOrUpdatePayload{TeamId: assignmentCustom.TeamId, ProjectId: projectId, Role: assignmentCustom.Role}).Times(1).Return(&assignmentCustom, nil), + mock.EXPECT().TeamRoleAssignments(&client.TeamRoleAssignmentListPayload{ProjectId: projectId}).Times(2).Return([]client.TeamRoleAssignmentPayload{assignmentCustom}, nil), + mock.EXPECT().TeamRoleAssignmentDelete(&client.TeamRoleAssignmentDeletePayload{TeamId: assignmentCustom.TeamId, ProjectId: projectId}).Times(1).Return(nil), + mock.EXPECT().TeamRoleAssignmentCreateOrUpdate(&client.TeamRoleAssignmentCreateOrUpdatePayload{TeamId: updateAssignmentCustom.TeamId, ProjectId: projectId, Role: updateAssignmentCustom.Role}).Times(1).Return(&updateAssignmentCustom, nil), + mock.EXPECT().TeamRoleAssignments(&client.TeamRoleAssignmentListPayload{ProjectId: projectId}).Times(2).Return([]client.TeamRoleAssignmentPayload{updateAssignmentCustom}, nil), + mock.EXPECT().TeamRoleAssignmentCreateOrUpdate(&client.TeamRoleAssignmentCreateOrUpdatePayload{TeamId: updateAssignmentCustom.TeamId, ProjectId: projectId, Role: updateAssignment.Role}).Times(1).Return(&updateAssignment, nil), + mock.EXPECT().TeamRoleAssignments(&client.TeamRoleAssignmentListPayload{ProjectId: projectId}).Times(1).Return([]client.TeamRoleAssignmentPayload{updateAssignment}, nil), + mock.EXPECT().TeamRoleAssignmentDelete(&client.TeamRoleAssignmentDeletePayload{TeamId: updateAssignment.TeamId, ProjectId: projectId}).Times(1).Return(nil), ) }) }) @@ -176,23 +174,23 @@ func TestUnitTeamProjectAssignmentResource(t *testing.T) { { Config: resourceConfigCreate(resourceType, resourceName, map[string]interface{}{ "team_id": assignment.TeamId, - "project_id": assignment.ProjectId, + "project_id": projectId, "role": assignment.Role, }), }, { ResourceName: resourceNameImport, ImportState: true, - ImportStateId: assignment.TeamId + "_" + assignment.ProjectId, + ImportStateId: assignment.TeamId + "_" + projectId, ImportStateVerify: true, }, }, } runUnitTest(t, testCase, func(mock *client.MockApiClientInterface) { - mock.EXPECT().TeamProjectAssignmentCreateOrUpdate(&client.TeamProjectAssignmentPayload{TeamId: assignment.TeamId, ProjectId: assignment.ProjectId, Role: assignment.Role}).Times(1).Return(&assignment, nil) - mock.EXPECT().TeamProjectAssignments(assignment.ProjectId).Times(3).Return([]client.TeamProjectAssignment{assignment}, nil) - mock.EXPECT().TeamProjectAssignmentDelete(assignment.ProjectId, assignment.TeamId).Times(1).Return(nil) + mock.EXPECT().TeamRoleAssignmentCreateOrUpdate(&client.TeamRoleAssignmentCreateOrUpdatePayload{TeamId: assignment.TeamId, ProjectId: projectId, Role: assignment.Role}).Times(1).Return(&assignment, nil) + mock.EXPECT().TeamRoleAssignments(&client.TeamRoleAssignmentListPayload{ProjectId: projectId}).Times(3).Return([]client.TeamRoleAssignmentPayload{assignment}, nil) + mock.EXPECT().TeamRoleAssignmentDelete(&client.TeamRoleAssignmentDeletePayload{TeamId: assignment.TeamId, ProjectId: projectId}).Times(1).Return(nil) }) }) @@ -202,23 +200,23 @@ func TestUnitTeamProjectAssignmentResource(t *testing.T) { { Config: resourceConfigCreate(resourceType, resourceName, map[string]interface{}{ "team_id": assignmentCustom.TeamId, - "project_id": assignmentCustom.ProjectId, + "project_id": projectId, "custom_role_id": assignmentCustom.Role, }), }, { ResourceName: resourceNameImport, ImportState: true, - ImportStateId: assignmentCustom.TeamId + "_" + assignmentCustom.ProjectId, + ImportStateId: assignmentCustom.TeamId + "_" + projectId, ImportStateVerify: true, }, }, } runUnitTest(t, testCase, func(mock *client.MockApiClientInterface) { - mock.EXPECT().TeamProjectAssignmentCreateOrUpdate(&client.TeamProjectAssignmentPayload{TeamId: assignmentCustom.TeamId, ProjectId: assignmentCustom.ProjectId, Role: assignmentCustom.Role}).Times(1).Return(&assignmentCustom, nil) - mock.EXPECT().TeamProjectAssignments(assignmentCustom.ProjectId).Times(3).Return([]client.TeamProjectAssignment{assignmentCustom}, nil) - mock.EXPECT().TeamProjectAssignmentDelete(assignmentCustom.ProjectId, assignmentCustom.TeamId).Times(1).Return(nil) + mock.EXPECT().TeamRoleAssignmentCreateOrUpdate(&client.TeamRoleAssignmentCreateOrUpdatePayload{TeamId: assignmentCustom.TeamId, ProjectId: projectId, Role: assignmentCustom.Role}).Times(1).Return(&assignmentCustom, nil) + mock.EXPECT().TeamRoleAssignments(&client.TeamRoleAssignmentListPayload{ProjectId: projectId}).Times(3).Return([]client.TeamRoleAssignmentPayload{assignmentCustom}, nil) + mock.EXPECT().TeamRoleAssignmentDelete(&client.TeamRoleAssignmentDeletePayload{TeamId: assignmentCustom.TeamId, ProjectId: projectId}).Times(1).Return(nil) }) }) @@ -228,14 +226,14 @@ func TestUnitTeamProjectAssignmentResource(t *testing.T) { { Config: resourceConfigCreate(resourceType, resourceName, map[string]interface{}{ "team_id": assignment.TeamId, - "project_id": assignment.ProjectId, + "project_id": projectId, "role": assignment.Role, }), }, { ResourceName: resourceNameImport, ImportState: true, - ImportStateId: assignment.TeamId + "_" + assignment.ProjectId, + ImportStateId: assignment.TeamId + "_" + projectId, ImportStateVerify: true, ExpectError: regexp.MustCompile("not found"), }, @@ -243,10 +241,10 @@ func TestUnitTeamProjectAssignmentResource(t *testing.T) { } runUnitTest(t, testCase, func(mock *client.MockApiClientInterface) { - mock.EXPECT().TeamProjectAssignmentCreateOrUpdate(&client.TeamProjectAssignmentPayload{TeamId: assignment.TeamId, ProjectId: assignment.ProjectId, Role: assignment.Role}).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.ProjectId, assignment.TeamId).Times(1) + mock.EXPECT().TeamRoleAssignmentCreateOrUpdate(&client.TeamRoleAssignmentCreateOrUpdatePayload{TeamId: assignment.TeamId, ProjectId: projectId, Role: assignment.Role}).Times(1).Return(&assignment, nil) + mock.EXPECT().TeamRoleAssignments(&client.TeamRoleAssignmentListPayload{ProjectId: projectId}).Times(1).Return([]client.TeamRoleAssignmentPayload{assignment}, nil) + mock.EXPECT().TeamRoleAssignments(&client.TeamRoleAssignmentListPayload{ProjectId: projectId}).Times(1).Return([]client.TeamRoleAssignmentPayload{}, nil) + mock.EXPECT().TeamRoleAssignmentDelete(&client.TeamRoleAssignmentDeletePayload{TeamId: assignment.TeamId, ProjectId: projectId}).Times(1) }) }) } diff --git a/env0/validators.go b/env0/validators.go index 35dab28b..726bcb7f 100644 --- a/env0/validators.go +++ b/env0/validators.go @@ -56,7 +56,7 @@ func ValidateRetries(i interface{}, path cty.Path) diag.Diagnostics { } func ValidateRole(i interface{}, path cty.Path) diag.Diagnostics { - role := client.ProjectRole(i.(string)) + role := i.(string) if role == "" || role != client.Admin && role != client.Deployer &&