treewide: DeleteFailReason as an object with translated details

This commit is contained in:
Yousong Zhou
2020-09-07 18:15:43 +08:00
parent 776ef9b8b5
commit 6415de33d3
3 changed files with 10 additions and 7 deletions
+3 -1
View File
@@ -16,6 +16,8 @@ package apis
import (
"time"
"yunion.io/x/onecloud/pkg/httperrors"
)
type ModelBaseDetails struct {
@@ -26,7 +28,7 @@ type ModelBaseDetails struct {
CanDelete bool `json:"can_delete"`
// 资源不能删除的原因
DeleteFailReason string `json:"delete_fail_reason"`
DeleteFailReason httperrors.Error `json:"delete_fail_reason"`
// 资源是否可以更新, 若为false,update_fail_reason会返回资源不能删除的原因
// example: true
+1 -1
View File
@@ -778,7 +778,7 @@ func getModelExtraDetails(item IModel, ctx context.Context, showReason bool) api
if err != nil {
out.CanDelete = false
if showReason {
out.DeleteFailReason = err.Error()
out.DeleteFailReason = httperrors.NewErrorFromGeneralError(ctx, err)
}
}
err = item.ValidateUpdateCondition(ctx)
+6 -5
View File
@@ -368,17 +368,18 @@ func (man *SCommonAlertManager) FetchCustomizeColumns(
alertRows := man.SAlertManager.FetchCustomizeColumns(ctx, userCred, query, objs, fields, isList)
for i := range rows {
rows[i].AlertDetails = alertRows[i]
rows[i], _ = objs[i].(*SCommonAlert).GetMoreDetails(rows[i])
rows[i], _ = objs[i].(*SCommonAlert).GetMoreDetails(ctx, rows[i])
}
return rows
}
func (alert *SCommonAlert) validateDeleteCondition(out *monitor.CommonAlertDetails) {
func (alert *SCommonAlert) validateDeleteCondition(ctx context.Context, out *monitor.CommonAlertDetails) {
alert_type := alert.getAlertType()
switch alert_type {
case monitor.CommonAlertSystemAlertType:
je := httperrors.NewInputParameterError("Cannot delete system alert")
out.CanDelete = false
out.DeleteFailReason = httperrors.NewInputParameterError("Cannot delete system alert").Error()
out.DeleteFailReason = httperrors.NewErrorFromJCError(ctx, je)
default:
}
}
@@ -398,8 +399,8 @@ func (alert *SCommonAlert) AllowDeleteItem(ctx context.Context, userCred mcclien
}
}
func (alert *SCommonAlert) GetMoreDetails(out monitor.CommonAlertDetails) (monitor.CommonAlertDetails, error) {
alert.validateDeleteCondition(&out)
func (alert *SCommonAlert) GetMoreDetails(ctx context.Context, out monitor.CommonAlertDetails) (monitor.CommonAlertDetails, error) {
alert.validateDeleteCondition(ctx, &out)
var err error
alertNotis, err := alert.GetNotifications()