Skip to content

Commit

Permalink
SAT-1809 UserGroup membership
Browse files Browse the repository at this point in the history
  • Loading branch information
kgibson-spotnana committed Mar 15, 2024
1 parent 34f4d58 commit 155a274
Show file tree
Hide file tree
Showing 10 changed files with 43 additions and 26 deletions.
2 changes: 1 addition & 1 deletion docs/resources/app.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,6 @@ resource "jumpcloud_app" "example" {
Import is supported using the following syntax:

```shell
# Order can be imported by specifying the app `id` and `name` in the `terraform import` command.
# Apps can be imported by specifying the app `id` and `name` in the `terraform import` command.
terraform import snjumpcloud_app.example 64f8c031123131314ad6a7
```
7 changes: 6 additions & 1 deletion docs/resources/usergroup.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ description: |-
resource "jumpcloud_usergroup" "example" {
name = "example-name"
description = "example description"
members = [
"example-member-1",
"example-member-2",
]
}
```

Expand All @@ -25,6 +29,7 @@ resource "jumpcloud_usergroup" "example" {
### Optional

- `description` (String) User Group Description
- `members` (Set of String) This is a set of user IDs associated with this group.
- `name` (String) User Group Name

### Read-Only
Expand All @@ -39,6 +44,6 @@ resource "jumpcloud_usergroup" "example" {
Import is supported using the following syntax:

```shell
# Order can be imported by specifying the usergroup `id` and `name` in the `terraform import` command.
# UserGroups can be imported by specifying the usergroup `id` and `name` in the `terraform import` command.
terraform import snjumpcloud_usergroup.example 64f8c031123131314ad6a7
```
3 changes: 3 additions & 0 deletions examples/jumpcloud/app/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,17 @@ provider "jumpcloud" {
resource "jumpcloud_usergroup" "group1" {
name = "Test-Terraform-Group1"
description = "This group was created by Spotnana Terraform Provider!"
members = []
}
resource "jumpcloud_usergroup" "group2" {
name = "Test-Terraform-Group2"
description = "This group was also created by Spotnana Terraform Provider!"
members = []
}
resource "jumpcloud_usergroup" "group3" {
name = "Test-Terraform-Group3"
description = "This group was the 3rd created by Spotnana Terraform Provider!"
members = []
}

# Importing the app association via applicationID
Expand Down
1 change: 1 addition & 0 deletions examples/jumpcloud/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ data "jumpcloud_apps" "all_apps" {}
resource "jumpcloud_usergroup" "new_usergroup" {
name = "tf-provider-test-new_usergroup"
description = "This is a new usergroup from the Terraform provider"
members = []
}

# Importing the app association via applicationID
Expand Down
4 changes: 1 addition & 3 deletions examples/jumpcloud/usergroup/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ provider "jumpcloud" {
resource "jumpcloud_usergroup" "example_group" {
name = "example-terraform-group"
description = "This group was created by Spotnana Terraform Provider!"
members = [
"kgibson@spotnana.com",
"bgodard@spotnana.com",]
members = []
}

output "group_details" {
Expand Down
2 changes: 1 addition & 1 deletion examples/resources/jumpcloud_app/import.sh
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# Order can be imported by specifying the app `id` and `name` in the `terraform import` command.
# Apps can be imported by specifying the app `id` and `name` in the `terraform import` command.
terraform import snjumpcloud_app.example 64f8c031123131314ad6a7
2 changes: 1 addition & 1 deletion examples/resources/jumpcloud_usergroup/import.sh
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# Order can be imported by specifying the usergroup `id` and `name` in the `terraform import` command.
# UserGroups can be imported by specifying the usergroup `id` and `name` in the `terraform import` command.
terraform import snjumpcloud_usergroup.example 64f8c031123131314ad6a7
4 changes: 4 additions & 0 deletions examples/resources/jumpcloud_usergroup/resource.tf
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
resource "jumpcloud_usergroup" "example" {
name = "example-name"
description = "example description"
members = [
"example-member-1",
"example-member-2",
]
}
3 changes: 0 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ github.com/Microsoft/go-winio v0.6.1 h1:9/kr64B9VUZrLm5YYwbGtUJnMgqWVOdUAXu6Migc
github.com/Microsoft/go-winio v0.6.1/go.mod h1:LRdKpFKfdobln8UmuiYcKPot9D2v6svN5+sAH+4kjUM=
github.com/ProtonMail/go-crypto v0.0.0-20230828082145-3c4c8a2d2371 h1:kkhsdkhsCvIsutKu5zLMgWtgh9YxGCNAw8Ad8hjwfYg=
github.com/ProtonMail/go-crypto v0.0.0-20230828082145-3c4c8a2d2371/go.mod h1:EjAoLdwvbIOoOQr3ihjnSoLZRtE8azugULFRteWMNc0=
github.com/Spotnana-Tech/sec-jumpcloud-client-go v1.0.4-0.20240314213138-3c1a4a0a889e h1:QkH23GLwgzu12N9yiRRhEtRhbqNi4ra/cQzas3ZK3c0=
github.com/Spotnana-Tech/sec-jumpcloud-client-go v1.0.4-0.20240314213138-3c1a4a0a889e/go.mod h1:9iUtBe/vjsO9xmFmarSdTM9BiQv9cqlwDu7yJc+bsVo=
github.com/Spotnana-Tech/sec-jumpcloud-client-go v1.0.4-0.20240314215725-dbdd9ce20aec/go.mod h1:9iUtBe/vjsO9xmFmarSdTM9BiQv9cqlwDu7yJc+bsVo=
github.com/Spotnana-Tech/sec-jumpcloud-client-go v1.0.4-0.20240314225154-916f9eb85585 h1:HYGTRIQm1ZE7GC6tevmbEgHWjLpy8KwItitlJadGUa8=
github.com/Spotnana-Tech/sec-jumpcloud-client-go v1.0.4-0.20240314225154-916f9eb85585/go.mod h1:9iUtBe/vjsO9xmFmarSdTM9BiQv9cqlwDu7yJc+bsVo=
github.com/agext/levenshtein v1.2.2 h1:0S/Yg6LYmFJ5stwQeRp6EeOcCbj7xiqQSdNelsXvaqE=
Expand Down
41 changes: 25 additions & 16 deletions internal/provider/usergroups_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ func (r *jcUserGroupsResource) Update(ctx context.Context, req resource.UpdateRe
}

// Update group, reference the state's group Id
group, err := r.client.UpdateUserGroup(state.ID.ValueString(), groupModification)
_, err := r.client.UpdateUserGroup(state.ID.ValueString(), groupModification)
if err != nil {
resp.Diagnostics.AddError(
"Error Modifying Group",
Expand All @@ -260,11 +260,19 @@ func (r *jcUserGroupsResource) Update(ctx context.Context, req resource.UpdateRe
return
}

// Get current group membership
var currentMemberEmails []string
currentMembers, _ := r.client.GetGroupMembers(state.ID.ValueString())
for _, member := range currentMembers {
email, _ := r.client.GetUserEmailFromID(member.To.ID)
currentMemberEmails = append(currentMemberEmails, email)
}

// TODO if plan member not in current members, add to group
for _, member := range newMembers {
if !slices.Contains(currentMembers, member) {
// Add member to group
ok, _ := r.client.AddUserToGroup(state.ID.ValueString(), member)
if !slices.Contains(currentMemberEmails, member) {
uid, _ := r.client.GetUserIDFromEmail(member)
ok, _ := r.client.AddUserToGroup(state.ID.ValueString(), uid)
if !ok {
resp.Diagnostics.AddError(
"Error Adding User to Group",
Expand All @@ -273,13 +281,14 @@ func (r *jcUserGroupsResource) Update(ctx context.Context, req resource.UpdateRe
return
}
}

}

// TODO if current member not in plan members, remove from group
for _, member := range oldMembers {
if !slices.Contains(newMembers, member) {
// Remove member from group
ok, _ := r.client.RemoveUserFromGroup(state.ID.ValueString(), member)
uid, _ := r.client.GetUserIDFromEmail(member)
ok, _ := r.client.RemoveUserFromGroup(state.ID.ValueString(), uid)
if !ok {
resp.Diagnostics.AddError(
"Error Removing User from Group",
Expand All @@ -288,29 +297,29 @@ func (r *jcUserGroupsResource) Update(ctx context.Context, req resource.UpdateRe
return
}
}

}

// Get the updated group
groupstate, err := r.client.GetUserGroup(state.ID.ValueString()) //nolint:all
tflog.Info(ctx, fmt.Sprintf("Group Name: %s Group ID: %s", group.Name, group.ID))
groupState, err := r.client.GetUserGroup(state.ID.ValueString()) //nolint:all
tflog.Info(ctx, fmt.Sprintf("Group Name: %s Group ID: %s", groupState.Name, groupState.ID))

// Get the members
var updatedMemberEmails []attr.Value // This is the terraform structure requirement
updatedMembers, _ := r.client.GetGroupMembers(state.ID.ValueString())
// Iterate through the members and get their emails
for _, member := range updatedMembers {
email, _ := r.client.GetUserEmailFromID(member.To.ID)
tflog.Info(ctx, fmt.Sprintf("Member Email: %s", email))
updatedMemberEmails = append(updatedMemberEmails, types.StringValue(email))
}
finalMembers, _ := types.SetValue(types.StringType, updatedMemberEmails)
// Map response body to schema and populate Computed attribute values
plan = UserGroupResourceModel{
ID: types.StringValue(groupstate.ID),
Name: types.StringValue(groupstate.Name),
Description: types.StringValue(groupstate.Description),
Type: types.StringValue(groupstate.Type),
Email: types.StringValue(groupstate.Email),
MembershipMethod: types.StringValue(groupstate.MembershipMethod),
ID: types.StringValue(groupState.ID),
Name: types.StringValue(groupState.Name),
Description: types.StringValue(groupState.Description),
Type: types.StringValue(groupState.Type),
Email: types.StringValue(groupState.Email),
MembershipMethod: types.StringValue(groupState.MembershipMethod),
Members: finalMembers,
}

Expand Down

0 comments on commit 155a274

Please sign in to comment.