forked from pydio/cells
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ns-provider.go
170 lines (155 loc) · 4.21 KB
/
ns-provider.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
/*
* Copyright (c) 2019. Abstrium SAS <team (at) pydio.com>
* This file is part of Pydio Cells.
*
* Pydio Cells is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Pydio Cells is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Pydio Cells. If not, see <http://www.gnu.org/licenses/>.
*
* The latest code can be found at <https://pydio.com>.
*/
package meta
import (
"context"
"sync"
"github.com/golang/protobuf/proto"
"github.com/micro/go-micro/broker"
"github.com/pydio/cells/common"
"github.com/pydio/cells/common/micro"
"github.com/pydio/cells/common/proto/idm"
"github.com/pydio/cells/common/proto/tree"
"github.com/pydio/cells/common/registry"
)
// NamespaceProvider list all namespaces info from services declared ServiceMetaNsProvider
// It watches events to maintain the list
type NamespacesProvider struct {
sync.RWMutex // this handles a lock for the namespaces field
namespaces []*idm.UserMetaNamespace
loaded bool
streamers []tree.NodeProviderStreamer_ReadNodeStreamClient
}
func NewNamespacesProvider() *NamespacesProvider {
ns := &NamespacesProvider{}
ns.Watch()
return ns
}
func (p *NamespacesProvider) Namespaces() map[string]*idm.UserMetaNamespace {
if !p.loaded {
p.Load()
}
p.RLock()
defer p.RUnlock()
ns := make(map[string]*idm.UserMetaNamespace, len(p.namespaces))
for _, n := range p.namespaces {
ns[n.Namespace] = n
}
return ns
}
func (p *NamespacesProvider) ExcludeIndexes() map[string]struct{} {
if !p.loaded {
p.Load()
}
ni := make(map[string]struct{})
p.RLock()
defer p.RUnlock()
for _, ns := range p.namespaces {
if !ns.Indexable {
ni[ns.Namespace] = struct{}{}
}
}
return ni
}
func (p *NamespacesProvider) Load() {
// Other Meta Providers (running services only)
services, err := registry.ListServicesWithMicroMeta(ServiceMetaNsProvider, "list")
if err != nil {
return
}
defer func() {
p.loaded = true
}()
for _, srv := range services {
cl := idm.NewUserMetaServiceClient(srv.Name(), defaults.NewClient())
s, e := cl.ListUserMetaNamespace(context.Background(), &idm.ListUserMetaNamespaceRequest{})
if e != nil {
continue
}
p.Lock()
for {
r, er := s.Recv()
if er != nil {
break
}
p.namespaces = append(p.namespaces, r.UserMetaNamespace)
}
p.Unlock()
s.Close()
}
}
func (p *NamespacesProvider) InitStreamers(ctx context.Context) error {
services, err := registry.ListServicesWithMicroMeta(ServiceMetaNsProvider, "list")
if err != nil {
return err
}
for _, srv := range services {
c := tree.NewNodeProviderStreamerClient(srv.Name(), defaults.NewClient())
if s, e := c.ReadNodeStream(ctx); e == nil {
p.streamers = append(p.streamers, s)
}
}
return nil
}
func (p *NamespacesProvider) CloseStreamers() error {
var ers []error
for _, s := range p.streamers {
if e := s.Close(); e != nil {
ers = append(ers, e)
}
}
p.streamers = []tree.NodeProviderStreamer_ReadNodeStreamClient{}
if len(ers) > 0 {
return ers[0]
}
return nil
}
func (p *NamespacesProvider) ReadNode(node *tree.Node) (*tree.Node, error) {
out := node.Clone()
if out.MetaStore == nil {
out.MetaStore = make(map[string]string)
}
for _, s := range p.streamers {
s.Send(&tree.ReadNodeRequest{Node: node})
if resp, e := s.Recv(); e == nil && resp.Node.MetaStore != nil {
for k, v := range resp.Node.MetaStore {
out.MetaStore[k] = v
}
}
}
return out, nil
}
func (p *NamespacesProvider) Clear() {
p.Lock()
p.namespaces = nil
p.loaded = false
p.Unlock()
}
func (p *NamespacesProvider) Watch() {
defaults.Broker().Subscribe(common.TOPIC_IDM_EVENT, func(publication broker.Publication) error {
var ce idm.ChangeEvent
if e := proto.Unmarshal(publication.Message().Body, &ce); e == nil {
if ce.MetaNamespace != nil {
p.Clear()
}
}
return nil
})
}