Skip to content

Commit

Permalink
NSOF-6511 group_user_attachments: read groups for each user
Browse files Browse the repository at this point in the history
Now that 'users' attributes is removed from group:
- no need to get expanded groups
- In order to check if a user belongs to group we will read it from each user instead of reading it from the group
  • Loading branch information
hod-alpert committed Jul 10, 2022
1 parent 848f8f0 commit bf5ea4a
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 26 deletions.
4 changes: 2 additions & 2 deletions internal/client/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,15 @@ func UpdateGroup(ctx context.Context, c *Client, gID string, g *Group) (*Group,

func GetGroupById(ctx context.Context, c *Client, gID string) (*Group, error) {
url := fmt.Sprintf("%s/%s/%s", c.BaseURL, groupEndpoint, gID)
resp, err := c.Get(ctx, url, u.Values{"expand": {"true"}})
resp, err := c.Get(ctx, url, nil)
if err != nil {
return nil, err
}
return parseGroup(resp)
}
func GetGroupByName(ctx context.Context, c *Client, name string) (*Group, error) {
url := fmt.Sprintf("%s/%s", c.BaseURL, groupEndpoint)
resp, err := c.Get(ctx, url, u.Values{"name": {name}, "expand": {"true"}})
resp, err := c.Get(ctx, url, u.Values{"name": {name}})
if err != nil {
return nil, err
}
Expand Down
1 change: 1 addition & 0 deletions internal/client/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type User struct {
Name string `json:"name,omitempty"`
Tags []Tag `json:"tags,omitempty"`
Roles []string `json:"roles,omitempty"`
Groups []string `json:"groups,omitempty"`
}

func NewUser(d *schema.ResourceData) *User {
Expand Down
65 changes: 42 additions & 23 deletions internal/provider/group_users_attachment/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/nsofnetworks/terraform-provider-pfptmeta/internal/client"
"log"
"net/http"
"sync"
)

func generateID(gID string, users []string) string {
Expand All @@ -18,31 +19,10 @@ func generateID(gID string, users []string) string {
return fmt.Sprintf("%s-%d", gID, hash)
}

func groupToUsersAttachmentResource(d *schema.ResourceData, g *client.Group) (diags diag.Diagnostics) {
err := d.Set("group_id", g.ID)
if err != nil {
return diag.FromErr(err)
}
gUsers := &schema.Set{F: schema.HashString}
for _, i := range g.Users {
gUsers.Add(i)
}
schemaUsers := d.Get("users").(*schema.Set)
u := schema.NewSet(schema.HashString, schemaUsers.List())
intersection := gUsers.Intersection(u)
users := client.ResourceTypeSetToStringSlice(intersection)
err = d.Set("users", users)
if err != nil {
return diag.FromErr(err)
}
d.SetId(generateID(g.ID, users))
return
}

func readResource(ctx context.Context, d *schema.ResourceData, meta interface{}) (diags diag.Diagnostics) {
c := meta.(*client.Client)

gID := d.Get("group_id").(string)
uIDs := client.ResourceTypeSetToStringSlice(d.Get("users").(*schema.Set))
g, err := client.GetGroupById(ctx, c, gID)
if err != nil {
errResponse, ok := err.(*client.ErrorResponse)
Expand All @@ -54,8 +34,47 @@ func readResource(ctx context.Context, d *schema.ResourceData, meta interface{})
return diag.FromErr(err)
}
}
return groupToUsersAttachmentResource(d, g)
usersInGroup, diags := checkUsersInGroup(ctx, uIDs, c, gID)
err = d.Set("group_id", g.ID)
if err != nil {
diags = append(diags, diag.FromErr(err)...)
}
err = d.Set("users", usersInGroup)
if err != nil {
diags = append(diags, diag.FromErr(err)...)
}
d.SetId(generateID(g.ID, uIDs))
return diags
}

func checkUsersInGroup(ctx context.Context, uIDs []string, c *client.Client, gID string) ([]string, diag.Diagnostics) {
var diags diag.Diagnostics
var wg sync.WaitGroup
wg.Add(len(uIDs))
mu := &sync.Mutex{}
usersInGroup := make([]string, 0)
diagsChan := make(chan diag.Diagnostics, len(uIDs))
for _, user := range uIDs {
user := user
go func() {
defer wg.Done()
var diags diag.Diagnostics
u, err := client.GetUserByID(ctx, c, user)
if err != nil {
diags = append(diags, diag.FromErr(err)...)
} else if client.Contains(gID, u.Groups) {
mu.Lock()
usersInGroup = append(usersInGroup, user)
mu.Unlock()
}
diagsChan <- diags
}()
}
wg.Wait()
diags = append(diags, <-diagsChan...)
return usersInGroup, diags
}

func createResource(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
c := meta.(*client.Client)

Expand Down
2 changes: 1 addition & 1 deletion internal/provider/user/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Users can be provisioned in the system either by locally creating the users and
tagsDesc = "Key/value attributes for combining elements together into Smart Groups, and placed as targets or sources in Policies"
)

var excludedKeys = []string{"id", "tags", "roles"}
var excludedKeys = []string{"id", "tags", "roles", "groups"}

func userToResource(u *client.User, d *schema.ResourceData) (diags diag.Diagnostics) {
err := client.MapResponseToResource(u, d, excludedKeys)
Expand Down

0 comments on commit bf5ea4a

Please sign in to comment.