-
Notifications
You must be signed in to change notification settings - Fork 5
/
usergroup.go
77 lines (61 loc) · 2.26 KB
/
usergroup.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
package illumioapi
import (
"encoding/json"
"fmt"
"net/url"
"strconv"
)
// GetAllADUserGroups gets all user groups in the PCE
func (p *PCE) GetAllADUserGroups() ([]ConsumingSecurityPrincipals, APIResponse, error) {
// Build the API URL
apiURL, err := url.Parse("https://" + pceSanitization(p.FQDN) + ":" + strconv.Itoa(p.Port) + "/api/v2/orgs/" + strconv.Itoa(p.Org) + "/security_principals")
if err != nil {
return nil, APIResponse{}, fmt.Errorf("GetAllADUserGroups - %s", err)
}
// Call the API
api, err := apicall("GET", apiURL.String(), *p, nil, false)
if err != nil {
return nil, api, fmt.Errorf("GetAllADUserGroups - %s", err)
}
// Unmarshal response to struct
var adUserGroups []ConsumingSecurityPrincipals
json.Unmarshal([]byte(api.RespBody), &adUserGroups)
// If adUserGroups is 500, re-run with async
if len(adUserGroups) >= 500 {
api, err = apicall("GET", apiURL.String(), *p, nil, true)
if err != nil {
return nil, api, fmt.Errorf("adUserGroups - %s", err)
}
// Unmarshal response to struct
var asyncADUserGroups []ConsumingSecurityPrincipals
json.Unmarshal([]byte(api.RespBody), &asyncADUserGroups)
return asyncADUserGroups, api, nil
}
// Return if less than 500
return adUserGroups, api, nil
}
// CreateADUserGroup creates a user group policy object in the PCE
func (p *PCE) CreateADUserGroup(g ConsumingSecurityPrincipals) (ConsumingSecurityPrincipals, APIResponse, error) {
var api APIResponse
var err error
// Build the API URL
apiURL, err := url.Parse("https://" + pceSanitization(p.FQDN) + ":" + strconv.Itoa(p.Port) + "/api/v2/orgs/" + strconv.Itoa(p.Org) + "/security_principals")
if err != nil {
return ConsumingSecurityPrincipals{}, api, fmt.Errorf("CreateADUserGroup - %s", err)
}
// Create payload
userGroupJSON, err := json.Marshal(g)
if err != nil {
return ConsumingSecurityPrincipals{}, api, fmt.Errorf("CreateADUserGroup - %s", err)
}
api.ReqBody = string(userGroupJSON)
// Call the API
api, err = apicall("POST", apiURL.String(), *p, userGroupJSON, false)
if err != nil {
return ConsumingSecurityPrincipals{}, api, fmt.Errorf("CreateADUserGroup - %s", err)
}
// Unmarshal new label
var newGroup ConsumingSecurityPrincipals
json.Unmarshal([]byte(api.RespBody), &newGroup)
return newGroup, api, nil
}