From ad2a12eb31fd85454ae380cf1d0c59e6ba1b4677 Mon Sep 17 00:00:00 2001 From: Qiu Jian Date: Mon, 5 Dec 2022 09:52:18 +0800 Subject: [PATCH] feature: allow update hostname --- cmd/climc/shell/compute/hosts.go | 2 ++ pkg/apis/compute/guests.go | 2 ++ pkg/apis/compute/host.go | 1 + pkg/compute/models/guests.go | 7 +++++++ pkg/compute/models/hostnameresource.go | 4 ++-- pkg/compute/models/hosts.go | 7 +++++++ pkg/mcclient/options/compute/servers.go | 2 ++ 7 files changed, 23 insertions(+), 2 deletions(-) diff --git a/cmd/climc/shell/compute/hosts.go b/cmd/climc/shell/compute/hosts.go index f0043333e2..68da5506a3 100644 --- a/cmd/climc/shell/compute/hosts.go +++ b/cmd/climc/shell/compute/hosts.go @@ -174,6 +174,8 @@ func init() { IpmiIpAddr string `help:"IPMI ip_addr"` Sn string `help:"serial number"` + + Hostname string `help:"update host name"` } R(&HostUpdateOptions{}, "host-update", "Update information of a host", func(s *mcclient.ClientSession, args *HostUpdateOptions) error { params := jsonutils.NewDict() diff --git a/pkg/apis/compute/guests.go b/pkg/apis/compute/guests.go index fd9cb3041b..fe04acab1b 100644 --- a/pkg/apis/compute/guests.go +++ b/pkg/apis/compute/guests.go @@ -730,6 +730,8 @@ type ServerChangeConfigInput struct { type ServerUpdateInput struct { apis.VirtualResourceBaseUpdateInput + HostnameInput + // 删除保护开关 DisableDelete *bool `json:"disable_delete"` // 启动顺序 diff --git a/pkg/apis/compute/host.go b/pkg/apis/compute/host.go index c972a496ca..09ec50d60f 100644 --- a/pkg/apis/compute/host.go +++ b/pkg/apis/compute/host.go @@ -457,6 +457,7 @@ type HostUpdateInput struct { HostAccessAttributes HostSizeAttributes HostIpmiAttributes + HostnameInput // IPMI info IpmiInfo jsonutils.JSONObject `json:"ipmi_info"` diff --git a/pkg/compute/models/guests.go b/pkg/compute/models/guests.go index b6d163a3d9..bd27939971 100644 --- a/pkg/compute/models/guests.go +++ b/pkg/compute/models/guests.go @@ -1137,6 +1137,13 @@ func (self *SGuest) ValidateUpdateData(ctx context.Context, userCred mcclient.To return input, httperrors.NewInputParameterError("name is too short") } + // validate Hostname + if len(input.Hostname) > 0 { + if !regutils.MatchDomainName(input.Hostname) { + return input, httperrors.NewInputParameterError("hostname should be a legal domain name") + } + } + var err error input, err = self.GetDriver().ValidateUpdateData(ctx, self, userCred, input) if err != nil { diff --git a/pkg/compute/models/hostnameresource.go b/pkg/compute/models/hostnameresource.go index 2898a908f5..22ae4132c7 100644 --- a/pkg/compute/models/hostnameresource.go +++ b/pkg/compute/models/hostnameresource.go @@ -26,7 +26,7 @@ import ( ) type SHostnameResourceBase struct { - Hostname string `width:"60" charset:"ascii" nullable:"true" list:"user" create:"optional"` + Hostname string `width:"60" charset:"ascii" nullable:"true" list:"user" create:"optional" update:"user"` } type SHostnameResourceBaseManager struct { @@ -59,7 +59,7 @@ func (manager *SHostnameResourceBaseManager) ValidateHostname(name string, osTyp if len(input.Hostname) > 60 { input.Hostname = input.Hostname[:60] } - if strings.ToLower(osType) == strings.ToLower(osprofile.OS_TYPE_WINDOWS) { + if strings.EqualFold(osType, osprofile.OS_TYPE_WINDOWS) { if num, err := strconv.Atoi(input.Hostname); err == nil && num > 0 { return input, httperrors.NewInputParameterError("hostname cannot be number %d", num) } diff --git a/pkg/compute/models/hosts.go b/pkg/compute/models/hosts.go index 2499872fb7..f8a85c6fb1 100644 --- a/pkg/compute/models/hosts.go +++ b/pkg/compute/models/hosts.go @@ -3675,6 +3675,13 @@ func (manager *SHostManager) ValidateCreateData( } func (self *SHost) ValidateUpdateData(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, input api.HostUpdateInput) (api.HostUpdateInput, error) { + // validate Hostname + if len(input.Hostname) > 0 { + if !regutils.MatchDomainName(input.Hostname) { + return input, httperrors.NewInputParameterError("hostname should be a legal domain name") + } + } + var err error input.HostAccessAttributes, err = HostManager.inputUniquenessCheck(input.HostAccessAttributes, self.ZoneId, self.Id) if err != nil { diff --git a/pkg/mcclient/options/compute/servers.go b/pkg/mcclient/options/compute/servers.go index 8ac470f4b8..97262791f6 100644 --- a/pkg/mcclient/options/compute/servers.go +++ b/pkg/mcclient/options/compute/servers.go @@ -642,6 +642,8 @@ type ServerUpdateOptions struct { IsDaemon *bool `help:"Daemon server" negative:"no-daemon"` PendingDeletedAt string `help:"change pending deleted time"` + + Hostname string `help:"host name of server"` } func (opts *ServerUpdateOptions) Params() (jsonutils.JSONObject, error) {