Merge pull request #10672 from zhaoxiangchun/automated-cherry-pick-of-#10671-upstream-release-3.7

Automated cherry pick of #10671: fix(monitor): alertPanel add dumplicate name check
This commit is contained in:
Zexi Li
2021-04-12 13:17:54 +08:00
committed by GitHub
2 changed files with 41 additions and 7 deletions
+31
View File
@@ -5,6 +5,7 @@ import (
"time"
"yunion.io/x/jsonutils"
"yunion.io/x/log"
"yunion.io/x/pkg/errors"
"yunion.io/x/sqlchemy"
@@ -109,6 +110,36 @@ func (dash *SAlertDashBoard) CustomizeCreate(
return dash.SScopedResourceBase.CustomizeCreate(ctx, userCred, ownerId, query, data)
}
func (dash *SAlertDashBoard) PostCreate(ctx context.Context,
userCred mcclient.TokenCredential, ownerId mcclient.IIdentityProvider,
query jsonutils.JSONObject, data jsonutils.JSONObject) {
_, err := dash.PerformSetScope(ctx, userCred, query, data)
if err != nil {
log.Errorln(errors.Wrap(err, "dash PerformSetScope"))
}
}
func (dash *SAlertDashBoard) PerformSetScope(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, data jsonutils.JSONObject) (jsonutils.JSONObject, error) {
domainId := jsonutils.GetAnyString(data, []string{"domain_id", "domain", "project_domain_id", "project_domain"})
projectId := jsonutils.GetAnyString(data, []string{"project_id", "project"})
if len(domainId) == 0 && len(projectId) == 0 {
scope, _ := data.GetString("scope")
if len(scope) != 0 {
switch rbacutils.TRbacScope(scope) {
case rbacutils.ScopeSystem:
case rbacutils.ScopeDomain:
domainId = userCred.GetProjectDomainId()
data.(*jsonutils.JSONDict).Set("domain_id", jsonutils.NewString(domainId))
case rbacutils.ScopeProject:
projectId = userCred.GetProjectId()
data.(*jsonutils.JSONDict).Set("project_id", jsonutils.NewString(projectId))
}
}
}
return db.PerformSetScope(ctx, dash, userCred, data)
}
func (man *SAlertDashBoardManager) ListItemFilter(
ctx context.Context, q *sqlchemy.SQuery,
userCred mcclient.TokenCredential,
+10 -7
View File
@@ -117,11 +117,10 @@ func (man *SAlertPanelManager) ValidateCreateData(
}
}
name, err := CommonAlertManager.genName(ownerId, data.Name)
name, err := db.GenerateName(man, ownerId, data.Name)
if err != nil {
return data, err
}
data.Name = name
alertCreateInput := man.toAlertCreateInput(data)
data.AlertCreateInput = alertCreateInput
@@ -129,6 +128,8 @@ func (man *SAlertPanelManager) ValidateCreateData(
if data.Enabled == nil {
data.Enabled = &enable
}
data.Name = name
return data, nil
}
@@ -212,8 +213,8 @@ func (man *SAlertPanelManager) FetchCustomizeColumns(
return rows
}
func (dash *SAlertPanel) GetMoreDetails(out monitor.PanelDetails) (monitor.PanelDetails, error) {
setting, err := dash.GetSettings()
func (panel *SAlertPanel) GetMoreDetails(out monitor.PanelDetails) (monitor.PanelDetails, error) {
setting, err := panel.GetSettings()
if err != nil {
return out, err
}
@@ -223,17 +224,19 @@ func (dash *SAlertPanel) GetMoreDetails(out monitor.PanelDetails) (monitor.Panel
out.CommonAlertMetricDetails = make([]*monitor.CommonAlertMetricDetails, len(setting.Conditions))
for i, cond := range setting.Conditions {
metricDetails := dash.GetCommonAlertMetricDetailsFromAlertCondition(i, cond)
metricDetails := panel.GetCommonAlertMetricDetailsFromAlertCondition(i, &cond)
out.CommonAlertMetricDetails[i] = metricDetails
setting.Conditions[i] = cond
}
panel.Settings = jsonutils.Marshal(setting)
return out, nil
}
func (dash *SAlertPanel) GetCommonAlertMetricDetailsFromAlertCondition(index int,
cond monitor.AlertCondition) *monitor.
cond *monitor.AlertCondition) *monitor.
CommonAlertMetricDetails {
metricDetails := new(monitor.CommonAlertMetricDetails)
getCommonAlertMetricDetailsFromCondition(&cond, metricDetails)
getCommonAlertMetricDetailsFromCondition(cond, metricDetails)
return metricDetails
}