SystemVMService.go 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047
  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 StartSystemVmParams struct {
  25. p map[string]interface{}
  26. }
  27. func (p *StartSystemVmParams) toURLValues() url.Values {
  28. u := url.Values{}
  29. if p.p == nil {
  30. return u
  31. }
  32. if v, found := p.p["id"]; found {
  33. u.Set("id", v.(string))
  34. }
  35. return u
  36. }
  37. func (p *StartSystemVmParams) SetId(v string) {
  38. if p.p == nil {
  39. p.p = make(map[string]interface{})
  40. }
  41. p.p["id"] = v
  42. return
  43. }
  44. // You should always use this function to get a new StartSystemVmParams instance,
  45. // as then you are sure you have configured all required params
  46. func (s *SystemVMService) NewStartSystemVmParams(id string) *StartSystemVmParams {
  47. p := &StartSystemVmParams{}
  48. p.p = make(map[string]interface{})
  49. p.p["id"] = id
  50. return p
  51. }
  52. // Starts a system virtual machine.
  53. func (s *SystemVMService) StartSystemVm(p *StartSystemVmParams) (*StartSystemVmResponse, error) {
  54. resp, err := s.cs.newRequest("startSystemVm", p.toURLValues())
  55. if err != nil {
  56. return nil, err
  57. }
  58. var r StartSystemVmResponse
  59. if err := json.Unmarshal(resp, &r); err != nil {
  60. return nil, err
  61. }
  62. // If we have a async client, we need to wait for the async result
  63. if s.cs.async {
  64. b, err := s.cs.GetAsyncJobResult(r.JobID, s.cs.timeout)
  65. if err != nil {
  66. if err == AsyncTimeoutErr {
  67. return &r, err
  68. }
  69. return nil, err
  70. }
  71. b, err = getRawValue(b)
  72. if err != nil {
  73. return nil, err
  74. }
  75. if err := json.Unmarshal(b, &r); err != nil {
  76. return nil, err
  77. }
  78. }
  79. return &r, nil
  80. }
  81. type StartSystemVmResponse struct {
  82. JobID string `json:"jobid,omitempty"`
  83. Activeviewersessions int `json:"activeviewersessions,omitempty"`
  84. Created string `json:"created,omitempty"`
  85. Dns1 string `json:"dns1,omitempty"`
  86. Dns2 string `json:"dns2,omitempty"`
  87. Gateway string `json:"gateway,omitempty"`
  88. Hostid string `json:"hostid,omitempty"`
  89. Hostname string `json:"hostname,omitempty"`
  90. Hypervisor string `json:"hypervisor,omitempty"`
  91. Id string `json:"id,omitempty"`
  92. Jobid string `json:"jobid,omitempty"`
  93. Jobstatus int `json:"jobstatus,omitempty"`
  94. Linklocalip string `json:"linklocalip,omitempty"`
  95. Linklocalmacaddress string `json:"linklocalmacaddress,omitempty"`
  96. Linklocalnetmask string `json:"linklocalnetmask,omitempty"`
  97. Name string `json:"name,omitempty"`
  98. Networkdomain string `json:"networkdomain,omitempty"`
  99. Podid string `json:"podid,omitempty"`
  100. Privateip string `json:"privateip,omitempty"`
  101. Privatemacaddress string `json:"privatemacaddress,omitempty"`
  102. Privatenetmask string `json:"privatenetmask,omitempty"`
  103. Publicip string `json:"publicip,omitempty"`
  104. Publicmacaddress string `json:"publicmacaddress,omitempty"`
  105. Publicnetmask string `json:"publicnetmask,omitempty"`
  106. State string `json:"state,omitempty"`
  107. Systemvmtype string `json:"systemvmtype,omitempty"`
  108. Templateid string `json:"templateid,omitempty"`
  109. Zoneid string `json:"zoneid,omitempty"`
  110. Zonename string `json:"zonename,omitempty"`
  111. }
  112. type RebootSystemVmParams struct {
  113. p map[string]interface{}
  114. }
  115. func (p *RebootSystemVmParams) toURLValues() url.Values {
  116. u := url.Values{}
  117. if p.p == nil {
  118. return u
  119. }
  120. if v, found := p.p["id"]; found {
  121. u.Set("id", v.(string))
  122. }
  123. return u
  124. }
  125. func (p *RebootSystemVmParams) SetId(v string) {
  126. if p.p == nil {
  127. p.p = make(map[string]interface{})
  128. }
  129. p.p["id"] = v
  130. return
  131. }
  132. // You should always use this function to get a new RebootSystemVmParams instance,
  133. // as then you are sure you have configured all required params
  134. func (s *SystemVMService) NewRebootSystemVmParams(id string) *RebootSystemVmParams {
  135. p := &RebootSystemVmParams{}
  136. p.p = make(map[string]interface{})
  137. p.p["id"] = id
  138. return p
  139. }
  140. // Reboots a system VM.
  141. func (s *SystemVMService) RebootSystemVm(p *RebootSystemVmParams) (*RebootSystemVmResponse, error) {
  142. resp, err := s.cs.newRequest("rebootSystemVm", p.toURLValues())
  143. if err != nil {
  144. return nil, err
  145. }
  146. var r RebootSystemVmResponse
  147. if err := json.Unmarshal(resp, &r); err != nil {
  148. return nil, err
  149. }
  150. // If we have a async client, we need to wait for the async result
  151. if s.cs.async {
  152. b, err := s.cs.GetAsyncJobResult(r.JobID, s.cs.timeout)
  153. if err != nil {
  154. if err == AsyncTimeoutErr {
  155. return &r, err
  156. }
  157. return nil, err
  158. }
  159. b, err = getRawValue(b)
  160. if err != nil {
  161. return nil, err
  162. }
  163. if err := json.Unmarshal(b, &r); err != nil {
  164. return nil, err
  165. }
  166. }
  167. return &r, nil
  168. }
  169. type RebootSystemVmResponse struct {
  170. JobID string `json:"jobid,omitempty"`
  171. Activeviewersessions int `json:"activeviewersessions,omitempty"`
  172. Created string `json:"created,omitempty"`
  173. Dns1 string `json:"dns1,omitempty"`
  174. Dns2 string `json:"dns2,omitempty"`
  175. Gateway string `json:"gateway,omitempty"`
  176. Hostid string `json:"hostid,omitempty"`
  177. Hostname string `json:"hostname,omitempty"`
  178. Hypervisor string `json:"hypervisor,omitempty"`
  179. Id string `json:"id,omitempty"`
  180. Jobid string `json:"jobid,omitempty"`
  181. Jobstatus int `json:"jobstatus,omitempty"`
  182. Linklocalip string `json:"linklocalip,omitempty"`
  183. Linklocalmacaddress string `json:"linklocalmacaddress,omitempty"`
  184. Linklocalnetmask string `json:"linklocalnetmask,omitempty"`
  185. Name string `json:"name,omitempty"`
  186. Networkdomain string `json:"networkdomain,omitempty"`
  187. Podid string `json:"podid,omitempty"`
  188. Privateip string `json:"privateip,omitempty"`
  189. Privatemacaddress string `json:"privatemacaddress,omitempty"`
  190. Privatenetmask string `json:"privatenetmask,omitempty"`
  191. Publicip string `json:"publicip,omitempty"`
  192. Publicmacaddress string `json:"publicmacaddress,omitempty"`
  193. Publicnetmask string `json:"publicnetmask,omitempty"`
  194. State string `json:"state,omitempty"`
  195. Systemvmtype string `json:"systemvmtype,omitempty"`
  196. Templateid string `json:"templateid,omitempty"`
  197. Zoneid string `json:"zoneid,omitempty"`
  198. Zonename string `json:"zonename,omitempty"`
  199. }
  200. type StopSystemVmParams struct {
  201. p map[string]interface{}
  202. }
  203. func (p *StopSystemVmParams) toURLValues() url.Values {
  204. u := url.Values{}
  205. if p.p == nil {
  206. return u
  207. }
  208. if v, found := p.p["forced"]; found {
  209. vv := strconv.FormatBool(v.(bool))
  210. u.Set("forced", vv)
  211. }
  212. if v, found := p.p["id"]; found {
  213. u.Set("id", v.(string))
  214. }
  215. return u
  216. }
  217. func (p *StopSystemVmParams) SetForced(v bool) {
  218. if p.p == nil {
  219. p.p = make(map[string]interface{})
  220. }
  221. p.p["forced"] = v
  222. return
  223. }
  224. func (p *StopSystemVmParams) SetId(v string) {
  225. if p.p == nil {
  226. p.p = make(map[string]interface{})
  227. }
  228. p.p["id"] = v
  229. return
  230. }
  231. // You should always use this function to get a new StopSystemVmParams instance,
  232. // as then you are sure you have configured all required params
  233. func (s *SystemVMService) NewStopSystemVmParams(id string) *StopSystemVmParams {
  234. p := &StopSystemVmParams{}
  235. p.p = make(map[string]interface{})
  236. p.p["id"] = id
  237. return p
  238. }
  239. // Stops a system VM.
  240. func (s *SystemVMService) StopSystemVm(p *StopSystemVmParams) (*StopSystemVmResponse, error) {
  241. resp, err := s.cs.newRequest("stopSystemVm", p.toURLValues())
  242. if err != nil {
  243. return nil, err
  244. }
  245. var r StopSystemVmResponse
  246. if err := json.Unmarshal(resp, &r); err != nil {
  247. return nil, err
  248. }
  249. // If we have a async client, we need to wait for the async result
  250. if s.cs.async {
  251. b, err := s.cs.GetAsyncJobResult(r.JobID, s.cs.timeout)
  252. if err != nil {
  253. if err == AsyncTimeoutErr {
  254. return &r, err
  255. }
  256. return nil, err
  257. }
  258. b, err = getRawValue(b)
  259. if err != nil {
  260. return nil, err
  261. }
  262. if err := json.Unmarshal(b, &r); err != nil {
  263. return nil, err
  264. }
  265. }
  266. return &r, nil
  267. }
  268. type StopSystemVmResponse struct {
  269. JobID string `json:"jobid,omitempty"`
  270. Activeviewersessions int `json:"activeviewersessions,omitempty"`
  271. Created string `json:"created,omitempty"`
  272. Dns1 string `json:"dns1,omitempty"`
  273. Dns2 string `json:"dns2,omitempty"`
  274. Gateway string `json:"gateway,omitempty"`
  275. Hostid string `json:"hostid,omitempty"`
  276. Hostname string `json:"hostname,omitempty"`
  277. Hypervisor string `json:"hypervisor,omitempty"`
  278. Id string `json:"id,omitempty"`
  279. Jobid string `json:"jobid,omitempty"`
  280. Jobstatus int `json:"jobstatus,omitempty"`
  281. Linklocalip string `json:"linklocalip,omitempty"`
  282. Linklocalmacaddress string `json:"linklocalmacaddress,omitempty"`
  283. Linklocalnetmask string `json:"linklocalnetmask,omitempty"`
  284. Name string `json:"name,omitempty"`
  285. Networkdomain string `json:"networkdomain,omitempty"`
  286. Podid string `json:"podid,omitempty"`
  287. Privateip string `json:"privateip,omitempty"`
  288. Privatemacaddress string `json:"privatemacaddress,omitempty"`
  289. Privatenetmask string `json:"privatenetmask,omitempty"`
  290. Publicip string `json:"publicip,omitempty"`
  291. Publicmacaddress string `json:"publicmacaddress,omitempty"`
  292. Publicnetmask string `json:"publicnetmask,omitempty"`
  293. State string `json:"state,omitempty"`
  294. Systemvmtype string `json:"systemvmtype,omitempty"`
  295. Templateid string `json:"templateid,omitempty"`
  296. Zoneid string `json:"zoneid,omitempty"`
  297. Zonename string `json:"zonename,omitempty"`
  298. }
  299. type DestroySystemVmParams struct {
  300. p map[string]interface{}
  301. }
  302. func (p *DestroySystemVmParams) toURLValues() url.Values {
  303. u := url.Values{}
  304. if p.p == nil {
  305. return u
  306. }
  307. if v, found := p.p["id"]; found {
  308. u.Set("id", v.(string))
  309. }
  310. return u
  311. }
  312. func (p *DestroySystemVmParams) SetId(v string) {
  313. if p.p == nil {
  314. p.p = make(map[string]interface{})
  315. }
  316. p.p["id"] = v
  317. return
  318. }
  319. // You should always use this function to get a new DestroySystemVmParams instance,
  320. // as then you are sure you have configured all required params
  321. func (s *SystemVMService) NewDestroySystemVmParams(id string) *DestroySystemVmParams {
  322. p := &DestroySystemVmParams{}
  323. p.p = make(map[string]interface{})
  324. p.p["id"] = id
  325. return p
  326. }
  327. // Destroyes a system virtual machine.
  328. func (s *SystemVMService) DestroySystemVm(p *DestroySystemVmParams) (*DestroySystemVmResponse, error) {
  329. resp, err := s.cs.newRequest("destroySystemVm", p.toURLValues())
  330. if err != nil {
  331. return nil, err
  332. }
  333. var r DestroySystemVmResponse
  334. if err := json.Unmarshal(resp, &r); err != nil {
  335. return nil, err
  336. }
  337. // If we have a async client, we need to wait for the async result
  338. if s.cs.async {
  339. b, err := s.cs.GetAsyncJobResult(r.JobID, s.cs.timeout)
  340. if err != nil {
  341. if err == AsyncTimeoutErr {
  342. return &r, err
  343. }
  344. return nil, err
  345. }
  346. b, err = getRawValue(b)
  347. if err != nil {
  348. return nil, err
  349. }
  350. if err := json.Unmarshal(b, &r); err != nil {
  351. return nil, err
  352. }
  353. }
  354. return &r, nil
  355. }
  356. type DestroySystemVmResponse struct {
  357. JobID string `json:"jobid,omitempty"`
  358. Activeviewersessions int `json:"activeviewersessions,omitempty"`
  359. Created string `json:"created,omitempty"`
  360. Dns1 string `json:"dns1,omitempty"`
  361. Dns2 string `json:"dns2,omitempty"`
  362. Gateway string `json:"gateway,omitempty"`
  363. Hostid string `json:"hostid,omitempty"`
  364. Hostname string `json:"hostname,omitempty"`
  365. Hypervisor string `json:"hypervisor,omitempty"`
  366. Id string `json:"id,omitempty"`
  367. Jobid string `json:"jobid,omitempty"`
  368. Jobstatus int `json:"jobstatus,omitempty"`
  369. Linklocalip string `json:"linklocalip,omitempty"`
  370. Linklocalmacaddress string `json:"linklocalmacaddress,omitempty"`
  371. Linklocalnetmask string `json:"linklocalnetmask,omitempty"`
  372. Name string `json:"name,omitempty"`
  373. Networkdomain string `json:"networkdomain,omitempty"`
  374. Podid string `json:"podid,omitempty"`
  375. Privateip string `json:"privateip,omitempty"`
  376. Privatemacaddress string `json:"privatemacaddress,omitempty"`
  377. Privatenetmask string `json:"privatenetmask,omitempty"`
  378. Publicip string `json:"publicip,omitempty"`
  379. Publicmacaddress string `json:"publicmacaddress,omitempty"`
  380. Publicnetmask string `json:"publicnetmask,omitempty"`
  381. State string `json:"state,omitempty"`
  382. Systemvmtype string `json:"systemvmtype,omitempty"`
  383. Templateid string `json:"templateid,omitempty"`
  384. Zoneid string `json:"zoneid,omitempty"`
  385. Zonename string `json:"zonename,omitempty"`
  386. }
  387. type ListSystemVmsParams struct {
  388. p map[string]interface{}
  389. }
  390. func (p *ListSystemVmsParams) toURLValues() url.Values {
  391. u := url.Values{}
  392. if p.p == nil {
  393. return u
  394. }
  395. if v, found := p.p["hostid"]; found {
  396. u.Set("hostid", v.(string))
  397. }
  398. if v, found := p.p["id"]; found {
  399. u.Set("id", v.(string))
  400. }
  401. if v, found := p.p["keyword"]; found {
  402. u.Set("keyword", v.(string))
  403. }
  404. if v, found := p.p["name"]; found {
  405. u.Set("name", v.(string))
  406. }
  407. if v, found := p.p["page"]; found {
  408. vv := strconv.Itoa(v.(int))
  409. u.Set("page", vv)
  410. }
  411. if v, found := p.p["pagesize"]; found {
  412. vv := strconv.Itoa(v.(int))
  413. u.Set("pagesize", vv)
  414. }
  415. if v, found := p.p["podid"]; found {
  416. u.Set("podid", v.(string))
  417. }
  418. if v, found := p.p["state"]; found {
  419. u.Set("state", v.(string))
  420. }
  421. if v, found := p.p["storageid"]; found {
  422. u.Set("storageid", v.(string))
  423. }
  424. if v, found := p.p["systemvmtype"]; found {
  425. u.Set("systemvmtype", v.(string))
  426. }
  427. if v, found := p.p["zoneid"]; found {
  428. u.Set("zoneid", v.(string))
  429. }
  430. return u
  431. }
  432. func (p *ListSystemVmsParams) SetHostid(v string) {
  433. if p.p == nil {
  434. p.p = make(map[string]interface{})
  435. }
  436. p.p["hostid"] = v
  437. return
  438. }
  439. func (p *ListSystemVmsParams) SetId(v string) {
  440. if p.p == nil {
  441. p.p = make(map[string]interface{})
  442. }
  443. p.p["id"] = v
  444. return
  445. }
  446. func (p *ListSystemVmsParams) SetKeyword(v string) {
  447. if p.p == nil {
  448. p.p = make(map[string]interface{})
  449. }
  450. p.p["keyword"] = v
  451. return
  452. }
  453. func (p *ListSystemVmsParams) SetName(v string) {
  454. if p.p == nil {
  455. p.p = make(map[string]interface{})
  456. }
  457. p.p["name"] = v
  458. return
  459. }
  460. func (p *ListSystemVmsParams) SetPage(v int) {
  461. if p.p == nil {
  462. p.p = make(map[string]interface{})
  463. }
  464. p.p["page"] = v
  465. return
  466. }
  467. func (p *ListSystemVmsParams) SetPagesize(v int) {
  468. if p.p == nil {
  469. p.p = make(map[string]interface{})
  470. }
  471. p.p["pagesize"] = v
  472. return
  473. }
  474. func (p *ListSystemVmsParams) SetPodid(v string) {
  475. if p.p == nil {
  476. p.p = make(map[string]interface{})
  477. }
  478. p.p["podid"] = v
  479. return
  480. }
  481. func (p *ListSystemVmsParams) SetState(v string) {
  482. if p.p == nil {
  483. p.p = make(map[string]interface{})
  484. }
  485. p.p["state"] = v
  486. return
  487. }
  488. func (p *ListSystemVmsParams) SetStorageid(v string) {
  489. if p.p == nil {
  490. p.p = make(map[string]interface{})
  491. }
  492. p.p["storageid"] = v
  493. return
  494. }
  495. func (p *ListSystemVmsParams) SetSystemvmtype(v string) {
  496. if p.p == nil {
  497. p.p = make(map[string]interface{})
  498. }
  499. p.p["systemvmtype"] = v
  500. return
  501. }
  502. func (p *ListSystemVmsParams) SetZoneid(v string) {
  503. if p.p == nil {
  504. p.p = make(map[string]interface{})
  505. }
  506. p.p["zoneid"] = v
  507. return
  508. }
  509. // You should always use this function to get a new ListSystemVmsParams instance,
  510. // as then you are sure you have configured all required params
  511. func (s *SystemVMService) NewListSystemVmsParams() *ListSystemVmsParams {
  512. p := &ListSystemVmsParams{}
  513. p.p = make(map[string]interface{})
  514. return p
  515. }
  516. // This is a courtesy helper function, which in some cases may not work as expected!
  517. func (s *SystemVMService) GetSystemVmID(name string, opts ...OptionFunc) (string, int, error) {
  518. p := &ListSystemVmsParams{}
  519. p.p = make(map[string]interface{})
  520. p.p["name"] = name
  521. for _, fn := range opts {
  522. if err := fn(s.cs, p); err != nil {
  523. return "", -1, err
  524. }
  525. }
  526. l, err := s.ListSystemVms(p)
  527. if err != nil {
  528. return "", -1, err
  529. }
  530. if l.Count == 0 {
  531. return "", l.Count, fmt.Errorf("No match found for %s: %+v", name, l)
  532. }
  533. if l.Count == 1 {
  534. return l.SystemVms[0].Id, l.Count, nil
  535. }
  536. if l.Count > 1 {
  537. for _, v := range l.SystemVms {
  538. if v.Name == name {
  539. return v.Id, l.Count, nil
  540. }
  541. }
  542. }
  543. return "", l.Count, fmt.Errorf("Could not find an exact match for %s: %+v", name, l)
  544. }
  545. // This is a courtesy helper function, which in some cases may not work as expected!
  546. func (s *SystemVMService) GetSystemVmByName(name string, opts ...OptionFunc) (*SystemVm, int, error) {
  547. id, count, err := s.GetSystemVmID(name, opts...)
  548. if err != nil {
  549. return nil, count, err
  550. }
  551. r, count, err := s.GetSystemVmByID(id, opts...)
  552. if err != nil {
  553. return nil, count, err
  554. }
  555. return r, count, nil
  556. }
  557. // This is a courtesy helper function, which in some cases may not work as expected!
  558. func (s *SystemVMService) GetSystemVmByID(id string, opts ...OptionFunc) (*SystemVm, int, error) {
  559. p := &ListSystemVmsParams{}
  560. p.p = make(map[string]interface{})
  561. p.p["id"] = id
  562. for _, fn := range opts {
  563. if err := fn(s.cs, p); err != nil {
  564. return nil, -1, err
  565. }
  566. }
  567. l, err := s.ListSystemVms(p)
  568. if err != nil {
  569. if strings.Contains(err.Error(), fmt.Sprintf(
  570. "Invalid parameter id value=%s due to incorrect long value format, "+
  571. "or entity does not exist", id)) {
  572. return nil, 0, fmt.Errorf("No match found for %s: %+v", id, l)
  573. }
  574. return nil, -1, err
  575. }
  576. if l.Count == 0 {
  577. return nil, l.Count, fmt.Errorf("No match found for %s: %+v", id, l)
  578. }
  579. if l.Count == 1 {
  580. return l.SystemVms[0], l.Count, nil
  581. }
  582. return nil, l.Count, fmt.Errorf("There is more then one result for SystemVm UUID: %s!", id)
  583. }
  584. // List system virtual machines.
  585. func (s *SystemVMService) ListSystemVms(p *ListSystemVmsParams) (*ListSystemVmsResponse, error) {
  586. resp, err := s.cs.newRequest("listSystemVms", p.toURLValues())
  587. if err != nil {
  588. return nil, err
  589. }
  590. var r ListSystemVmsResponse
  591. if err := json.Unmarshal(resp, &r); err != nil {
  592. return nil, err
  593. }
  594. return &r, nil
  595. }
  596. type ListSystemVmsResponse struct {
  597. Count int `json:"count"`
  598. SystemVms []*SystemVm `json:"systemvm"`
  599. }
  600. type SystemVm struct {
  601. Activeviewersessions int `json:"activeviewersessions,omitempty"`
  602. Created string `json:"created,omitempty"`
  603. Dns1 string `json:"dns1,omitempty"`
  604. Dns2 string `json:"dns2,omitempty"`
  605. Gateway string `json:"gateway,omitempty"`
  606. Hostid string `json:"hostid,omitempty"`
  607. Hostname string `json:"hostname,omitempty"`
  608. Hypervisor string `json:"hypervisor,omitempty"`
  609. Id string `json:"id,omitempty"`
  610. Jobid string `json:"jobid,omitempty"`
  611. Jobstatus int `json:"jobstatus,omitempty"`
  612. Linklocalip string `json:"linklocalip,omitempty"`
  613. Linklocalmacaddress string `json:"linklocalmacaddress,omitempty"`
  614. Linklocalnetmask string `json:"linklocalnetmask,omitempty"`
  615. Name string `json:"name,omitempty"`
  616. Networkdomain string `json:"networkdomain,omitempty"`
  617. Podid string `json:"podid,omitempty"`
  618. Privateip string `json:"privateip,omitempty"`
  619. Privatemacaddress string `json:"privatemacaddress,omitempty"`
  620. Privatenetmask string `json:"privatenetmask,omitempty"`
  621. Publicip string `json:"publicip,omitempty"`
  622. Publicmacaddress string `json:"publicmacaddress,omitempty"`
  623. Publicnetmask string `json:"publicnetmask,omitempty"`
  624. State string `json:"state,omitempty"`
  625. Systemvmtype string `json:"systemvmtype,omitempty"`
  626. Templateid string `json:"templateid,omitempty"`
  627. Zoneid string `json:"zoneid,omitempty"`
  628. Zonename string `json:"zonename,omitempty"`
  629. }
  630. type MigrateSystemVmParams struct {
  631. p map[string]interface{}
  632. }
  633. func (p *MigrateSystemVmParams) toURLValues() url.Values {
  634. u := url.Values{}
  635. if p.p == nil {
  636. return u
  637. }
  638. if v, found := p.p["hostid"]; found {
  639. u.Set("hostid", v.(string))
  640. }
  641. if v, found := p.p["virtualmachineid"]; found {
  642. u.Set("virtualmachineid", v.(string))
  643. }
  644. return u
  645. }
  646. func (p *MigrateSystemVmParams) SetHostid(v string) {
  647. if p.p == nil {
  648. p.p = make(map[string]interface{})
  649. }
  650. p.p["hostid"] = v
  651. return
  652. }
  653. func (p *MigrateSystemVmParams) SetVirtualmachineid(v string) {
  654. if p.p == nil {
  655. p.p = make(map[string]interface{})
  656. }
  657. p.p["virtualmachineid"] = v
  658. return
  659. }
  660. // You should always use this function to get a new MigrateSystemVmParams instance,
  661. // as then you are sure you have configured all required params
  662. func (s *SystemVMService) NewMigrateSystemVmParams(hostid string, virtualmachineid string) *MigrateSystemVmParams {
  663. p := &MigrateSystemVmParams{}
  664. p.p = make(map[string]interface{})
  665. p.p["hostid"] = hostid
  666. p.p["virtualmachineid"] = virtualmachineid
  667. return p
  668. }
  669. // Attempts Migration of a system virtual machine to the host specified.
  670. func (s *SystemVMService) MigrateSystemVm(p *MigrateSystemVmParams) (*MigrateSystemVmResponse, error) {
  671. resp, err := s.cs.newRequest("migrateSystemVm", p.toURLValues())
  672. if err != nil {
  673. return nil, err
  674. }
  675. var r MigrateSystemVmResponse
  676. if err := json.Unmarshal(resp, &r); err != nil {
  677. return nil, err
  678. }
  679. // If we have a async client, we need to wait for the async result
  680. if s.cs.async {
  681. b, err := s.cs.GetAsyncJobResult(r.JobID, s.cs.timeout)
  682. if err != nil {
  683. if err == AsyncTimeoutErr {
  684. return &r, err
  685. }
  686. return nil, err
  687. }
  688. b, err = getRawValue(b)
  689. if err != nil {
  690. return nil, err
  691. }
  692. if err := json.Unmarshal(b, &r); err != nil {
  693. return nil, err
  694. }
  695. }
  696. return &r, nil
  697. }
  698. type MigrateSystemVmResponse struct {
  699. JobID string `json:"jobid,omitempty"`
  700. Activeviewersessions int `json:"activeviewersessions,omitempty"`
  701. Created string `json:"created,omitempty"`
  702. Dns1 string `json:"dns1,omitempty"`
  703. Dns2 string `json:"dns2,omitempty"`
  704. Gateway string `json:"gateway,omitempty"`
  705. Hostid string `json:"hostid,omitempty"`
  706. Hostname string `json:"hostname,omitempty"`
  707. Hypervisor string `json:"hypervisor,omitempty"`
  708. Id string `json:"id,omitempty"`
  709. Jobid string `json:"jobid,omitempty"`
  710. Jobstatus int `json:"jobstatus,omitempty"`
  711. Linklocalip string `json:"linklocalip,omitempty"`
  712. Linklocalmacaddress string `json:"linklocalmacaddress,omitempty"`
  713. Linklocalnetmask string `json:"linklocalnetmask,omitempty"`
  714. Name string `json:"name,omitempty"`
  715. Networkdomain string `json:"networkdomain,omitempty"`
  716. Podid string `json:"podid,omitempty"`
  717. Privateip string `json:"privateip,omitempty"`
  718. Privatemacaddress string `json:"privatemacaddress,omitempty"`
  719. Privatenetmask string `json:"privatenetmask,omitempty"`
  720. Publicip string `json:"publicip,omitempty"`
  721. Publicmacaddress string `json:"publicmacaddress,omitempty"`
  722. Publicnetmask string `json:"publicnetmask,omitempty"`
  723. State string `json:"state,omitempty"`
  724. Systemvmtype string `json:"systemvmtype,omitempty"`
  725. Templateid string `json:"templateid,omitempty"`
  726. Zoneid string `json:"zoneid,omitempty"`
  727. Zonename string `json:"zonename,omitempty"`
  728. }
  729. type ChangeServiceForSystemVmParams struct {
  730. p map[string]interface{}
  731. }
  732. func (p *ChangeServiceForSystemVmParams) toURLValues() url.Values {
  733. u := url.Values{}
  734. if p.p == nil {
  735. return u
  736. }
  737. if v, found := p.p["details"]; found {
  738. i := 0
  739. for k, vv := range v.(map[string]string) {
  740. u.Set(fmt.Sprintf("details[%d].key", i), k)
  741. u.Set(fmt.Sprintf("details[%d].value", i), vv)
  742. i++
  743. }
  744. }
  745. if v, found := p.p["id"]; found {
  746. u.Set("id", v.(string))
  747. }
  748. if v, found := p.p["serviceofferingid"]; found {
  749. u.Set("serviceofferingid", v.(string))
  750. }
  751. return u
  752. }
  753. func (p *ChangeServiceForSystemVmParams) SetDetails(v map[string]string) {
  754. if p.p == nil {
  755. p.p = make(map[string]interface{})
  756. }
  757. p.p["details"] = v
  758. return
  759. }
  760. func (p *ChangeServiceForSystemVmParams) SetId(v string) {
  761. if p.p == nil {
  762. p.p = make(map[string]interface{})
  763. }
  764. p.p["id"] = v
  765. return
  766. }
  767. func (p *ChangeServiceForSystemVmParams) SetServiceofferingid(v string) {
  768. if p.p == nil {
  769. p.p = make(map[string]interface{})
  770. }
  771. p.p["serviceofferingid"] = v
  772. return
  773. }
  774. // You should always use this function to get a new ChangeServiceForSystemVmParams instance,
  775. // as then you are sure you have configured all required params
  776. func (s *SystemVMService) NewChangeServiceForSystemVmParams(id string, serviceofferingid string) *ChangeServiceForSystemVmParams {
  777. p := &ChangeServiceForSystemVmParams{}
  778. p.p = make(map[string]interface{})
  779. p.p["id"] = id
  780. p.p["serviceofferingid"] = serviceofferingid
  781. return p
  782. }
  783. // Changes the service offering for a system vm (console proxy or secondary storage). The system vm must be in a "Stopped" state for this command to take effect.
  784. func (s *SystemVMService) ChangeServiceForSystemVm(p *ChangeServiceForSystemVmParams) (*ChangeServiceForSystemVmResponse, error) {
  785. resp, err := s.cs.newRequest("changeServiceForSystemVm", p.toURLValues())
  786. if err != nil {
  787. return nil, err
  788. }
  789. var r ChangeServiceForSystemVmResponse
  790. if err := json.Unmarshal(resp, &r); err != nil {
  791. return nil, err
  792. }
  793. return &r, nil
  794. }
  795. type ChangeServiceForSystemVmResponse struct {
  796. Activeviewersessions int `json:"activeviewersessions,omitempty"`
  797. Created string `json:"created,omitempty"`
  798. Dns1 string `json:"dns1,omitempty"`
  799. Dns2 string `json:"dns2,omitempty"`
  800. Gateway string `json:"gateway,omitempty"`
  801. Hostid string `json:"hostid,omitempty"`
  802. Hostname string `json:"hostname,omitempty"`
  803. Hypervisor string `json:"hypervisor,omitempty"`
  804. Id string `json:"id,omitempty"`
  805. Jobid string `json:"jobid,omitempty"`
  806. Jobstatus int `json:"jobstatus,omitempty"`
  807. Linklocalip string `json:"linklocalip,omitempty"`
  808. Linklocalmacaddress string `json:"linklocalmacaddress,omitempty"`
  809. Linklocalnetmask string `json:"linklocalnetmask,omitempty"`
  810. Name string `json:"name,omitempty"`
  811. Networkdomain string `json:"networkdomain,omitempty"`
  812. Podid string `json:"podid,omitempty"`
  813. Privateip string `json:"privateip,omitempty"`
  814. Privatemacaddress string `json:"privatemacaddress,omitempty"`
  815. Privatenetmask string `json:"privatenetmask,omitempty"`
  816. Publicip string `json:"publicip,omitempty"`
  817. Publicmacaddress string `json:"publicmacaddress,omitempty"`
  818. Publicnetmask string `json:"publicnetmask,omitempty"`
  819. State string `json:"state,omitempty"`
  820. Systemvmtype string `json:"systemvmtype,omitempty"`
  821. Templateid string `json:"templateid,omitempty"`
  822. Zoneid string `json:"zoneid,omitempty"`
  823. Zonename string `json:"zonename,omitempty"`
  824. }
  825. type ScaleSystemVmParams struct {
  826. p map[string]interface{}
  827. }
  828. func (p *ScaleSystemVmParams) toURLValues() url.Values {
  829. u := url.Values{}
  830. if p.p == nil {
  831. return u
  832. }
  833. if v, found := p.p["details"]; found {
  834. i := 0
  835. for k, vv := range v.(map[string]string) {
  836. u.Set(fmt.Sprintf("details[%d].key", i), k)
  837. u.Set(fmt.Sprintf("details[%d].value", i), vv)
  838. i++
  839. }
  840. }
  841. if v, found := p.p["id"]; found {
  842. u.Set("id", v.(string))
  843. }
  844. if v, found := p.p["serviceofferingid"]; found {
  845. u.Set("serviceofferingid", v.(string))
  846. }
  847. return u
  848. }
  849. func (p *ScaleSystemVmParams) SetDetails(v map[string]string) {
  850. if p.p == nil {
  851. p.p = make(map[string]interface{})
  852. }
  853. p.p["details"] = v
  854. return
  855. }
  856. func (p *ScaleSystemVmParams) SetId(v string) {
  857. if p.p == nil {
  858. p.p = make(map[string]interface{})
  859. }
  860. p.p["id"] = v
  861. return
  862. }
  863. func (p *ScaleSystemVmParams) SetServiceofferingid(v string) {
  864. if p.p == nil {
  865. p.p = make(map[string]interface{})
  866. }
  867. p.p["serviceofferingid"] = v
  868. return
  869. }
  870. // You should always use this function to get a new ScaleSystemVmParams instance,
  871. // as then you are sure you have configured all required params
  872. func (s *SystemVMService) NewScaleSystemVmParams(id string, serviceofferingid string) *ScaleSystemVmParams {
  873. p := &ScaleSystemVmParams{}
  874. p.p = make(map[string]interface{})
  875. p.p["id"] = id
  876. p.p["serviceofferingid"] = serviceofferingid
  877. return p
  878. }
  879. // Scale the service offering for a system vm (console proxy or secondary storage). The system vm must be in a "Stopped" state for this command to take effect.
  880. func (s *SystemVMService) ScaleSystemVm(p *ScaleSystemVmParams) (*ScaleSystemVmResponse, error) {
  881. resp, err := s.cs.newRequest("scaleSystemVm", p.toURLValues())
  882. if err != nil {
  883. return nil, err
  884. }
  885. var r ScaleSystemVmResponse
  886. if err := json.Unmarshal(resp, &r); err != nil {
  887. return nil, err
  888. }
  889. // If we have a async client, we need to wait for the async result
  890. if s.cs.async {
  891. b, err := s.cs.GetAsyncJobResult(r.JobID, s.cs.timeout)
  892. if err != nil {
  893. if err == AsyncTimeoutErr {
  894. return &r, err
  895. }
  896. return nil, err
  897. }
  898. b, err = getRawValue(b)
  899. if err != nil {
  900. return nil, err
  901. }
  902. if err := json.Unmarshal(b, &r); err != nil {
  903. return nil, err
  904. }
  905. }
  906. return &r, nil
  907. }
  908. type ScaleSystemVmResponse struct {
  909. JobID string `json:"jobid,omitempty"`
  910. Activeviewersessions int `json:"activeviewersessions,omitempty"`
  911. Created string `json:"created,omitempty"`
  912. Dns1 string `json:"dns1,omitempty"`
  913. Dns2 string `json:"dns2,omitempty"`
  914. Gateway string `json:"gateway,omitempty"`
  915. Hostid string `json:"hostid,omitempty"`
  916. Hostname string `json:"hostname,omitempty"`
  917. Hypervisor string `json:"hypervisor,omitempty"`
  918. Id string `json:"id,omitempty"`
  919. Jobid string `json:"jobid,omitempty"`
  920. Jobstatus int `json:"jobstatus,omitempty"`
  921. Linklocalip string `json:"linklocalip,omitempty"`
  922. Linklocalmacaddress string `json:"linklocalmacaddress,omitempty"`
  923. Linklocalnetmask string `json:"linklocalnetmask,omitempty"`
  924. Name string `json:"name,omitempty"`
  925. Networkdomain string `json:"networkdomain,omitempty"`
  926. Podid string `json:"podid,omitempty"`
  927. Privateip string `json:"privateip,omitempty"`
  928. Privatemacaddress string `json:"privatemacaddress,omitempty"`
  929. Privatenetmask string `json:"privatenetmask,omitempty"`
  930. Publicip string `json:"publicip,omitempty"`
  931. Publicmacaddress string `json:"publicmacaddress,omitempty"`
  932. Publicnetmask string `json:"publicnetmask,omitempty"`
  933. State string `json:"state,omitempty"`
  934. Systemvmtype string `json:"systemvmtype,omitempty"`
  935. Templateid string `json:"templateid,omitempty"`
  936. Zoneid string `json:"zoneid,omitempty"`
  937. Zonename string `json:"zonename,omitempty"`
  938. }