generated from hashicorp/terraform-provider-scaffolding
-
Notifications
You must be signed in to change notification settings - Fork 3
/
user.go
142 lines (128 loc) · 3.59 KB
/
user.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
package client
import (
"context"
"encoding/json"
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
u "net/url"
)
const UsersEndpoint = "v1/users"
type User struct {
ID string `json:"id,omitempty"`
GivenName string `json:"given_name,omitempty"`
FamilyName string `json:"family_name,omitempty"`
Description string `json:"description"`
Email string `json:"email,omitempty"`
Enabled *bool `json:"enabled,omitempty"`
Phone *string `json:"phone"`
Name string `json:"name,omitempty"`
Tags []Tag `json:"tags,omitempty"`
Roles []string `json:"roles,omitempty"`
Groups []string `json:"groups,omitempty"`
}
func NewUser(d *schema.ResourceData) *User {
res := &User{}
if d.HasChange("given_name") {
res.GivenName = d.Get("given_name").(string)
}
if d.HasChange("family_name") {
res.FamilyName = d.Get("family_name").(string)
}
res.Description = d.Get("description").(string)
if d.HasChange("email") {
res.Email = d.Get("email").(string)
}
enabled := d.Get("enabled").(bool)
res.Enabled = &enabled
p := d.Get("phone").(string)
if p == "" {
res.Phone = nil
} else {
res.Phone = &p
}
return res
}
func parseUser(resp []byte) (*User, error) {
user := &User{}
err := json.Unmarshal(resp, user)
if err != nil {
return nil, fmt.Errorf("could not parse user response: %v", err)
}
return user, nil
}
func CreateUser(ctx context.Context, c *Client, ed *User) (*User, error) {
uUrl := fmt.Sprintf("%s/%s", c.BaseURL, UsersEndpoint)
body, err := json.Marshal(ed)
if err != nil {
return nil, fmt.Errorf("could not convert user to json: %v", err)
}
resp, err := c.Post(ctx, uUrl, body)
if err != nil {
return nil, err
}
return parseUser(resp)
}
func UpdateUser(ctx context.Context, c *Client, edID string, ed *User) (*User, error) {
uUrl := fmt.Sprintf("%s/%s/%s", c.BaseURL, UsersEndpoint, edID)
body, err := json.Marshal(ed)
if err != nil {
return nil, fmt.Errorf("could not convert user to json: %v", err)
}
resp, err := c.Patch(ctx, uUrl, body)
if err != nil {
return nil, err
}
return parseUser(resp)
}
func GetUserByID(ctx context.Context, c *Client, uID string) (*User, error) {
uUrl := fmt.Sprintf("%s/%s/%s", c.BaseURL, UsersEndpoint, uID)
resp, err := c.Get(ctx, uUrl, u.Values{"expand": {"true"}})
if err != nil {
return nil, err
}
return parseUser(resp)
}
func GetUserByEmail(ctx context.Context, c *Client, email string) (*User, error) {
url := fmt.Sprintf("%s/%s", c.BaseURL, UsersEndpoint)
resp, err := c.Get(ctx, url, u.Values{"expand": {"true"}, "email": {email}})
if err != nil {
return nil, err
}
respBody := &struct {
Users []User `json:"items"`
}{}
err = json.Unmarshal(resp, respBody)
if err != nil {
return nil, fmt.Errorf("could not parse user response: %v", err)
}
for _, r := range respBody.Users {
if r.Email == email {
return &r, nil
}
}
return nil, nil
}
func DeleteUser(ctx context.Context, c *Client, uID string) (*User, error) {
uUrl := fmt.Sprintf("%s/%s/%s", c.BaseURL, UsersEndpoint, uID)
resp, err := c.Delete(ctx, uUrl, nil)
if err != nil {
return nil, err
}
return parseUser(resp)
}
func AssignRolesToUser(ctx context.Context, c *Client, uID string, roles []string) ([]string, error) {
url := fmt.Sprintf("%s/%s/%s/roles", c.BaseURL, UsersEndpoint, uID)
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
}
user, err := parseUser(resp)
if err != nil {
return nil, err
}
return user.Roles, nil
}