Skip to content

Commit

Permalink
Merge pull request #187 from openinfradev/fix_import_admin
Browse files Browse the repository at this point in the history
feature. generate clusterId to tks-admin when import admin cluster
  • Loading branch information
cho4036 authored Oct 31, 2023
2 parents 0b0fb0a + dd2c02f commit cc5a938
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
4 changes: 4 additions & 0 deletions internal/helper/id.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
)

var (
TKS_ADMIN = "tks-admin"
PREFIX_USER_ID = "u"
PREFIX_CLUSTER_ID = "c"
PREFIX_ORGANIZATION_ID = "o"
Expand All @@ -29,6 +30,9 @@ func GenerateApplicaionGroupId() string {
}

func ValidateClusterId(id string) bool {
if id == TKS_ADMIN {
return true
}
if !strings.HasPrefix(id, PREFIX_CLUSTER_ID) {
return false
}
Expand Down
10 changes: 5 additions & 5 deletions internal/repository/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,6 @@ type Cluster struct {
Updator User `gorm:"foreignKey:UpdatorId"`
}

func (c *Cluster) BeforeCreate(tx *gorm.DB) (err error) {
c.ID = domain.ClusterId(helper.GenerateClusterId())
return nil
}

type ClusterFavorite struct {
gorm.Model

Expand Down Expand Up @@ -219,6 +214,7 @@ func (r *ClusterRepository) Create(dto domain.Cluster) (clusterId domain.Cluster
cloudAccountId = nil
}
cluster := Cluster{
ID: domain.ClusterId(helper.GenerateClusterId()),
OrganizationId: dto.OrganizationId,
Name: dto.Name,
Description: dto.Description,
Expand All @@ -242,6 +238,10 @@ func (r *ClusterRepository) Create(dto domain.Cluster) (clusterId domain.Cluster
TksUserNodeMax: dto.Conf.TksUserNodeMax,
TksUserNodeType: dto.Conf.TksUserNodeType,
}
if dto.ID != "" {
cluster.ID = dto.ID
}

res := r.db.Create(&cluster)
if res.Error != nil {
log.Error(res.Error)
Expand Down
4 changes: 4 additions & 0 deletions internal/usecase/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,10 @@ func (u *ClusterUsecase) Import(ctx context.Context, dto domain.Cluster) (cluste

userId := user.GetUserId()
dto.CreatorId = &userId
if dto.ClusterType == domain.ClusterType_ADMIN {
dto.ID = "tks-admin"
dto.Name = "tks-admin"
}
clusterId, err = u.repo.Create(dto)
if err != nil {
return "", errors.Wrap(err, "Failed to create cluster")
Expand Down

0 comments on commit cc5a938

Please sign in to comment.