/
client.go
204 lines (178 loc) · 6.13 KB
/
client.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
// Copyright (c) Mainflux
// SPDX-License-Identifier: Apache-2.0
// Adapted for Orb project, modifications licensed under MPL v. 2.0:
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
package grpc
import (
"context"
"time"
"github.com/go-kit/kit/endpoint"
kitot "github.com/go-kit/kit/tracing/opentracing"
kitgrpc "github.com/go-kit/kit/transport/grpc"
opentracing "github.com/opentracing/opentracing-go"
"github.com/orb-community/orb/policies/pb"
"google.golang.org/grpc"
)
var _ pb.PolicyServiceClient = (*grpcClient)(nil)
type grpcClient struct {
timeout time.Duration
retrievePolicy endpoint.Endpoint
retrievePoliciesByGroups endpoint.Endpoint
retrieveDataset endpoint.Endpoint
retrieveDatasetsByGroups endpoint.Endpoint
}
func (client grpcClient) RetrieveDatasetsByGroups(ctx context.Context, in *pb.DatasetsByGroupsReq, opts ...grpc.CallOption) (*pb.DatasetsRes, error) {
ctx, cancel := context.WithTimeout(ctx, client.timeout)
defer cancel()
ar := accessByGroupIDReq{
GroupIDs: in.GroupIDs,
OwnerID: in.OwnerID,
}
res, err := client.retrieveDatasetsByGroups(ctx, ar)
if err != nil {
return nil, err
}
ir := res.(datasetListRes)
dsList := make([]*pb.DatasetRes, len(ir.datasets))
for i, ds := range ir.datasets {
dsList[i] = &pb.DatasetRes{Id: ds.id, SinkIds: ds.sinkIDs, PolicyId: ds.policyID, AgentGroupId: ds.agentGroupID}
}
return &pb.DatasetsRes{DatasetList: dsList}, nil
}
func (client grpcClient) RetrievePolicy(ctx context.Context, in *pb.PolicyByIDReq, opts ...grpc.CallOption) (*pb.PolicyRes, error) {
ctx, cancel := context.WithTimeout(ctx, client.timeout)
defer cancel()
ar := accessByIDReq{
PolicyID: in.PolicyID,
OwnerID: in.OwnerID,
}
res, err := client.retrievePolicy(ctx, ar)
if err != nil {
return nil, err
}
ir := res.(policyRes)
return &pb.PolicyRes{Id: ir.id, Name: ir.name, Data: ir.data, Backend: ir.backend, Format: ir.format, Version: ir.version}, nil
}
func (client grpcClient) RetrievePoliciesByGroups(ctx context.Context, in *pb.PoliciesByGroupsReq, _ ...grpc.CallOption) (*pb.PolicyInDSListRes, error) {
ctx, cancel := context.WithTimeout(ctx, client.timeout)
defer cancel()
ar := accessByGroupIDReq{
GroupIDs: in.GroupIDs,
OwnerID: in.OwnerID,
}
res, err := client.retrievePoliciesByGroups(ctx, ar)
if err != nil {
return nil, err
}
ir := res.(policyInDSListRes)
plist := make([]*pb.PolicyInDSRes, len(ir.policies))
for i, p := range ir.policies {
plist[i] = &pb.PolicyInDSRes{
Id: p.id,
Name: p.name,
Data: p.data,
Backend: p.backend,
Version: p.version,
DatasetId: p.datasetID,
AgentGroupId: p.agentGroupID,
Format: p.format,
}
}
return &pb.PolicyInDSListRes{Policies: plist}, nil
}
func (client grpcClient) RetrieveDataset(ctx context.Context, in *pb.DatasetByIDReq, opts ...grpc.CallOption) (*pb.DatasetRes, error) {
ctx, cancel := context.WithTimeout(ctx, client.timeout)
defer cancel()
ar := accessDatasetByIDReq{
datasetID: in.DatasetID,
ownerID: in.OwnerID,
}
res, err := client.retrieveDataset(ctx, ar)
if err != nil {
return nil, err
}
ir := res.(datasetRes)
return &pb.DatasetRes{
Id: ir.id,
AgentGroupId: ir.agentGroupID,
PolicyId: ir.policyID,
SinkIds: ir.sinkIDs,
}, nil
}
// NewClient returns new gRPC client instance.
func NewClient(tracer opentracing.Tracer, conn *grpc.ClientConn, timeout time.Duration) pb.PolicyServiceClient {
svcName := "policies.PolicyService"
return &grpcClient{
timeout: timeout,
retrievePolicy: kitot.TraceClient(tracer, "retrieve_policy")(kitgrpc.NewClient(
conn,
svcName,
"RetrievePolicy",
encodeRetrievePolicyRequest,
decodePolicyResponse,
pb.PolicyRes{},
).Endpoint()),
retrievePoliciesByGroups: kitot.TraceClient(tracer, "retrieve_policies_by_groups")(kitgrpc.NewClient(
conn,
svcName,
"RetrievePoliciesByGroups",
encodeRetrievePoliciesByGroupsRequest,
decodePolicyListResponse,
pb.PolicyInDSListRes{},
).Endpoint()),
retrieveDataset: kitot.TraceClient(tracer, "retrieve_dataset")(kitgrpc.NewClient(
conn,
svcName,
"RetrieveDataset",
encodeRetrieveDatasetRequest,
decodeDatasetResponse,
pb.DatasetRes{},
).Endpoint()),
}
}
func encodeRetrievePolicyRequest(_ context.Context, grpcReq interface{}) (interface{}, error) {
req := grpcReq.(accessByIDReq)
return &pb.PolicyByIDReq{PolicyID: req.PolicyID, OwnerID: req.OwnerID}, nil
}
func encodeRetrievePoliciesByGroupsRequest(_ context.Context, grpcReq interface{}) (interface{}, error) {
req := grpcReq.(accessByGroupIDReq)
return &pb.PoliciesByGroupsReq{GroupIDs: req.GroupIDs, OwnerID: req.OwnerID}, nil
}
func encodeRetrieveDatasetRequest(_ context.Context, grpcReq interface{}) (interface{}, error) {
req := grpcReq.(accessDatasetByIDReq)
return &pb.DatasetByIDReq{
DatasetID: req.datasetID,
OwnerID: req.ownerID,
}, nil
}
func decodePolicyResponse(_ context.Context, grpcRes interface{}) (interface{}, error) {
res := grpcRes.(*pb.PolicyRes)
return policyRes{id: res.GetId(), name: res.GetName(), data: res.GetData(), version: res.GetVersion(), backend: res.GetBackend(), format: res.GetFormat()}, nil
}
func decodeDatasetResponse(_ context.Context, grpcRes interface{}) (interface{}, error) {
res := grpcRes.(*pb.DatasetRes)
return datasetRes{
id: res.GetId(),
agentGroupID: res.GetAgentGroupId(),
policyID: res.GetPolicyId(),
sinkIDs: res.GetSinkIds(),
}, nil
}
func decodePolicyListResponse(_ context.Context, grpcRes interface{}) (interface{}, error) {
res := grpcRes.(*pb.PolicyInDSListRes)
policies := make([]policyInDSRes, len(res.Policies))
for i, p := range res.Policies {
policies[i] = policyInDSRes{id: p.GetId(),
name: p.GetName(),
data: p.GetData(),
version: p.GetVersion(),
backend: p.GetBackend(),
datasetID: p.GetDatasetId(),
agentGroupID: p.GetAgentGroupId(),
format: p.GetFormat(),
}
}
return policyInDSListRes{policies: policies}, nil
}