hostnameresource.go 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package models
  15. import (
  16. "strconv"
  17. "strings"
  18. "yunion.io/x/pkg/util/osprofile"
  19. "yunion.io/x/pkg/util/pinyinutils"
  20. api "yunion.io/x/onecloud/pkg/apis/compute"
  21. "yunion.io/x/onecloud/pkg/httperrors"
  22. )
  23. type SHostnameResourceBase struct {
  24. Hostname string `width:"128" charset:"ascii" nullable:"true" list:"user" create:"optional" update:"user"`
  25. }
  26. type SHostnameResourceBaseManager struct {
  27. }
  28. func (manager *SHostnameResourceBaseManager) ValidateHostname(name string, osType string, input api.HostnameInput) (api.HostnameInput, error) {
  29. inputHostname := true
  30. if len(input.Hostname) == 0 {
  31. inputHostname = false
  32. if len(name) == 0 {
  33. return input, httperrors.NewMissingParameterError("name")
  34. }
  35. input.Hostname = pinyinutils.Text2Pinyin(name)
  36. }
  37. hostname := ""
  38. for _, s := range input.Hostname {
  39. if (s >= '0' && s <= '9') || (s >= 'a' && s <= 'z') || (s >= 'A' && s <= 'Z') || strings.Contains(".-", string(s)) {
  40. hostname += string(s)
  41. }
  42. }
  43. input.Hostname = hostname
  44. for strings.HasPrefix(input.Hostname, ".") || strings.HasPrefix(input.Hostname, "-") ||
  45. strings.HasSuffix(input.Hostname, ".") || strings.HasSuffix(input.Hostname, "-") ||
  46. strings.Contains(input.Hostname, "..") || strings.Contains(input.Hostname, "--") {
  47. input.Hostname = strings.TrimPrefix(input.Hostname, ".")
  48. input.Hostname = strings.TrimPrefix(input.Hostname, "-")
  49. input.Hostname = strings.TrimSuffix(input.Hostname, ".")
  50. input.Hostname = strings.TrimSuffix(input.Hostname, "-")
  51. input.Hostname = strings.ReplaceAll(input.Hostname, "--", "")
  52. input.Hostname = strings.ReplaceAll(input.Hostname, "..", "")
  53. }
  54. if len(input.Hostname) > 60 {
  55. input.Hostname = input.Hostname[:60]
  56. }
  57. if strings.EqualFold(osType, osprofile.OS_TYPE_WINDOWS) {
  58. if num, err := strconv.Atoi(input.Hostname); err == nil && num > 0 {
  59. return input, httperrors.NewInputParameterError("hostname cannot be number %d", num)
  60. }
  61. input.Hostname = strings.ReplaceAll(input.Hostname, ".", "")
  62. if len(input.Hostname) > api.MAX_WINDOWS_COMPUTER_NAME_LENGTH {
  63. if inputHostname {
  64. return input, httperrors.NewInputParameterError("Windows hostname cannot be longer than %d characters", api.MAX_WINDOWS_COMPUTER_NAME_LENGTH)
  65. }
  66. input.Hostname = input.Hostname[:15]
  67. }
  68. }
  69. for strings.HasSuffix(input.Hostname, "-") {
  70. input.Hostname = strings.TrimSuffix(input.Hostname, "-")
  71. }
  72. if len(input.Hostname) < 2 {
  73. return input, httperrors.NewInputParameterError("the hostname length must be greater than or equal to 2")
  74. }
  75. return input, nil
  76. }