/
tenant_server.go
127 lines (109 loc) · 3.62 KB
/
tenant_server.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
// Copyright 2022 Monoskope Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package queryhandler
import (
"context"
"time"
api "github.com/finleap-connect/monoskope/pkg/api/domain"
projections "github.com/finleap-connect/monoskope/pkg/api/domain/projections"
"github.com/finleap-connect/monoskope/pkg/domain/errors"
"github.com/finleap-connect/monoskope/pkg/domain/repositories"
grpcUtil "github.com/finleap-connect/monoskope/pkg/grpc"
"github.com/golang/protobuf/ptypes/wrappers"
"github.com/google/uuid"
"google.golang.org/grpc"
)
// tenantServer is the implementation of the TenantService API
type tenantServer struct {
api.UnimplementedTenantServer
repoTenant repositories.TenantRepository
repoUsers repositories.TenantUserRepository
}
// NewTenantServiceServer returns a new configured instance of tenantServiceServer
func NewTenantServer(tenantRepo repositories.TenantRepository, tenantUserRepo repositories.TenantUserRepository) *tenantServer {
return &tenantServer{
repoTenant: tenantRepo,
repoUsers: tenantUserRepo,
}
}
func NewTenantClient(ctx context.Context, queryHandlerAddr string) (*grpc.ClientConn, api.TenantClient, error) {
conn, err := grpcUtil.
NewGrpcConnectionFactoryWithInsecure(queryHandlerAddr).
WithOpenTelemetry().
ConnectWithTimeout(ctx, 10*time.Second)
if err != nil {
return nil, nil, errors.TranslateToGrpcError(err)
}
return conn, api.NewTenantClient(conn), nil
}
// GetById returns the tenant found by the given id.
func (s *tenantServer) GetById(ctx context.Context, id *wrappers.StringValue) (*projections.Tenant, error) {
uuid, err := uuid.Parse(id.GetValue())
if err != nil {
return nil, err
}
tenant, err := s.repoTenant.ById(ctx, uuid)
if err != nil {
return nil, err
}
return tenant.Proto(), nil
}
// GetByName returns the tenant found by the given name.
func (s *tenantServer) GetByName(ctx context.Context, name *wrappers.StringValue) (*projections.Tenant, error) {
tenant, err := s.repoTenant.ByName(ctx, name.GetValue())
if err != nil {
return nil, errors.TranslateToGrpcError(err)
}
return tenant.Proto(), nil
}
// GetAll returns all tenants.
func (s *tenantServer) GetAll(request *api.GetAllRequest, stream api.Tenant_GetAllServer) error {
tenants, err := s.repoTenant.AllWith(stream.Context(), request.GetIncludeDeleted())
if err != nil {
return errors.TranslateToGrpcError(err)
}
for _, t := range tenants {
err := stream.Send(t.Proto())
if err != nil {
return errors.TranslateToGrpcError(err)
}
}
return nil
}
// GetUsers returns users belonging to the given tenant id.
func (s *tenantServer) GetUsers(id *wrappers.StringValue, stream api.Tenant_GetUsersServer) error {
uuid, err := uuid.Parse(id.GetValue())
if err != nil {
return err
}
tenant, err := s.repoTenant.ById(stream.Context(), uuid)
if err != nil {
return err
}
// skip deleted
if tenant.Metadata.GetDeleted() != nil {
return nil
}
users, err := s.repoUsers.GetTenantUsersById(stream.Context(), uuid)
if err != nil {
return err
}
for _, u := range users {
err := stream.Send(u.Proto())
if err != nil {
return errors.TranslateToGrpcError(err)
}
}
return nil
}