-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathjcapi-tags.go
175 lines (135 loc) · 4.36 KB
/
jcapi-tags.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package jcapi
import (
"encoding/json"
"fmt"
"strings"
)
const (
TAGS_PATH string = "/tags"
)
type JCTagResults struct {
Results []JCTag `json:"results"`
}
type JCTag struct {
Id string `json:"_id,omitempty"`
Name string `json:"name"`
GroupName string `json:"groupname"`
Systems []string `json:"systems"`
SystemUsers []string `json:"systemusers"`
RegularExpressions []string `json:"regularExpressions"`
ExpirationTime string `json:"expirationTime"`
Expired bool `json:"expired"`
Selected bool `json:"selected"`
//
// For identification as an external user directory source
//
ExternallyManaged bool `json:"externallyManaged"`
ExternalDN string `json:"externalDN,omitempty"`
ExternalSourceType string `json:"externalSourceType,omitempty"`
ApplyToJumpCloud bool
}
func (tag JCTag) ToString() string {
return fmt.Sprintf("tag id=%s - name='%s' - groupName='%s' - expires='%s' - systems='%s' - systemusers='%s' - applyToJC='%t' - externally_managed='%t' (%s)",
tag.Id, tag.Name, tag.GroupName, tag.ExpirationTime, strings.Join(tag.Systems, ","),
strings.Join(tag.SystemUsers, ","), tag.ApplyToJumpCloud, tag.ExternallyManaged, tag.ExternalDN)
}
func GetTagNames(tags []JCTag) []string {
var returnVal []string
for _, tag := range tags {
returnVal = append(returnVal, tag.Name)
}
return returnVal
}
func getJCTagsFromResults(result []byte) (tags []JCTag, err JCError) {
tag := JCTag{}
// Try unmarshalling as a single tag first, and if that works, stop there
err = json.Unmarshal(result, &tag)
if err != nil || tag.Id == "" {
err = nil
tagResults := JCTagResults{}
// Nope, must be a results array...
err = json.Unmarshal(result, &tagResults)
if err != nil {
err = fmt.Errorf("Could not unmarshal result '%s', err='%s'", string(result), err.Error())
}
tags = tagResults.Results
} else {
tags = append(tags, tag)
}
return
}
func (jc JCAPI) GetTagsByUrl(urlPath string) (tagList []JCTag, err JCError) {
result, err := jc.DoBytes(MapJCOpToHTTP(Read), urlPath, nil)
if err != nil {
return nil, fmt.Errorf("ERROR: Get tags from JumpCloud failed with urlPath='%s', err='%s'", urlPath, err.Error())
}
tagList, err = getJCTagsFromResults(result)
if err != nil {
return nil, fmt.Errorf("ERROR: Could not get tags from results, err='%s'", err.Error())
}
return
}
func (jc JCAPI) GetAllTags() (tagList []JCTag, err JCError) {
for skip := 0; skip == 0 || len(tagList) == searchLimit; skip += searchSkipInterval {
url := fmt.Sprintf("%s?sort=username&skip=%d&limit=%d", TAGS_PATH, skip, searchLimit)
tags, err := jc.GetTagsByUrl(url)
if err != nil {
return nil, fmt.Errorf("ERROR: Could not query tags, err='%s'", err)
}
for _, tag := range tags {
tagList = append(tagList, tag)
}
}
return
}
func (jc JCAPI) GetTagByName(tagName string) (tag JCTag, err JCError) {
url := fmt.Sprintf("%s/%s", TAGS_PATH, tagName)
tags, err := jc.GetTagsByUrl(url)
if err != nil {
err = fmt.Errorf("ERROR: Could not get tags by name for '%s', url='%s', err='%s'", tagName, url, err.Error())
return
}
if len(tags) > 0 {
tag = tags[0]
}
return
}
//
// Add or Update a tag in place on JumpCloud
//
func (jc JCAPI) AddUpdateTag(op JCOp, tag JCTag) (tagId string, err JCError) {
data, err := json.Marshal(tag)
if err != nil {
return "", fmt.Errorf("ERROR: Could not marshal JCTag object, err='%s'", err)
}
url := TAGS_PATH
if op == Update {
url += "/" + tag.Id
}
result, err := jc.DoBytes(MapJCOpToHTTP(op), url, data)
if err != nil {
return "", fmt.Errorf("ERROR: Could not post new JCTag object, err='%s'", err)
}
tagList, err := getJCTagsFromResults(result)
if err != nil {
return "", fmt.Errorf("ERROR: Could not get tags from results, err='%s'", err.Error())
}
var resultTag JCTag
if len(tagList) > 0 {
resultTag = tagList[0]
} else {
return "", fmt.Errorf("ERROR: Got no result back from JumpCloud, cannot return object ID")
}
if resultTag.Name != tag.Name {
return "", fmt.Errorf("ERROR: JumpCloud did not return the same tag name - this should never happen!")
}
tagId = resultTag.Id
return
}
func (jc JCAPI) DeleteTag(tag JCTag) JCError {
_, err := jc.Delete(fmt.Sprintf("%s/%s", TAGS_PATH, tag.Id))
if err != nil {
return fmt.Errorf("ERROR: Could not delete tag ID '%s': err='%s'", tag.Id, err)
}
return nil
}