forked from lusis/go-artifactory
/
permissions_targets.go
70 lines (63 loc) · 1.96 KB
/
permissions_targets.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
package artifactory
import (
"bytes"
"encoding/json"
)
// PermissionTarget represents the json returned by Artifactory for a permission target
type PermissionTarget struct {
Name string `json:"name"`
URI string `json:"uri"`
}
// PermissionTargetDetails represents the json returned by Artifactory for permission target details
type PermissionTargetDetails struct {
Name string `json:"name,omitempty"`
IncludesPattern string `json:"includesPattern,omitempty"`
ExcludesPattern string `json:"excludesPattern,omitempty"`
Repositories []string `json:"repositories,omitempty"`
Principals Principals `json:"principals,omitempty"`
}
// Principals represents the json response for principals in Artifactory
type Principals struct {
Users map[string][]string `json:"users"`
Groups map[string][]string `json:"groups"`
}
// GetPermissionTargets returns all permission targets
func (c *Client) GetPermissionTargets() ([]PermissionTarget, error) {
var res []PermissionTarget
d, e := c.Get("/api/security/permissions", make(map[string]string))
if e != nil {
return res, e
}
err := json.Unmarshal(d, &res)
if err != nil {
return res, err
}
return res, e
}
// GetPermissionTargetDetails returns the details of the provided permission target
func (c *Client) GetPermissionTargetDetails(u string) (PermissionTargetDetails, error) {
var res PermissionTargetDetails
d, e := c.Get("/api/security/permissions/"+u, make(map[string]string))
if e != nil {
return res, e
}
err := json.Unmarshal(d, &res)
if err != nil {
return res, err
}
return res, e
}
// CreatePermissionTarget creates the named permission target
func (c *Client) CreatePermissionTarget(u string, p PermissionTargetDetails, q map[string]string) error {
j, err := json.Marshal(p)
if err != nil {
return err
}
_, err = c.HTTPRequest(Request{
Verb: "PUT",
Path: "/api/security/permissions/" + u,
Body: bytes.NewReader(j),
QueryParams: q,
})
return err
}