alertresourcejoints.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. "context"
  17. "yunion.io/x/jsonutils"
  18. "yunion.io/x/log"
  19. "yunion.io/x/onecloud/pkg/apis"
  20. "yunion.io/x/onecloud/pkg/apis/monitor"
  21. "yunion.io/x/onecloud/pkg/cloudcommon/db"
  22. "yunion.io/x/onecloud/pkg/mcclient"
  23. "yunion.io/x/onecloud/pkg/util/stringutils2"
  24. )
  25. type IAlertResourceJointModel interface {
  26. db.IJointModel
  27. GetAlertResource() (*SAlertResource, error)
  28. GetDetails(base monitor.AlertResourceJointBaseDetails, isList bool) interface{}
  29. }
  30. // +onecloud:swagger-gen-ignore
  31. type SAlertResourceJointsManager struct {
  32. db.SJointResourceBaseManager
  33. }
  34. // +onecloud:swagger-gen-ignore
  35. type SAlertResourceJointsBase struct {
  36. db.SJointResourceBase
  37. AlertResourceId string `width:"36" charset:"ascii" nullable:"false" list:"user" create:"required" index:"true"`
  38. }
  39. func NewAlertResourceJointManager(dt interface{}, tableName string, keyword string, keywordPlural string, slave db.IStandaloneModelManager) *SAlertResourceJointsManager {
  40. return &SAlertResourceJointsManager{
  41. SJointResourceBaseManager: db.NewJointResourceBaseManager(
  42. dt, tableName, keyword, keywordPlural, GetAlertResourceManager(), slave,
  43. ),
  44. }
  45. }
  46. func (m SAlertResourceJointsManager) GetMasterFieldName() string {
  47. return "alert_resource_id"
  48. }
  49. func (obj *SAlertResourceJointsBase) GetAlertResource() (*SAlertResource, error) {
  50. mMan := obj.GetJointModelManager().GetMasterManager()
  51. mObj, err := mMan.FetchById(obj.AlertResourceId)
  52. if err != nil {
  53. return nil, err
  54. }
  55. return mObj.(*SAlertResource), nil
  56. }
  57. func (m *SAlertResourceJointsManager) FetchCustomizeColumns(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, objs []interface{}, fields stringutils2.SSortedStrings, isList bool) []interface{} {
  58. baseGet := func(obj interface{}) interface{} {
  59. jRows := m.SJointResourceBaseManager.FetchCustomizeColumns(ctx, userCred, query, []interface{}{obj}, fields, isList)
  60. return jRows[0]
  61. }
  62. ret := make([]interface{}, len(objs))
  63. for idx := range objs {
  64. obj := objs[idx].(IAlertResourceJointModel)
  65. baseDetail := baseGet(obj).(apis.JointResourceBaseDetails)
  66. outBase := monitor.AlertResourceJointBaseDetails{
  67. JointResourceBaseDetails: baseDetail,
  68. }
  69. resObj, err := obj.GetAlertResource()
  70. if err == nil {
  71. outBase.AlertResource = resObj.GetName()
  72. outBase.Type = resObj.GetType()
  73. } else {
  74. log.Errorf("Get alert resource error: %v", err)
  75. }
  76. out := obj.GetDetails(outBase, isList)
  77. ret[idx] = out
  78. }
  79. return ret
  80. }