Merge pull request #10482 from ioito/hotfix/qx-imported-at-time

fix: before insert
This commit is contained in:
yunion-ci-robot
2021-04-01 22:26:48 +08:00
committed by GitHub
7 changed files with 47 additions and 8 deletions
+1 -1
View File
@@ -152,7 +152,7 @@ require (
yunion.io/x/ovsdb v0.0.0-20200526071744-27bf0940cbc7
yunion.io/x/pkg v0.0.0-20210218105412-13a69f60034c
yunion.io/x/s3cli v0.0.0-20190917004522-13ac36d8687e
yunion.io/x/sqlchemy v0.0.0-20210329171939-2c520f93aee3
yunion.io/x/sqlchemy v0.0.0-20210401131841-cdb9ab689824
yunion.io/x/structarg v0.0.0-20200720093445-9f850fa222ce
)
+2 -2
View File
@@ -928,7 +928,7 @@ yunion.io/x/pkg v0.0.0-20210218105412-13a69f60034c h1:J/joqbA1N2mAlOl0Uqd4LpAq3+
yunion.io/x/pkg v0.0.0-20210218105412-13a69f60034c/go.mod h1:t6rEGG2sQ4J7DhFxSZVOTjNd0YO/KlfWQyK1W4tog+E=
yunion.io/x/s3cli v0.0.0-20190917004522-13ac36d8687e h1:v+EzIadodSwkdZ/7bremd7J8J50Cise/HCylsOJngmo=
yunion.io/x/s3cli v0.0.0-20190917004522-13ac36d8687e/go.mod h1:0iFKpOs1y4lbCxeOmq3Xx/0AcQoewVPwj62eRluioEo=
yunion.io/x/sqlchemy v0.0.0-20210329171939-2c520f93aee3 h1:C4yasW3gXronDIlIVUqHSbkA38PM1X9gxUWTYGsuIuI=
yunion.io/x/sqlchemy v0.0.0-20210329171939-2c520f93aee3/go.mod h1:FTdwPdGhMgh4E+UFXc9klI1Ok34fMuybTT+jLhOaIjI=
yunion.io/x/sqlchemy v0.0.0-20210401131841-cdb9ab689824 h1:3/VcCiO+vr3+zZFLNvre41ARF2xE6NVq6Ue7JPFuJvU=
yunion.io/x/sqlchemy v0.0.0-20210401131841-cdb9ab689824/go.mod h1:FTdwPdGhMgh4E+UFXc9klI1Ok34fMuybTT+jLhOaIjI=
yunion.io/x/structarg v0.0.0-20200720093445-9f850fa222ce h1:kU8xE7O5uZ1GSJVMZHoJ+jrNL7csUQHYGyAPW9QfNpE=
yunion.io/x/structarg v0.0.0-20200720093445-9f850fa222ce/go.mod h1:EP6NSv2C0zzqBDTKumv8hPWLb3XvgMZDHQRfyuOrQng=
+5
View File
@@ -20,6 +20,11 @@ import (
"yunion.io/x/onecloud/pkg/httperrors"
)
const (
EXTERNAL_RESOURCE_SOURCE_LOCAL = "local"
EXTERNAL_RESOURCE_SOURCE_CLOUD = "cloud"
)
type ModelBaseDetails struct {
Meta
+12
View File
@@ -17,6 +17,7 @@ package db
import (
"context"
"database/sql"
"time"
"yunion.io/x/sqlchemy"
@@ -28,6 +29,17 @@ import (
type SExternalizedResourceBase struct {
// 外部Id, 对用公有云私有资源自身的Id
ExternalId string `width:"256" charset:"utf8" index:"true" list:"user" create:"domain_optional" update:"admin" json:"external_id"`
// 资源导入时间
ImportedAt time.Time `nullable:"true" created_at:"true" index:"true" get:"user" list:"user" json:"imported_at"`
Source string `width:"12" charset:"ascii" get:"user" list:"user" create:"optional" json:"source"`
}
func (model *SExternalizedResourceBase) BeforeInsert() {
model.Source = apis.EXTERNAL_RESOURCE_SOURCE_LOCAL
if len(model.ExternalId) > 0 {
model.Source = apis.EXTERNAL_RESOURCE_SOURCE_CLOUD
}
}
type SExternalizedResourceBaseManager struct{}
+5
View File
@@ -30,6 +30,7 @@ import (
"yunion.io/x/pkg/utils"
"yunion.io/x/sqlchemy"
"yunion.io/x/onecloud/pkg/apis"
api "yunion.io/x/onecloud/pkg/apis/cloudid"
"yunion.io/x/onecloud/pkg/cloudcommon/db"
"yunion.io/x/onecloud/pkg/cloudcommon/db/lockman"
@@ -504,6 +505,10 @@ func (self *SClouduser) PostCreate(ctx context.Context, userCred mcclient.TokenC
if len(input.Password) > 0 {
self.SavePassword(input.Password)
}
db.Update(self, func() error {
self.Source = apis.EXTERNAL_RESOURCE_SOURCE_LOCAL
return nil
})
account, err := self.GetCloudaccount()
if err != nil {
return
+1 -1
View File
@@ -1177,7 +1177,7 @@ yunion.io/x/pkg/util/workqueue
yunion.io/x/pkg/utils
# yunion.io/x/s3cli v0.0.0-20190917004522-13ac36d8687e
yunion.io/x/s3cli
# yunion.io/x/sqlchemy v0.0.0-20210329171939-2c520f93aee3
# yunion.io/x/sqlchemy v0.0.0-20210401131841-cdb9ab689824
yunion.io/x/sqlchemy
# yunion.io/x/structarg v0.0.0-20200720093445-9f850fa222ce
yunion.io/x/structarg
+21 -4
View File
@@ -148,11 +148,28 @@ func (t *STableSpec) insertSqlPrep(dataFields reflectutils.SStructFieldValueSet,
return insertSql, values, nil
}
func (t *STableSpec) insert(data interface{}, update bool, debug bool) error {
beforeInsertFunc := reflect.ValueOf(data).MethodByName("BeforeInsert")
if beforeInsertFunc.IsValid() && !beforeInsertFunc.IsNil() {
beforeInsertFunc.Call([]reflect.Value{})
func beforeInsert(val reflect.Value) {
switch val.Kind() {
case reflect.Struct:
structType := val.Type()
for i := 0; i < val.NumField(); i++ {
fieldType := structType.Field(i)
if fieldType.Anonymous {
beforeInsert(val.Field(i))
}
}
valPtr := val.Addr()
afterMarshalFunc := valPtr.MethodByName("BeforeInsert")
if afterMarshalFunc.IsValid() && !afterMarshalFunc.IsNil() {
afterMarshalFunc.Call([]reflect.Value{})
}
case reflect.Ptr:
beforeInsert(val.Elem())
}
}
func (t *STableSpec) insert(data interface{}, update bool, debug bool) error {
beforeInsert(reflect.ValueOf(data))
dataValue := reflect.ValueOf(data).Elem()
dataFields := reflectutils.FetchStructFieldValueSet(dataValue)