mirror of
https://github.com/yunionio/cloudpods.git
synced 2026-09-24 16:03:43 +08:00
fix(region): auto rename when change resource owner with same name (#20593)
This commit is contained in:
@@ -117,6 +117,7 @@ var (
|
||||
"platform_name",
|
||||
"enable_cloud_shell",
|
||||
"platform_names",
|
||||
"enable_change_owner_auto_rename",
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
@@ -26,6 +26,8 @@ var (
|
||||
defaultDBConnectionString string
|
||||
|
||||
defaultDBChecksumHashAlgorithm string
|
||||
|
||||
enableChangeOwnerAutoRename = false
|
||||
)
|
||||
|
||||
func SetDefaultDB(dialect, connStr string) {
|
||||
@@ -52,3 +54,11 @@ func DefaultDBChecksumHashAlgorithm() string {
|
||||
}
|
||||
return "sha256"
|
||||
}
|
||||
|
||||
func SetChangeOwnerAutoRename(enable bool) {
|
||||
enableChangeOwnerAutoRename = enable
|
||||
}
|
||||
|
||||
func GetChangeOwnerAutoRename() bool {
|
||||
return enableChangeOwnerAutoRename
|
||||
}
|
||||
|
||||
@@ -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 {
|
||||
@@ -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)
|
||||
@@ -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)
|
||||
|
||||
|
||||
@@ -62,6 +62,10 @@ func OnBaseOptionsChange(oOpts, nOpts interface{}) bool {
|
||||
if oldOpts.ApiServer != newOpts.ApiServer {
|
||||
log.Debugf("api_server changed from %s to %s", oldOpts.ApiServer, newOpts.ApiServer)
|
||||
}
|
||||
|
||||
if oldOpts.EnableChangeOwnerAutoRename != newOpts.EnableChangeOwnerAutoRename {
|
||||
consts.SetChangeOwnerAutoRename(newOpts.EnableChangeOwnerAutoRename)
|
||||
}
|
||||
return changed
|
||||
}
|
||||
|
||||
|
||||
@@ -119,6 +119,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 (
|
||||
|
||||
Reference in New Issue
Block a user