Skip to content

Commit

Permalink
SCIM patch to remove resources must not contain a value field (#3374)
Browse files Browse the repository at this point in the history
* SCIM patch to remove resources must not contain a value field

* simplification

* remove accidental extra test
  • Loading branch information
mgyucht authored Mar 15, 2024
1 parent d032b9d commit 11bceaa
Show file tree
Hide file tree
Showing 15 changed files with 34 additions and 33 deletions.
4 changes: 2 additions & 2 deletions aws/resource_group_instance_profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ func ResourceGroupInstanceProfile() common.Resource {
return err
},
CreateContext: func(ctx context.Context, groupID, roleARN string, c *common.DatabricksClient) error {
return scim.NewGroupsAPI(ctx, c).Patch(groupID, scim.PatchRequest("add", "roles", roleARN))
return scim.NewGroupsAPI(ctx, c).Patch(groupID, scim.PatchRequestWithValue("add", "roles", roleARN))
},
DeleteContext: func(ctx context.Context, groupID, roleARN string, c *common.DatabricksClient) error {
return scim.NewGroupsAPI(ctx, c).Patch(groupID, scim.PatchRequest(
"remove", fmt.Sprintf(`roles[value eq "%s"]`, roleARN), ""))
"remove", fmt.Sprintf(`roles[value eq "%s"]`, roleARN)))
},
})
r.DeprecationMessage = "Please migrate to `databricks_group_role`"
Expand Down
5 changes: 2 additions & 3 deletions aws/resource_group_instance_profile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func TestResourceGroupInstanceProfileCreate(t *testing.T) {
{
Method: "PATCH",
Resource: "/api/2.0/preview/scim/v2/Groups/abc",
ExpectedRequest: scim.PatchRequest(
ExpectedRequest: scim.PatchRequestWithValue(
"add",
"roles",
"arn:aws:iam::999999999999:instance-profile/my-fake-instance-profile"),
Expand Down Expand Up @@ -215,8 +215,7 @@ func TestResourceGroupInstanceProfileDelete(t *testing.T) {
Resource: "/api/2.0/preview/scim/v2/Groups/abc",
ExpectedRequest: scim.PatchRequest(
"remove",
`roles[value eq "arn:aws:iam::999999999999:instance-profile/my-fake-instance-profile"]`,
""),
`roles[value eq "arn:aws:iam::999999999999:instance-profile/my-fake-instance-profile"]`),
},
},
Resource: ResourceGroupInstanceProfile(),
Expand Down
4 changes: 2 additions & 2 deletions aws/resource_service_principal_role.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
func ResourceServicePrincipalRole() common.Resource {
r := common.NewPairID("service_principal_id", "role").BindResource(common.BindResource{
CreateContext: func(ctx context.Context, servicePrincipalID, role string, c *common.DatabricksClient) error {
return scim.NewServicePrincipalsAPI(ctx, c).Patch(servicePrincipalID, scim.PatchRequest("add", "roles", role))
return scim.NewServicePrincipalsAPI(ctx, c).Patch(servicePrincipalID, scim.PatchRequestWithValue("add", "roles", role))
},
ReadContext: func(ctx context.Context, servicePrincipalID, roleARN string, c *common.DatabricksClient) error {
servicePrincipal, err := scim.NewServicePrincipalsAPI(ctx, c).Read(servicePrincipalID, "roles")
Expand All @@ -25,7 +25,7 @@ func ResourceServicePrincipalRole() common.Resource {
},
DeleteContext: func(ctx context.Context, servicePrincipalID, roleARN string, c *common.DatabricksClient) error {
return scim.NewServicePrincipalsAPI(ctx, c).Patch(servicePrincipalID, scim.PatchRequest(
"remove", fmt.Sprintf(`roles[value eq "%s"]`, roleARN), ""))
"remove", fmt.Sprintf(`roles[value eq "%s"]`, roleARN)))
},
})
return r
Expand Down
2 changes: 1 addition & 1 deletion aws/resource_service_principal_role_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func TestResourceServicePrincipalRoleCreate(t *testing.T) {
{
Method: "PATCH",
Resource: "/api/2.0/preview/scim/v2/ServicePrincipals/abc",
ExpectedRequest: scim.PatchRequest(
ExpectedRequest: scim.PatchRequestWithValue(
"add",
"roles",
"arn:aws:iam::999999999999:instance-profile/my-fake-instance-profile"),
Expand Down
4 changes: 2 additions & 2 deletions aws/resource_user_instance_profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func ResourceUserInstanceProfile() common.Resource {
return m
}).BindResource(common.BindResource{
CreateContext: func(ctx context.Context, userID, roleARN string, c *common.DatabricksClient) error {
return scim.NewUsersAPI(ctx, c).Patch(userID, scim.PatchRequest("add", "roles", roleARN))
return scim.NewUsersAPI(ctx, c).Patch(userID, scim.PatchRequestWithValue("add", "roles", roleARN))
},
ReadContext: func(ctx context.Context, userID, roleARN string, c *common.DatabricksClient) error {
user, err := scim.NewUsersAPI(ctx, c).Read(userID, "roles")
Expand All @@ -31,7 +31,7 @@ func ResourceUserInstanceProfile() common.Resource {
},
DeleteContext: func(ctx context.Context, userID, roleARN string, c *common.DatabricksClient) error {
return scim.NewUsersAPI(ctx, c).Patch(userID, scim.PatchRequest(
"remove", fmt.Sprintf(`roles[value eq "%s"]`, roleARN), ""))
"remove", fmt.Sprintf(`roles[value eq "%s"]`, roleARN)))
},
})
r.DeprecationMessage = "Please migrate to `databricks_user_role`. This resource will be removed in v0.5.x"
Expand Down
5 changes: 2 additions & 3 deletions aws/resource_user_instance_profile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func TestResourceUserInstanceProfileCreate(t *testing.T) {
{
Method: "PATCH",
Resource: "/api/2.0/preview/scim/v2/Users/abc",
ExpectedRequest: scim.PatchRequest(
ExpectedRequest: scim.PatchRequestWithValue(
"add",
"roles",
"arn:aws:iam::999999999999:instance-profile/my-fake-instance-profile"),
Expand Down Expand Up @@ -181,8 +181,7 @@ func TestResourceUserInstanceProfileDelete(t *testing.T) {
Resource: "/api/2.0/preview/scim/v2/Users/abc",
ExpectedRequest: scim.PatchRequest(
"remove",
`roles[value eq "arn:aws:iam::999999999999:instance-profile/my-fake-instance-profile"]`,
""),
`roles[value eq "arn:aws:iam::999999999999:instance-profile/my-fake-instance-profile"]`),
},
},
Resource: ResourceUserInstanceProfile(),
Expand Down
4 changes: 2 additions & 2 deletions aws/resource_user_role.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
func ResourceUserRole() common.Resource {
return common.NewPairID("user_id", "role").BindResource(common.BindResource{
CreateContext: func(ctx context.Context, userID, role string, c *common.DatabricksClient) error {
return scim.NewUsersAPI(ctx, c).Patch(userID, scim.PatchRequest("add", "roles", role))
return scim.NewUsersAPI(ctx, c).Patch(userID, scim.PatchRequestWithValue("add", "roles", role))
},
ReadContext: func(ctx context.Context, userID, roleARN string, c *common.DatabricksClient) error {
user, err := scim.NewUsersAPI(ctx, c).Read(userID, "roles")
Expand All @@ -24,7 +24,7 @@ func ResourceUserRole() common.Resource {
},
DeleteContext: func(ctx context.Context, userID, roleARN string, c *common.DatabricksClient) error {
return scim.NewUsersAPI(ctx, c).Patch(userID, scim.PatchRequest(
"remove", fmt.Sprintf(`roles[value eq "%s"]`, roleARN), ""))
"remove", fmt.Sprintf(`roles[value eq "%s"]`, roleARN)))
},
})
}
2 changes: 1 addition & 1 deletion aws/resource_user_role_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func TestUserRoleCreate_AndGetResourceDrift(t *testing.T) {
{
Method: "PATCH",
Resource: "/api/2.0/preview/scim/v2/Users/a",
ExpectedRequest: scim.PatchRequest("add", "roles", "b"),
ExpectedRequest: scim.PatchRequestWithValue("add", "roles", "b"),
},
{
Method: "GET",
Expand Down
4 changes: 2 additions & 2 deletions scim/resource_group_member.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
func ResourceGroupMember() common.Resource {
return common.NewPairID("group_id", "member_id").BindResource(common.BindResource{
CreateContext: func(ctx context.Context, groupID, memberID string, c *common.DatabricksClient) error {
return NewGroupsAPI(ctx, c).Patch(groupID, PatchRequest("add", "members", memberID))
return NewGroupsAPI(ctx, c).Patch(groupID, PatchRequestWithValue("add", "members", memberID))
},
ReadContext: func(ctx context.Context, groupID, memberID string, c *common.DatabricksClient) error {
group, err := NewGroupsAPI(ctx, c).Read(groupID, "members")
Expand All @@ -24,7 +24,7 @@ func ResourceGroupMember() common.Resource {
},
DeleteContext: func(ctx context.Context, groupID, memberID string, c *common.DatabricksClient) error {
return NewGroupsAPI(ctx, c).Patch(groupID, PatchRequest(
"remove", fmt.Sprintf(`members[value eq "%s"]`, memberID), ""))
"remove", fmt.Sprintf(`members[value eq "%s"]`, memberID)))
},
})
}
5 changes: 2 additions & 3 deletions scim/resource_group_member_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func TestResourceGroupMemberCreate(t *testing.T) {
{
Method: "PATCH",
Resource: "/api/2.0/preview/scim/v2/Groups/abc",
ExpectedRequest: PatchRequest("add", "members", "bcd"),
ExpectedRequest: PatchRequestWithValue("add", "members", "bcd"),
Response: Group{
ID: "abc",
},
Expand Down Expand Up @@ -164,8 +164,7 @@ func TestResourceGroupMemberDelete(t *testing.T) {
Resource: "/api/2.0/preview/scim/v2/Groups/abc",
ExpectedRequest: PatchRequest(
"remove",
`members[value eq "bcd"]`,
""),
`members[value eq "bcd"]`),
},
},
Resource: ResourceGroupMember(),
Expand Down
4 changes: 2 additions & 2 deletions scim/resource_group_role.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
func ResourceGroupRole() common.Resource {
return common.NewPairID("group_id", "role").BindResource(common.BindResource{
CreateContext: func(ctx context.Context, groupID, role string, c *common.DatabricksClient) error {
return NewGroupsAPI(ctx, c).Patch(groupID, PatchRequest("add", "roles", role))
return NewGroupsAPI(ctx, c).Patch(groupID, PatchRequestWithValue("add", "roles", role))
},
ReadContext: func(ctx context.Context, groupID, role string, c *common.DatabricksClient) error {
group, err := NewGroupsAPI(ctx, c).Read(groupID, "roles")
Expand All @@ -24,7 +24,7 @@ func ResourceGroupRole() common.Resource {
},
DeleteContext: func(ctx context.Context, groupID, role string, c *common.DatabricksClient) error {
return NewGroupsAPI(ctx, c).Patch(groupID, PatchRequest(
"remove", fmt.Sprintf(`roles[value eq "%s"]`, role), ""))
"remove", fmt.Sprintf(`roles[value eq "%s"]`, role)))
},
})
}
5 changes: 2 additions & 3 deletions scim/resource_group_role_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func TestResourceGroupRoleCreate(t *testing.T) {
{
Method: "PATCH",
Resource: "/api/2.0/preview/scim/v2/Groups/abc",
ExpectedRequest: PatchRequest("add", "roles", "arn:aws:iam::000000000000:role/test-role"),
ExpectedRequest: PatchRequestWithValue("add", "roles", "arn:aws:iam::000000000000:role/test-role"),
Response: Group{
ID: "abc",
},
Expand Down Expand Up @@ -155,8 +155,7 @@ func TestResourceGroupRoleDelete(t *testing.T) {
Resource: "/api/2.0/preview/scim/v2/Groups/abc",
ExpectedRequest: PatchRequest(
"remove",
`roles[value eq "arn:aws:iam::000000000000:role/test-role"]`,
""),
`roles[value eq "arn:aws:iam::000000000000:role/test-role"]`),
},
},
Resource: ResourceGroupRole(),
Expand Down
2 changes: 1 addition & 1 deletion scim/resource_service_principal.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ func ResourceServicePrincipal() common.Resource {
}
// Disable or delete
if isDisable {
r := PatchRequest("replace", "active", "false")
r := PatchRequestWithValue("replace", "active", "false")
err = spAPI.Patch(d.Id(), r)
} else {
err = spAPI.Delete(d.Id())
Expand Down
2 changes: 1 addition & 1 deletion scim/resource_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ func ResourceUser() common.Resource {
}
// Disable or delete
if isDisable {
r := PatchRequest("replace", "active", "false")
r := PatchRequestWithValue("replace", "active", "false")
err = user.Patch(d.Id(), r)
} else {
err = user.Delete(d.Id())
Expand Down
15 changes: 10 additions & 5 deletions scim/scim.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,14 +175,19 @@ type patchRequest struct {
Operations []patchOperation `json:"Operations,omitempty"`
}

func PatchRequest(op, path, value string) patchRequest {
func PatchRequest(op, path string) patchRequest {
o := patchOperation{
Op: op,
Path: path,
}
return PatchRequestComplexValue([]patchOperation{o})
}

func PatchRequestWithValue(op, path, value string) patchRequest {
o := patchOperation{
Op: op,
Path: path,
Value: value,
}
if value != "" {
o.Value = []ComplexValue{{Value: value}}
Value: []ComplexValue{{Value: value}},
}
return PatchRequestComplexValue([]patchOperation{o})
}
Expand Down

0 comments on commit 11bceaa

Please sign in to comment.