yaml_config_ops.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. // Copyright 2022 EMQ Technologies Co., Ltd.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package conf
  15. import (
  16. "encoding/json"
  17. "fmt"
  18. "path"
  19. "reflect"
  20. "sync"
  21. "github.com/lf-edge/ekuiper/internal/pkg/filex"
  22. "github.com/lf-edge/ekuiper/pkg/cast"
  23. )
  24. // ConfKeysOperator define interface to query/add/update/delete the configs in memory
  25. type ConfKeysOperator interface {
  26. GetPluginName() string
  27. GetConfContentByte() ([]byte, error)
  28. // CopyConfContent get the configurations in etc and data folder
  29. CopyConfContent() map[string]map[string]interface{}
  30. // CopyReadOnlyConfContent get the configurations in etc folder
  31. CopyReadOnlyConfContent() map[string]map[string]interface{}
  32. // CopyUpdatableConfContent get the configurations in data folder
  33. CopyUpdatableConfContent() map[string]map[string]interface{}
  34. // CopyUpdatableConfContentFor get the configuration for the specific configKeys
  35. CopyUpdatableConfContentFor(configKeys []string) map[string]map[string]interface{}
  36. // LoadConfContent load the configurations into data configuration part
  37. LoadConfContent(cf map[string]map[string]interface{})
  38. GetConfKeys() (keys []string)
  39. GetReadOnlyConfKeys() (keys []string)
  40. GetUpdatableConfKeys() (keys []string)
  41. DeleteConfKey(confKey string)
  42. DeleteConfKeyField(confKey string, reqField map[string]interface{}) error
  43. AddConfKey(confKey string, reqField map[string]interface{}) error
  44. AddConfKeyField(confKey string, reqField map[string]interface{}) error
  45. ClearConfKeys()
  46. }
  47. // ConfigOperator define interface to query/add/update/delete the configs in disk
  48. type ConfigOperator interface {
  49. ConfKeysOperator
  50. SaveCfgToFile() error
  51. }
  52. // ConfigKeys implement ConfKeysOperator interface, load the configs from etc/sources/xx.yaml and et/connections/connection.yaml
  53. // Hold the connection configs for each connection type in etcCfg field
  54. // Provide method to query/add/update/delete the configs
  55. type ConfigKeys struct {
  56. lock sync.RWMutex
  57. pluginName string // source type, can be mqtt/edgex/httppull
  58. etcCfg map[string]map[string]interface{} // configs defined in etc/sources/yaml
  59. dataCfg map[string]map[string]interface{} // configs defined in etc/sources/
  60. }
  61. func (c *ConfigKeys) GetPluginName() string {
  62. return c.pluginName
  63. }
  64. func (c *ConfigKeys) GetConfContentByte() ([]byte, error) {
  65. cf := make(map[string]map[string]interface{})
  66. c.lock.RLock()
  67. defer c.lock.RUnlock()
  68. for key, kvs := range c.etcCfg {
  69. aux := make(map[string]interface{})
  70. for k, v := range kvs {
  71. aux[k] = v
  72. }
  73. cf[key] = aux
  74. }
  75. for key, kvs := range c.dataCfg {
  76. aux := make(map[string]interface{})
  77. for k, v := range kvs {
  78. aux[k] = v
  79. }
  80. cf[key] = aux
  81. }
  82. return json.Marshal(cf)
  83. }
  84. func (c *ConfigKeys) CopyConfContent() map[string]map[string]interface{} {
  85. cf := make(map[string]map[string]interface{})
  86. c.lock.RLock()
  87. defer c.lock.RUnlock()
  88. for key, kvs := range c.etcCfg {
  89. aux := make(map[string]interface{})
  90. for k, v := range kvs {
  91. aux[k] = v
  92. }
  93. cf[key] = aux
  94. }
  95. // note: config keys in data directory will overwrite those in etc directory with same name
  96. for key, kvs := range c.dataCfg {
  97. aux := make(map[string]interface{})
  98. for k, v := range kvs {
  99. aux[k] = v
  100. }
  101. cf[key] = aux
  102. }
  103. return cf
  104. }
  105. func (c *ConfigKeys) LoadConfContent(cf map[string]map[string]interface{}) {
  106. c.lock.Lock()
  107. defer c.lock.Unlock()
  108. for key, kvs := range cf {
  109. aux := make(map[string]interface{})
  110. for k, v := range kvs {
  111. aux[k] = v
  112. }
  113. c.dataCfg[key] = aux
  114. }
  115. }
  116. func (c *ConfigKeys) CopyReadOnlyConfContent() map[string]map[string]interface{} {
  117. cf := make(map[string]map[string]interface{})
  118. c.lock.RLock()
  119. defer c.lock.RUnlock()
  120. for key, kvs := range c.etcCfg {
  121. aux := make(map[string]interface{})
  122. for k, v := range kvs {
  123. aux[k] = v
  124. }
  125. cf[key] = aux
  126. }
  127. return cf
  128. }
  129. func (c *ConfigKeys) CopyUpdatableConfContent() map[string]map[string]interface{} {
  130. cf := make(map[string]map[string]interface{})
  131. c.lock.RLock()
  132. defer c.lock.RUnlock()
  133. for key, kvs := range c.dataCfg {
  134. aux := make(map[string]interface{})
  135. for k, v := range kvs {
  136. aux[k] = v
  137. }
  138. cf[key] = aux
  139. }
  140. return cf
  141. }
  142. func (c *ConfigKeys) CopyUpdatableConfContentFor(configKeys []string) map[string]map[string]interface{} {
  143. cf := make(map[string]map[string]interface{})
  144. c.lock.RLock()
  145. defer c.lock.RUnlock()
  146. for _, key := range configKeys {
  147. aux := make(map[string]interface{})
  148. if kvs, ok := c.dataCfg[key]; ok {
  149. for k, v := range kvs {
  150. aux[k] = v
  151. }
  152. }
  153. cf[key] = aux
  154. }
  155. return cf
  156. }
  157. func (c *ConfigKeys) GetConfKeys() (keys []string) {
  158. ro := c.GetReadOnlyConfKeys()
  159. keys = append(keys, ro...)
  160. up := c.GetUpdatableConfKeys()
  161. keys = append(keys, up...)
  162. return keys
  163. }
  164. func (c *ConfigKeys) GetReadOnlyConfKeys() (keys []string) {
  165. c.lock.RLock()
  166. defer c.lock.RUnlock()
  167. for k := range c.etcCfg {
  168. keys = append(keys, k)
  169. }
  170. return keys
  171. }
  172. func (c *ConfigKeys) GetUpdatableConfKeys() (keys []string) {
  173. c.lock.RLock()
  174. defer c.lock.RUnlock()
  175. for k := range c.dataCfg {
  176. keys = append(keys, k)
  177. }
  178. return keys
  179. }
  180. func (c *ConfigKeys) DeleteConfKey(confKey string) {
  181. c.lock.Lock()
  182. defer c.lock.Unlock()
  183. delete(c.dataCfg, confKey)
  184. }
  185. func (c *ConfigKeys) ClearConfKeys() {
  186. keys := c.GetUpdatableConfKeys()
  187. for _, key := range keys {
  188. c.DeleteConfKey(key)
  189. }
  190. }
  191. func recursionDelMap(cf, fields map[string]interface{}) error {
  192. for k, v := range fields {
  193. if nil == v {
  194. delete(cf, k)
  195. continue
  196. }
  197. if delKey, ok := v.(string); ok {
  198. if 0 == len(delKey) {
  199. delete(cf, k)
  200. continue
  201. }
  202. var auxCf map[string]interface{}
  203. if err := cast.MapToStruct(cf[k], &auxCf); nil != err {
  204. return fmt.Errorf(`%s%s.%s`, "type_conversion_fail", k, delKey)
  205. }
  206. cf[k] = auxCf
  207. delete(auxCf, delKey)
  208. continue
  209. }
  210. if reflect.TypeOf(v) != nil && reflect.Map == reflect.TypeOf(v).Kind() {
  211. var auxCf, auxFields map[string]interface{}
  212. if err := cast.MapToStruct(cf[k], &auxCf); nil != err {
  213. return fmt.Errorf(`%s%s.%v`, "type_conversion_fail", k, v)
  214. }
  215. cf[k] = auxCf
  216. if err := cast.MapToStruct(v, &auxFields); nil != err {
  217. return fmt.Errorf(`%s%s.%v`, "type_conversion_fail", k, v)
  218. }
  219. if err := recursionDelMap(auxCf, auxFields); nil != err {
  220. return err
  221. }
  222. }
  223. }
  224. return nil
  225. }
  226. func (c *ConfigKeys) DeleteConfKeyField(confKey string, reqField map[string]interface{}) error {
  227. c.lock.Lock()
  228. defer c.lock.Unlock()
  229. err := recursionDelMap(c.dataCfg[confKey], reqField)
  230. if nil != err {
  231. return err
  232. }
  233. return nil
  234. }
  235. func (c *ConfigKeys) AddConfKey(confKey string, reqField map[string]interface{}) error {
  236. c.lock.Lock()
  237. defer c.lock.Unlock()
  238. c.dataCfg[confKey] = reqField
  239. return nil
  240. }
  241. func (c *ConfigKeys) AddConfKeyField(confKey string, reqField map[string]interface{}) error {
  242. c.lock.Lock()
  243. defer c.lock.Unlock()
  244. if nil == c.dataCfg[confKey] {
  245. return fmt.Errorf(`%s`, "not_found_confkey")
  246. }
  247. for k, v := range reqField {
  248. c.dataCfg[confKey][k] = v
  249. }
  250. return nil
  251. }
  252. // SourceConfigKeysOps implement ConfOperator interface, load the configs from etc/sources/xx.yaml
  253. type SourceConfigKeysOps struct {
  254. *ConfigKeys
  255. }
  256. func (c *SourceConfigKeysOps) SaveCfgToFile() error {
  257. pluginName := c.pluginName
  258. confDir, err := GetDataLoc()
  259. if nil != err {
  260. return err
  261. }
  262. dir := path.Join(confDir, "sources")
  263. filePath := path.Join(dir, pluginName+".yaml")
  264. cfg := c.CopyUpdatableConfContent()
  265. err = filex.WriteYamlMarshal(filePath, cfg)
  266. if nil != err {
  267. return err
  268. }
  269. return nil
  270. }
  271. // SinkConfigKeysOps implement ConfOperator interface, load the configs from data/sinks/xx.yaml
  272. type SinkConfigKeysOps struct {
  273. *ConfigKeys
  274. }
  275. func (c *SinkConfigKeysOps) SaveCfgToFile() error {
  276. pluginName := c.pluginName
  277. confDir, err := GetDataLoc()
  278. if nil != err {
  279. return err
  280. }
  281. dir := path.Join(confDir, "sinks")
  282. filePath := path.Join(dir, pluginName+".yaml")
  283. cfg := c.CopyUpdatableConfContent()
  284. err = filex.WriteYamlMarshal(filePath, cfg)
  285. if nil != err {
  286. return err
  287. }
  288. return nil
  289. }
  290. // ConnectionConfigKeysOps implement ConfOperator interface, load the configs from et/connections/connection.yaml
  291. type ConnectionConfigKeysOps struct {
  292. *ConfigKeys
  293. }
  294. func (p *ConnectionConfigKeysOps) SaveCfgToFile() error {
  295. pluginName := p.pluginName
  296. confDir, err := GetDataLoc()
  297. if nil != err {
  298. return err
  299. }
  300. cfg := p.CopyUpdatableConfContent()
  301. yamlPath := path.Join(confDir, "connections/connection.yaml")
  302. yamlData := make(map[string]interface{})
  303. err = filex.ReadYamlUnmarshal(yamlPath, &yamlData)
  304. if nil != err {
  305. return err
  306. }
  307. yamlData[pluginName] = cfg
  308. return filex.WriteYamlMarshal(yamlPath, yamlData)
  309. }
  310. // NewConfigOperatorForSource construct function
  311. func NewConfigOperatorForSource(pluginName string) ConfigOperator {
  312. c := &SourceConfigKeysOps{
  313. &ConfigKeys{
  314. lock: sync.RWMutex{},
  315. pluginName: pluginName,
  316. etcCfg: map[string]map[string]interface{}{},
  317. dataCfg: map[string]map[string]interface{}{},
  318. },
  319. }
  320. return c
  321. }
  322. // NewConfigOperatorFromSourceYaml construct function, Load the configs from etc/sources/xx.yaml
  323. func NewConfigOperatorFromSourceYaml(pluginName string) (ConfigOperator, error) {
  324. c := &SourceConfigKeysOps{
  325. &ConfigKeys{
  326. lock: sync.RWMutex{},
  327. pluginName: pluginName,
  328. etcCfg: map[string]map[string]interface{}{},
  329. dataCfg: map[string]map[string]interface{}{},
  330. },
  331. }
  332. confDir, err := GetConfLoc()
  333. if nil != err {
  334. return nil, err
  335. }
  336. dir := path.Join(confDir, "sources")
  337. fileName := pluginName
  338. if "mqtt" == pluginName {
  339. fileName = "mqtt_source"
  340. dir = confDir
  341. }
  342. filePath := path.Join(dir, fileName+`.yaml`)
  343. // Just ignore error if yaml not found
  344. _ = LoadConfigFromPath(filePath, &c.etcCfg)
  345. dataDir, err := GetDataLoc()
  346. if nil != err {
  347. return nil, err
  348. }
  349. dir = path.Join(dataDir, "sources")
  350. fileName = pluginName
  351. filePath = path.Join(dir, fileName+`.yaml`)
  352. _ = filex.ReadYamlUnmarshal(filePath, &c.dataCfg)
  353. return c, nil
  354. }
  355. // NewConfigOperatorForSink construct function
  356. func NewConfigOperatorForSink(pluginName string) ConfigOperator {
  357. c := &SinkConfigKeysOps{
  358. &ConfigKeys{
  359. lock: sync.RWMutex{},
  360. pluginName: pluginName,
  361. etcCfg: map[string]map[string]interface{}{},
  362. dataCfg: map[string]map[string]interface{}{},
  363. },
  364. }
  365. return c
  366. }
  367. // NewConfigOperatorFromSinkYaml construct function, Load the configs from etc/sources/xx.yaml
  368. func NewConfigOperatorFromSinkYaml(pluginName string) (ConfigOperator, error) {
  369. c := &SinkConfigKeysOps{
  370. &ConfigKeys{
  371. lock: sync.RWMutex{},
  372. pluginName: pluginName,
  373. etcCfg: map[string]map[string]interface{}{},
  374. dataCfg: map[string]map[string]interface{}{},
  375. },
  376. }
  377. dataDir, err := GetDataLoc()
  378. if nil != err {
  379. return nil, err
  380. }
  381. dir := path.Join(dataDir, "sinks")
  382. filePath := path.Join(dir, pluginName+`.yaml`)
  383. _ = filex.ReadYamlUnmarshal(filePath, &c.dataCfg)
  384. return c, nil
  385. }
  386. // NewConfigOperatorForConnection construct function
  387. func NewConfigOperatorForConnection(pluginName string) ConfigOperator {
  388. c := &ConnectionConfigKeysOps{
  389. &ConfigKeys{
  390. lock: sync.RWMutex{},
  391. pluginName: pluginName,
  392. etcCfg: map[string]map[string]interface{}{},
  393. dataCfg: map[string]map[string]interface{}{},
  394. },
  395. }
  396. return c
  397. }
  398. // NewConfigOperatorFromConnectionYaml construct function, Load the configs from et/connections/connection.yaml
  399. func NewConfigOperatorFromConnectionYaml(pluginName string) (ConfigOperator, error) {
  400. c := &ConnectionConfigKeysOps{
  401. &ConfigKeys{
  402. lock: sync.RWMutex{},
  403. pluginName: pluginName,
  404. etcCfg: map[string]map[string]interface{}{},
  405. dataCfg: map[string]map[string]interface{}{},
  406. },
  407. }
  408. confDir, err := GetConfLoc()
  409. if nil != err {
  410. return nil, err
  411. }
  412. yamlPath := path.Join(confDir, "connections/connection.yaml")
  413. yamlData := make(map[string]interface{})
  414. err = LoadConfigFromPath(yamlPath, &yamlData)
  415. if nil != err {
  416. return nil, err
  417. }
  418. if plgCnfs, ok := yamlData[pluginName]; ok {
  419. if cf, ok1 := plgCnfs.(map[string]interface{}); ok1 {
  420. for confKey, confVal := range cf {
  421. if conf, ok := confVal.(map[string]interface{}); ok {
  422. c.etcCfg[confKey] = conf
  423. } else {
  424. return nil, fmt.Errorf("file content is not right: %s.%v", confKey, confVal)
  425. }
  426. }
  427. } else {
  428. return nil, fmt.Errorf("file content is not right: %v", plgCnfs)
  429. }
  430. } else {
  431. return nil, fmt.Errorf("not find the target connection type: %s", c.pluginName)
  432. }
  433. confDir, err = GetDataLoc()
  434. if nil != err {
  435. return nil, err
  436. }
  437. yamlPath = path.Join(confDir, "connections/connection.yaml")
  438. yamlData = make(map[string]interface{})
  439. _ = filex.ReadYamlUnmarshal(yamlPath, &yamlData)
  440. if plgCnfs, ok := yamlData[pluginName]; ok {
  441. if cf, ok1 := plgCnfs.(map[string]interface{}); ok1 {
  442. for confKey, confVal := range cf {
  443. if conf, ok := confVal.(map[string]interface{}); ok {
  444. c.dataCfg[confKey] = conf
  445. } else {
  446. return nil, fmt.Errorf("file content is not right: %s.%v", confKey, confVal)
  447. }
  448. }
  449. } else {
  450. return nil, fmt.Errorf("file content is not right: %v", plgCnfs)
  451. }
  452. }
  453. return c, nil
  454. }