ClusterService.go 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060
  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 AddClusterParams struct {
  25. p map[string]interface{}
  26. }
  27. func (p *AddClusterParams) toURLValues() url.Values {
  28. u := url.Values{}
  29. if p.p == nil {
  30. return u
  31. }
  32. if v, found := p.p["allocationstate"]; found {
  33. u.Set("allocationstate", v.(string))
  34. }
  35. if v, found := p.p["clustername"]; found {
  36. u.Set("clustername", v.(string))
  37. }
  38. if v, found := p.p["clustertype"]; found {
  39. u.Set("clustertype", v.(string))
  40. }
  41. if v, found := p.p["guestvswitchname"]; found {
  42. u.Set("guestvswitchname", v.(string))
  43. }
  44. if v, found := p.p["guestvswitchtype"]; found {
  45. u.Set("guestvswitchtype", v.(string))
  46. }
  47. if v, found := p.p["hypervisor"]; found {
  48. u.Set("hypervisor", v.(string))
  49. }
  50. if v, found := p.p["ovm3cluster"]; found {
  51. u.Set("ovm3cluster", v.(string))
  52. }
  53. if v, found := p.p["ovm3pool"]; found {
  54. u.Set("ovm3pool", v.(string))
  55. }
  56. if v, found := p.p["ovm3vip"]; found {
  57. u.Set("ovm3vip", v.(string))
  58. }
  59. if v, found := p.p["password"]; found {
  60. u.Set("password", v.(string))
  61. }
  62. if v, found := p.p["podid"]; found {
  63. u.Set("podid", v.(string))
  64. }
  65. if v, found := p.p["publicvswitchname"]; found {
  66. u.Set("publicvswitchname", v.(string))
  67. }
  68. if v, found := p.p["publicvswitchtype"]; found {
  69. u.Set("publicvswitchtype", v.(string))
  70. }
  71. if v, found := p.p["url"]; found {
  72. u.Set("url", v.(string))
  73. }
  74. if v, found := p.p["username"]; found {
  75. u.Set("username", v.(string))
  76. }
  77. if v, found := p.p["vsmipaddress"]; found {
  78. u.Set("vsmipaddress", v.(string))
  79. }
  80. if v, found := p.p["vsmpassword"]; found {
  81. u.Set("vsmpassword", v.(string))
  82. }
  83. if v, found := p.p["vsmusername"]; found {
  84. u.Set("vsmusername", v.(string))
  85. }
  86. if v, found := p.p["zoneid"]; found {
  87. u.Set("zoneid", v.(string))
  88. }
  89. return u
  90. }
  91. func (p *AddClusterParams) SetAllocationstate(v string) {
  92. if p.p == nil {
  93. p.p = make(map[string]interface{})
  94. }
  95. p.p["allocationstate"] = v
  96. return
  97. }
  98. func (p *AddClusterParams) SetClustername(v string) {
  99. if p.p == nil {
  100. p.p = make(map[string]interface{})
  101. }
  102. p.p["clustername"] = v
  103. return
  104. }
  105. func (p *AddClusterParams) SetClustertype(v string) {
  106. if p.p == nil {
  107. p.p = make(map[string]interface{})
  108. }
  109. p.p["clustertype"] = v
  110. return
  111. }
  112. func (p *AddClusterParams) SetGuestvswitchname(v string) {
  113. if p.p == nil {
  114. p.p = make(map[string]interface{})
  115. }
  116. p.p["guestvswitchname"] = v
  117. return
  118. }
  119. func (p *AddClusterParams) SetGuestvswitchtype(v string) {
  120. if p.p == nil {
  121. p.p = make(map[string]interface{})
  122. }
  123. p.p["guestvswitchtype"] = v
  124. return
  125. }
  126. func (p *AddClusterParams) SetHypervisor(v string) {
  127. if p.p == nil {
  128. p.p = make(map[string]interface{})
  129. }
  130. p.p["hypervisor"] = v
  131. return
  132. }
  133. func (p *AddClusterParams) SetOvm3cluster(v string) {
  134. if p.p == nil {
  135. p.p = make(map[string]interface{})
  136. }
  137. p.p["ovm3cluster"] = v
  138. return
  139. }
  140. func (p *AddClusterParams) SetOvm3pool(v string) {
  141. if p.p == nil {
  142. p.p = make(map[string]interface{})
  143. }
  144. p.p["ovm3pool"] = v
  145. return
  146. }
  147. func (p *AddClusterParams) SetOvm3vip(v string) {
  148. if p.p == nil {
  149. p.p = make(map[string]interface{})
  150. }
  151. p.p["ovm3vip"] = v
  152. return
  153. }
  154. func (p *AddClusterParams) SetPassword(v string) {
  155. if p.p == nil {
  156. p.p = make(map[string]interface{})
  157. }
  158. p.p["password"] = v
  159. return
  160. }
  161. func (p *AddClusterParams) SetPodid(v string) {
  162. if p.p == nil {
  163. p.p = make(map[string]interface{})
  164. }
  165. p.p["podid"] = v
  166. return
  167. }
  168. func (p *AddClusterParams) SetPublicvswitchname(v string) {
  169. if p.p == nil {
  170. p.p = make(map[string]interface{})
  171. }
  172. p.p["publicvswitchname"] = v
  173. return
  174. }
  175. func (p *AddClusterParams) SetPublicvswitchtype(v string) {
  176. if p.p == nil {
  177. p.p = make(map[string]interface{})
  178. }
  179. p.p["publicvswitchtype"] = v
  180. return
  181. }
  182. func (p *AddClusterParams) SetUrl(v string) {
  183. if p.p == nil {
  184. p.p = make(map[string]interface{})
  185. }
  186. p.p["url"] = v
  187. return
  188. }
  189. func (p *AddClusterParams) SetUsername(v string) {
  190. if p.p == nil {
  191. p.p = make(map[string]interface{})
  192. }
  193. p.p["username"] = v
  194. return
  195. }
  196. func (p *AddClusterParams) SetVsmipaddress(v string) {
  197. if p.p == nil {
  198. p.p = make(map[string]interface{})
  199. }
  200. p.p["vsmipaddress"] = v
  201. return
  202. }
  203. func (p *AddClusterParams) SetVsmpassword(v string) {
  204. if p.p == nil {
  205. p.p = make(map[string]interface{})
  206. }
  207. p.p["vsmpassword"] = v
  208. return
  209. }
  210. func (p *AddClusterParams) SetVsmusername(v string) {
  211. if p.p == nil {
  212. p.p = make(map[string]interface{})
  213. }
  214. p.p["vsmusername"] = v
  215. return
  216. }
  217. func (p *AddClusterParams) SetZoneid(v string) {
  218. if p.p == nil {
  219. p.p = make(map[string]interface{})
  220. }
  221. p.p["zoneid"] = v
  222. return
  223. }
  224. // You should always use this function to get a new AddClusterParams instance,
  225. // as then you are sure you have configured all required params
  226. func (s *ClusterService) NewAddClusterParams(clustername string, clustertype string, hypervisor string, podid string, zoneid string) *AddClusterParams {
  227. p := &AddClusterParams{}
  228. p.p = make(map[string]interface{})
  229. p.p["clustername"] = clustername
  230. p.p["clustertype"] = clustertype
  231. p.p["hypervisor"] = hypervisor
  232. p.p["podid"] = podid
  233. p.p["zoneid"] = zoneid
  234. return p
  235. }
  236. // Adds a new cluster
  237. func (s *ClusterService) AddCluster(p *AddClusterParams) (*AddClusterResponse, error) {
  238. resp, err := s.cs.newRequest("addCluster", p.toURLValues())
  239. if err != nil {
  240. return nil, err
  241. }
  242. var r AddClusterResponse
  243. if err := json.Unmarshal(resp, &r); err != nil {
  244. return nil, err
  245. }
  246. return &r, nil
  247. }
  248. type AddClusterResponse struct {
  249. Allocationstate string `json:"allocationstate,omitempty"`
  250. Capacity []struct {
  251. Capacitytotal int64 `json:"capacitytotal,omitempty"`
  252. Capacityused int64 `json:"capacityused,omitempty"`
  253. Clusterid string `json:"clusterid,omitempty"`
  254. Clustername string `json:"clustername,omitempty"`
  255. Percentused string `json:"percentused,omitempty"`
  256. Podid string `json:"podid,omitempty"`
  257. Podname string `json:"podname,omitempty"`
  258. Type int `json:"type,omitempty"`
  259. Zoneid string `json:"zoneid,omitempty"`
  260. Zonename string `json:"zonename,omitempty"`
  261. } `json:"capacity,omitempty"`
  262. Clustertype string `json:"clustertype,omitempty"`
  263. Cpuovercommitratio string `json:"cpuovercommitratio,omitempty"`
  264. Hypervisortype string `json:"hypervisortype,omitempty"`
  265. Id string `json:"id,omitempty"`
  266. Managedstate string `json:"managedstate,omitempty"`
  267. Memoryovercommitratio string `json:"memoryovercommitratio,omitempty"`
  268. Name string `json:"name,omitempty"`
  269. Ovm3vip string `json:"ovm3vip,omitempty"`
  270. Podid string `json:"podid,omitempty"`
  271. Podname string `json:"podname,omitempty"`
  272. Zoneid string `json:"zoneid,omitempty"`
  273. Zonename string `json:"zonename,omitempty"`
  274. }
  275. type DeleteClusterParams struct {
  276. p map[string]interface{}
  277. }
  278. func (p *DeleteClusterParams) toURLValues() url.Values {
  279. u := url.Values{}
  280. if p.p == nil {
  281. return u
  282. }
  283. if v, found := p.p["id"]; found {
  284. u.Set("id", v.(string))
  285. }
  286. return u
  287. }
  288. func (p *DeleteClusterParams) SetId(v string) {
  289. if p.p == nil {
  290. p.p = make(map[string]interface{})
  291. }
  292. p.p["id"] = v
  293. return
  294. }
  295. // You should always use this function to get a new DeleteClusterParams instance,
  296. // as then you are sure you have configured all required params
  297. func (s *ClusterService) NewDeleteClusterParams(id string) *DeleteClusterParams {
  298. p := &DeleteClusterParams{}
  299. p.p = make(map[string]interface{})
  300. p.p["id"] = id
  301. return p
  302. }
  303. // Deletes a cluster.
  304. func (s *ClusterService) DeleteCluster(p *DeleteClusterParams) (*DeleteClusterResponse, error) {
  305. resp, err := s.cs.newRequest("deleteCluster", p.toURLValues())
  306. if err != nil {
  307. return nil, err
  308. }
  309. var r DeleteClusterResponse
  310. if err := json.Unmarshal(resp, &r); err != nil {
  311. return nil, err
  312. }
  313. return &r, nil
  314. }
  315. type DeleteClusterResponse struct {
  316. Displaytext string `json:"displaytext,omitempty"`
  317. Success string `json:"success,omitempty"`
  318. }
  319. type UpdateClusterParams struct {
  320. p map[string]interface{}
  321. }
  322. func (p *UpdateClusterParams) toURLValues() url.Values {
  323. u := url.Values{}
  324. if p.p == nil {
  325. return u
  326. }
  327. if v, found := p.p["allocationstate"]; found {
  328. u.Set("allocationstate", v.(string))
  329. }
  330. if v, found := p.p["clustername"]; found {
  331. u.Set("clustername", v.(string))
  332. }
  333. if v, found := p.p["clustertype"]; found {
  334. u.Set("clustertype", v.(string))
  335. }
  336. if v, found := p.p["hypervisor"]; found {
  337. u.Set("hypervisor", v.(string))
  338. }
  339. if v, found := p.p["id"]; found {
  340. u.Set("id", v.(string))
  341. }
  342. if v, found := p.p["managedstate"]; found {
  343. u.Set("managedstate", v.(string))
  344. }
  345. return u
  346. }
  347. func (p *UpdateClusterParams) SetAllocationstate(v string) {
  348. if p.p == nil {
  349. p.p = make(map[string]interface{})
  350. }
  351. p.p["allocationstate"] = v
  352. return
  353. }
  354. func (p *UpdateClusterParams) SetClustername(v string) {
  355. if p.p == nil {
  356. p.p = make(map[string]interface{})
  357. }
  358. p.p["clustername"] = v
  359. return
  360. }
  361. func (p *UpdateClusterParams) SetClustertype(v string) {
  362. if p.p == nil {
  363. p.p = make(map[string]interface{})
  364. }
  365. p.p["clustertype"] = v
  366. return
  367. }
  368. func (p *UpdateClusterParams) SetHypervisor(v string) {
  369. if p.p == nil {
  370. p.p = make(map[string]interface{})
  371. }
  372. p.p["hypervisor"] = v
  373. return
  374. }
  375. func (p *UpdateClusterParams) SetId(v string) {
  376. if p.p == nil {
  377. p.p = make(map[string]interface{})
  378. }
  379. p.p["id"] = v
  380. return
  381. }
  382. func (p *UpdateClusterParams) SetManagedstate(v string) {
  383. if p.p == nil {
  384. p.p = make(map[string]interface{})
  385. }
  386. p.p["managedstate"] = v
  387. return
  388. }
  389. // You should always use this function to get a new UpdateClusterParams instance,
  390. // as then you are sure you have configured all required params
  391. func (s *ClusterService) NewUpdateClusterParams(id string) *UpdateClusterParams {
  392. p := &UpdateClusterParams{}
  393. p.p = make(map[string]interface{})
  394. p.p["id"] = id
  395. return p
  396. }
  397. // Updates an existing cluster
  398. func (s *ClusterService) UpdateCluster(p *UpdateClusterParams) (*UpdateClusterResponse, error) {
  399. resp, err := s.cs.newRequest("updateCluster", p.toURLValues())
  400. if err != nil {
  401. return nil, err
  402. }
  403. var r UpdateClusterResponse
  404. if err := json.Unmarshal(resp, &r); err != nil {
  405. return nil, err
  406. }
  407. return &r, nil
  408. }
  409. type UpdateClusterResponse struct {
  410. Allocationstate string `json:"allocationstate,omitempty"`
  411. Capacity []struct {
  412. Capacitytotal int64 `json:"capacitytotal,omitempty"`
  413. Capacityused int64 `json:"capacityused,omitempty"`
  414. Clusterid string `json:"clusterid,omitempty"`
  415. Clustername string `json:"clustername,omitempty"`
  416. Percentused string `json:"percentused,omitempty"`
  417. Podid string `json:"podid,omitempty"`
  418. Podname string `json:"podname,omitempty"`
  419. Type int `json:"type,omitempty"`
  420. Zoneid string `json:"zoneid,omitempty"`
  421. Zonename string `json:"zonename,omitempty"`
  422. } `json:"capacity,omitempty"`
  423. Clustertype string `json:"clustertype,omitempty"`
  424. Cpuovercommitratio string `json:"cpuovercommitratio,omitempty"`
  425. Hypervisortype string `json:"hypervisortype,omitempty"`
  426. Id string `json:"id,omitempty"`
  427. Managedstate string `json:"managedstate,omitempty"`
  428. Memoryovercommitratio string `json:"memoryovercommitratio,omitempty"`
  429. Name string `json:"name,omitempty"`
  430. Ovm3vip string `json:"ovm3vip,omitempty"`
  431. Podid string `json:"podid,omitempty"`
  432. Podname string `json:"podname,omitempty"`
  433. Zoneid string `json:"zoneid,omitempty"`
  434. Zonename string `json:"zonename,omitempty"`
  435. }
  436. type ListClustersParams struct {
  437. p map[string]interface{}
  438. }
  439. func (p *ListClustersParams) toURLValues() url.Values {
  440. u := url.Values{}
  441. if p.p == nil {
  442. return u
  443. }
  444. if v, found := p.p["allocationstate"]; found {
  445. u.Set("allocationstate", v.(string))
  446. }
  447. if v, found := p.p["clustertype"]; found {
  448. u.Set("clustertype", v.(string))
  449. }
  450. if v, found := p.p["hypervisor"]; found {
  451. u.Set("hypervisor", v.(string))
  452. }
  453. if v, found := p.p["id"]; found {
  454. u.Set("id", v.(string))
  455. }
  456. if v, found := p.p["keyword"]; found {
  457. u.Set("keyword", v.(string))
  458. }
  459. if v, found := p.p["managedstate"]; found {
  460. u.Set("managedstate", v.(string))
  461. }
  462. if v, found := p.p["name"]; found {
  463. u.Set("name", v.(string))
  464. }
  465. if v, found := p.p["page"]; found {
  466. vv := strconv.Itoa(v.(int))
  467. u.Set("page", vv)
  468. }
  469. if v, found := p.p["pagesize"]; found {
  470. vv := strconv.Itoa(v.(int))
  471. u.Set("pagesize", vv)
  472. }
  473. if v, found := p.p["podid"]; found {
  474. u.Set("podid", v.(string))
  475. }
  476. if v, found := p.p["showcapacities"]; found {
  477. vv := strconv.FormatBool(v.(bool))
  478. u.Set("showcapacities", vv)
  479. }
  480. if v, found := p.p["zoneid"]; found {
  481. u.Set("zoneid", v.(string))
  482. }
  483. return u
  484. }
  485. func (p *ListClustersParams) SetAllocationstate(v string) {
  486. if p.p == nil {
  487. p.p = make(map[string]interface{})
  488. }
  489. p.p["allocationstate"] = v
  490. return
  491. }
  492. func (p *ListClustersParams) SetClustertype(v string) {
  493. if p.p == nil {
  494. p.p = make(map[string]interface{})
  495. }
  496. p.p["clustertype"] = v
  497. return
  498. }
  499. func (p *ListClustersParams) SetHypervisor(v string) {
  500. if p.p == nil {
  501. p.p = make(map[string]interface{})
  502. }
  503. p.p["hypervisor"] = v
  504. return
  505. }
  506. func (p *ListClustersParams) SetId(v string) {
  507. if p.p == nil {
  508. p.p = make(map[string]interface{})
  509. }
  510. p.p["id"] = v
  511. return
  512. }
  513. func (p *ListClustersParams) SetKeyword(v string) {
  514. if p.p == nil {
  515. p.p = make(map[string]interface{})
  516. }
  517. p.p["keyword"] = v
  518. return
  519. }
  520. func (p *ListClustersParams) SetManagedstate(v string) {
  521. if p.p == nil {
  522. p.p = make(map[string]interface{})
  523. }
  524. p.p["managedstate"] = v
  525. return
  526. }
  527. func (p *ListClustersParams) SetName(v string) {
  528. if p.p == nil {
  529. p.p = make(map[string]interface{})
  530. }
  531. p.p["name"] = v
  532. return
  533. }
  534. func (p *ListClustersParams) SetPage(v int) {
  535. if p.p == nil {
  536. p.p = make(map[string]interface{})
  537. }
  538. p.p["page"] = v
  539. return
  540. }
  541. func (p *ListClustersParams) SetPagesize(v int) {
  542. if p.p == nil {
  543. p.p = make(map[string]interface{})
  544. }
  545. p.p["pagesize"] = v
  546. return
  547. }
  548. func (p *ListClustersParams) SetPodid(v string) {
  549. if p.p == nil {
  550. p.p = make(map[string]interface{})
  551. }
  552. p.p["podid"] = v
  553. return
  554. }
  555. func (p *ListClustersParams) SetShowcapacities(v bool) {
  556. if p.p == nil {
  557. p.p = make(map[string]interface{})
  558. }
  559. p.p["showcapacities"] = v
  560. return
  561. }
  562. func (p *ListClustersParams) SetZoneid(v string) {
  563. if p.p == nil {
  564. p.p = make(map[string]interface{})
  565. }
  566. p.p["zoneid"] = v
  567. return
  568. }
  569. // You should always use this function to get a new ListClustersParams instance,
  570. // as then you are sure you have configured all required params
  571. func (s *ClusterService) NewListClustersParams() *ListClustersParams {
  572. p := &ListClustersParams{}
  573. p.p = make(map[string]interface{})
  574. return p
  575. }
  576. // This is a courtesy helper function, which in some cases may not work as expected!
  577. func (s *ClusterService) GetClusterID(name string, opts ...OptionFunc) (string, int, error) {
  578. p := &ListClustersParams{}
  579. p.p = make(map[string]interface{})
  580. p.p["name"] = name
  581. for _, fn := range opts {
  582. if err := fn(s.cs, p); err != nil {
  583. return "", -1, err
  584. }
  585. }
  586. l, err := s.ListClusters(p)
  587. if err != nil {
  588. return "", -1, err
  589. }
  590. if l.Count == 0 {
  591. return "", l.Count, fmt.Errorf("No match found for %s: %+v", name, l)
  592. }
  593. if l.Count == 1 {
  594. return l.Clusters[0].Id, l.Count, nil
  595. }
  596. if l.Count > 1 {
  597. for _, v := range l.Clusters {
  598. if v.Name == name {
  599. return v.Id, l.Count, nil
  600. }
  601. }
  602. }
  603. return "", l.Count, fmt.Errorf("Could not find an exact match for %s: %+v", name, l)
  604. }
  605. // This is a courtesy helper function, which in some cases may not work as expected!
  606. func (s *ClusterService) GetClusterByName(name string, opts ...OptionFunc) (*Cluster, int, error) {
  607. id, count, err := s.GetClusterID(name, opts...)
  608. if err != nil {
  609. return nil, count, err
  610. }
  611. r, count, err := s.GetClusterByID(id, opts...)
  612. if err != nil {
  613. return nil, count, err
  614. }
  615. return r, count, nil
  616. }
  617. // This is a courtesy helper function, which in some cases may not work as expected!
  618. func (s *ClusterService) GetClusterByID(id string, opts ...OptionFunc) (*Cluster, int, error) {
  619. p := &ListClustersParams{}
  620. p.p = make(map[string]interface{})
  621. p.p["id"] = id
  622. for _, fn := range opts {
  623. if err := fn(s.cs, p); err != nil {
  624. return nil, -1, err
  625. }
  626. }
  627. l, err := s.ListClusters(p)
  628. if err != nil {
  629. if strings.Contains(err.Error(), fmt.Sprintf(
  630. "Invalid parameter id value=%s due to incorrect long value format, "+
  631. "or entity does not exist", id)) {
  632. return nil, 0, fmt.Errorf("No match found for %s: %+v", id, l)
  633. }
  634. return nil, -1, err
  635. }
  636. if l.Count == 0 {
  637. return nil, l.Count, fmt.Errorf("No match found for %s: %+v", id, l)
  638. }
  639. if l.Count == 1 {
  640. return l.Clusters[0], l.Count, nil
  641. }
  642. return nil, l.Count, fmt.Errorf("There is more then one result for Cluster UUID: %s!", id)
  643. }
  644. // Lists clusters.
  645. func (s *ClusterService) ListClusters(p *ListClustersParams) (*ListClustersResponse, error) {
  646. resp, err := s.cs.newRequest("listClusters", p.toURLValues())
  647. if err != nil {
  648. return nil, err
  649. }
  650. var r ListClustersResponse
  651. if err := json.Unmarshal(resp, &r); err != nil {
  652. return nil, err
  653. }
  654. return &r, nil
  655. }
  656. type ListClustersResponse struct {
  657. Count int `json:"count"`
  658. Clusters []*Cluster `json:"cluster"`
  659. }
  660. type Cluster struct {
  661. Allocationstate string `json:"allocationstate,omitempty"`
  662. Capacity []struct {
  663. Capacitytotal int64 `json:"capacitytotal,omitempty"`
  664. Capacityused int64 `json:"capacityused,omitempty"`
  665. Clusterid string `json:"clusterid,omitempty"`
  666. Clustername string `json:"clustername,omitempty"`
  667. Percentused string `json:"percentused,omitempty"`
  668. Podid string `json:"podid,omitempty"`
  669. Podname string `json:"podname,omitempty"`
  670. Type int `json:"type,omitempty"`
  671. Zoneid string `json:"zoneid,omitempty"`
  672. Zonename string `json:"zonename,omitempty"`
  673. } `json:"capacity,omitempty"`
  674. Clustertype string `json:"clustertype,omitempty"`
  675. Cpuovercommitratio string `json:"cpuovercommitratio,omitempty"`
  676. Hypervisortype string `json:"hypervisortype,omitempty"`
  677. Id string `json:"id,omitempty"`
  678. Managedstate string `json:"managedstate,omitempty"`
  679. Memoryovercommitratio string `json:"memoryovercommitratio,omitempty"`
  680. Name string `json:"name,omitempty"`
  681. Ovm3vip string `json:"ovm3vip,omitempty"`
  682. Podid string `json:"podid,omitempty"`
  683. Podname string `json:"podname,omitempty"`
  684. Zoneid string `json:"zoneid,omitempty"`
  685. Zonename string `json:"zonename,omitempty"`
  686. }
  687. type DedicateClusterParams struct {
  688. p map[string]interface{}
  689. }
  690. func (p *DedicateClusterParams) toURLValues() url.Values {
  691. u := url.Values{}
  692. if p.p == nil {
  693. return u
  694. }
  695. if v, found := p.p["account"]; found {
  696. u.Set("account", v.(string))
  697. }
  698. if v, found := p.p["clusterid"]; found {
  699. u.Set("clusterid", v.(string))
  700. }
  701. if v, found := p.p["domainid"]; found {
  702. u.Set("domainid", v.(string))
  703. }
  704. return u
  705. }
  706. func (p *DedicateClusterParams) SetAccount(v string) {
  707. if p.p == nil {
  708. p.p = make(map[string]interface{})
  709. }
  710. p.p["account"] = v
  711. return
  712. }
  713. func (p *DedicateClusterParams) SetClusterid(v string) {
  714. if p.p == nil {
  715. p.p = make(map[string]interface{})
  716. }
  717. p.p["clusterid"] = v
  718. return
  719. }
  720. func (p *DedicateClusterParams) SetDomainid(v string) {
  721. if p.p == nil {
  722. p.p = make(map[string]interface{})
  723. }
  724. p.p["domainid"] = v
  725. return
  726. }
  727. // You should always use this function to get a new DedicateClusterParams instance,
  728. // as then you are sure you have configured all required params
  729. func (s *ClusterService) NewDedicateClusterParams(clusterid string, domainid string) *DedicateClusterParams {
  730. p := &DedicateClusterParams{}
  731. p.p = make(map[string]interface{})
  732. p.p["clusterid"] = clusterid
  733. p.p["domainid"] = domainid
  734. return p
  735. }
  736. // Dedicate an existing cluster
  737. func (s *ClusterService) DedicateCluster(p *DedicateClusterParams) (*DedicateClusterResponse, error) {
  738. resp, err := s.cs.newRequest("dedicateCluster", p.toURLValues())
  739. if err != nil {
  740. return nil, err
  741. }
  742. var r DedicateClusterResponse
  743. if err := json.Unmarshal(resp, &r); err != nil {
  744. return nil, err
  745. }
  746. // If we have a async client, we need to wait for the async result
  747. if s.cs.async {
  748. b, err := s.cs.GetAsyncJobResult(r.JobID, s.cs.timeout)
  749. if err != nil {
  750. if err == AsyncTimeoutErr {
  751. return &r, err
  752. }
  753. return nil, err
  754. }
  755. b, err = getRawValue(b)
  756. if err != nil {
  757. return nil, err
  758. }
  759. if err := json.Unmarshal(b, &r); err != nil {
  760. return nil, err
  761. }
  762. }
  763. return &r, nil
  764. }
  765. type DedicateClusterResponse struct {
  766. JobID string `json:"jobid,omitempty"`
  767. Accountid string `json:"accountid,omitempty"`
  768. Affinitygroupid string `json:"affinitygroupid,omitempty"`
  769. Clusterid string `json:"clusterid,omitempty"`
  770. Clustername string `json:"clustername,omitempty"`
  771. Domainid string `json:"domainid,omitempty"`
  772. Id string `json:"id,omitempty"`
  773. }
  774. type ReleaseDedicatedClusterParams struct {
  775. p map[string]interface{}
  776. }
  777. func (p *ReleaseDedicatedClusterParams) toURLValues() url.Values {
  778. u := url.Values{}
  779. if p.p == nil {
  780. return u
  781. }
  782. if v, found := p.p["clusterid"]; found {
  783. u.Set("clusterid", v.(string))
  784. }
  785. return u
  786. }
  787. func (p *ReleaseDedicatedClusterParams) SetClusterid(v string) {
  788. if p.p == nil {
  789. p.p = make(map[string]interface{})
  790. }
  791. p.p["clusterid"] = v
  792. return
  793. }
  794. // You should always use this function to get a new ReleaseDedicatedClusterParams instance,
  795. // as then you are sure you have configured all required params
  796. func (s *ClusterService) NewReleaseDedicatedClusterParams(clusterid string) *ReleaseDedicatedClusterParams {
  797. p := &ReleaseDedicatedClusterParams{}
  798. p.p = make(map[string]interface{})
  799. p.p["clusterid"] = clusterid
  800. return p
  801. }
  802. // Release the dedication for cluster
  803. func (s *ClusterService) ReleaseDedicatedCluster(p *ReleaseDedicatedClusterParams) (*ReleaseDedicatedClusterResponse, error) {
  804. resp, err := s.cs.newRequest("releaseDedicatedCluster", p.toURLValues())
  805. if err != nil {
  806. return nil, err
  807. }
  808. var r ReleaseDedicatedClusterResponse
  809. if err := json.Unmarshal(resp, &r); err != nil {
  810. return nil, err
  811. }
  812. // If we have a async client, we need to wait for the async result
  813. if s.cs.async {
  814. b, err := s.cs.GetAsyncJobResult(r.JobID, s.cs.timeout)
  815. if err != nil {
  816. if err == AsyncTimeoutErr {
  817. return &r, err
  818. }
  819. return nil, err
  820. }
  821. if err := json.Unmarshal(b, &r); err != nil {
  822. return nil, err
  823. }
  824. }
  825. return &r, nil
  826. }
  827. type ReleaseDedicatedClusterResponse struct {
  828. JobID string `json:"jobid,omitempty"`
  829. Displaytext string `json:"displaytext,omitempty"`
  830. Success bool `json:"success,omitempty"`
  831. }
  832. type ListDedicatedClustersParams struct {
  833. p map[string]interface{}
  834. }
  835. func (p *ListDedicatedClustersParams) toURLValues() url.Values {
  836. u := url.Values{}
  837. if p.p == nil {
  838. return u
  839. }
  840. if v, found := p.p["account"]; found {
  841. u.Set("account", v.(string))
  842. }
  843. if v, found := p.p["affinitygroupid"]; found {
  844. u.Set("affinitygroupid", v.(string))
  845. }
  846. if v, found := p.p["clusterid"]; found {
  847. u.Set("clusterid", v.(string))
  848. }
  849. if v, found := p.p["domainid"]; found {
  850. u.Set("domainid", v.(string))
  851. }
  852. if v, found := p.p["keyword"]; found {
  853. u.Set("keyword", v.(string))
  854. }
  855. if v, found := p.p["page"]; found {
  856. vv := strconv.Itoa(v.(int))
  857. u.Set("page", vv)
  858. }
  859. if v, found := p.p["pagesize"]; found {
  860. vv := strconv.Itoa(v.(int))
  861. u.Set("pagesize", vv)
  862. }
  863. return u
  864. }
  865. func (p *ListDedicatedClustersParams) SetAccount(v string) {
  866. if p.p == nil {
  867. p.p = make(map[string]interface{})
  868. }
  869. p.p["account"] = v
  870. return
  871. }
  872. func (p *ListDedicatedClustersParams) SetAffinitygroupid(v string) {
  873. if p.p == nil {
  874. p.p = make(map[string]interface{})
  875. }
  876. p.p["affinitygroupid"] = v
  877. return
  878. }
  879. func (p *ListDedicatedClustersParams) SetClusterid(v string) {
  880. if p.p == nil {
  881. p.p = make(map[string]interface{})
  882. }
  883. p.p["clusterid"] = v
  884. return
  885. }
  886. func (p *ListDedicatedClustersParams) SetDomainid(v string) {
  887. if p.p == nil {
  888. p.p = make(map[string]interface{})
  889. }
  890. p.p["domainid"] = v
  891. return
  892. }
  893. func (p *ListDedicatedClustersParams) SetKeyword(v string) {
  894. if p.p == nil {
  895. p.p = make(map[string]interface{})
  896. }
  897. p.p["keyword"] = v
  898. return
  899. }
  900. func (p *ListDedicatedClustersParams) SetPage(v int) {
  901. if p.p == nil {
  902. p.p = make(map[string]interface{})
  903. }
  904. p.p["page"] = v
  905. return
  906. }
  907. func (p *ListDedicatedClustersParams) SetPagesize(v int) {
  908. if p.p == nil {
  909. p.p = make(map[string]interface{})
  910. }
  911. p.p["pagesize"] = v
  912. return
  913. }
  914. // You should always use this function to get a new ListDedicatedClustersParams instance,
  915. // as then you are sure you have configured all required params
  916. func (s *ClusterService) NewListDedicatedClustersParams() *ListDedicatedClustersParams {
  917. p := &ListDedicatedClustersParams{}
  918. p.p = make(map[string]interface{})
  919. return p
  920. }
  921. // Lists dedicated clusters.
  922. func (s *ClusterService) ListDedicatedClusters(p *ListDedicatedClustersParams) (*ListDedicatedClustersResponse, error) {
  923. resp, err := s.cs.newRequest("listDedicatedClusters", p.toURLValues())
  924. if err != nil {
  925. return nil, err
  926. }
  927. var r ListDedicatedClustersResponse
  928. if err := json.Unmarshal(resp, &r); err != nil {
  929. return nil, err
  930. }
  931. return &r, nil
  932. }
  933. type ListDedicatedClustersResponse struct {
  934. Count int `json:"count"`
  935. DedicatedClusters []*DedicatedCluster `json:"dedicatedcluster"`
  936. }
  937. type DedicatedCluster struct {
  938. Accountid string `json:"accountid,omitempty"`
  939. Affinitygroupid string `json:"affinitygroupid,omitempty"`
  940. Clusterid string `json:"clusterid,omitempty"`
  941. Clustername string `json:"clustername,omitempty"`
  942. Domainid string `json:"domainid,omitempty"`
  943. Id string `json:"id,omitempty"`
  944. }