sourceMeta.go 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. package plugins
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "github.com/emqx/kuiper/common"
  6. "io/ioutil"
  7. "path"
  8. "reflect"
  9. "strings"
  10. )
  11. type (
  12. fileSource struct {
  13. About *fileAbout `json:"about"`
  14. Libs []string `json:"libs"`
  15. ConfKeys map[string][]*fileField `json:"properties"`
  16. }
  17. uiSource struct {
  18. About *about `json:"about"`
  19. Libs []string `json:"libs"`
  20. ConfKeys map[string][]*field `json:"properties"`
  21. }
  22. sourceProperty struct {
  23. cf map[string]map[string]interface{}
  24. meta *uiSource
  25. }
  26. )
  27. func newUiSource(fi *fileSource) (*uiSource, error) {
  28. if nil == fi {
  29. return nil, nil
  30. }
  31. var err error
  32. ui := new(uiSource)
  33. ui.Libs = fi.Libs
  34. ui.About = newAbout(fi.About)
  35. ui.ConfKeys = make(map[string][]*field)
  36. for k, fields := range fi.ConfKeys {
  37. if ui.ConfKeys[k], err = newField(fields); nil != err {
  38. return nil, err
  39. }
  40. }
  41. return ui, nil
  42. }
  43. var g_sourceProperty map[string]*sourceProperty
  44. func (m *Manager) readSourceMetaFile(filePath string) error {
  45. fileName := path.Base(filePath)
  46. ptrMeta := new(fileSource)
  47. err := common.ReadJsonUnmarshal(filePath, ptrMeta)
  48. if nil != err || 0 == len(ptrMeta.ConfKeys) {
  49. return fmt.Errorf("file:%s err:%v", filePath, err)
  50. }
  51. if 0 == len(ptrMeta.ConfKeys["default"]) {
  52. return fmt.Errorf("not found default confKey %s", filePath)
  53. }
  54. if nil == ptrMeta.About {
  55. return fmt.Errorf("not found about of %s", filePath)
  56. } else {
  57. _, ptrMeta.About.Installed = m.registry.Get(SOURCE, strings.TrimSuffix(fileName, `.json`))
  58. }
  59. yamlData := make(map[string]map[string]interface{})
  60. filePath = strings.TrimSuffix(filePath, `.json`) + `.yaml`
  61. err = common.ReadYamlUnmarshal(filePath, &yamlData)
  62. if nil != err {
  63. return fmt.Errorf("file:%s err:%v", filePath, err)
  64. }
  65. if 0 == len(yamlData["default"]) {
  66. return fmt.Errorf("not found default confKey from %s", filePath)
  67. }
  68. property := new(sourceProperty)
  69. property.cf = yamlData
  70. property.meta, err = newUiSource(ptrMeta)
  71. if nil != err {
  72. return err
  73. }
  74. g_sourceProperty[fileName] = property
  75. return err
  76. }
  77. func (m *Manager) readSourceMetaDir() error {
  78. g_sourceProperty = make(map[string]*sourceProperty)
  79. confDir, err := common.GetConfLoc()
  80. if nil != err {
  81. return err
  82. }
  83. dir := path.Join(confDir, "sources")
  84. infos, err := ioutil.ReadDir(dir)
  85. if nil != err {
  86. return err
  87. }
  88. if err = m.readSourceMetaFile(path.Join(confDir, "mqtt_source.json")); nil != err {
  89. return err
  90. }
  91. for _, info := range infos {
  92. fileName := info.Name()
  93. if strings.HasSuffix(fileName, ".json") {
  94. filePath := path.Join(dir, fileName)
  95. if err = m.readSourceMetaFile(filePath); nil != err {
  96. return err
  97. }
  98. common.Log.Infof("sourceMeta file : %s", fileName)
  99. }
  100. }
  101. return nil
  102. }
  103. func GetSourceMeta(pluginName string) (ptrSourceProperty *uiSource, err error) {
  104. property, ok := g_sourceProperty[pluginName+".json"]
  105. if ok {
  106. return property.cfToMeta()
  107. }
  108. return nil, fmt.Errorf("not found plugin %s", pluginName)
  109. }
  110. func GetSources() (sources []*pluginfo) {
  111. for fileName, v := range g_sourceProperty {
  112. node := new(pluginfo)
  113. node.Name = strings.TrimSuffix(fileName, `.json`)
  114. if nil == v.meta {
  115. continue
  116. }
  117. if nil == v.meta.About {
  118. continue
  119. }
  120. node.About = v.meta.About
  121. i := 0
  122. for ; i < len(sources); i++ {
  123. if node.Name <= sources[i].Name {
  124. sources = append(sources, node)
  125. copy(sources[i+1:], sources[i:])
  126. sources[i] = node
  127. break
  128. }
  129. }
  130. if len(sources) == i {
  131. sources = append(sources, node)
  132. }
  133. }
  134. return sources
  135. }
  136. func GetSourceConfKeys(pluginName string) (keys []string) {
  137. property := g_sourceProperty[pluginName+".json"]
  138. if nil == property {
  139. return keys
  140. }
  141. for k, _ := range property.cf {
  142. keys = append(keys, k)
  143. }
  144. return keys
  145. }
  146. func DelSourceConfKey(pluginName, confKey string) error {
  147. property := g_sourceProperty[pluginName+".json"]
  148. if nil == property {
  149. return fmt.Errorf("not found plugin %s", pluginName)
  150. }
  151. if nil == property.cf {
  152. return fmt.Errorf("not found confKey %s", confKey)
  153. }
  154. delete(property.cf, confKey)
  155. return property.saveCf(pluginName)
  156. }
  157. func AddSourceConfKey(pluginName, confKey string, content []byte) error {
  158. reqField := make(map[string]interface{})
  159. err := json.Unmarshal(content, &reqField)
  160. if nil != err {
  161. return err
  162. }
  163. property := g_sourceProperty[pluginName+".json"]
  164. if nil == property {
  165. return fmt.Errorf("not found plugin %s", pluginName)
  166. }
  167. if nil == property.cf {
  168. property.cf = make(map[string]map[string]interface{})
  169. }
  170. if 0 != len(property.cf[confKey]) {
  171. return fmt.Errorf("exist confKey %s", confKey)
  172. }
  173. property.cf[confKey] = reqField
  174. g_sourceProperty[pluginName+".json"] = property
  175. return property.saveCf(pluginName)
  176. }
  177. func AddSourceConfKeyField(pluginName, confKey string, content []byte) error {
  178. reqField := make(map[string]interface{})
  179. err := json.Unmarshal(content, &reqField)
  180. if nil != err {
  181. return err
  182. }
  183. property := g_sourceProperty[pluginName+".json"]
  184. if nil == property {
  185. return fmt.Errorf("not found plugin %s", pluginName)
  186. }
  187. if nil == property.cf {
  188. return fmt.Errorf("not found confKey %s", confKey)
  189. }
  190. if nil == property.cf[confKey] {
  191. return fmt.Errorf("not found confKey %s", confKey)
  192. }
  193. for k, v := range reqField {
  194. property.cf[confKey][k] = v
  195. }
  196. return property.saveCf(pluginName)
  197. }
  198. func recursionDelMap(cf, fields map[string]interface{}) error {
  199. for k, v := range fields {
  200. if nil == v {
  201. delete(cf, k)
  202. continue
  203. }
  204. if delKey, ok := v.(string); ok {
  205. if 0 == len(delKey) {
  206. delete(cf, k)
  207. continue
  208. }
  209. var auxCf map[string]interface{}
  210. if err := common.MapToStruct(cf[k], &auxCf); nil != err {
  211. return fmt.Errorf("not found second key:%s.%s", k, delKey)
  212. }
  213. cf[k] = auxCf
  214. delete(auxCf, delKey)
  215. continue
  216. }
  217. if reflect.Map == reflect.TypeOf(v).Kind() {
  218. var auxCf, auxFields map[string]interface{}
  219. if err := common.MapToStruct(cf[k], &auxCf); nil != err {
  220. return fmt.Errorf("not found second key:%s.%v", k, v)
  221. }
  222. cf[k] = auxCf
  223. if err := common.MapToStruct(v, &auxFields); nil != err {
  224. return fmt.Errorf("requestef format err:%s.%v", k, v)
  225. }
  226. if err := recursionDelMap(auxCf, auxFields); nil != err {
  227. return err
  228. }
  229. }
  230. }
  231. return nil
  232. }
  233. func DelSourceConfKeyField(pluginName, confKey string, content []byte) error {
  234. reqField := make(map[string]interface{})
  235. err := json.Unmarshal(content, &reqField)
  236. if nil != err {
  237. return err
  238. }
  239. property := g_sourceProperty[pluginName+".json"]
  240. if nil == property {
  241. return fmt.Errorf("not found plugin %s", pluginName)
  242. }
  243. if nil == property.cf {
  244. return fmt.Errorf("not found confKey %s", confKey)
  245. }
  246. if nil == property.cf[confKey] {
  247. return fmt.Errorf("not found confKey %s", confKey)
  248. }
  249. err = recursionDelMap(property.cf[confKey], reqField)
  250. if nil != err {
  251. return err
  252. }
  253. return property.saveCf(pluginName)
  254. }
  255. func recursionNewFields(template []*field, conf map[string]interface{}, ret *[]*field) error {
  256. for i := 0; i < len(template); i++ {
  257. p := new(field)
  258. *p = *template[i]
  259. *ret = append(*ret, p)
  260. v, ok := conf[template[i].Name]
  261. if ok {
  262. p.Exist = true
  263. } else {
  264. p.Exist = false
  265. continue
  266. }
  267. var auxRet, auxTemplate []*field
  268. p.Default = &auxRet
  269. if nil == v {
  270. p.Default = v
  271. } else {
  272. if reflect.Map == reflect.TypeOf(v).Kind() {
  273. var nextCf map[string]interface{}
  274. if tmp, ok := v.(map[interface{}]interface{}); ok {
  275. nextCf = common.ConvertMap(tmp)
  276. } else {
  277. if err := common.MapToStruct(v, &nextCf); nil != err {
  278. return err
  279. }
  280. }
  281. if err := common.MapToStruct(template[i].Default, &auxTemplate); nil != err {
  282. return err
  283. }
  284. if err := recursionNewFields(auxTemplate, nextCf, &auxRet); nil != err {
  285. return err
  286. }
  287. } else {
  288. p.Default = v
  289. }
  290. }
  291. }
  292. return nil
  293. }
  294. func (this *sourceProperty) cfToMeta() (*uiSource, error) {
  295. fields := this.meta.ConfKeys["default"]
  296. ret := make(map[string][]*field)
  297. for k, kvs := range this.cf {
  298. var sli []*field
  299. err := recursionNewFields(fields, kvs, &sli)
  300. if nil != err {
  301. return nil, err
  302. }
  303. ret[k] = sli
  304. }
  305. meta := new(uiSource)
  306. *meta = *(this.meta)
  307. meta.ConfKeys = ret
  308. return meta, nil
  309. }
  310. func (this *sourceProperty) saveCf(pluginName string) error {
  311. confDir, err := common.GetConfLoc()
  312. if nil != err {
  313. return err
  314. }
  315. dir := path.Join(confDir, "sources")
  316. if "mqtt_source" == pluginName {
  317. dir = confDir
  318. }
  319. filePath := path.Join(dir, pluginName+".yaml")
  320. return common.WriteYamlMarshal(filePath, this.cf)
  321. }