feat(region): set password use same interface

Signed-off-by: wanyaoqi <d3lx.yq@gmail.com>
This commit is contained in:
wanyaoqi
2022-09-06 15:51:29 +08:00
parent a4e9f86c2a
commit 10bf6e574f
6 changed files with 57 additions and 8 deletions
+31
View File
@@ -32,6 +32,7 @@ import (
"yunion.io/x/pkg/errors"
"yunion.io/x/pkg/tristate"
"yunion.io/x/pkg/util/fileutils"
"yunion.io/x/pkg/util/osprofile"
"yunion.io/x/pkg/util/regutils"
"yunion.io/x/pkg/util/sets"
"yunion.io/x/pkg/utils"
@@ -665,6 +666,36 @@ func (self *SGuest) GetDetailsCreateParams(ctx context.Context, userCred mcclien
return input.JSON(input), nil
}
func (self *SGuest) PerformSetPassword(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, input api.ServerSetPasswordInput) (jsonutils.JSONObject, error) {
switch self.Status {
case api.VM_RUNNING:
inputQga := &api.ServerQgaSetPasswordInput{
Username: input.Username,
Password: input.Password,
}
if inputQga.Username == "" {
if self.OsType == osprofile.OS_TYPE_WINDOWS {
inputQga.Username = api.VM_DEFAULT_WINDOWS_LOGIN_USER
} else {
inputQga.Username = api.VM_DEFAULT_LINUX_LOGIN_USER
}
}
if inputQga.Password == "" && input.ResetPassword {
inputQga.Password = seclib2.RandomPassword2(12)
}
return self.PerformQgaSetPassword(ctx, userCred, query, inputQga)
case api.VM_READY:
inputDeploy := api.ServerDeployInput{
Password: input.Password,
ResetPassword: input.ResetPassword,
AutoStart: input.AutoStart,
}
return self.PerformDeploy(ctx, userCred, query, inputDeploy)
default:
return nil, httperrors.NewServerStatusError("Cannot deploy in status %s", self.Status)
}
}
func (self *SGuest) saveOldPassword(ctx context.Context, userCred mcclient.TokenCredential) {
loginKey := self.GetMetadata(ctx, api.VM_METADATA_LOGIN_KEY, userCred)
if len(loginKey) > 0 {
@@ -59,7 +59,7 @@ func (self *GuestQgaSetPasswordTask) OnInit(ctx context.Context, obj db.IStandal
guest := obj.(*models.SGuest)
self.SetStage("OnQgaGuestPing", nil)
if err := self.guestPing(ctx, guest); err != nil {
self.OnQgaGuestPingFailed(ctx, guest, nil)
self.taskFailed(ctx, guest, err.Error())
}
}