forked from anasinnyk/terraform-provider-onepassword
-
Notifications
You must be signed in to change notification settings - Fork 0
/
group.go
93 lines (80 loc) · 2.29 KB
/
group.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
package onepassword
import (
"encoding/json"
"fmt"
)
const (
// GroupResource is 1Password's internal designator for Groups
GroupResource = "group"
// GroupStateActive indicates an Active Group
GroupStateActive = "A"
// GroupStateDeleted indicates a Deleted Group
GroupStateDeleted = "D"
)
// Group represents a 1Password Group resource
type Group struct {
UUID string
Name string
State string
}
// ReadGroup gets an existing 1Password Group
func (o *OnePassClient) ReadGroup(id string) (*Group, error) {
group := &Group{}
res, err := o.runCmd(opPasswordGet, GroupResource, id)
if err != nil {
return nil, err
}
if err = json.Unmarshal(res, group); err != nil {
return nil, err
}
return group, nil
}
// ListGroupMembers lists the existing Users in a given Group
func (o *OnePassClient) ListGroupMembers(id string) ([]User, error) {
users := []User{}
if id == "" {
return users, fmt.Errorf("Must provide an identifier to list group members")
}
res, err := o.runCmd(opPasswordList, "users", "--"+GroupResource, id)
if err != nil {
return nil, err
}
if err = json.Unmarshal(res, &users); err != nil {
return nil, err
}
return users, nil
}
// CreateGroup creates a new 1Password Group
func (o *OnePassClient) CreateGroup(v *Group) (*Group, error) {
args := []string{opPasswordCreate, GroupResource, v.Name}
res, err := o.runCmd(args...)
if err != nil {
return nil, err
}
if err = json.Unmarshal(res, v); err != nil {
return nil, err
}
return v, nil
}
// CreateGroupMember adds a User to a Group
func (o *OnePassClient) CreateGroupMember(groupID string, userID string) error {
args := []string{opPasswordAdd, UserResource, userID, groupID}
_, err := o.runCmd(args...)
return err
}
// UpdateGroup updates an existing 1Password Group
func (o *OnePassClient) UpdateGroup(id string, v *Group) error {
args := []string{opPasswordEdit, GroupResource, id, "--name=" + v.Name}
_, err := o.runCmd(args...)
return err
}
// DeleteGroup deletes a 1Password Group
func (o *OnePassClient) DeleteGroup(id string) error {
return o.Delete(GroupResource, id)
}
// DeleteGroupMember removes a User from a Group
func (o *OnePassClient) DeleteGroupMember(groupID string, userID string) error {
args := []string{opPasswordRemove, UserResource, userID, groupID}
_, err := o.runCmd(args...)
return err
}