display_info.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 session
  15. import (
  16. "context"
  17. "net/url"
  18. "yunion.io/x/pkg/errors"
  19. compute_api "yunion.io/x/onecloud/pkg/apis/compute"
  20. identity_api "yunion.io/x/onecloud/pkg/apis/identity"
  21. "yunion.io/x/onecloud/pkg/mcclient"
  22. "yunion.io/x/onecloud/pkg/mcclient/auth"
  23. "yunion.io/x/onecloud/pkg/mcclient/modules/compute"
  24. "yunion.io/x/onecloud/pkg/mcclient/modules/identity"
  25. "yunion.io/x/onecloud/pkg/webconsole/options"
  26. )
  27. type SDisplayInfo struct {
  28. WaterMark string `json:"water_mark"`
  29. InstanceName string `json:"instance_name"`
  30. Ips string `json:"ips"`
  31. Hypervisor string `json:"hypervisor"`
  32. OsType string `json:"os_type"`
  33. OsName string `json:"os_name"`
  34. OsArch string `json:"os_arch"`
  35. OsDistribution string `json:"os_distribution"`
  36. SecretLevel string `json:"secret_level"`
  37. }
  38. func (dispInfo *SDisplayInfo) fetchGuestInfo(guestDetails *compute_api.ServerDetails) {
  39. dispInfo.Hypervisor = guestDetails.Hypervisor
  40. dispInfo.OsName = guestDetails.OsName
  41. dispInfo.OsType = guestDetails.OsType
  42. dispInfo.OsArch = guestDetails.OsArch
  43. dispInfo.OsDistribution = guestDetails.Metadata[compute_api.VM_METADATA_OS_DISTRO]
  44. dispInfo.InstanceName = guestDetails.Name
  45. dispInfo.Ips = guestDetails.IPs
  46. dispInfo.SecretLevel = guestDetails.Metadata["cls:secret_level"]
  47. }
  48. func (dispInfo *SDisplayInfo) fetchHostInfo(hostDetails *compute_api.HostDetails) {
  49. dispInfo.Hypervisor = hostDetails.HostType
  50. dispInfo.OsArch = hostDetails.CpuArchitecture
  51. dispInfo.InstanceName = hostDetails.Name
  52. dispInfo.Ips = hostDetails.AccessIp
  53. }
  54. func (dispInfo *SDisplayInfo) populateParams(params url.Values) url.Values {
  55. if options.Options.EnableWatermark && len(dispInfo.WaterMark) > 0 {
  56. params["water_mark"] = []string{dispInfo.WaterMark}
  57. }
  58. if len(dispInfo.InstanceName) > 0 {
  59. params["instance_name"] = []string{dispInfo.InstanceName}
  60. }
  61. if len(dispInfo.Ips) > 0 {
  62. params["ips"] = []string{dispInfo.Ips}
  63. }
  64. if len(dispInfo.Hypervisor) > 0 {
  65. params["hypervisor"] = []string{dispInfo.Hypervisor}
  66. }
  67. if len(dispInfo.OsType) > 0 {
  68. params["os_type"] = []string{dispInfo.OsType}
  69. }
  70. if len(dispInfo.OsName) > 0 {
  71. params["os_name"] = []string{dispInfo.OsName}
  72. }
  73. if len(dispInfo.OsArch) > 0 {
  74. params["os_arch"] = []string{dispInfo.OsArch}
  75. }
  76. if len(dispInfo.OsDistribution) > 0 {
  77. params["os_distribution"] = []string{dispInfo.OsDistribution}
  78. }
  79. if len(dispInfo.SecretLevel) > 0 {
  80. params["secret_level"] = []string{dispInfo.SecretLevel}
  81. }
  82. return params
  83. }
  84. func fetchWaterMark(userInfo *identity_api.UserDetails) string {
  85. info := userInfo.Name
  86. if len(userInfo.Displayname) > 0 {
  87. info += " (" + userInfo.Displayname + ")"
  88. }
  89. info += "<br/>"
  90. if len(userInfo.Mobile) > 0 {
  91. info += userInfo.Mobile
  92. } else if len(userInfo.Email) > 0 {
  93. info += userInfo.Email
  94. } else {
  95. info += userInfo.Id
  96. }
  97. return info
  98. }
  99. func fetchUserInfo(ctx context.Context, s *mcclient.ClientSession) (*identity_api.UserDetails, error) {
  100. usrObj, err := identity.UsersV3.GetById(auth.GetAdminSession(ctx, s.GetRegion()), s.GetUserId(), nil)
  101. if err != nil {
  102. return nil, errors.Wrap(err, "GetById")
  103. }
  104. usr := identity_api.UserDetails{}
  105. err = usrObj.Unmarshal(&usr)
  106. if err != nil {
  107. return nil, errors.Wrap(err, "Unmarshal")
  108. }
  109. return &usr, nil
  110. }
  111. func FetchServerInfo(ctx context.Context, s *mcclient.ClientSession, sid string) (*compute_api.ServerDetails, error) {
  112. guestInfo, err := compute.Servers.Get(s, sid, nil)
  113. if err != nil {
  114. return nil, errors.Wrapf(err, "GetById %s", sid)
  115. }
  116. guestDetails := compute_api.ServerDetails{}
  117. err = guestInfo.Unmarshal(&guestDetails)
  118. if err != nil {
  119. return nil, errors.Wrap(err, "Unmarshal guest info")
  120. }
  121. return &guestDetails, nil
  122. }
  123. func FetchHostInfo(ctx context.Context, s *mcclient.ClientSession, id string) (*compute_api.HostDetails, error) {
  124. hostInfo, err := compute.Hosts.Get(s, id, nil)
  125. if err != nil {
  126. return nil, errors.Wrapf(err, "GetById %s", id)
  127. }
  128. hostDetails := compute_api.HostDetails{}
  129. err = hostInfo.Unmarshal(&hostDetails)
  130. if err != nil {
  131. return nil, errors.Wrap(err, "Unmarshal guest info")
  132. }
  133. return &hostDetails, nil
  134. }