forked from aiven/aiven-go-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
service_user.go
61 lines (49 loc) · 1.25 KB
/
service_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
package aiven
import (
"encoding/json"
"errors"
"fmt"
)
type (
ServiceUser struct {
Username string `json:"username"`
Password string `json:"password"`
Type string `json:"type"`
AccessCert string `json:"access_cert"`
AccessKey string `json:"access_key"`
}
ServiceUsersHandler struct {
client *Client
}
CreateServiceUserRequest struct {
Username string `json:"username"`
}
ServiceUserResponse struct {
APIResponse
User *ServiceUser `json:"user"`
}
)
func (h *ServiceUsersHandler) Create(project, service string, req CreateServiceUserRequest) (*ServiceUser, error) {
bts, err := h.client.doPostRequest(fmt.Sprintf("/project/%s/service/%s/user", project, service), req)
if err != nil {
return nil, err
}
var rsp *ServiceUserResponse
if err := json.Unmarshal(bts, &rsp); err != nil {
return nil, err
}
if rsp == nil {
return nil, ErrNoResponseData
}
if rsp.Errors != nil && len(rsp.Errors) != 0 {
return nil, errors.New(rsp.Message)
}
return rsp.User, nil
}
func (h *ServiceUsersHandler) Delete(project, service, user string) error {
bts, err := h.client.doDeleteRequest(fmt.Sprintf("/project/%s/service/%s/user/%s", project, service, user), nil)
if err != nil {
return err
}
return handleDeleteResponse(bts)
}