zone.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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 zstack
  15. import (
  16. "strconv"
  17. "yunion.io/x/log"
  18. "yunion.io/x/pkg/util/fileutils"
  19. api "yunion.io/x/cloudmux/pkg/apis/compute"
  20. "yunion.io/x/cloudmux/pkg/cloudprovider"
  21. "yunion.io/x/cloudmux/pkg/multicloud"
  22. )
  23. type SZone struct {
  24. multicloud.SResourceBase
  25. ZStackTags
  26. region *SRegion
  27. ZStackBasic
  28. Type string
  29. State string
  30. cpuCmtbound float32
  31. memCmtbound float32
  32. reservedMemeoryMb int
  33. iwires []cloudprovider.ICloudWire
  34. istorages []cloudprovider.ICloudStorage
  35. ihosts []cloudprovider.ICloudHost
  36. }
  37. func (zone *SZone) GetId() string {
  38. return zone.Name
  39. }
  40. func (zone *SZone) GetName() string {
  41. return zone.Name
  42. }
  43. func (zone *SZone) GetI18n() cloudprovider.SModelI18nTable {
  44. table := cloudprovider.SModelI18nTable{}
  45. table["name"] = cloudprovider.NewSModelI18nEntry(zone.GetName()).CN(zone.GetName())
  46. return table
  47. }
  48. func (zone *SZone) GetGlobalId() string {
  49. return zone.GetId()
  50. }
  51. func (zone *SZone) IsEmulated() bool {
  52. return false
  53. }
  54. func (zone *SZone) GetStatus() string {
  55. if zone.State == "Enabled" {
  56. return api.ZONE_ENABLE
  57. }
  58. return api.ZONE_DISABLE
  59. }
  60. func (zone *SZone) Refresh() error {
  61. // do nothing
  62. return nil
  63. }
  64. func (zone *SZone) GetIRegion() cloudprovider.ICloudRegion {
  65. return zone.region
  66. }
  67. func (zone *SZone) fetchHostCmtbound() {
  68. if zone.cpuCmtbound > 0 || zone.memCmtbound > 0 || zone.reservedMemeoryMb > 0 {
  69. return
  70. }
  71. configurations, err := zone.region.GetConfigrations()
  72. if err != nil {
  73. log.Errorf("failed to get global configurations error: %v", err)
  74. return
  75. }
  76. for _, config := range configurations {
  77. if config.Name == "cpu.overProvisioning.ratio" && config.Category == "host" {
  78. if cpuCmtbound, err := strconv.ParseFloat(config.Value, 32); err == nil {
  79. zone.cpuCmtbound = float32(cpuCmtbound)
  80. }
  81. }
  82. if config.Name == "reservedMemory" && config.Category == "kvm" {
  83. zone.reservedMemeoryMb, _ = fileutils.GetSizeMb(config.Value, 'M', 1024)
  84. }
  85. if config.Name == "overProvisioning.memory" && config.Category == "mevoco" {
  86. if memCmtbound, err := strconv.ParseFloat(config.Value, 32); err == nil {
  87. zone.memCmtbound = float32(memCmtbound)
  88. }
  89. }
  90. }
  91. }
  92. func (zone *SZone) fetchStorages(clusterId string) error {
  93. storages, err := zone.region.getIStorages(zone.UUID)
  94. if err != nil {
  95. return err
  96. }
  97. zone.istorages = storages
  98. return nil
  99. }
  100. func (zone *SZone) GetIStorages() ([]cloudprovider.ICloudStorage, error) {
  101. if zone.istorages == nil || len(zone.istorages) == 0 {
  102. return zone.istorages, zone.fetchStorages("")
  103. }
  104. return zone.istorages, nil
  105. }
  106. func (zone *SZone) GetIStorageById(storageId string) (cloudprovider.ICloudStorage, error) {
  107. err := zone.fetchStorages("")
  108. if err != nil {
  109. return nil, err
  110. }
  111. for i := 0; i < len(zone.istorages); i++ {
  112. if zone.istorages[i].GetGlobalId() == storageId {
  113. return zone.istorages[i], nil
  114. }
  115. }
  116. return nil, cloudprovider.ErrNotFound
  117. }
  118. func (zone *SZone) GetIHostById(hostId string) (cloudprovider.ICloudHost, error) {
  119. host, err := zone.region.GetHost(hostId)
  120. if err != nil {
  121. return nil, err
  122. }
  123. if host.ZoneUUID != zone.UUID {
  124. return nil, cloudprovider.ErrNotFound
  125. }
  126. host.zone = zone
  127. return host, nil
  128. }
  129. func (zone *SZone) fetchHosts() error {
  130. hosts, err := zone.region.GetHosts(zone.UUID, "")
  131. if err != nil {
  132. return err
  133. }
  134. zone.ihosts = []cloudprovider.ICloudHost{}
  135. for i := 0; i < len(hosts); i++ {
  136. hosts[i].zone = zone
  137. zone.ihosts = append(zone.ihosts, &hosts[i])
  138. }
  139. return nil
  140. }
  141. func (zone *SZone) GetIHosts() ([]cloudprovider.ICloudHost, error) {
  142. if zone.ihosts == nil || len(zone.ihosts) == 0 {
  143. return zone.ihosts, zone.fetchHosts()
  144. }
  145. return zone.ihosts, nil
  146. }
  147. func (zone *SZone) GetIWires() ([]cloudprovider.ICloudWire, error) {
  148. if zone.iwires == nil || len(zone.iwires) == 0 {
  149. wires, err := zone.region.GetWires(zone.UUID, "", "")
  150. if err != nil {
  151. return nil, err
  152. }
  153. zone.iwires = []cloudprovider.ICloudWire{}
  154. for i := 0; i < len(wires); i++ {
  155. zone.iwires = append(zone.iwires, &wires[i])
  156. }
  157. }
  158. return zone.iwires, nil
  159. }