From 2f3a3766c5b1f45537ca48f11577dd2b28b2d96e Mon Sep 17 00:00:00 2001 From: Jian Qiu Date: Sun, 17 Jan 2021 11:33:09 +0800 Subject: [PATCH] fix(cloudcommon): missing set_meta opslog (#9915) Co-authored-by: Qiu Jian --- pkg/cloudcommon/db/metadata.go | 18 +++++++++--------- pkg/cloudcommon/db/opslog.go | 3 ++- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/pkg/cloudcommon/db/metadata.go b/pkg/cloudcommon/db/metadata.go index d4221d6058..faa7541fe4 100644 --- a/pkg/cloudcommon/db/metadata.go +++ b/pkg/cloudcommon/db/metadata.go @@ -461,17 +461,17 @@ func (manager *SMetadataManager) SetValue(ctx context.Context, obj IModel, key s } func (manager *SMetadataManager) SetValuesWithLog(ctx context.Context, obj IModel, store map[string]interface{}, userCred mcclient.TokenCredential) error { - changes, err := manager.SetValues(ctx, obj, store, userCred) + changes, err := manager.setValues(ctx, obj, store, userCred) if err != nil { return err } if len(changes) > 0 { - OpsLog.LogEvent(obj, ACT_SET_METADATA, jsonutils.Marshal(changes), userCred) + OpsLog.LogEvent(obj.GetIModel(), ACT_SET_METADATA, jsonutils.Marshal(changes), userCred) } return nil } -func (manager *SMetadataManager) SetValues(ctx context.Context, obj IModel, store map[string]interface{}, userCred mcclient.TokenCredential) ([]sMetadataChange, error) { +func (manager *SMetadataManager) setValues(ctx context.Context, obj IModel, store map[string]interface{}, userCred mcclient.TokenCredential) ([]sMetadataChange, error) { idStr := GetObjectIdstr(obj) // no need to lock @@ -548,9 +548,9 @@ func (manager *SMetadataManager) SetValues(ctx context.Context, obj IModel, stor } func (manager *SMetadataManager) SetAll(ctx context.Context, obj IModel, store map[string]interface{}, userCred mcclient.TokenCredential, delRange string) error { - changes, err := manager.SetValues(ctx, obj, store, userCred) + changes, err := manager.setValues(ctx, obj, store, userCred) if err != nil { - return err + return errors.Wrap(err, "setValues") } idStr := GetObjectIdstr(obj) @@ -567,11 +567,11 @@ func (manager *SMetadataManager) SetAll(ctx context.Context, obj IModel, store m q := manager.Query().Equals("id", idStr).NotLike("key", `\_\_%`) //避免删除系统内置的metadata, _ 在mysql里面有特殊含义,需要转义 switch delRange { case USER_TAG_PREFIX: - q = q.Like("key", USER_TAG_PREFIX+"%") + q = q.Startswith("key", USER_TAG_PREFIX) case CLOUD_TAG_PREFIX: - q = q.Like("key", CLOUD_TAG_PREFIX+"%") + q = q.Startswith("key", CLOUD_TAG_PREFIX) case SYS_CLOUD_TAG_PREFIX: - q = q.Like("key", SYS_CLOUD_TAG_PREFIX+"%") + q = q.Startswith("key", SYS_CLOUD_TAG_PREFIX) } q = q.Filter(sqlchemy.NOT(sqlchemy.In(q.Field("key"), keys))) if err := FetchModelObjects(manager, q, &records); err != nil { @@ -585,7 +585,7 @@ func (manager *SMetadataManager) SetAll(ctx context.Context, obj IModel, store m changes = append(changes, sMetadataChange{Key: rec.Key, OValue: rec.Value}) } if len(changes) > 0 { - OpsLog.LogEvent(obj, ACT_SET_METADATA, jsonutils.Marshal(changes), userCred) + OpsLog.LogEvent(obj.GetIModel(), ACT_SET_METADATA, jsonutils.Marshal(changes), userCred) } return nil } diff --git a/pkg/cloudcommon/db/opslog.go b/pkg/cloudcommon/db/opslog.go index 7b76135d49..4e45674dfc 100644 --- a/pkg/cloudcommon/db/opslog.go +++ b/pkg/cloudcommon/db/opslog.go @@ -126,7 +126,8 @@ func (manager *SOpsLogManager) LogEvent(model IModel, action string, notes inter if !consts.OpsLogEnabled() { return } - if len(model.GetId()) == 0 || len(model.GetName()) == 0 { + if len(model.GetId()) == 0 { + log.Errorf("logevent for an object without ID???") return } if action == ACT_UPDATE {