Skip to content

Commit

Permalink
fix(region): auto rename when change resource owner with same name (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
ioito authored Jun 20, 2024
1 parent a6ccf0a commit f01b9fb
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 10 deletions.
1 change: 1 addition & 0 deletions pkg/apis/identity/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ var (
"platform_name",
"enable_cloud_shell",
"platform_names",
"enable_change_owner_auto_rename",
},
}

Expand Down
10 changes: 10 additions & 0 deletions pkg/cloudcommon/consts/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ var (

taskWorkerCount int
localTaskWorkerCount int

enableChangeOwnerAutoRename = false
)

func SetDefaultDB(dialect, connStr string) {
Expand Down Expand Up @@ -64,6 +66,14 @@ func SetLocalTaskWorkerCount(cnt int) {
localTaskWorkerCount = cnt
}

func SetChangeOwnerAutoRename(enable bool) {
enableChangeOwnerAutoRename = enable
}

func GetChangeOwnerAutoRename() bool {
return enableChangeOwnerAutoRename
}

func TaskWorkerCount() int {
return taskWorkerCount
}
Expand Down
31 changes: 21 additions & 10 deletions pkg/cloudcommon/db/virtualresource.go
Original file line number Diff line number Diff line change
Expand Up @@ -379,16 +379,18 @@ func (model *SVirtualResourceBase) PerformChangeOwner(ctx context.Context, userC
return nil, errors.Wrap(err, "objectConfirmPolicyTags")
}

q := manager.Query().Equals("name", model.GetName())
q = manager.FilterByOwner(ctx, q, manager, userCred, ownerId, manager.NamespaceScope())
q = manager.FilterBySystemAttributes(q, nil, nil, manager.ResourceScope())
q = q.NotEquals("id", model.GetId())
cnt, err := q.CountWithError()
if err != nil {
return nil, httperrors.NewInternalServerError("check name duplication error: %s", err)
}
if cnt > 0 {
return nil, httperrors.NewDuplicateNameError("name", model.GetName())
if !consts.GetChangeOwnerAutoRename() {
q := manager.Query().Equals("name", model.GetName())
q = manager.FilterByOwner(ctx, q, manager, userCred, ownerId, manager.NamespaceScope())
q = manager.FilterBySystemAttributes(q, nil, nil, manager.ResourceScope())
q = q.NotEquals("id", model.GetId())
cnt, err := q.CountWithError()
if err != nil {
return nil, httperrors.NewInternalServerError("check name duplication error: %s", err)
}
if cnt > 0 {
return nil, httperrors.NewDuplicateNameError("name", model.GetName())
}
}
former, _ := TenantCacheManager.FetchTenantById(ctx, model.ProjectId)
if former == nil {
Expand Down Expand Up @@ -428,7 +430,12 @@ func (model *SVirtualResourceBase) PerformChangeOwner(ctx context.Context, userC
// cancel usage
model.cleanModelUsages(ctx, userCred)

oldName := model.Name
_, err = Update(model, func() error {
model.Name, err = GenerateName(ctx, manager, ownerId, oldName)
if err != nil {
return err
}
model.DomainId = ownerId.GetProjectDomainId()
model.ProjectId = ownerId.GetProjectId()
model.ProjectSrc = string(apis.OWNER_SOURCE_LOCAL)
Expand All @@ -438,6 +445,10 @@ func (model *SVirtualResourceBase) PerformChangeOwner(ctx context.Context, userC
return nil, errors.Wrap(err, "Update")
}

if oldName != model.Name {
model.SetMetadata(ctx, "old_name", oldName, userCred)
}

// add usage
model.RecoverUsages(ctx, userCred)

Expand Down
3 changes: 3 additions & 0 deletions pkg/cloudcommon/options/changes.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ func OnBaseOptionsChange(oOpts, nOpts interface{}) bool {
if oldOpts.LocalTaskWorkerCount != newOpts.LocalTaskWorkerCount {
consts.SetLocalTaskWorkerCount(newOpts.LocalTaskWorkerCount)
}
if oldOpts.EnableChangeOwnerAutoRename != newOpts.EnableChangeOwnerAutoRename {
consts.SetChangeOwnerAutoRename(newOpts.EnableChangeOwnerAutoRename)
}
return changed
}

Expand Down
2 changes: 2 additions & 0 deletions pkg/cloudcommon/options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ type BaseOptions struct {
PlatformNames map[string]string `help:"identity name of this platform by language"`

EnableAppProfiling bool `help:"enable profiling API" default:"false"`

EnableChangeOwnerAutoRename bool `help:"Allows renaming when changing names" default:"false"`
}

const (
Expand Down

0 comments on commit f01b9fb

Please sign in to comment.