Skip to content

Commit

Permalink
Convert user policies fromList to Set (#587)
Browse files Browse the repository at this point in the history
Signed-off-by: peterdeme <[email protected]>
  • Loading branch information
peterdeme authored Nov 22, 2024
1 parent f715e37 commit 0b0d800
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 10 deletions.
2 changes: 1 addition & 1 deletion docs/resources/idp_group_mapping.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ resource "spacelift_idp_group_mapping" "test" {
### Required

- `name` (String) Name of the user group - should be unique in one account
- `policy` (Block List, Min: 1) (see [below for nested schema](#nestedblock--policy))
- `policy` (Block Set, Min: 1) (see [below for nested schema](#nestedblock--policy))

### Read-Only

Expand Down
2 changes: 1 addition & 1 deletion docs/resources/user.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ description: |-

### Required

- `policy` (Block List, Min: 1) (see [below for nested schema](#nestedblock--policy))
- `policy` (Block Set, Min: 1) (see [below for nested schema](#nestedblock--policy))
- `username` (String) Username of the user

### Optional
Expand Down
18 changes: 11 additions & 7 deletions spacelift/resource_idp_group_mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func resourceIdpGroupMapping() *schema.Resource {
ValidateDiagFunc: validations.DisallowEmptyString,
},
"policy": {
Type: schema.TypeList,
Type: schema.TypeSet,
MinItems: 1,
Required: true,
Elem: &schema.Resource{
Expand All @@ -63,6 +63,7 @@ func resourceIdpGroupMapping() *schema.Resource {
},
},
},
Set: userPolicyHash,
},
},
}
Expand Down Expand Up @@ -163,12 +164,15 @@ func resourceIdpGroupMappingDelete(ctx context.Context, d *schema.ResourceData,

func getAccessRules(d *schema.ResourceData) []structs.SpaceAccessRuleInput {
var accessRules []structs.SpaceAccessRuleInput
for _, a := range d.Get("policy").([]interface{}) {
access := a.(map[string]interface{})
accessRules = append(accessRules, structs.SpaceAccessRuleInput{
Space: toID(access["space_id"]),
SpaceAccessLevel: structs.SpaceAccessLevel(access["role"].(string)),
})
if policies, ok := d.Get("policy").(*schema.Set); ok {
for _, a := range policies.List() {
access := a.(map[string]interface{})
accessRules = append(accessRules, structs.SpaceAccessRuleInput{
Space: toID(access["space_id"]),
SpaceAccessLevel: structs.SpaceAccessLevel(access["role"].(string)),
})
}
}

return accessRules
}
16 changes: 15 additions & 1 deletion spacelift/resource_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func resourceUser() *schema.Resource {
Description: "Username of the user",
},
"policy": {
Type: schema.TypeList,
Type: schema.TypeSet,
MinItems: 1,
Required: true,
Elem: &schema.Resource{
Expand All @@ -55,6 +55,7 @@ func resourceUser() *schema.Resource {
},
},
},
Set: userPolicyHash,
},
"invitation_email": {
Type: schema.TypeString,
Expand All @@ -65,6 +66,19 @@ func resourceUser() *schema.Resource {
}
}

func userPolicyHash(v interface{}) int {
m, ok := v.(map[string]interface{})
if !ok {
return 0
}

spaceID, _ := m["space_id"].(string)
role, _ := m["role"].(string)

key := spaceID + "-" + role
return schema.HashString(key)
}

func resourceUserCreate(ctx context.Context, d *schema.ResourceData, i interface{}) diag.Diagnostics {
// send an Invite (create) mutation to the API
var mutation struct {
Expand Down
41 changes: 41 additions & 0 deletions spacelift/resource_user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,21 @@ resource "spacelift_user" "test" {
}
`

var userWithTwoAccessesDifferentOrder = `
resource "spacelift_user" "test" {
invitation_email = "%s"
username = "%s"
policy {
space_id = "legacy"
role = "READ"
}
policy {
space_id = "root"
role = "ADMIN"
}
}
`

func TestUserResource(t *testing.T) {
const resourceName = "spacelift_user.test"

Expand Down Expand Up @@ -157,4 +172,30 @@ func TestUserResource(t *testing.T) {
})
})

t.Run("can change policy order without update", func(t *testing.T) {
randomUsername := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum)
exampleEmail := fmt.Sprintf("%[email protected]", randomUsername)

testSteps(t, []resource.TestStep{
{
Config: fmt.Sprintf(userWithTwoAccesses, exampleEmail, randomUsername),
Check: Resource(
resourceName,
Attribute("invitation_email", Equals(exampleEmail)),
Attribute("username", Equals(randomUsername)),
SetContains("policy", "root", "ADMIN"),
SetContains("policy", "legacy", "READ")),
},
{
Config: fmt.Sprintf(userWithTwoAccessesDifferentOrder, exampleEmail, randomUsername),
Check: Resource(
resourceName,
Attribute("invitation_email", Equals(exampleEmail)),
Attribute("username", Equals(randomUsername)),
SetContains("policy", "root", "ADMIN"),
SetContains("policy", "legacy", "READ")),
},
})

})
}

0 comments on commit 0b0d800

Please sign in to comment.