Skip to content

Commit

Permalink
fix create region (labring#4632)
Browse files Browse the repository at this point in the history
  • Loading branch information
bxy4543 authored Mar 27, 2024
1 parent ee6b129 commit c3d58c0
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 4 deletions.
13 changes: 10 additions & 3 deletions controllers/job/init/internal/util/database/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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{
Expand Down
2 changes: 1 addition & 1 deletion controllers/pkg/database/cockroach/accountv2.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
24 changes: 24 additions & 0 deletions controllers/pkg/types/global.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down

0 comments on commit c3d58c0

Please sign in to comment.