cluster.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. "fmt"
  17. "net/url"
  18. "sort"
  19. )
  20. type SClusterResource struct {
  21. Maxcpu int `json:"maxcpu,omitempty"`
  22. Uptime int `json:"uptime,omitempty"`
  23. Template int `json:"template,omitempty"`
  24. Netin int `json:"netin,omitempty"`
  25. Mem int `json:"mem,omitempty"`
  26. Node string `json:"node"`
  27. VmId int `json:"vmid,omitempty"`
  28. Maxdisk int64 `json:"maxdisk"`
  29. Netout int `json:"netout,omitempty"`
  30. Diskwrite int `json:"diskwrite,omitempty"`
  31. Diskread int `json:"diskread,omitempty"`
  32. Maxmem int64 `json:"maxmem,omitempty"`
  33. Disk int `json:"disk"`
  34. CPU int `json:"cpu,omitempty"`
  35. Id string `json:"id"`
  36. Type string `json:"type"`
  37. Status string `json:"status"`
  38. Name string `json:"name,omitempty"`
  39. Level string `json:"level,omitempty"`
  40. Storage string `json:"storage,omitempty"`
  41. Plugintype string `json:"plugintype,omitempty"`
  42. Content string `json:"content,omitempty"`
  43. Shared int `json:"shared,omitempty"`
  44. }
  45. type SStorageResource struct {
  46. Id string
  47. Path string
  48. Node string
  49. Name string
  50. Shared int
  51. Content string
  52. }
  53. type SNodeResource struct {
  54. Id string
  55. Node string
  56. }
  57. type SVmResource struct {
  58. VmId int
  59. Id string
  60. Name string
  61. Node string
  62. NodeId string
  63. Status string
  64. Template bool
  65. }
  66. func (self *SRegion) GetClusterAllResources() ([]SClusterResource, error) {
  67. resources := []SClusterResource{}
  68. err := self.get("/cluster/resources", url.Values{}, &resources)
  69. return resources, err
  70. }
  71. func (self *SRegion) GetClusterResources(resType string) ([]SClusterResource, error) {
  72. resources := []SClusterResource{}
  73. params := url.Values{}
  74. if len(resType) > 0 {
  75. params.Set("type", resType)
  76. }
  77. err := self.get("/cluster/resources", params, &resources)
  78. return resources, err
  79. }
  80. func (self *SRegion) GetClusterNodeResources() (map[string]SNodeResource, error) {
  81. resources := []SClusterResource{}
  82. nodeResources := map[string]SNodeResource{}
  83. params := url.Values{}
  84. params.Set("type", "node")
  85. err := self.get("/cluster/resources", params, &resources)
  86. if err != nil {
  87. return nil, err
  88. }
  89. for _, res := range resources {
  90. if res.Type == "node" {
  91. nres := SNodeResource{
  92. Id: res.Id,
  93. Node: res.Node,
  94. }
  95. nodeResources[nres.Id] = nres
  96. }
  97. }
  98. return nodeResources, nil
  99. }
  100. func (self *SRegion) GetClusterVmResources() (map[int]SVmResource, error) {
  101. resources := []SClusterResource{}
  102. VmResources := map[int]SVmResource{}
  103. err := self.get("/cluster/resources", url.Values{}, &resources)
  104. if err != nil {
  105. return nil, err
  106. }
  107. for _, res := range resources {
  108. if res.Type == "qemu" {
  109. vres := SVmResource{
  110. VmId: res.VmId,
  111. Id: res.Id,
  112. Name: res.Name,
  113. Node: res.Node,
  114. NodeId: fmt.Sprintf("node/%s", res.Node),
  115. Status: res.Status,
  116. Template: Itob(res.Template),
  117. }
  118. VmResources[vres.VmId] = vres
  119. }
  120. }
  121. return VmResources, nil
  122. }
  123. func (self *SRegion) GetClusterVmMaxId() int {
  124. resources := []SClusterResource{}
  125. idxs := []int{}
  126. err := self.get("/cluster/resources", url.Values{}, &resources)
  127. if err != nil {
  128. return -1
  129. }
  130. for i := range resources {
  131. if resources[i].Type == "qemu" {
  132. idxs = append(idxs, resources[i].VmId)
  133. }
  134. }
  135. if len(idxs) < 1 {
  136. return 99
  137. }
  138. sort.Ints(idxs)
  139. return idxs[len(idxs)-1]
  140. }