HypervisorService.go 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. //
  2. // Copyright 2016, Sander van Harmelen
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. //
  16. package cloudstack
  17. import (
  18. "encoding/json"
  19. "fmt"
  20. "net/url"
  21. "strconv"
  22. "strings"
  23. )
  24. type ListHypervisorsParams struct {
  25. p map[string]interface{}
  26. }
  27. func (p *ListHypervisorsParams) toURLValues() url.Values {
  28. u := url.Values{}
  29. if p.p == nil {
  30. return u
  31. }
  32. if v, found := p.p["zoneid"]; found {
  33. u.Set("zoneid", v.(string))
  34. }
  35. return u
  36. }
  37. func (p *ListHypervisorsParams) SetZoneid(v string) {
  38. if p.p == nil {
  39. p.p = make(map[string]interface{})
  40. }
  41. p.p["zoneid"] = v
  42. return
  43. }
  44. // You should always use this function to get a new ListHypervisorsParams instance,
  45. // as then you are sure you have configured all required params
  46. func (s *HypervisorService) NewListHypervisorsParams() *ListHypervisorsParams {
  47. p := &ListHypervisorsParams{}
  48. p.p = make(map[string]interface{})
  49. return p
  50. }
  51. // List hypervisors
  52. func (s *HypervisorService) ListHypervisors(p *ListHypervisorsParams) (*ListHypervisorsResponse, error) {
  53. resp, err := s.cs.newRequest("listHypervisors", p.toURLValues())
  54. if err != nil {
  55. return nil, err
  56. }
  57. var r ListHypervisorsResponse
  58. if err := json.Unmarshal(resp, &r); err != nil {
  59. return nil, err
  60. }
  61. return &r, nil
  62. }
  63. type ListHypervisorsResponse struct {
  64. Count int `json:"count"`
  65. Hypervisors []*Hypervisor `json:"hypervisor"`
  66. }
  67. type Hypervisor struct {
  68. Name string `json:"name,omitempty"`
  69. }
  70. type UpdateHypervisorCapabilitiesParams struct {
  71. p map[string]interface{}
  72. }
  73. func (p *UpdateHypervisorCapabilitiesParams) toURLValues() url.Values {
  74. u := url.Values{}
  75. if p.p == nil {
  76. return u
  77. }
  78. if v, found := p.p["id"]; found {
  79. u.Set("id", v.(string))
  80. }
  81. if v, found := p.p["maxguestslimit"]; found {
  82. vv := strconv.FormatInt(v.(int64), 10)
  83. u.Set("maxguestslimit", vv)
  84. }
  85. if v, found := p.p["securitygroupenabled"]; found {
  86. vv := strconv.FormatBool(v.(bool))
  87. u.Set("securitygroupenabled", vv)
  88. }
  89. return u
  90. }
  91. func (p *UpdateHypervisorCapabilitiesParams) SetId(v string) {
  92. if p.p == nil {
  93. p.p = make(map[string]interface{})
  94. }
  95. p.p["id"] = v
  96. return
  97. }
  98. func (p *UpdateHypervisorCapabilitiesParams) SetMaxguestslimit(v int64) {
  99. if p.p == nil {
  100. p.p = make(map[string]interface{})
  101. }
  102. p.p["maxguestslimit"] = v
  103. return
  104. }
  105. func (p *UpdateHypervisorCapabilitiesParams) SetSecuritygroupenabled(v bool) {
  106. if p.p == nil {
  107. p.p = make(map[string]interface{})
  108. }
  109. p.p["securitygroupenabled"] = v
  110. return
  111. }
  112. // You should always use this function to get a new UpdateHypervisorCapabilitiesParams instance,
  113. // as then you are sure you have configured all required params
  114. func (s *HypervisorService) NewUpdateHypervisorCapabilitiesParams() *UpdateHypervisorCapabilitiesParams {
  115. p := &UpdateHypervisorCapabilitiesParams{}
  116. p.p = make(map[string]interface{})
  117. return p
  118. }
  119. // Updates a hypervisor capabilities.
  120. func (s *HypervisorService) UpdateHypervisorCapabilities(p *UpdateHypervisorCapabilitiesParams) (*UpdateHypervisorCapabilitiesResponse, error) {
  121. resp, err := s.cs.newRequest("updateHypervisorCapabilities", p.toURLValues())
  122. if err != nil {
  123. return nil, err
  124. }
  125. var r UpdateHypervisorCapabilitiesResponse
  126. if err := json.Unmarshal(resp, &r); err != nil {
  127. return nil, err
  128. }
  129. return &r, nil
  130. }
  131. type UpdateHypervisorCapabilitiesResponse struct {
  132. Hypervisor string `json:"hypervisor,omitempty"`
  133. Hypervisorversion string `json:"hypervisorversion,omitempty"`
  134. Id string `json:"id,omitempty"`
  135. Maxdatavolumeslimit int `json:"maxdatavolumeslimit,omitempty"`
  136. Maxguestslimit int64 `json:"maxguestslimit,omitempty"`
  137. Maxhostspercluster int `json:"maxhostspercluster,omitempty"`
  138. Securitygroupenabled bool `json:"securitygroupenabled,omitempty"`
  139. Storagemotionenabled bool `json:"storagemotionenabled,omitempty"`
  140. }
  141. type ListHypervisorCapabilitiesParams struct {
  142. p map[string]interface{}
  143. }
  144. func (p *ListHypervisorCapabilitiesParams) toURLValues() url.Values {
  145. u := url.Values{}
  146. if p.p == nil {
  147. return u
  148. }
  149. if v, found := p.p["hypervisor"]; found {
  150. u.Set("hypervisor", v.(string))
  151. }
  152. if v, found := p.p["id"]; found {
  153. u.Set("id", v.(string))
  154. }
  155. if v, found := p.p["keyword"]; found {
  156. u.Set("keyword", v.(string))
  157. }
  158. if v, found := p.p["page"]; found {
  159. vv := strconv.Itoa(v.(int))
  160. u.Set("page", vv)
  161. }
  162. if v, found := p.p["pagesize"]; found {
  163. vv := strconv.Itoa(v.(int))
  164. u.Set("pagesize", vv)
  165. }
  166. return u
  167. }
  168. func (p *ListHypervisorCapabilitiesParams) SetHypervisor(v string) {
  169. if p.p == nil {
  170. p.p = make(map[string]interface{})
  171. }
  172. p.p["hypervisor"] = v
  173. return
  174. }
  175. func (p *ListHypervisorCapabilitiesParams) SetId(v string) {
  176. if p.p == nil {
  177. p.p = make(map[string]interface{})
  178. }
  179. p.p["id"] = v
  180. return
  181. }
  182. func (p *ListHypervisorCapabilitiesParams) SetKeyword(v string) {
  183. if p.p == nil {
  184. p.p = make(map[string]interface{})
  185. }
  186. p.p["keyword"] = v
  187. return
  188. }
  189. func (p *ListHypervisorCapabilitiesParams) SetPage(v int) {
  190. if p.p == nil {
  191. p.p = make(map[string]interface{})
  192. }
  193. p.p["page"] = v
  194. return
  195. }
  196. func (p *ListHypervisorCapabilitiesParams) SetPagesize(v int) {
  197. if p.p == nil {
  198. p.p = make(map[string]interface{})
  199. }
  200. p.p["pagesize"] = v
  201. return
  202. }
  203. // You should always use this function to get a new ListHypervisorCapabilitiesParams instance,
  204. // as then you are sure you have configured all required params
  205. func (s *HypervisorService) NewListHypervisorCapabilitiesParams() *ListHypervisorCapabilitiesParams {
  206. p := &ListHypervisorCapabilitiesParams{}
  207. p.p = make(map[string]interface{})
  208. return p
  209. }
  210. // This is a courtesy helper function, which in some cases may not work as expected!
  211. func (s *HypervisorService) GetHypervisorCapabilityByID(id string, opts ...OptionFunc) (*HypervisorCapability, int, error) {
  212. p := &ListHypervisorCapabilitiesParams{}
  213. p.p = make(map[string]interface{})
  214. p.p["id"] = id
  215. for _, fn := range opts {
  216. if err := fn(s.cs, p); err != nil {
  217. return nil, -1, err
  218. }
  219. }
  220. l, err := s.ListHypervisorCapabilities(p)
  221. if err != nil {
  222. if strings.Contains(err.Error(), fmt.Sprintf(
  223. "Invalid parameter id value=%s due to incorrect long value format, "+
  224. "or entity does not exist", id)) {
  225. return nil, 0, fmt.Errorf("No match found for %s: %+v", id, l)
  226. }
  227. return nil, -1, err
  228. }
  229. if l.Count == 0 {
  230. return nil, l.Count, fmt.Errorf("No match found for %s: %+v", id, l)
  231. }
  232. if l.Count == 1 {
  233. return l.HypervisorCapabilities[0], l.Count, nil
  234. }
  235. return nil, l.Count, fmt.Errorf("There is more then one result for HypervisorCapability UUID: %s!", id)
  236. }
  237. // Lists all hypervisor capabilities.
  238. func (s *HypervisorService) ListHypervisorCapabilities(p *ListHypervisorCapabilitiesParams) (*ListHypervisorCapabilitiesResponse, error) {
  239. resp, err := s.cs.newRequest("listHypervisorCapabilities", p.toURLValues())
  240. if err != nil {
  241. return nil, err
  242. }
  243. var r ListHypervisorCapabilitiesResponse
  244. if err := json.Unmarshal(resp, &r); err != nil {
  245. return nil, err
  246. }
  247. return &r, nil
  248. }
  249. type ListHypervisorCapabilitiesResponse struct {
  250. Count int `json:"count"`
  251. HypervisorCapabilities []*HypervisorCapability `json:"hypervisorcapability"`
  252. }
  253. type HypervisorCapability struct {
  254. Hypervisor string `json:"hypervisor,omitempty"`
  255. Hypervisorversion string `json:"hypervisorversion,omitempty"`
  256. Id string `json:"id,omitempty"`
  257. Maxdatavolumeslimit int `json:"maxdatavolumeslimit,omitempty"`
  258. Maxguestslimit int64 `json:"maxguestslimit,omitempty"`
  259. Maxhostspercluster int `json:"maxhostspercluster,omitempty"`
  260. Securitygroupenabled bool `json:"securitygroupenabled,omitempty"`
  261. Storagemotionenabled bool `json:"storagemotionenabled,omitempty"`
  262. }