forked from aiven/aiven-go-client
-
Notifications
You must be signed in to change notification settings - Fork 1
/
service_user.go
150 lines (127 loc) · 4.8 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
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
// Copyright (c) 2017 jelmersnoeck
// Copyright (c) 2018 Aiven, Helsinki, Finland. https://aiven.io/
package aiven
import (
"errors"
"fmt"
)
const (
UpdateOperationResetCredentials = "reset-credentials"
UpdateOperationSetAccessControl = "set-access-control"
)
type (
// ServiceUser is the representation of a Service User in the Aiven API.
ServiceUser struct {
Username string `json:"username"`
Password string `json:"password"`
Type string `json:"type"`
AccessCert string `json:"access_cert"`
AccessKey string `json:"access_key"`
AccessCertNotValidAfterTime string `json:"access_cert_not_valid_after_time"`
AccessControl AccessControl `json:"access_control,omitempty"`
}
AccessControl struct {
M3Group *string `json:"m3_group,omitempty"`
RedisACLCategories []string `json:"redis_acl_categories,omitempty"`
RedisACLCommands []string `json:"redis_acl_commands,omitempty"`
RedisACLKeys []string `json:"redis_acl_keys,omitempty"`
RedisACLChannels []string `json:"redis_acl_channels,omitempty"`
PostgresAllowReplication *bool `json:"pg_allow_replication,omitempty"`
}
// ServiceUsersHandler is the client that interacts with the ServiceUsers
// endpoints.
ServiceUsersHandler struct {
client *Client
}
// CreateServiceUserRequest are the parameters required to create a
// ServiceUser.
CreateServiceUserRequest struct {
Username string `json:"username"`
AccessControl *AccessControl `json:"access_control,omitempty"`
}
// ModifyServiceUserRequest params required to modify a ServiceUser
ModifyServiceUserRequest struct {
Operation *string `json:"operation"`
Authentication *string `json:"authentication,omitempty"`
NewPassword *string `json:"new_password,omitempty"`
AccessControl *AccessControl `json:"access_control,omitempty"`
}
// ServiceUserResponse represents the response after creating a ServiceUser.
ServiceUserResponse struct {
APIResponse
User *ServiceUser `json:"user"`
}
)
// Create creates the given User on Aiven.
func (h *ServiceUsersHandler) Create(project, service string, req CreateServiceUserRequest) (*ServiceUser, error) {
path := buildPath("project", project, "service", service, "user")
bts, err := h.client.doPostRequest(path, req)
if err != nil {
return nil, err
}
var r ServiceUserResponse
errR := checkAPIResponse(bts, &r)
return r.User, errR
}
// List Service Users for given service in Aiven.
func (h *ServiceUsersHandler) List(project, serviceName string) ([]*ServiceUser, error) {
// Aiven API does not provide list operation for service users, need to get them via service info instead
service, err := h.client.Services.Get(project, serviceName)
if err != nil {
return nil, err
}
return service.Users, nil
}
// Get specific Service User in Aiven.
func (h *ServiceUsersHandler) Get(project, serviceName, username string) (*ServiceUser, error) {
// Aiven API does not provide get operation for service users, need to get them via list instead
users, err := h.List(project, serviceName)
if err != nil {
return nil, err
}
for _, user := range users {
if user.Username == username {
return user, nil
}
}
err = Error{Message: fmt.Sprintf("Service user with username %v not found", username), Status: 404}
return nil, err
}
// Update modifies the given Service User in Aiven.
func (h *ServiceUsersHandler) Update(project, service, username string, update ModifyServiceUserRequest) (*ServiceUser, error) {
var DefaultOperation = UpdateOperationResetCredentials
if update.Operation == nil {
update.Operation = &DefaultOperation
}
if update.AccessControl != nil && *update.Operation != UpdateOperationSetAccessControl {
return nil, errors.New("wrong operation for updating access control")
}
if (update.NewPassword != nil || update.Authentication != nil) && *update.Operation != UpdateOperationResetCredentials {
return nil, errors.New("wrong operation for updating credentials")
}
path := buildPath("project", project, "service", service, "user", username)
svc, err := h.client.doPutRequest(path, update)
if err != nil {
return nil, err
}
var r ServiceResponse
errR := checkAPIResponse(svc, &r)
if errR == nil {
for _, user := range r.Service.Users {
if user.Username == username {
return user, nil
}
}
return nil, errors.New("user not found")
}
return nil, errR
}
// Delete deletes the given Service User in Aiven.
func (h *ServiceUsersHandler) Delete(project, service, user string) error {
path := buildPath("project", project, "service", service, "user", user)
bts, err := h.client.doDeleteRequest(path, nil)
if err != nil {
return err
}
return checkAPIResponse(bts, nil)
}