/
http_client_urm.go
128 lines (111 loc) · 4.21 KB
/
http_client_urm.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
package tenant
import (
"context"
"path"
"github.com/influxdata/influxdb/v2"
"github.com/influxdata/influxdb/v2/kit/platform"
"github.com/influxdata/influxdb/v2/pkg/httpc"
)
type UserResourceMappingClient struct {
Client *httpc.Client
}
// CreateUserResourceMapping will create a user resource mapping
func (s *UserResourceMappingClient) CreateUserResourceMapping(ctx context.Context, m *influxdb.UserResourceMapping) error {
if err := m.Validate(); err != nil {
return err
}
urlPath := resourceIDPath(m.ResourceType, m.ResourceID, string(m.UserType)+"s")
return s.Client.
PostJSON(influxdb.User{ID: m.UserID}, urlPath).
DecodeJSON(m).
Do(ctx)
}
// FindUserResourceMappings returns the user resource mappings
func (s *UserResourceMappingClient) FindUserResourceMappings(ctx context.Context, f influxdb.UserResourceMappingFilter, opt ...influxdb.FindOptions) ([]*influxdb.UserResourceMapping, int, error) {
var results resourceUsersResponse
err := s.Client.
Get(resourceIDPath(f.ResourceType, f.ResourceID, string(f.UserType)+"s")).
DecodeJSON(&results).
Do(ctx)
if err != nil {
return nil, 0, err
}
urs := make([]*influxdb.UserResourceMapping, len(results.Users))
for k, item := range results.Users {
urs[k] = &influxdb.UserResourceMapping{
ResourceID: f.ResourceID,
ResourceType: f.ResourceType,
UserID: item.User.ID,
UserType: item.Role,
}
}
return urs, len(urs), nil
}
// DeleteUserResourceMapping will delete user resource mapping based in criteria.
func (s *UserResourceMappingClient) DeleteUserResourceMapping(ctx context.Context, resourceID platform.ID, userID platform.ID) error {
urlPath := resourceIDUserPath(influxdb.OrgsResourceType, resourceID, influxdb.Member, userID)
return s.Client.
Delete(urlPath).
Do(ctx)
}
// SpecificURMSvc returns a urm service with specific resource and user types.
// this will help us stay compatible with the existing service contract but also allow for urm deletes to go through the correct
// api
func (s *UserResourceMappingClient) SpecificURMSvc(rt influxdb.ResourceType, ut influxdb.UserType) *SpecificURMSvc {
return &SpecificURMSvc{
Client: s.Client,
rt: rt,
ut: ut,
}
}
// SpecificURMSvc is a URM client that speaks to a specific resource with a specified user type
type SpecificURMSvc struct {
Client *httpc.Client
rt influxdb.ResourceType
ut influxdb.UserType
}
// FindUserResourceMappings returns the user resource mappings
func (s *SpecificURMSvc) FindUserResourceMappings(ctx context.Context, f influxdb.UserResourceMappingFilter, opt ...influxdb.FindOptions) ([]*influxdb.UserResourceMapping, int, error) {
var results resourceUsersResponse
err := s.Client.
Get(resourceIDPath(s.rt, f.ResourceID, string(s.ut)+"s")).
DecodeJSON(&results).
Do(ctx)
if err != nil {
return nil, 0, err
}
urs := make([]*influxdb.UserResourceMapping, len(results.Users))
for k, item := range results.Users {
urs[k] = &influxdb.UserResourceMapping{
ResourceID: f.ResourceID,
ResourceType: f.ResourceType,
UserID: item.User.ID,
UserType: item.Role,
}
}
return urs, len(urs), nil
}
// CreateUserResourceMapping will create a user resource mapping
func (s *SpecificURMSvc) CreateUserResourceMapping(ctx context.Context, m *influxdb.UserResourceMapping) error {
if err := m.Validate(); err != nil {
return err
}
urlPath := resourceIDPath(s.rt, m.ResourceID, string(s.ut)+"s")
return s.Client.
PostJSON(influxdb.User{ID: m.UserID}, urlPath).
DecodeJSON(m).
Do(ctx)
}
// DeleteUserResourceMapping will delete user resource mapping based in criteria.
func (s *SpecificURMSvc) DeleteUserResourceMapping(ctx context.Context, resourceID platform.ID, userID platform.ID) error {
urlPath := resourceIDUserPath(s.rt, resourceID, s.ut, userID)
return s.Client.
Delete(urlPath).
Do(ctx)
}
func resourceIDPath(resourceType influxdb.ResourceType, resourceID platform.ID, p string) string {
return path.Join("/api/v2/", string(resourceType), resourceID.String(), p)
}
func resourceIDUserPath(resourceType influxdb.ResourceType, resourceID platform.ID, userType influxdb.UserType, userID platform.ID) string {
return path.Join("/api/v2/", string(resourceType), resourceID.String(), string(userType)+"s", userID.String())
}