Debian Codegraph @

lang/core/world/kvlookup_func.go

raw symbols SCIP
1 // Mgmt
2 // Copyright (C) 2013-2024+ James Shubin and the project contributors
3 // Written by James Shubin <[email protected]> and the project contributors
4 //
5 // This program is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program. If not, see <https://www.gnu.org/licenses/>.
17 //
18 // Additional permission under GNU GPL version 3 section 7
19 //
20 // If you modify this program, or any covered work, by linking or combining it
21 // with embedded mcl code and modules (and that the embedded mcl code and
22 // modules which link with this program, contain a copy of their source code in
23 // the authoritative form) containing parts covered by the terms of any other
24 // license, the licensors of this program grant you additional permission to
25 // convey the resulting work. Furthermore, the licensors of this program grant
26 // the original author, James Shubin, additional permission to update this
27 // additional permission if he deems it necessary to achieve the goals of this
28 // additional permission.
29
30 package coreworldscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/core/world`/
31
32 import (
33 "contextscip-go gomod github.com/golang/go/src go1.21 context/"
34 "fmtscip-go gomod github.com/golang/go/src go1.21 fmt/"
35
36 "github.com/purpleidea/mgmt/lang/funcsscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/funcs`/"
37 "github.com/purpleidea/mgmt/lang/interfacesscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/interfaces`/"
38 "github.com/purpleidea/mgmt/lang/typesscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/types`/"
39 "github.com/purpleidea/mgmt/util/errwrapscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/util/errwrap`/"
40 )
41
42 const (
43 // KVLookupFuncName is the name this function is registered as.
44 KVLookupFuncNamescip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/core/world`/KVLookupFuncName. = "kvlookup"
45
46 // arg names...
47 kvLookupArgNameNamespacescip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/core/world`/kvLookupArgNameNamespace. = "namespace"
48 )
49
50 func initscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/core/world`/init().() {
51 funcsscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/funcs`/.ModuleRegisterscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/funcs`/ModuleRegister().(ModuleNamescip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/core/world`/ModuleName., KVLookupFuncNamescip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/core/world`/KVLookupFuncName., func() interfacesscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/interfaces`/.Funcscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/interfaces`/Func# { return &KVLookupFuncscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/core/world`/KVLookupFunc#{} })
52 }
53
54 // KVLookupFunc is special function which returns all the values of a given key
55 // in the exposed world. It is similar to exchange, but it does not set a key.
56 type KVLookupFuncscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/core/world`/KVLookupFunc# struct {
57 initscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/core/world`/KVLookupFunc#init. *interfacesscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/interfaces`/.Initscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/interfaces`/Init#
58
59 namespacescip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/core/world`/KVLookupFunc#namespace. string
60
61 lastscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/core/world`/KVLookupFunc#last. typesscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/types`/.Valuescip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/types`/Value#
62 resultscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/core/world`/KVLookupFunc#result. typesscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/types`/.Valuescip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/types`/Value# // last calculated output
63
64 watchChanscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/core/world`/KVLookupFunc#watchChan. chan error
65 }
66
67 // String returns a simple name for this function. This is needed so this struct
68 // can satisfy the pgraph.Vertex interface.
69 func (obj *KVLookupFuncscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/core/world`/KVLookupFunc#) Stringscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/core/world`/KVLookupFunc#String().() string {
70 return KVLookupFuncNamescip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/core/world`/KVLookupFuncName.
71 }
72
73 // ArgGen returns the Nth arg name for this function.
74 func (obj *KVLookupFuncscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/core/world`/KVLookupFunc#) ArgGenscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/core/world`/KVLookupFunc#ArgGen().(index int) (string, error) {
75 seq := []string{kvLookupArgNameNamespacescip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/core/world`/kvLookupArgNameNamespace.}
76 if l := len(seq); index >= l {
77 return "", fmtscip-go gomod github.com/golang/go/src go1.21 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.21 fmt/Errorf().("index %d exceeds arg length of %d", index, l)
78 }
79 return seq[index], nil
80 }
81
82 // Validate makes sure we've built our struct properly. It is usually unused for
83 // normal functions that users can use directly.
84 func (obj *KVLookupFuncscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/core/world`/KVLookupFunc#) Validatescip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/core/world`/KVLookupFunc#Validate().() error {
85 return nil
86 }
87
88 // Info returns some static info about itself.
89 func (obj *KVLookupFuncscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/core/world`/KVLookupFunc#) Infoscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/core/world`/KVLookupFunc#Info().() *interfacesscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/interfaces`/.Infoscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/interfaces`/Info# {
90 return &interfacesscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/interfaces`/.Infoscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/interfaces`/Info#{
91 Purescip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/interfaces`/Info#Pure.: false, // definitely false
92 Memoscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/interfaces`/Info#Memo.: false,
93 // output is map of: hostname => value
94 Sigscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/interfaces`/Info#Sig.: typesscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/types`/.NewTypescip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/types`/NewType().(fmtscip-go gomod github.com/golang/go/src go1.21 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.21 fmt/Sprintf().("func(%s str) map{str: str}", kvLookupArgNameNamespacescip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/core/world`/kvLookupArgNameNamespace.)),
95 Errscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/interfaces`/Info#Err.: obj.Validatescip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/core/world`/KVLookupFunc#Validate().(),
96 }
97 }
98
99 // Init runs some startup code for this function.
100 func (obj *KVLookupFuncscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/core/world`/KVLookupFunc#) Initscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/core/world`/KVLookupFunc#Init().(init *interfacesscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/interfaces`/.Initscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/interfaces`/Init#) error {
101 obj.initscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/core/world`/KVLookupFunc#init. = init
102 obj.watchChanscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/core/world`/KVLookupFunc#watchChan. = make(chan error) // XXX: sender should close this, but did I implement that part yet???
103 return nil
104 }
105
106 // Stream returns the changing values that this func has over time.
107 func (obj *KVLookupFuncscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/core/world`/KVLookupFunc#) Streamscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/core/world`/KVLookupFunc#Stream().(ctx contextscip-go gomod github.com/golang/go/src go1.21 context/.Contextscip-go gomod github.com/golang/go/src go1.21 context/Context#) error {
108 defer close(obj.initscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/core/world`/KVLookupFunc#init..Outputscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/interfaces`/Init#Output.) // the sender closes
109 ctx, cancel := contextscip-go gomod github.com/golang/go/src go1.21 context/.WithCancelscip-go gomod github.com/golang/go/src go1.21 context/WithCancel().(ctx)
110 defer cancel()
111 for {
112 select {
113 // TODO: should this first chan be run as a priority channel to
114 // avoid some sort of glitch? is that even possible? can our
115 // hostname check with reality (below) fix that?
116 case input, ok := <-obj.initscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/core/world`/KVLookupFunc#init..Inputscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/interfaces`/Init#Input.:
117 if !ok {
118 obj.initscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/core/world`/KVLookupFunc#init..Inputscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/interfaces`/Init#Input. = nil // don't infinite loop back
119 continue // no more inputs, but don't return!
120 }
121 //if err := input.Type().Cmp(obj.Info().Sig.Input); err != nil {
122 // return errwrap.Wrapf(err, "wrong function input")
123 //}
124
125 if obj.lastscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/core/world`/KVLookupFunc#last. != nil && input.Cmpscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/types`/Value#Cmp.(obj.lastscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/core/world`/KVLookupFunc#last.) == nil {
126 continue // value didn't change, skip it
127 }
128 obj.lastscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/core/world`/KVLookupFunc#last. = input // store for next
129
130 namespace := input.Structscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/types`/Value#Struct.()[kvLookupArgNameNamespacescip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/core/world`/kvLookupArgNameNamespace.].Strscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/types`/Value#Str.()
131 if namespace == "" {
132 return fmtscip-go gomod github.com/golang/go/src go1.21 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.21 fmt/Errorf().("can't use an empty namespace")
133 }
134 if obj.initscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/core/world`/KVLookupFunc#init..Debugscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/interfaces`/Init#Debug. {
135 obj.initscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/core/world`/KVLookupFunc#init..Logfscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/interfaces`/Init#Logf.("namespace: %s", namespace)
136 }
137
138 // TODO: support changing the namespace over time...
139 // TODO: possibly removing our stored value there first!
140 if obj.namespacescip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/core/world`/KVLookupFunc#namespace. == "" {
141 obj.namespacescip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/core/world`/KVLookupFunc#namespace. = namespace // store it
142 var err error
143 obj.watchChanscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/core/world`/KVLookupFunc#watchChan., err = obj.initscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/core/world`/KVLookupFunc#init..Worldscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/interfaces`/Init#World..StrMapWatchscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/engine`/World#StrMapWatch.(ctx, obj.namespacescip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/core/world`/KVLookupFunc#namespace.) // watch for var changes
144 if err != nil {
145 return err
146 }
147
148 result, err := obj.buildMapscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/core/world`/KVLookupFunc#buildMap().(ctx) // build the map...
149 if err != nil {
150 return err
151 }
152 select {
153 case obj.initscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/core/world`/KVLookupFunc#init..Outputscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/interfaces`/Init#Output. <- result: // send one!
154 // pass
155 case <-ctx.Donescip-go gomod github.com/golang/go/src go1.21 context/Context#Done().():
156 return nil
157 }
158
159 } else if obj.namespacescip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/core/world`/KVLookupFunc#namespace. != namespace {
160 return fmtscip-go gomod github.com/golang/go/src go1.21 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.21 fmt/Errorf().("can't change namespace, previously: `%s`", obj.namespacescip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/core/world`/KVLookupFunc#namespace.)
161 }
162
163 continue // we get values on the watch chan, not here!
164
165 case err, ok := <-obj.watchChanscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/core/world`/KVLookupFunc#watchChan.:
166 if !ok { // closed
167 // XXX: if we close, perhaps the engine is
168 // switching etcd hosts and we should retry?
169 // maybe instead we should get an "etcd
170 // reconnect" signal, and the lang will restart?
171 return nil
172 }
173 if err != nil {
174 return errwrapscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/util/errwrap`/.Wrapfscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/util/errwrap`/Wrapf().(err, "channel watch failed on `%s`", obj.namespacescip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/core/world`/KVLookupFunc#namespace.)
175 }
176
177 result, err := obj.buildMapscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/core/world`/KVLookupFunc#buildMap().(ctx) // build the map...
178 if err != nil {
179 return err
180 }
181
182 // if the result is still the same, skip sending an update...
183 if obj.resultscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/core/world`/KVLookupFunc#result. != nil && result.Cmpscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/types`/Value#Cmp.(obj.resultscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/core/world`/KVLookupFunc#result.) == nil {
184 continue // result didn't change
185 }
186 obj.resultscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/core/world`/KVLookupFunc#result. = result // store new result
187
188 case <-ctx.Donescip-go gomod github.com/golang/go/src go1.21 context/Context#Done().():
189 return nil
190 }
191
192 select {
193 case obj.initscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/core/world`/KVLookupFunc#init..Outputscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/interfaces`/Init#Output. <- obj.resultscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/core/world`/KVLookupFunc#result.: // send
194 // pass
195 case <-ctx.Donescip-go gomod github.com/golang/go/src go1.21 context/Context#Done().():
196 return nil
197 }
198 }
199 }
200
201 // buildMap builds the result map which we'll need. It uses struct variables.
202 func (obj *KVLookupFuncscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/core/world`/KVLookupFunc#) buildMapscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/core/world`/KVLookupFunc#buildMap().(ctx contextscip-go gomod github.com/golang/go/src go1.21 context/.Contextscip-go gomod github.com/golang/go/src go1.21 context/Context#) (typesscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/types`/.Valuescip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/types`/Value#, error) {
203 keyMap, err := obj.initscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/core/world`/KVLookupFunc#init..Worldscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/interfaces`/Init#World..StrMapGetscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/engine`/World#StrMapGet.(ctx, obj.namespacescip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/core/world`/KVLookupFunc#namespace.)
204 if err != nil {
205 return nil, errwrapscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/util/errwrap`/.Wrapfscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/util/errwrap`/Wrapf().(err, "channel read failed on `%s`", obj.namespacescip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/core/world`/KVLookupFunc#namespace.)
206 }
207
208 d := typesscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/types`/.NewMapscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/types`/NewMap().(obj.Infoscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/core/world`/KVLookupFunc#Info().().Sigscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/interfaces`/Info#Sig..Outscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/types`/Type#Out.)
209 for k, v := range keyMap {
210 key := &typesscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/types`/.StrValuescip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/types`/StrValue#{Vscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/types`/StrValue#V.: k}
211 val := &typesscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/types`/.StrValuescip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/types`/StrValue#{Vscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/types`/StrValue#V.: v}
212 if err := d.Addscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/lang/types`/MapValue#Add().(key, val); err != nil {
213 return nil, errwrapscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/util/errwrap`/.Wrapfscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/util/errwrap`/Wrapf().(err, "map could not add key `%s`, val: `%s`", k, v)
214 }
215 }
216 return d, nil // put map into interface type
217 }