tree.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. package service
  2. import (
  3. "context"
  4. "go-common/app/admin/ep/merlin/model"
  5. "go-common/library/ecode"
  6. )
  7. const (
  8. _startsWith = "startswith"
  9. _merlinHostnameRegex = ".*-[0-9]-0"
  10. )
  11. // UserTreeAsOption get user tree as option.
  12. func (s *Service) UserTreeAsOption(c context.Context, sessionID string) (firstRetMap []map[string]interface{}, err error) {
  13. var treeMap *model.UserTree
  14. if treeMap, err = s.dao.UserTree(c, sessionID); err != nil {
  15. err = ecode.MerlinGetUserTreeFailed
  16. return
  17. }
  18. if treeMap.Bilibili == nil {
  19. return
  20. }
  21. firstLevelMapTmp := treeMap.Bilibili
  22. if firstLevelMapTmp["children"] == nil {
  23. return
  24. }
  25. firstLevelMap := firstLevelMapTmp["children"].(map[string]interface{})
  26. for firstLevelKey, firstLevelChildren := range firstLevelMap {
  27. secondLevelMapTmp := firstLevelChildren.(map[string]interface{})
  28. if secondLevelMapTmp["children"] == nil {
  29. continue
  30. }
  31. secondLevelMap := secondLevelMapTmp["children"].(map[string]interface{})
  32. var secondRetMap []map[string]interface{}
  33. for secondLevelKey, secondLevelChildren := range secondLevelMap {
  34. thirdLevelMapTmp := secondLevelChildren.(map[string]interface{})
  35. if thirdLevelMapTmp["children"] == nil {
  36. continue
  37. }
  38. thirdLevelMap := thirdLevelMapTmp["children"].(map[string]interface{})
  39. var thirdRetMap []map[string]interface{}
  40. for thirdLevelKey, thirdLevelChildren := range thirdLevelMap {
  41. if thirdLevelKey != "" && thirdLevelChildren != nil {
  42. childID := int64(thirdLevelChildren.(map[string]interface{})["id"].(float64))
  43. thirdTmp := make(map[string]interface{})
  44. thirdTmp["label"] = thirdLevelKey
  45. v := new(struct {
  46. ID int64 `json:"id"`
  47. Name string `json:"name"`
  48. })
  49. v.ID = childID
  50. v.Name = thirdLevelKey
  51. thirdTmp["value"] = v
  52. thirdRetMap = append(thirdRetMap, thirdTmp)
  53. }
  54. }
  55. if secondLevelKey != "" {
  56. secondTmp := make(map[string]interface{})
  57. secondTmp["value"] = secondLevelKey
  58. secondTmp["label"] = secondLevelKey
  59. secondTmp["children"] = thirdRetMap
  60. secondRetMap = append(secondRetMap, secondTmp)
  61. }
  62. }
  63. if firstLevelKey != "" {
  64. firstTmp := make(map[string]interface{})
  65. firstTmp["value"] = firstLevelKey
  66. firstTmp["label"] = firstLevelKey
  67. firstTmp["children"] = secondRetMap
  68. firstRetMap = append(firstRetMap, firstTmp)
  69. }
  70. }
  71. return
  72. }
  73. // VerifyTreeContainerNode verify tree containers.
  74. func (s *Service) VerifyTreeContainerNode(c context.Context, sessionID string, tnr *model.TreeNode) (err error) {
  75. var (
  76. sonMap map[string]interface{}
  77. ok bool
  78. )
  79. if sonMap, err = s.dao.TreeSon(c, sessionID, tnr.TreePath()); err != nil {
  80. err = ecode.MerlinGetUserTreeFailed
  81. return
  82. }
  83. if sonMap, ok = sonMap["dev"].(map[string]interface{}); !ok {
  84. err = ecode.MerlinLoseTreeContainerNodeErr
  85. return
  86. }
  87. if sonMap, ok = sonMap["children"].(map[string]interface{}); !ok {
  88. err = ecode.MerlinLoseTreeContainerNodeErr
  89. return
  90. }
  91. if sonMap["containers"] == nil {
  92. err = ecode.MerlinLoseTreeContainerNodeErr
  93. }
  94. return
  95. }
  96. // TreeRoleAsAuditor tree role as auditor.
  97. func (s *Service) TreeRoleAsAuditor(c context.Context, sessionID, firstNode string) (auditors []string, err error) {
  98. var treeRoles []*model.TreeRole
  99. if treeRoles, err = s.dao.TreeRoles(c, sessionID, firstNode); err != nil {
  100. return
  101. }
  102. for _, treeRole := range treeRoles {
  103. if treeRole.Role == model.TreeRoleAdmin {
  104. auditors = append(auditors, treeRole.UserName)
  105. }
  106. }
  107. return
  108. }
  109. // TreeMachineIsExist tree machine is exist.
  110. func (s *Service) TreeMachineIsExist(c context.Context, podName string, tn *model.TreeNode) (b bool, err error) {
  111. var (
  112. pathAndPodNameMap = make(map[string][]string)
  113. result map[string]bool
  114. )
  115. pathAndPodNameMap[tn.TreePath()] = []string{podName}
  116. if result, err = s.TreeMachinesIsExist(c, pathAndPodNameMap); err != nil {
  117. return
  118. }
  119. b = result[podName]
  120. return
  121. }
  122. // TreeMachinesIsExist tree machines is exist.
  123. func (s *Service) TreeMachinesIsExist(c context.Context, pathAndMachinesMap map[string][]string) (machinesIsExist map[string]bool, err error) {
  124. var (
  125. treeAppInstances map[string][]*model.TreeAppInstance
  126. paths []string
  127. )
  128. machinesIsExist = make(map[string]bool)
  129. for path, machineNames := range pathAndMachinesMap {
  130. paths = append(paths, path)
  131. for _, machineName := range machineNames {
  132. machinesIsExist[machineName] = false
  133. }
  134. }
  135. if treeAppInstances, err = s.dao.TreeAppInstance(c, paths); err != nil {
  136. return
  137. }
  138. for _, machinesInTree := range treeAppInstances {
  139. for _, machineInTree := range machinesInTree {
  140. if _, ok := machinesIsExist[machineInTree.HostName]; ok {
  141. machinesIsExist[machineInTree.HostName] = true
  142. }
  143. }
  144. }
  145. return
  146. }
  147. // QueryTreeInstanceForMerlin query TreeInstance for merlin.
  148. func (s *Service) QueryTreeInstanceForMerlin(c context.Context, sessionID string, tn *model.TreeNode) (res map[string]*model.TreeInstance, err error) {
  149. tir := &model.TreeInstanceRequest{
  150. HostnameRegex: _merlinHostnameRegex,
  151. }
  152. if path := tn.TreePathWithoutEmptyField(); path != "" {
  153. tir.Path = path
  154. tir.PathFuzzy = _startsWith
  155. }
  156. res, err = s.dao.QueryTreeInstances(c, sessionID, tir)
  157. return
  158. }