forked from cheelim1/terraform-provider-jumpcloud
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b74fd8d
commit 1837ec4
Showing
2 changed files
with
98 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |