From 0cfc5f2311ab3cb9cb66cc3cfd64aa8f54a8c924 Mon Sep 17 00:00:00 2001 From: Yousong Zhou Date: Wed, 27 Jan 2021 18:46:59 +0800 Subject: [PATCH] feat(cloudcommon): opslog: log joint models --- pkg/cloudcommon/db/opslog.go | 30 +++++++++++++++++++++++++----- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/pkg/cloudcommon/db/opslog.go b/pkg/cloudcommon/db/opslog.go index 4e45674dfc..23eb109537 100644 --- a/pkg/cloudcommon/db/opslog.go +++ b/pkg/cloudcommon/db/opslog.go @@ -18,6 +18,7 @@ import ( "context" "database/sql" "fmt" + "runtime/debug" "strconv" "strings" "time" @@ -126,10 +127,29 @@ func (manager *SOpsLogManager) LogEvent(model IModel, action string, notes inter if !consts.OpsLogEnabled() { return } - if len(model.GetId()) == 0 { - log.Errorf("logevent for an object without ID???") - return + + var ( + objId = model.GetId() + objName = model.GetName() + ) + if objId == "" { + if joint, ok := model.(IJointModel); ok { + var ( + mm = JointMaster(joint) + ms = JointSlave(joint) + ) + if mm == nil || ms == nil { + log.Errorf("logevent for jointmodel with nil sides %v/%v\n%s", mm, ms, debug.Stack()) + return + } + objId = mm.GetId() + "/" + ms.GetId() + objName = mm.GetName() + "/" + ms.GetName() + } else { + log.Errorf("logevent for an object without ID: %T\n%s", model, debug.Stack()) + return + } } + if action == ACT_UPDATE { // skip empty diff if notes == nil { @@ -142,8 +162,8 @@ func (manager *SOpsLogManager) LogEvent(model IModel, action string, notes inter opslog := &SOpsLog{ OpsTime: time.Now().UTC(), ObjType: model.Keyword(), - ObjId: model.GetId(), - ObjName: model.GetName(), + ObjId: objId, + ObjName: objName, Action: action, Notes: stringutils.Interface2String(notes),