Skip to content

Commit

Permalink
Added unassign
Browse files Browse the repository at this point in the history
  • Loading branch information
TomerHeber committed Apr 21, 2024
1 parent 78cdcba commit 8ea9c50
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 14 deletions.
2 changes: 1 addition & 1 deletion client/api_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ type ApiClientInterface interface {
UserRoleEnvironmentAssignments(environmentId string) ([]UserRoleEnvironmentAssignment, error)
ApprovalPolicies(name string) ([]ApprovalPolicy, error)
ApprovalPolicyAssign(assignment *ApprovalPolicyAssignment) (*ApprovalPolicyAssignment, error)
ApprovalPolicyUnassign(scope string, scopeId string) error
ApprovalPolicyUnassign(id string) error
ApprovalPolicyByScope(scope string, scopeId string) ([]ApprovalPolicyByScope, error)
ApprovalPolicyCreate(payload *ApprovalPolicyCreatePayload) (*ApprovalPolicy, error)
ApprovalPolicyUpdate(payload *ApprovalPolicyUpdatePayload) (*ApprovalPolicy, error)
Expand Down
8 changes: 4 additions & 4 deletions client/api_client_mock.go

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

4 changes: 2 additions & 2 deletions client/approval_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,8 @@ func (client *ApiClient) ApprovalPolicyAssign(assignment *ApprovalPolicyAssignme
return &result, nil
}

func (client *ApiClient) ApprovalPolicyUnassign(scope string, scopeId string) error {
return client.http.Delete(fmt.Sprintf("/approval-policy/assignment/%s/%s", scope, scopeId), nil)
func (client *ApiClient) ApprovalPolicyUnassign(id string) error {
return client.http.Delete("/approval-policy/assignment", map[string]string{"id": id})
}

func (client *ApiClient) ApprovalPolicyByScope(scope string, scopeId string) ([]ApprovalPolicyByScope, error) {
Expand Down
5 changes: 3 additions & 2 deletions client/approval_policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,12 @@ var _ = Describe("Approval Policy Client", func() {

Describe("Unassign Custom Flow", func() {
var err error
id := fmt.Sprintf("%s#%s#%s", mockAssignment.Scope, mockAssignment.ScopeId, mockAssignment.BlueprintId)

BeforeEach(func() {
httpCall = mockHttpClient.EXPECT().Delete(fmt.Sprintf("/approval-policy/assignment/%s/%s", ApprovalPolicyProjectScope, "scope_id"), nil)
httpCall = mockHttpClient.EXPECT().Delete("/approval-policy/assignment", map[string]string{"id": id})
httpCall.Times(1)
err = apiClient.ApprovalPolicyUnassign(string(ApprovalPolicyProjectScope), "scope_id")
err = apiClient.ApprovalPolicyUnassign(id)
})

It("Should not return an error", func() {
Expand Down
7 changes: 5 additions & 2 deletions env0/resource_approval_policy_assignment.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,12 @@ func resourceApprovalPolicyAssignmentDelete(ctx context.Context, d *schema.Resou

scope := d.Get("scope").(string)
scopeId := d.Get("scope_id").(string)
bluePrintId := d.Get("blueprint_id").(string)

if err := apiClient.ApprovalPolicyUnassign(scope, scopeId); err != nil {
return diag.Errorf("failed to unassign approval policy from scope %s %s: %v", scope, scopeId, err)
id := fmt.Sprintf("%s#%s#%s", scope, scopeId, bluePrintId)

if err := apiClient.ApprovalPolicyUnassign(id); err != nil {
return diag.Errorf("failed to unassign approval policy %s: %v", id, err)
}

return nil
Expand Down
8 changes: 5 additions & 3 deletions env0/resource_approval_policy_assignment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ func TestUnitResourceApprovalPolicyAssignmentResource(t *testing.T) {
BlueprintId: "blueprint_id",
}

id := fmt.Sprintf("%s#%s#%s", assignment.Scope, assignment.ScopeId, assignment.BlueprintId)

stepConfig := resourceConfigCreate(resourceType, resourceName, map[string]interface{}{
"scope_id": assignment.ScopeId,
"blueprint_id": assignment.BlueprintId,
Expand Down Expand Up @@ -61,7 +63,7 @@ func TestUnitResourceApprovalPolicyAssignmentResource(t *testing.T) {
mock.EXPECT().Template(assignment.BlueprintId).Times(1).Return(validTemplate, nil),
mock.EXPECT().ApprovalPolicyAssign(&assignment).Times(1).Return(&assignment, nil),
mock.EXPECT().ApprovalPolicyByScope(string(assignment.Scope), assignment.ScopeId).Times(1).Return([]client.ApprovalPolicyByScope{approvalPolicyByScope}, nil),
mock.EXPECT().ApprovalPolicyUnassign(string(assignment.Scope), assignment.ScopeId).Times(1).Return(nil),
mock.EXPECT().ApprovalPolicyUnassign(id).Times(1).Return(nil),
)
})
})
Expand Down Expand Up @@ -142,7 +144,7 @@ func TestUnitResourceApprovalPolicyAssignmentResource(t *testing.T) {
mock.EXPECT().Template(assignment.BlueprintId).Times(1).Return(validTemplate, nil),
mock.EXPECT().ApprovalPolicyAssign(&assignment).Times(1).Return(&assignment, nil),
mock.EXPECT().ApprovalPolicyByScope(string(assignment.Scope), assignment.ScopeId).Times(1).Return(nil, &client.NotFoundError{}),
mock.EXPECT().ApprovalPolicyUnassign(string(assignment.Scope), assignment.ScopeId).Times(1).Return(nil),
mock.EXPECT().ApprovalPolicyUnassign(id).Times(1).Return(nil),
)
})
})
Expand Down Expand Up @@ -177,7 +179,7 @@ func TestUnitResourceApprovalPolicyAssignmentResource(t *testing.T) {
mock.EXPECT().Template(assignment.BlueprintId).Times(1).Return(validTemplate, nil),
mock.EXPECT().ApprovalPolicyAssign(&assignment).Times(1).Return(&assignment, nil),
mock.EXPECT().ApprovalPolicyByScope(string(assignment.Scope), assignment.ScopeId).Times(1).Return([]client.ApprovalPolicyByScope{approvalPolicyByScopeMismatch}, nil),
mock.EXPECT().ApprovalPolicyUnassign(string(assignment.Scope), assignment.ScopeId).Times(1).Return(nil),
mock.EXPECT().ApprovalPolicyUnassign(id).Times(1).Return(nil),
)
})
})
Expand Down

0 comments on commit 8ea9c50

Please sign in to comment.