diff --git a/cmd/climc/shell/monitor/alertnotification.go b/cmd/climc/shell/monitor/alertnotification.go index ee5f51cca29..31fc631712d 100644 --- a/cmd/climc/shell/monitor/alertnotification.go +++ b/cmd/climc/shell/monitor/alertnotification.go @@ -36,9 +36,7 @@ func initAlertNotification() { return err } var result *printutils.ListResult - if len(args.Alert) > 0 { - result, err = monitor.Alertnotification.ListDescendent(s, args.Alert, params) - } else if len(args.Notification) > 0 { + if len(args.Notification) > 0 { result, err = monitor.Alertnotification.ListDescendent2(s, args.Notification, params) } else { result, err = monitor.Alertnotification.List(s, params) diff --git a/pkg/apis/monitor/alertjoint.go b/pkg/apis/monitor/alertjoint.go new file mode 100644 index 00000000000..2d2e5aad18b --- /dev/null +++ b/pkg/apis/monitor/alertjoint.go @@ -0,0 +1,28 @@ +// Copyright 2019 Yunion +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package monitor + +import "yunion.io/x/onecloud/pkg/apis" + +type AlertJointCreateInput struct { + apis.Meta + + AlertId string `json:"alert_id"` +} + +type AlertJointListInput struct { + apis.JointResourceBaseListInput + AlertIds []string `json:"alert_ids"` +} diff --git a/pkg/apis/monitor/alertnotification.go b/pkg/apis/monitor/alertnotification.go index 77f55ac0a4d..14322b72a19 100644 --- a/pkg/apis/monitor/alertnotification.go +++ b/pkg/apis/monitor/alertnotification.go @@ -35,12 +35,7 @@ type AlertJointResourceBaseDetails struct { type AlertnotificationDetails struct { AlertJointResourceBaseDetails Notification string `json:"notification"` -} - -type AlertJointCreateInput struct { - apis.Meta - - AlertId string `json:"alert_id"` + Frequency int64 `json:"frequency"` } type AlertnotificationCreateInput struct { @@ -50,3 +45,7 @@ type AlertnotificationCreateInput struct { UsedBy string `json:"used_by"` Params jsonutils.JSONObject `json:"params"` } + +type AlertNotificationListInput struct { + AlertJointListInput +} diff --git a/pkg/mcclient/modules/monitor/alert.go b/pkg/mcclient/modules/monitor/alert.go index 5b4f49486dd..c78fbb605be 100644 --- a/pkg/mcclient/modules/monitor/alert.go +++ b/pkg/mcclient/modules/monitor/alert.go @@ -52,7 +52,7 @@ type SNotificationManager struct { func NewNotificationManager() *SNotificationManager { man := modules.NewMonitorV2Manager( "alert_notification", "alert_notifications", - []string{"id", "name", "type", "is_default", "disable_resolve_message", "send_reminder", "settings"}, + []string{"id", "name", "type", "is_default", "disable_resolve_message", "send_reminder", "frequency", "settings"}, []string{}) return &SNotificationManager{ ResourceManager: &man, @@ -65,7 +65,7 @@ type SAlertnotificationManager struct { func NewAlertnotificationManager() *SAlertnotificationManager { man := modules.NewJointMonitorV2Manager("alertnotification", "alertnotifications", - []string{"Alert_ID", "Alert", "Notification_ID", "Notification", "Used_by", "State"}, + []string{"Alert_ID", "Alert", "Notification_ID", "Notification", "Used_by", "State", "Frequency"}, []string{}, Alerts, Notifications) return &SAlertnotificationManager{&man} diff --git a/pkg/mcclient/options/monitor/alert.go b/pkg/mcclient/options/monitor/alert.go index 5798f59d0ee..ad655724e39 100644 --- a/pkg/mcclient/options/monitor/alert.go +++ b/pkg/mcclient/options/monitor/alert.go @@ -206,8 +206,8 @@ type AlertNotificationAttachOptions struct { type AlertNotificationListOptions struct { options.BaseListOptions - Alert string `help:"ID or name of alert" short-token:"a"` - Notification string `help:"ID or name of notification" short-token:"n"` + Notification string `help:"ID or name of notification" short-token:"n"` + AlertId []string `help:"ID or name of alert" short-token:"a"` } func (o AlertNotificationListOptions) Params() (*jsonutils.JSONDict, error) { @@ -215,5 +215,8 @@ func (o AlertNotificationListOptions) Params() (*jsonutils.JSONDict, error) { if err != nil { return nil, err } + if len(o.AlertId) > 0 { + params.Add(jsonutils.NewStringArray(o.AlertId), "alert_ids") + } return params, nil } diff --git a/pkg/monitor/models/alertjoint.go b/pkg/monitor/models/alertjoint.go index 5cde6eb03fe..869a6310df1 100644 --- a/pkg/monitor/models/alertjoint.go +++ b/pkg/monitor/models/alertjoint.go @@ -19,7 +19,9 @@ import ( "yunion.io/x/jsonutils" "yunion.io/x/log" + "yunion.io/x/pkg/errors" "yunion.io/x/pkg/util/reflectutils" + "yunion.io/x/sqlchemy" "yunion.io/x/onecloud/pkg/apis/monitor" "yunion.io/x/onecloud/pkg/cloudcommon/db" @@ -58,6 +60,21 @@ func (man *SAlertJointsManager) GetMasterFieldName() string { return "alert_id" } +func (man *SAlertJointsManager) ListItemFilter( + ctx context.Context, + q *sqlchemy.SQuery, + userCred mcclient.TokenCredential, + query monitor.AlertJointListInput) (*sqlchemy.SQuery, error) { + q, err := man.SJointResourceBaseManager.ListItemFilter(ctx, q, userCred, query.JointResourceBaseListInput) + if err != nil { + return nil, errors.Wrap(err, "SJointResourceBaseManager.ListItemFilter") + } + if len(query.AlertIds) != 0 { + q = q.In("alert_id", query.AlertIds) + } + return q, nil +} + func (man *SAlertJointsManager) FetchCustomizeColumns( ctx context.Context, userCred mcclient.TokenCredential, diff --git a/pkg/monitor/models/alertnotification.go b/pkg/monitor/models/alertnotification.go index da33dc29208..f712ae0e371 100644 --- a/pkg/monitor/models/alertnotification.go +++ b/pkg/monitor/models/alertnotification.go @@ -20,6 +20,7 @@ import ( "yunion.io/x/jsonutils" "yunion.io/x/pkg/errors" + "yunion.io/x/sqlchemy" "yunion.io/x/onecloud/pkg/apis/monitor" "yunion.io/x/onecloud/pkg/cloudcommon/db" @@ -79,6 +80,14 @@ func (man *SAlertNotificationManager) Get(alertId string, notiId string) (*SAler return obj, err } +func (man *SAlertNotificationManager) ListItemFilter( + ctx context.Context, + q *sqlchemy.SQuery, + userCred mcclient.TokenCredential, + query monitor.AlertNotificationListInput) (*sqlchemy.SQuery, error) { + return man.SAlertJointsManager.ListItemFilter(ctx, q, userCred, query.AlertJointListInput) +} + func (man *SAlertNotificationManager) FetchCustomizeColumns( ctx context.Context, userCred mcclient.TokenCredential, @@ -106,6 +115,7 @@ func (man *SAlertNotificationManager) FetchCustomizeColumns( for i := range rows { if noti, ok := notis[notiIds[i]]; ok { rows[i].Notification = noti.Name + rows[i].Frequency = noti.Frequency } } return rows