modelset.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. // Copyright 2019 Yunion
  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 models
  15. import (
  16. "yunion.io/x/jsonutils"
  17. "yunion.io/x/onecloud/pkg/apihelper"
  18. compute_api "yunion.io/x/onecloud/pkg/apis/compute"
  19. keystone_api "yunion.io/x/onecloud/pkg/apis/identity"
  20. "yunion.io/x/onecloud/pkg/apis/monitor"
  21. "yunion.io/x/onecloud/pkg/cloudcommon/db"
  22. "yunion.io/x/onecloud/pkg/mcclient/modulebase"
  23. "yunion.io/x/onecloud/pkg/mcclient/modules/compute"
  24. "yunion.io/x/onecloud/pkg/mcclient/modules/identity"
  25. )
  26. const (
  27. INTERNAL_ID = "internal"
  28. )
  29. type IMonitorResModelSet interface {
  30. apihelper.IModelSet
  31. GetResType() string
  32. NeedSync() bool
  33. }
  34. type (
  35. // +onecloud:swagger-gen-ignore
  36. Servers map[string]*Guest
  37. // +onecloud:swagger-gen-ignore
  38. Hosts map[string]*Host
  39. // +onecloud:swagger-gen-ignore
  40. Rds map[string]*SRds
  41. // +onecloud:swagger-gen-ignore
  42. Redis map[string]*SRedis
  43. // +onecloud:swagger-gen-ignore
  44. Oss map[string]*SOss
  45. // +onecloud:swagger-gen-ignore
  46. Accounts map[string]*SAccount
  47. // +onecloud:swagger-gen-ignore
  48. Storages map[string]*SStorage
  49. // +onecloud:swagger-gen-ignore
  50. Domains map[string]*SDomain
  51. // +onecloud:swagger-gen-ignore
  52. Projects map[string]*SProject
  53. )
  54. // +onecloud:swagger-gen-ignore
  55. type Guest struct {
  56. db.SModelBase
  57. compute_api.ServerDetails
  58. }
  59. // +onecloud:swagger-gen-ignore
  60. type Host struct {
  61. db.SModelBase
  62. compute_api.HostDetails
  63. }
  64. // +onecloud:swagger-gen-ignore
  65. type SRds struct {
  66. db.SModelBase
  67. compute_api.DBInstanceDetails
  68. }
  69. // +onecloud:swagger-gen-ignore
  70. type SRedis struct {
  71. db.SModelBase
  72. compute_api.ElasticcacheDetails
  73. }
  74. // +onecloud:swagger-gen-ignore
  75. type SOss struct {
  76. db.SModelBase
  77. compute_api.BucketDetails
  78. }
  79. // +onecloud:swagger-gen-ignore
  80. type SStorage struct {
  81. db.SModelBase
  82. compute_api.StorageDetails
  83. }
  84. // +onecloud:swagger-gen-ignore
  85. type SAccount struct {
  86. db.SModelBase
  87. compute_api.CloudaccountDetail
  88. }
  89. // +onecloud:swagger-gen-ignore
  90. type SDomain struct {
  91. db.SModelBase
  92. keystone_api.DomainDetails
  93. }
  94. // +onecloud:swagger-gen-ignore
  95. type SProject struct {
  96. db.SModelBase
  97. keystone_api.ProjectDetails
  98. }
  99. func (s Servers) ModelManager() modulebase.IBaseManager {
  100. return &compute.Servers
  101. }
  102. func (s Servers) NewModel() db.IModel {
  103. return &Guest{}
  104. }
  105. func (s Servers) AddModel(i db.IModel) {
  106. resource := i.(*Guest)
  107. s[resource.Id] = resource
  108. }
  109. func (s Servers) Copy() apihelper.IModelSet {
  110. return s
  111. }
  112. func (s Servers) GetResType() string {
  113. return monitor.METRIC_RES_TYPE_GUEST
  114. }
  115. func (s Servers) NeedSync() bool {
  116. return true
  117. }
  118. func (s Servers) ModelFilter() []string {
  119. return []string{"hypervisor.notin(container)"}
  120. }
  121. func (h Hosts) AddModel(i db.IModel) {
  122. resource := i.(*Host)
  123. h[resource.Id] = resource
  124. }
  125. func (h Hosts) Copy() apihelper.IModelSet {
  126. return h
  127. }
  128. func (h Hosts) ModelManager() modulebase.IBaseManager {
  129. return &compute.Hosts
  130. }
  131. func (h Hosts) NewModel() db.IModel {
  132. return &Host{}
  133. }
  134. func (h Hosts) GetResType() string {
  135. return monitor.METRIC_RES_TYPE_HOST
  136. }
  137. func (s Hosts) NeedSync() bool {
  138. return true
  139. }
  140. func (s Hosts) ModelParamFilter() jsonutils.JSONObject {
  141. param := jsonutils.NewDict()
  142. param.Set("baremetal", jsonutils.NewBool(false))
  143. return param
  144. }
  145. func (r Rds) ModelManager() modulebase.IBaseManager {
  146. return &compute.DBInstance
  147. }
  148. func (r Rds) NewModel() db.IModel {
  149. return &SRds{}
  150. }
  151. func (r Rds) AddModel(i db.IModel) {
  152. resource := i.(*SRds)
  153. r[resource.Id] = resource
  154. }
  155. func (r Rds) Copy() apihelper.IModelSet {
  156. return r
  157. }
  158. func (r Rds) GetResType() string {
  159. return monitor.METRIC_RES_TYPE_RDS
  160. }
  161. func (s Rds) NeedSync() bool {
  162. return true
  163. }
  164. func (r Redis) ModelManager() modulebase.IBaseManager {
  165. return &compute.ElasticCache
  166. }
  167. func (r Redis) NewModel() db.IModel {
  168. return &SRedis{}
  169. }
  170. func (r Redis) AddModel(i db.IModel) {
  171. resource := i.(*SRedis)
  172. r[resource.Id] = resource
  173. }
  174. func (r Redis) Copy() apihelper.IModelSet {
  175. return r
  176. }
  177. func (r Redis) GetResType() string {
  178. return monitor.METRIC_RES_TYPE_REDIS
  179. }
  180. func (s Redis) NeedSync() bool {
  181. return true
  182. }
  183. func (o Oss) ModelManager() modulebase.IBaseManager {
  184. return &compute.Buckets
  185. }
  186. func (o Oss) NewModel() db.IModel {
  187. return &SOss{}
  188. }
  189. func (o Oss) AddModel(i db.IModel) {
  190. resource := i.(*SOss)
  191. o[resource.Id] = resource
  192. }
  193. func (o Oss) Copy() apihelper.IModelSet {
  194. return o
  195. }
  196. func (o Oss) GetResType() string {
  197. return monitor.METRIC_RES_TYPE_OSS
  198. }
  199. func (s Oss) NeedSync() bool {
  200. return true
  201. }
  202. func (a Accounts) ModelManager() modulebase.IBaseManager {
  203. return &compute.Cloudaccounts
  204. }
  205. func (a Accounts) NewModel() db.IModel {
  206. return &SAccount{}
  207. }
  208. func (a Accounts) AddModel(i db.IModel) {
  209. resource := i.(*SAccount)
  210. resource.TenantId = INTERNAL_ID
  211. a[resource.Id] = resource
  212. }
  213. func (a Accounts) Copy() apihelper.IModelSet {
  214. return a
  215. }
  216. func (a Accounts) GetResType() string {
  217. return monitor.METRIC_RES_TYPE_CLOUDACCOUNT
  218. }
  219. func (s Accounts) NeedSync() bool {
  220. return true
  221. }
  222. func (s Storages) ModelManager() modulebase.IBaseManager {
  223. return &compute.Storages
  224. }
  225. func (s Storages) NewModel() db.IModel {
  226. return &SStorage{}
  227. }
  228. func (s Storages) AddModel(i db.IModel) {
  229. resource := i.(*SStorage)
  230. s[resource.Id] = resource
  231. }
  232. func (s Storages) Copy() apihelper.IModelSet {
  233. return s
  234. }
  235. func (s Storages) GetResType() string {
  236. return monitor.METRIC_RES_TYPE_STORAGE
  237. }
  238. func (s Storages) NeedSync() bool {
  239. return true
  240. }
  241. func (s Storages) SetModelListParams(params *jsonutils.JSONDict) *jsonutils.JSONDict {
  242. // list storages with details is too long
  243. params.Set("details", jsonutils.JSONFalse)
  244. return params
  245. }
  246. func (d Domains) ModelManager() modulebase.IBaseManager {
  247. return &identity.Domains
  248. }
  249. func (d Domains) NewModel() db.IModel {
  250. return &SDomain{}
  251. }
  252. func (d Domains) AddModel(i db.IModel) {
  253. resource := i.(*SDomain)
  254. d[resource.Id] = resource
  255. }
  256. func (d Domains) Copy() apihelper.IModelSet {
  257. return d
  258. }
  259. func (d Domains) GetResType() string {
  260. return monitor.METRIC_RES_TYPE_DOMAIN
  261. }
  262. func (d Domains) NeedSync() bool {
  263. return false
  264. }
  265. func (p Projects) ModelManager() modulebase.IBaseManager {
  266. return &identity.Projects
  267. }
  268. func (p Projects) NewModel() db.IModel {
  269. return &SProject{}
  270. }
  271. func (p Projects) AddModel(i db.IModel) {
  272. resource := i.(*SProject)
  273. p[resource.Id] = resource
  274. }
  275. func (p Projects) Copy() apihelper.IModelSet {
  276. return p
  277. }
  278. func (p Projects) GetResType() string {
  279. return monitor.METRIC_RES_TYPE_TENANT
  280. }
  281. func (p Projects) NeedSync() bool {
  282. return false
  283. }