generated from hashicorp/terraform-provider-scaffolding
-
Notifications
You must be signed in to change notification settings - Fork 3
/
group.go
152 lines (139 loc) · 3.82 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
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
package client
import (
"context"
"encoding/json"
"fmt"
u "net/url"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
const groupEndpoint string = "v1/groups"
type Group struct {
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Description string `json:"description"`
Expression *string `json:"expression"`
Roles []string `json:"roles,omitempty"`
Users []string `json:"users,omitempty"`
}
func NewGroup(d *schema.ResourceData) *Group {
res := &Group{}
if d.HasChange("name") {
res.Name = d.Get("name").(string)
}
res.Description = d.Get("description").(string)
e := d.Get("expression").(string)
if e == "" {
res.Expression = nil
} else {
res.Expression = &e
}
return res
}
func parseGroup(resp []byte) (*Group, error) {
g := &Group{}
err := json.Unmarshal(resp, g)
if err != nil {
return nil, fmt.Errorf("could not parse group response: %v", err)
}
return g, nil
}
func CreateGroup(ctx context.Context, c *Client, g *Group) (*Group, error) {
gUrl := fmt.Sprintf("%s/%s", c.BaseURL, groupEndpoint)
body, err := json.Marshal(g)
if err != nil {
return nil, fmt.Errorf("could not convert group to json: %v", err)
}
resp, err := c.Post(ctx, gUrl, body)
if err != nil {
return nil, err
}
return parseGroup(resp)
}
func UpdateGroup(ctx context.Context, c *Client, gID string, g *Group) (*Group, error) {
neUrl := fmt.Sprintf("%s/%s/%s", c.BaseURL, groupEndpoint, gID)
body, err := json.Marshal(g)
if err != nil {
return nil, fmt.Errorf("could not convert group to json: %v", err)
}
resp, err := c.Patch(ctx, neUrl, body)
if err != nil {
return nil, err
}
return parseGroup(resp)
}
func GetGroupById(ctx context.Context, c *Client, gID string) (*Group, error) {
url := fmt.Sprintf("%s/%s/%s", c.BaseURL, groupEndpoint, gID)
resp, err := c.Get(ctx, url, nil)
if err != nil {
return nil, err
}
return parseGroup(resp)
}
func GetGroupByName(ctx context.Context, c *Client, name string) (*Group, error) {
url := fmt.Sprintf("%s/%s", c.BaseURL, groupEndpoint)
resp, err := c.Get(ctx, url, u.Values{"name": {name}})
if err != nil {
return nil, err
}
respBody := &struct {
Groups []Group `json:"items"`
}{}
err = json.Unmarshal(resp, respBody)
if err != nil {
return nil, fmt.Errorf("could not parse group response: %v", err)
}
for _, g := range respBody.Groups {
if g.Name == name {
return &g, nil
}
}
return nil, nil
}
func DeleteGroup(ctx context.Context, c *Client, gID string) (*Group, error) {
url := fmt.Sprintf("%s/%s/%s", c.BaseURL, groupEndpoint, gID)
resp, err := c.Delete(ctx, url, nil)
if err != nil {
return nil, err
}
return parseGroup(resp)
}
func AssignRolesToGroup(ctx context.Context, c *Client, gID string, roles []string) ([]string, error) {
url := fmt.Sprintf("%s/%s/%s/roles", c.BaseURL, groupEndpoint, gID)
body, err := json.Marshal(roles)
if err != nil {
return nil, fmt.Errorf("could not convert roles to json: %v", err)
}
resp, err := c.Put(ctx, url, body)
if err != nil {
return nil, err
}
g, err := parseGroup(resp)
if err != nil {
return nil, err
}
return g.Roles, nil
}
func AddUsersToGroup(ctx context.Context, c *Client, gID string, users []string) error {
url := fmt.Sprintf("%s/%s/%s/add_users", c.BaseURL, groupEndpoint, gID)
body, err := json.Marshal(users)
if err != nil {
return fmt.Errorf("could not convert users to json: %v", err)
}
_, err = c.Post(ctx, url, body)
if err != nil {
return err
}
return nil
}
func RemoveUsersFromGroup(ctx context.Context, c *Client, gID string, users []string) error {
url := fmt.Sprintf("%s/%s/%s/remove_users", c.BaseURL, groupEndpoint, gID)
body, err := json.Marshal(users)
if err != nil {
return fmt.Errorf("could not convert users to json: %v", err)
}
_, err = c.Post(ctx, url, body)
if err != nil {
return err
}
return nil
}