From 7d56ec2a17ec1eed55dc8bcf1c8a4ad11588d44e Mon Sep 17 00:00:00 2001 From: zhaoxiangchun <1422928955@qq.com> Date: Thu, 13 Jan 2022 18:44:18 +0800 Subject: [PATCH] fix(monitor): alerting resourece filter by scope --- pkg/apis/monitor/monitor_resource_alert.go | 1 + pkg/monitor/models/monitor_resource_alert.go | 23 +++++++++++++++++++- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/pkg/apis/monitor/monitor_resource_alert.go b/pkg/apis/monitor/monitor_resource_alert.go index 12201f1e14..a5f187fae8 100644 --- a/pkg/apis/monitor/monitor_resource_alert.go +++ b/pkg/apis/monitor/monitor_resource_alert.go @@ -24,6 +24,7 @@ import ( type MonitorResourceJointListInput struct { apis.JointResourceBaseListInput + apis.VirtualResourceListInput MonitorResourceId string `json:"monitor_resource_id"` AlertId string `json:"alert_id"` JointId []int64 `json:"joint_id"` diff --git a/pkg/monitor/models/monitor_resource_alert.go b/pkg/monitor/models/monitor_resource_alert.go index 0809681add..4ab00fab92 100644 --- a/pkg/monitor/models/monitor_resource_alert.go +++ b/pkg/monitor/models/monitor_resource_alert.go @@ -26,6 +26,7 @@ import ( "yunion.io/x/onecloud/pkg/apis/monitor" "yunion.io/x/onecloud/pkg/cloudcommon/db" "yunion.io/x/onecloud/pkg/mcclient" + "yunion.io/x/onecloud/pkg/util/rbacutils" "yunion.io/x/onecloud/pkg/util/stringutils2" ) @@ -47,6 +48,7 @@ func init() { type SMonitorResourceAlertManager struct { db.SJointResourceBaseManager + SMonitorScopedResourceManager } type SMonitorResourceAlert struct { @@ -163,6 +165,7 @@ func (m *SMonitorResourceAlertManager) ListItemFilter(ctx context.Context, q *sq if err != nil { return q, errors.Wrap(err, "Get monitor in Query err") } + m.SMonitorScopedResourceManager.FilterByOwner(resQ, userCred, rbacutils.TRbacScope(input.Scope)) q.Filter(sqlchemy.In(q.Field("monitor_resource_id"), resQ.SubQuery())) } if len(input.SendState) != 0 { @@ -181,6 +184,7 @@ func (m *SMonitorResourceAlertManager) ListItemFilter(ctx context.Context, q *sq q.Filter(sqlchemy.In(q.Field("monitor_resource_id"), resQ.SubQuery())) } alertQuery := CommonAlertManager.Query("id") + m.SMonitorScopedResourceManager.FilterByOwner(alertQuery, userCred, rbacutils.TRbacScope(input.Scope)) if len(input.AlertName) != 0 { CommonAlertManager.FieldListFilter(alertQuery, monitor.CommonAlertListInput{Name: input.AlertName}) q.Filter(sqlchemy.In(q.Field(m.GetSlaveFieldName()), alertQuery.SubQuery())) @@ -189,7 +193,8 @@ func (m *SMonitorResourceAlertManager) ListItemFilter(ctx context.Context, q *sq CommonAlertManager.FieldListFilter(alertQuery, monitor.CommonAlertListInput{Level: input.Level}) q.Filter(sqlchemy.In(q.Field(m.GetSlaveFieldName()), alertQuery.SubQuery())) } - + q.Filter(sqlchemy.In(q.Field(m.GetSlaveFieldName()), alertQuery.SubQuery())) + q.Filter(sqlchemy.In(q.Field("alert_record_id"), AlertRecordManager.Query("id").SubQuery())) return q, nil } @@ -255,3 +260,19 @@ func (obj *SMonitorResourceAlert) getMoreDetails(detail monitor.MonitorResourceJ } return detail } + +func (manager *SMonitorResourceAlertManager) ResourceScope() rbacutils.TRbacScope { + return manager.SScopedResourceBaseManager.ResourceScope() +} + +func (manager *SMonitorResourceAlertManager) ListItemExportKeys(ctx context.Context, q *sqlchemy.SQuery, userCred mcclient.TokenCredential, keys stringutils2.SSortedStrings) (*sqlchemy.SQuery, error) { + q, err := manager.SScopedResourceBaseManager.ListItemExportKeys(ctx, q, userCred, keys) + if err != nil { + return nil, errors.Wrap(err, "SScopedResourceBaseManager.ListItemExportKeys") + } + return q, nil +} + +func (m *SMonitorResourceAlertManager) FilterByOwner(q *sqlchemy.SQuery, userCred mcclient.IIdentityProvider, scope rbacutils.TRbacScope) *sqlchemy.SQuery { + return q +}