From c3d58c03b2013ba6bda1b6853e738c55ad8dd424 Mon Sep 17 00:00:00 2001 From: Jiahui <4543bxy@gmail.com> Date: Wed, 27 Mar 2024 16:34:10 +0800 Subject: [PATCH] fix create region (#4632) --- .../job/init/internal/util/database/user.go | 13 +++++++--- .../pkg/database/cockroach/accountv2.go | 2 +- controllers/pkg/types/global.go | 24 +++++++++++++++++++ 3 files changed, 35 insertions(+), 4 deletions(-) diff --git a/controllers/job/init/internal/util/database/user.go b/controllers/job/init/internal/util/database/user.go index bbf49238c3d..486aa2771c9 100644 --- a/controllers/job/init/internal/util/database/user.go +++ b/controllers/job/init/internal/util/database/user.go @@ -67,7 +67,14 @@ func PresetAdminUser() error { Domain: domain, DisplayName: domain, Location: domain, - Description: domain, + Description: types.RegionDescriptionJSON(types.RegionDescription{ + Provider: domain + "-local", + Serial: "A", + Description: map[string]string{ + "zh": domain + "-本地", + "en": domain + "-local", + }, + }), }); err != nil { return fmt.Errorf("failed to create region: %v", err) } @@ -92,8 +99,8 @@ func PresetAdminUser() error { }, &types.User{ UID: common.AdminUID(), ID: userNanoID, - Name: adminUserName, - Nickname: userNanoID, + Name: userNanoID, + Nickname: adminUserName, CreatedAt: time.Now(), UpdatedAt: time.Now(), }, &types.Workspace{ diff --git a/controllers/pkg/database/cockroach/accountv2.go b/controllers/pkg/database/cockroach/accountv2.go index 9f0d6446ccb..e5cf8272907 100644 --- a/controllers/pkg/database/cockroach/accountv2.go +++ b/controllers/pkg/database/cockroach/accountv2.go @@ -53,7 +53,7 @@ const ( func (g *Cockroach) CreateUser(oAuth *types.OauthProvider, regionUserCr *types.RegionUserCr, user *types.User, workspace *types.Workspace, userWorkspace *types.UserWorkspace) error { findUser, findRegionUserCr, findUserWorkspace := &types.User{}, &types.RegionUserCr{}, &types.UserWorkspace{} - if g.DB.Where(&types.User{Name: user.Name}).First(findUser).Error == gorm.ErrRecordNotFound { + if g.DB.Where(&types.User{Nickname: user.Nickname}).First(findUser).Error == gorm.ErrRecordNotFound { findUser = user if err := g.DB.Save(user).Error; err != nil { return fmt.Errorf("failed to create user: %w", err) diff --git a/controllers/pkg/types/global.go b/controllers/pkg/types/global.go index e28e5c7e4ac..2a42ba18ca8 100644 --- a/controllers/pkg/types/global.go +++ b/controllers/pkg/types/global.go @@ -43,6 +43,30 @@ type Region struct { Description string `gorm:"column:description;type:text"` } +type RegionDescription struct { + Provider string `json:"provider"` + Serial string `json:"serial"` + Description map[string]string `json:"description"` +} + +func RegionDescriptionJSON(data RegionDescription) string { + jsonString := `{ + "provider": "` + data.Provider + `", + "serial": "` + data.Serial + `", + "description": {` + + for key, value := range data.Description { + jsonString += `"` + key + `": "` + value + `",` + } + + jsonString = jsonString[:len(jsonString)-1] + + jsonString += `} + }` + + return jsonString +} + // RegionUserCr is located in the region type RegionUserCr struct { UID uuid.UUID `gorm:"type:uuid;default:gen_random_uuid();primary_key"`