-
Notifications
You must be signed in to change notification settings - Fork 8
/
usergroups_object.go
100 lines (87 loc) · 3.6 KB
/
usergroups_object.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// usergroups_object.go
package usergroups
import (
"encoding/xml"
"fmt"
"log"
"strconv"
"github.com/deploymenttheory/go-api-sdk-jamfpro/sdk/jamfpro"
"github.com/deploymenttheory/terraform-provider-jamfpro/internal/endpoints/common/constructobject"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
// constructJamfProUserGroup constructs a ResourceUserGroup object from the provided schema data.
func constructJamfProUserGroup(d *schema.ResourceData) (*jamfpro.ResourceUserGroup, error) {
userGroup := &jamfpro.ResourceUserGroup{
Name: d.Get("name").(string),
IsSmart: d.Get("is_smart").(bool),
IsNotifyOnChange: d.Get("is_notify_on_change").(bool),
}
// Handle Site
if v, ok := d.GetOk("site"); ok {
userGroup.Site = constructobject.ConstructSharedResourceSite(v.([]interface{}))
} else {
// Set default values if 'site' data is not provided
userGroup.Site = constructobject.ConstructSharedResourceSite([]interface{}{})
}
criteria := d.Get("criteria").([]interface{})
for _, criterion := range criteria {
c := criterion.(map[string]interface{})
userGroup.Criteria = append(userGroup.Criteria, jamfpro.SharedSubsetCriteria{
Name: c["name"].(string),
Priority: c["priority"].(int),
AndOr: c["and_or"].(string),
SearchType: c["search_type"].(string),
Value: c["value"].(string),
OpeningParen: c["opening_paren"].(bool),
ClosingParen: c["closing_paren"].(bool),
})
}
// Handle 'users' attribute
if v, ok := d.GetOk("users"); ok && len(v.([]interface{})) > 0 {
usersBlock := v.([]interface{})[0].(map[string]interface{})
userIDList := usersBlock["id"].([]interface{})
for _, userID := range userIDList {
userIDStr, ok := userID.(string)
if !ok {
return nil, fmt.Errorf("user ID is not a string as expected: %v", userID)
}
intID, err := strconv.Atoi(userIDStr) // Convert string to int
if err != nil {
return nil, fmt.Errorf("error converting user ID '%s' to integer: %v", userIDStr, err)
}
userGroup.Users = append(userGroup.Users, jamfpro.UserGroupSubsetUserItem{
ID: intID,
})
}
}
userGroup.UserAdditions = extractUsers(d.Get("user_additions").([]interface{}))
userGroup.UserDeletions = extractUsers(d.Get("user_deletions").([]interface{}))
// Serialize and pretty-print the User Group object as XML for logging
resourceXML, err := xml.MarshalIndent(userGroup, "", " ")
if err != nil {
return nil, fmt.Errorf("failed to marshal Jamf Pro User Group '%s' to XML: %v", userGroup.Name, err)
}
// Use log.Printf instead of fmt.Printf for logging within the Terraform provider context
log.Printf("[DEBUG] Constructed Jamf Pro User Group XML:\n%s\n", string(resourceXML))
return userGroup, nil
}
// extractUsers converts a slice of interface{} that represents user data into a slice
// of jamfpro.UserGroupSubsetUserItem. It iterates over each user in the interface
// slice, extracts the relevant fields, and constructs a UserGroupSubsetUserItem for
// each user. The resulting slice of UserGroupSubsetUserItem is suitable for use in
// constructing a jamfpro.ResourceUserGroup object.
func extractUsers(usersInterface []interface{}) []jamfpro.UserGroupSubsetUserItem {
var users []jamfpro.UserGroupSubsetUserItem
for _, user := range usersInterface {
u := user.(map[string]interface{})
userItem := jamfpro.UserGroupSubsetUserItem{
ID: u["id"].(int),
Username: u["username"].(string),
FullName: u["full_name"].(string),
PhoneNumber: u["phone_number"].(string),
EmailAddress: u["email_address"].(string),
}
users = append(users, userItem)
}
return users
}