Skip to content

Commit

Permalink
add ldap and posix groups
Browse files Browse the repository at this point in the history
  • Loading branch information
gypsydiver committed Dec 15, 2018
1 parent b74fd8d commit 1837ec4
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 12 deletions.
40 changes: 28 additions & 12 deletions jumpcloud/resource_user_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,24 @@ func resourceUserGroup() *schema.Resource {
Type: schema.TypeString,
Optional: true,
},
"attributes": {
Type: schema.TypeMap,
Optional: true,
ForceNew: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"ldap_groups": {
Computed: true,
Type: schema.TypeString,
},
"posix_groups": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
},
},
},
},
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
Expand All @@ -36,16 +54,14 @@ func resourceUserGroupCreate(d *schema.ResourceData, m interface{}) error {
config := m.(*jcapiv2.Configuration)
client := jcapiv2.NewAPIClient(config)

body := jcapiv2.UserGroupPost{Name: d.Get("name").(string)}

if attr, ok := expandAttributes(d.Get("attributes")); ok {
body.Attributes = attr
}

req := map[string]interface{}{
"body": jcapiv2.UserGroupPost{
Name: d.Get("name").(string),
// Attributes: &jcapiv2.UserGroupPostAttributes{
// // Note: PosixGroups cannot be edited after group creation, only first member of slice is considered
// PosixGroups: []jcapiv2.UserGroupPostAttributesPosixGroups{
// jcapiv2.UserGroupPostAttributesPosixGroups{Id: int32(posixID), Name: posixName},
// },
// },
},
"body": body,
"xOrgId": d.Get("xorgid").(string),
}
group, res, err := client.UserGroupsApi.GroupsUserPost(context.TODO(), "", Accept, req)
Expand Down Expand Up @@ -76,9 +92,9 @@ func resourceUserGroupRead(d *schema.ResourceData, m interface{}) error {
if err := d.Set("name", group.Name); err != nil {
return err
}
// if err := d.Set("attributes", flattenAttributes(&group.Attributes)); err != nil {
// return err
// }
if err := d.Set("attributes", flattenAttributes(&group.Attributes)); err != nil {
return err
}

return nil
}
Expand Down
70 changes: 70 additions & 0 deletions jumpcloud/structures_user_group.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package jumpcloud

import (
"fmt"
"strconv"
"strings"

jcapiv2 "github.com/TheJumpCloud/jcapi-go/v2"
)

func flattenAttributes(attr *Attributes) map[string]interface{} {
return map[string]interface{}{
"ldap_groups": flattenLDAPGroups(attr.LDAPGroups),
"posix_groups": flattenPOSIXGroups(attr.POSIXGroups),
}
}

func flattenPOSIXGroups(pg []jcapiv2.UserGroupPostAttributesPosixGroups) string {
out := []string{}
for _, v := range pg {
out = append(out, fmt.Sprintf("%d:%s", v.Id, v.Name))
}
return strings.Join(out, ",")
}

func flattenLDAPGroups(lg []LDAPGroup) string {
out := []string{}
for _, v := range lg {
out = append(out, v.Name)
}
return strings.Join(out, ",")
}

// Note: PosixGroups cannot be edited after group creation, only first member of slice is considered
func expandAttributes(attr interface{}) (out *jcapiv2.UserGroupPostAttributes, ok bool) {
if attr == nil {
return
}
mapAttr, ok := attr.(map[string]interface{})
if !ok {
return
}
posixStr, ok := mapAttr["posix_groups"].(string)
if !ok {
return
}

groups := strings.Split(posixStr, ",")
posixGroups := []jcapiv2.UserGroupPostAttributesPosixGroups{}
for _, v := range groups {
g := strings.Split(v, ":")
if len(g) != 2 {
return
}
id, err := strconv.ParseInt(g[0], 10, 32)
if err != nil {
continue
}
posixGroups = append(posixGroups,
jcapiv2.UserGroupPostAttributesPosixGroups{
Id: int32(id), Name: g[1],
})
}

if len(posixGroups) == 0 {
return
}

return &jcapiv2.UserGroupPostAttributes{PosixGroups: posixGroups}, true
}

0 comments on commit 1837ec4

Please sign in to comment.