mod_servers.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 compute
  15. import (
  16. "strings"
  17. "yunion.io/x/jsonutils"
  18. "yunion.io/x/pkg/gotypes"
  19. "yunion.io/x/pkg/utils"
  20. "yunion.io/x/onecloud/pkg/httperrors"
  21. "yunion.io/x/onecloud/pkg/mcclient"
  22. "yunion.io/x/onecloud/pkg/mcclient/modulebase"
  23. "yunion.io/x/onecloud/pkg/mcclient/modules"
  24. "yunion.io/x/onecloud/pkg/util/seclib2"
  25. )
  26. type ServerManager struct {
  27. modulebase.ResourceManager
  28. }
  29. func (this *ServerManager) GetLoginInfo(s *mcclient.ClientSession, id string, params jsonutils.JSONObject) (jsonutils.JSONObject, error) {
  30. data, e := this.Get(s, id, nil)
  31. if e != nil {
  32. return nil, e
  33. }
  34. ret := jsonutils.NewDict()
  35. v, e := data.Get("metadata", "login_account")
  36. if e == nil {
  37. ret.Add(v, "username")
  38. }
  39. v, e = data.Get("metadata", "login_key_timestamp")
  40. if e == nil {
  41. ret.Add(v, "updated")
  42. }
  43. loginKey, e := data.GetString("metadata", "login_key")
  44. if e != nil {
  45. return nil, httperrors.NewNotFoundError("No login key: %s", e)
  46. }
  47. if len(loginKey) > 0 {
  48. ret.Add(jsonutils.NewString(loginKey), "login_key")
  49. var passwd string
  50. keypairId, _ := data.GetString("keypair_id")
  51. if len(keypairId) > 0 && !strings.EqualFold(keypairId, "none") {
  52. keypair, e := data.Get("keypair")
  53. if e == nil {
  54. ret.Add(keypair, "keypair")
  55. }
  56. if params != nil && !gotypes.IsNil(params) {
  57. privateKey, _ := params.GetString("private_key")
  58. privateKey = strings.TrimSpace(privateKey)
  59. if len(privateKey) > 0 {
  60. passwd, e = seclib2.DecryptBase64(privateKey, loginKey)
  61. if e != nil {
  62. return nil, e
  63. }
  64. }
  65. }
  66. } else {
  67. passwd, e = utils.DescryptAESBase64(id, loginKey)
  68. if e != nil {
  69. return nil, e
  70. }
  71. }
  72. if len(passwd) > 0 {
  73. ret.Add(jsonutils.NewString(passwd), "password")
  74. }
  75. }
  76. return ret, nil
  77. }
  78. func (this *ServerManager) DoChangeSetting(s *mcclient.ClientSession, id string, params jsonutils.JSONObject) (jsonutils.JSONObject, error) {
  79. return this.Update(s, id, params)
  80. }
  81. var (
  82. Servers ServerManager
  83. )
  84. func init() {
  85. Servers = ServerManager{modules.NewComputeManager("server", "servers",
  86. []string{"ID", "Name", "Billing_type",
  87. "IPs", "EIP", "Disk", "Status",
  88. "vcpu_count", "vmem_size",
  89. "ext_bw", "Zone_name",
  90. "Secgroup", "Secgrp_id",
  91. "vrouter", "vrouter_id",
  92. "Created_at", "Group_name",
  93. "Group_id", "Hypervisor", "os_type",
  94. "expired_at"},
  95. []string{"Host", "Tenant", "is_system", "auto_delete_at", "backup_host_name"})}
  96. modules.RegisterCompute(&Servers)
  97. }