zone.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 proxmox
  15. import (
  16. "net/url"
  17. "yunion.io/x/pkg/errors"
  18. api "yunion.io/x/cloudmux/pkg/apis/compute"
  19. "yunion.io/x/cloudmux/pkg/cloudprovider"
  20. "yunion.io/x/cloudmux/pkg/multicloud"
  21. )
  22. type SZone struct {
  23. multicloud.SResourceBase
  24. ProxmoxTags
  25. region *SRegion
  26. Name string
  27. Nodes int
  28. Quorate int
  29. Version int
  30. }
  31. type ClusterStatus struct {
  32. Nodes int `json:"nodes,omitempty"`
  33. Quorate int `json:"quorate,omitempty"`
  34. Version int `json:"version,omitempty"`
  35. Type string `json:"type"`
  36. ID string `json:"id"`
  37. Name string `json:"name"`
  38. Online int `json:"online,omitempty"`
  39. Level string `json:"level,omitempty"`
  40. Nodeid int `json:"nodeid,omitempty"`
  41. Local int `json:"local,omitempty"`
  42. IP string `json:"ip,omitempty"`
  43. }
  44. func (self *SZone) GetId() string {
  45. return self.Name
  46. }
  47. func (self *SZone) GetGlobalId() string {
  48. return self.Name
  49. }
  50. func (self *SZone) GetName() string {
  51. return self.Name
  52. }
  53. func (self *SZone) GetIRegion() cloudprovider.ICloudRegion {
  54. return self.region
  55. }
  56. func (self *SZone) GetStatus() string {
  57. return api.ZONE_ENABLE
  58. }
  59. func (self *SZone) GetI18n() cloudprovider.SModelI18nTable {
  60. table := cloudprovider.SModelI18nTable{}
  61. table["name"] = cloudprovider.NewSModelI18nEntry(self.GetName()).CN(self.GetName())
  62. return table
  63. }
  64. func (self *SZone) GetIHostById(id string) (cloudprovider.ICloudHost, error) {
  65. host, err := self.region.GetHost(id)
  66. if err != nil {
  67. return nil, err
  68. }
  69. host.zone = self
  70. return host, nil
  71. }
  72. func (self *SZone) GetIHosts() ([]cloudprovider.ICloudHost, error) {
  73. hosts, err := self.region.GetHosts()
  74. if err != nil {
  75. return nil, err
  76. }
  77. ret := []cloudprovider.ICloudHost{}
  78. for i := range hosts {
  79. hosts[i].zone = self
  80. ret = append(ret, &hosts[i])
  81. }
  82. return ret, nil
  83. }
  84. func (self *SZone) GetIStorages() ([]cloudprovider.ICloudStorage, error) {
  85. storages, err := self.region.GetStorages()
  86. if err != nil {
  87. return nil, err
  88. }
  89. ret := []cloudprovider.ICloudStorage{}
  90. for i := range storages {
  91. storages[i].zone = self
  92. ret = append(ret, &storages[i])
  93. }
  94. return ret, nil
  95. }
  96. func (self *SZone) GetIStorageById(id string) (cloudprovider.ICloudStorage, error) {
  97. storage, err := self.region.GetStorage(id)
  98. if err != nil {
  99. return nil, errors.Wrapf(err, "GetStorage %s", id)
  100. }
  101. storage.zone = self
  102. return storage, nil
  103. }
  104. func (self *SRegion) GetZone() (*SZone, error) {
  105. ret := &SZone{region: self}
  106. css := []ClusterStatus{}
  107. noneClusterMsg := true
  108. nodeNum := 0
  109. err := self.get("/cluster/status", url.Values{}, &css)
  110. if err != nil {
  111. return nil, err
  112. }
  113. for _, cs := range css {
  114. if cs.Type == "cluster" {
  115. ret.Name = cs.Name
  116. ret.Nodes = cs.Nodes
  117. ret.Quorate = cs.Quorate
  118. ret.Version = cs.Version
  119. noneClusterMsg = false
  120. break
  121. } else {
  122. nodeNum++
  123. }
  124. }
  125. if noneClusterMsg == true {
  126. ret.Name = "cluster"
  127. ret.Nodes = nodeNum
  128. }
  129. return ret, nil
  130. }