mirror of
https://github.com/yunionio/cloudpods.git
synced 2026-09-19 02:37:24 +08:00
fix: server change owner did not change disk's owner
This commit is contained in:
@@ -300,4 +300,21 @@ func init() {
|
||||
printObject(disk)
|
||||
return nil
|
||||
})
|
||||
|
||||
type DiskChangeOwnerOptions struct {
|
||||
ID string `help:"Disk to change owner" json:"-"`
|
||||
PROJECT string `help:"Project ID or change" json:"tenant"`
|
||||
}
|
||||
R(&DiskChangeOwnerOptions{}, "disk-change-owner", "Change owner porject of a disk", func(s *mcclient.ClientSession, opts *DiskChangeOwnerOptions) error {
|
||||
params, err := options.StructToParams(opts)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
srv, err := modules.Disks.PerformAction(s, opts.ID, "change-owner", params)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
printObject(srv)
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
@@ -535,21 +535,13 @@ func init() {
|
||||
})
|
||||
|
||||
type ServerChangeOwnerOptions struct {
|
||||
ID string `help:"Server to change owner"`
|
||||
PROJECT string `help:"Project ID or change"`
|
||||
RawId bool `help:"User raw ID, instead of name"`
|
||||
ID string `help:"Server to change owner" json:"-"`
|
||||
PROJECT string `help:"Project ID or change" json:"tenant"`
|
||||
}
|
||||
R(&ServerChangeOwnerOptions{}, "server-change-owner", "Change owner porject of a server", func(s *mcclient.ClientSession, opts *ServerChangeOwnerOptions) error {
|
||||
params := jsonutils.NewDict()
|
||||
if opts.RawId {
|
||||
projid, err := modules.Projects.GetId(s, opts.PROJECT, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
params.Add(jsonutils.NewString(projid), "tenant")
|
||||
params.Add(jsonutils.JSONTrue, "raw_id")
|
||||
} else {
|
||||
params.Add(jsonutils.NewString(opts.PROJECT), "tenant")
|
||||
params, err := options.StructToParams(opts)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
srv, err := modules.Servers.PerformAction(s, opts.ID, "change-owner", params)
|
||||
if err != nil {
|
||||
|
||||
@@ -19,9 +19,9 @@ import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
|
||||
"yunion.io/x/jsonutils"
|
||||
"yunion.io/x/log"
|
||||
"yunion.io/x/pkg/errors"
|
||||
"yunion.io/x/sqlchemy"
|
||||
|
||||
"yunion.io/x/onecloud/pkg/cloudcommon/consts"
|
||||
@@ -243,6 +243,7 @@ func FetchUserInfo(ctx context.Context, data jsonutils.JSONObject) (mcclient.IId
|
||||
}
|
||||
|
||||
func FetchProjectInfo(ctx context.Context, data jsonutils.JSONObject) (mcclient.IIdentityProvider, error) {
|
||||
log.Debugf("FetchProjectInfo %s", data)
|
||||
tenantId, key := jsonutils.GetAnyString2(data, []string{"project", "project_id", "tenant", "tenant_id"})
|
||||
if len(tenantId) > 0 {
|
||||
data.(*jsonutils.JSONDict).Remove(key)
|
||||
|
||||
@@ -119,15 +119,15 @@ func (manager *SStandaloneResourceBaseManager) ValidateName(name string) error {
|
||||
}
|
||||
|
||||
func (manager *SStandaloneResourceBaseManager) FetchById(idStr string) (IModel, error) {
|
||||
return FetchById(manager, idStr)
|
||||
return FetchById(manager.GetIStandaloneModelManager(), idStr)
|
||||
}
|
||||
|
||||
func (manager *SStandaloneResourceBaseManager) FetchByName(userCred mcclient.IIdentityProvider, idStr string) (IModel, error) {
|
||||
return FetchByName(manager, userCred, idStr)
|
||||
return FetchByName(manager.GetIStandaloneModelManager(), userCred, idStr)
|
||||
}
|
||||
|
||||
func (manager *SStandaloneResourceBaseManager) FetchByIdOrName(userCred mcclient.IIdentityProvider, idStr string) (IModel, error) {
|
||||
return FetchByIdOrName(manager, userCred, idStr)
|
||||
return FetchByIdOrName(manager.GetIStandaloneModelManager(), userCred, idStr)
|
||||
}
|
||||
|
||||
type STagValue struct {
|
||||
|
||||
@@ -317,7 +317,10 @@ func (model *SVirtualResourceBase) AllowPerformChangeOwner(ctx context.Context,
|
||||
}
|
||||
|
||||
func (model *SVirtualResourceBase) PerformChangeOwner(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, data jsonutils.JSONObject) (jsonutils.JSONObject, error) {
|
||||
ownerId, err := model.GetModelManager().FetchOwnerId(ctx, data)
|
||||
manager := model.GetModelManager()
|
||||
|
||||
log.Debugf("SVirtualResourceBase change_owner %s %s %#v", query, data, manager)
|
||||
ownerId, err := manager.FetchOwnerId(ctx, data)
|
||||
if err != nil {
|
||||
return nil, httperrors.NewGeneralError(err)
|
||||
}
|
||||
@@ -332,7 +335,6 @@ func (model *SVirtualResourceBase) PerformChangeOwner(ctx context.Context, userC
|
||||
})
|
||||
return nil, nil
|
||||
}
|
||||
manager := model.GetModelManager()
|
||||
q := manager.Query().Equals("name", model.GetName())
|
||||
q = manager.FilterByOwner(q, ownerId, manager.NamespaceScope())
|
||||
q = manager.FilterBySystemAttributes(q, nil, nil, manager.ResourceScope())
|
||||
|
||||
@@ -3446,3 +3446,19 @@ func (self *SGuest) PerformSyncFixNics(ctx context.Context,
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (guest *SGuest) PerformChangeOwner(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, data jsonutils.JSONObject) (jsonutils.JSONObject, error) {
|
||||
guestdisks := guest.GetDisks()
|
||||
for i := range guestdisks {
|
||||
disk := guestdisks[i].GetDisk()
|
||||
if disk == nil {
|
||||
return nil, httperrors.NewInternalServerError("some disk missing!!!")
|
||||
}
|
||||
dataCopy := jsonutils.DeepCopy(data)
|
||||
_, err := disk.PerformChangeOwner(ctx, userCred, query, dataCopy)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
return guest.SVirtualResourceBase.PerformChangeOwner(ctx, userCred, query, data)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user