GuestOSService.go 25 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 ListOsTypesParams struct {
  25. p map[string]interface{}
  26. }
  27. func (p *ListOsTypesParams) toURLValues() url.Values {
  28. u := url.Values{}
  29. if p.p == nil {
  30. return u
  31. }
  32. if v, found := p.p["description"]; found {
  33. u.Set("description", v.(string))
  34. }
  35. if v, found := p.p["id"]; found {
  36. u.Set("id", v.(string))
  37. }
  38. if v, found := p.p["keyword"]; found {
  39. u.Set("keyword", v.(string))
  40. }
  41. if v, found := p.p["oscategoryid"]; found {
  42. u.Set("oscategoryid", v.(string))
  43. }
  44. if v, found := p.p["page"]; found {
  45. vv := strconv.Itoa(v.(int))
  46. u.Set("page", vv)
  47. }
  48. if v, found := p.p["pagesize"]; found {
  49. vv := strconv.Itoa(v.(int))
  50. u.Set("pagesize", vv)
  51. }
  52. return u
  53. }
  54. func (p *ListOsTypesParams) SetDescription(v string) {
  55. if p.p == nil {
  56. p.p = make(map[string]interface{})
  57. }
  58. p.p["description"] = v
  59. return
  60. }
  61. func (p *ListOsTypesParams) SetId(v string) {
  62. if p.p == nil {
  63. p.p = make(map[string]interface{})
  64. }
  65. p.p["id"] = v
  66. return
  67. }
  68. func (p *ListOsTypesParams) SetKeyword(v string) {
  69. if p.p == nil {
  70. p.p = make(map[string]interface{})
  71. }
  72. p.p["keyword"] = v
  73. return
  74. }
  75. func (p *ListOsTypesParams) SetOscategoryid(v string) {
  76. if p.p == nil {
  77. p.p = make(map[string]interface{})
  78. }
  79. p.p["oscategoryid"] = v
  80. return
  81. }
  82. func (p *ListOsTypesParams) SetPage(v int) {
  83. if p.p == nil {
  84. p.p = make(map[string]interface{})
  85. }
  86. p.p["page"] = v
  87. return
  88. }
  89. func (p *ListOsTypesParams) SetPagesize(v int) {
  90. if p.p == nil {
  91. p.p = make(map[string]interface{})
  92. }
  93. p.p["pagesize"] = v
  94. return
  95. }
  96. // You should always use this function to get a new ListOsTypesParams instance,
  97. // as then you are sure you have configured all required params
  98. func (s *GuestOSService) NewListOsTypesParams() *ListOsTypesParams {
  99. p := &ListOsTypesParams{}
  100. p.p = make(map[string]interface{})
  101. return p
  102. }
  103. // This is a courtesy helper function, which in some cases may not work as expected!
  104. func (s *GuestOSService) GetOsTypeByID(id string, opts ...OptionFunc) (*OsType, int, error) {
  105. p := &ListOsTypesParams{}
  106. p.p = make(map[string]interface{})
  107. p.p["id"] = id
  108. for _, fn := range opts {
  109. if err := fn(s.cs, p); err != nil {
  110. return nil, -1, err
  111. }
  112. }
  113. l, err := s.ListOsTypes(p)
  114. if err != nil {
  115. if strings.Contains(err.Error(), fmt.Sprintf(
  116. "Invalid parameter id value=%s due to incorrect long value format, "+
  117. "or entity does not exist", id)) {
  118. return nil, 0, fmt.Errorf("No match found for %s: %+v", id, l)
  119. }
  120. return nil, -1, err
  121. }
  122. if l.Count == 0 {
  123. return nil, l.Count, fmt.Errorf("No match found for %s: %+v", id, l)
  124. }
  125. if l.Count == 1 {
  126. return l.OsTypes[0], l.Count, nil
  127. }
  128. return nil, l.Count, fmt.Errorf("There is more then one result for OsType UUID: %s!", id)
  129. }
  130. // Lists all supported OS types for this cloud.
  131. func (s *GuestOSService) ListOsTypes(p *ListOsTypesParams) (*ListOsTypesResponse, error) {
  132. resp, err := s.cs.newRequest("listOsTypes", p.toURLValues())
  133. if err != nil {
  134. return nil, err
  135. }
  136. var r ListOsTypesResponse
  137. if err := json.Unmarshal(resp, &r); err != nil {
  138. return nil, err
  139. }
  140. return &r, nil
  141. }
  142. type ListOsTypesResponse struct {
  143. Count int `json:"count"`
  144. OsTypes []*OsType `json:"ostype"`
  145. }
  146. type OsType struct {
  147. Description string `json:"description,omitempty"`
  148. Id string `json:"id,omitempty"`
  149. Isuserdefined string `json:"isuserdefined,omitempty"`
  150. Oscategoryid string `json:"oscategoryid,omitempty"`
  151. }
  152. type ListOsCategoriesParams struct {
  153. p map[string]interface{}
  154. }
  155. func (p *ListOsCategoriesParams) toURLValues() url.Values {
  156. u := url.Values{}
  157. if p.p == nil {
  158. return u
  159. }
  160. if v, found := p.p["id"]; found {
  161. u.Set("id", v.(string))
  162. }
  163. if v, found := p.p["keyword"]; found {
  164. u.Set("keyword", v.(string))
  165. }
  166. if v, found := p.p["name"]; found {
  167. u.Set("name", v.(string))
  168. }
  169. if v, found := p.p["page"]; found {
  170. vv := strconv.Itoa(v.(int))
  171. u.Set("page", vv)
  172. }
  173. if v, found := p.p["pagesize"]; found {
  174. vv := strconv.Itoa(v.(int))
  175. u.Set("pagesize", vv)
  176. }
  177. return u
  178. }
  179. func (p *ListOsCategoriesParams) SetId(v string) {
  180. if p.p == nil {
  181. p.p = make(map[string]interface{})
  182. }
  183. p.p["id"] = v
  184. return
  185. }
  186. func (p *ListOsCategoriesParams) SetKeyword(v string) {
  187. if p.p == nil {
  188. p.p = make(map[string]interface{})
  189. }
  190. p.p["keyword"] = v
  191. return
  192. }
  193. func (p *ListOsCategoriesParams) SetName(v string) {
  194. if p.p == nil {
  195. p.p = make(map[string]interface{})
  196. }
  197. p.p["name"] = v
  198. return
  199. }
  200. func (p *ListOsCategoriesParams) SetPage(v int) {
  201. if p.p == nil {
  202. p.p = make(map[string]interface{})
  203. }
  204. p.p["page"] = v
  205. return
  206. }
  207. func (p *ListOsCategoriesParams) SetPagesize(v int) {
  208. if p.p == nil {
  209. p.p = make(map[string]interface{})
  210. }
  211. p.p["pagesize"] = v
  212. return
  213. }
  214. // You should always use this function to get a new ListOsCategoriesParams instance,
  215. // as then you are sure you have configured all required params
  216. func (s *GuestOSService) NewListOsCategoriesParams() *ListOsCategoriesParams {
  217. p := &ListOsCategoriesParams{}
  218. p.p = make(map[string]interface{})
  219. return p
  220. }
  221. // This is a courtesy helper function, which in some cases may not work as expected!
  222. func (s *GuestOSService) GetOsCategoryID(name string, opts ...OptionFunc) (string, int, error) {
  223. p := &ListOsCategoriesParams{}
  224. p.p = make(map[string]interface{})
  225. p.p["name"] = name
  226. for _, fn := range opts {
  227. if err := fn(s.cs, p); err != nil {
  228. return "", -1, err
  229. }
  230. }
  231. l, err := s.ListOsCategories(p)
  232. if err != nil {
  233. return "", -1, err
  234. }
  235. if l.Count == 0 {
  236. return "", l.Count, fmt.Errorf("No match found for %s: %+v", name, l)
  237. }
  238. if l.Count == 1 {
  239. return l.OsCategories[0].Id, l.Count, nil
  240. }
  241. if l.Count > 1 {
  242. for _, v := range l.OsCategories {
  243. if v.Name == name {
  244. return v.Id, l.Count, nil
  245. }
  246. }
  247. }
  248. return "", l.Count, fmt.Errorf("Could not find an exact match for %s: %+v", name, l)
  249. }
  250. // This is a courtesy helper function, which in some cases may not work as expected!
  251. func (s *GuestOSService) GetOsCategoryByName(name string, opts ...OptionFunc) (*OsCategory, int, error) {
  252. id, count, err := s.GetOsCategoryID(name, opts...)
  253. if err != nil {
  254. return nil, count, err
  255. }
  256. r, count, err := s.GetOsCategoryByID(id, opts...)
  257. if err != nil {
  258. return nil, count, err
  259. }
  260. return r, count, nil
  261. }
  262. // This is a courtesy helper function, which in some cases may not work as expected!
  263. func (s *GuestOSService) GetOsCategoryByID(id string, opts ...OptionFunc) (*OsCategory, int, error) {
  264. p := &ListOsCategoriesParams{}
  265. p.p = make(map[string]interface{})
  266. p.p["id"] = id
  267. for _, fn := range opts {
  268. if err := fn(s.cs, p); err != nil {
  269. return nil, -1, err
  270. }
  271. }
  272. l, err := s.ListOsCategories(p)
  273. if err != nil {
  274. if strings.Contains(err.Error(), fmt.Sprintf(
  275. "Invalid parameter id value=%s due to incorrect long value format, "+
  276. "or entity does not exist", id)) {
  277. return nil, 0, fmt.Errorf("No match found for %s: %+v", id, l)
  278. }
  279. return nil, -1, err
  280. }
  281. if l.Count == 0 {
  282. return nil, l.Count, fmt.Errorf("No match found for %s: %+v", id, l)
  283. }
  284. if l.Count == 1 {
  285. return l.OsCategories[0], l.Count, nil
  286. }
  287. return nil, l.Count, fmt.Errorf("There is more then one result for OsCategory UUID: %s!", id)
  288. }
  289. // Lists all supported OS categories for this cloud.
  290. func (s *GuestOSService) ListOsCategories(p *ListOsCategoriesParams) (*ListOsCategoriesResponse, error) {
  291. resp, err := s.cs.newRequest("listOsCategories", p.toURLValues())
  292. if err != nil {
  293. return nil, err
  294. }
  295. var r ListOsCategoriesResponse
  296. if err := json.Unmarshal(resp, &r); err != nil {
  297. return nil, err
  298. }
  299. return &r, nil
  300. }
  301. type ListOsCategoriesResponse struct {
  302. Count int `json:"count"`
  303. OsCategories []*OsCategory `json:"oscategory"`
  304. }
  305. type OsCategory struct {
  306. Id string `json:"id,omitempty"`
  307. Name string `json:"name,omitempty"`
  308. }
  309. type AddGuestOsParams struct {
  310. p map[string]interface{}
  311. }
  312. func (p *AddGuestOsParams) toURLValues() url.Values {
  313. u := url.Values{}
  314. if p.p == nil {
  315. return u
  316. }
  317. if v, found := p.p["name"]; found {
  318. u.Set("name", v.(string))
  319. }
  320. if v, found := p.p["oscategoryid"]; found {
  321. u.Set("oscategoryid", v.(string))
  322. }
  323. if v, found := p.p["osdisplayname"]; found {
  324. u.Set("osdisplayname", v.(string))
  325. }
  326. return u
  327. }
  328. func (p *AddGuestOsParams) SetName(v string) {
  329. if p.p == nil {
  330. p.p = make(map[string]interface{})
  331. }
  332. p.p["name"] = v
  333. return
  334. }
  335. func (p *AddGuestOsParams) SetOscategoryid(v string) {
  336. if p.p == nil {
  337. p.p = make(map[string]interface{})
  338. }
  339. p.p["oscategoryid"] = v
  340. return
  341. }
  342. func (p *AddGuestOsParams) SetOsdisplayname(v string) {
  343. if p.p == nil {
  344. p.p = make(map[string]interface{})
  345. }
  346. p.p["osdisplayname"] = v
  347. return
  348. }
  349. // You should always use this function to get a new AddGuestOsParams instance,
  350. // as then you are sure you have configured all required params
  351. func (s *GuestOSService) NewAddGuestOsParams(oscategoryid string, osdisplayname string) *AddGuestOsParams {
  352. p := &AddGuestOsParams{}
  353. p.p = make(map[string]interface{})
  354. p.p["oscategoryid"] = oscategoryid
  355. p.p["osdisplayname"] = osdisplayname
  356. return p
  357. }
  358. // Add a new guest OS type
  359. func (s *GuestOSService) AddGuestOs(p *AddGuestOsParams) (*AddGuestOsResponse, error) {
  360. resp, err := s.cs.newRequest("addGuestOs", p.toURLValues())
  361. if err != nil {
  362. return nil, err
  363. }
  364. var r AddGuestOsResponse
  365. if err := json.Unmarshal(resp, &r); err != nil {
  366. return nil, err
  367. }
  368. // If we have a async client, we need to wait for the async result
  369. if s.cs.async {
  370. b, err := s.cs.GetAsyncJobResult(r.JobID, s.cs.timeout)
  371. if err != nil {
  372. if err == AsyncTimeoutErr {
  373. return &r, err
  374. }
  375. return nil, err
  376. }
  377. b, err = getRawValue(b)
  378. if err != nil {
  379. return nil, err
  380. }
  381. if err := json.Unmarshal(b, &r); err != nil {
  382. return nil, err
  383. }
  384. }
  385. return &r, nil
  386. }
  387. type AddGuestOsResponse struct {
  388. JobID string `json:"jobid,omitempty"`
  389. Description string `json:"description,omitempty"`
  390. Id string `json:"id,omitempty"`
  391. Isuserdefined string `json:"isuserdefined,omitempty"`
  392. Oscategoryid string `json:"oscategoryid,omitempty"`
  393. }
  394. type UpdateGuestOsParams struct {
  395. p map[string]interface{}
  396. }
  397. func (p *UpdateGuestOsParams) toURLValues() url.Values {
  398. u := url.Values{}
  399. if p.p == nil {
  400. return u
  401. }
  402. if v, found := p.p["id"]; found {
  403. u.Set("id", v.(string))
  404. }
  405. if v, found := p.p["osdisplayname"]; found {
  406. u.Set("osdisplayname", v.(string))
  407. }
  408. return u
  409. }
  410. func (p *UpdateGuestOsParams) SetId(v string) {
  411. if p.p == nil {
  412. p.p = make(map[string]interface{})
  413. }
  414. p.p["id"] = v
  415. return
  416. }
  417. func (p *UpdateGuestOsParams) SetOsdisplayname(v string) {
  418. if p.p == nil {
  419. p.p = make(map[string]interface{})
  420. }
  421. p.p["osdisplayname"] = v
  422. return
  423. }
  424. // You should always use this function to get a new UpdateGuestOsParams instance,
  425. // as then you are sure you have configured all required params
  426. func (s *GuestOSService) NewUpdateGuestOsParams(id string, osdisplayname string) *UpdateGuestOsParams {
  427. p := &UpdateGuestOsParams{}
  428. p.p = make(map[string]interface{})
  429. p.p["id"] = id
  430. p.p["osdisplayname"] = osdisplayname
  431. return p
  432. }
  433. // Updates the information about Guest OS
  434. func (s *GuestOSService) UpdateGuestOs(p *UpdateGuestOsParams) (*UpdateGuestOsResponse, error) {
  435. resp, err := s.cs.newRequest("updateGuestOs", p.toURLValues())
  436. if err != nil {
  437. return nil, err
  438. }
  439. var r UpdateGuestOsResponse
  440. if err := json.Unmarshal(resp, &r); err != nil {
  441. return nil, err
  442. }
  443. // If we have a async client, we need to wait for the async result
  444. if s.cs.async {
  445. b, err := s.cs.GetAsyncJobResult(r.JobID, s.cs.timeout)
  446. if err != nil {
  447. if err == AsyncTimeoutErr {
  448. return &r, err
  449. }
  450. return nil, err
  451. }
  452. b, err = getRawValue(b)
  453. if err != nil {
  454. return nil, err
  455. }
  456. if err := json.Unmarshal(b, &r); err != nil {
  457. return nil, err
  458. }
  459. }
  460. return &r, nil
  461. }
  462. type UpdateGuestOsResponse struct {
  463. JobID string `json:"jobid,omitempty"`
  464. Description string `json:"description,omitempty"`
  465. Id string `json:"id,omitempty"`
  466. Isuserdefined string `json:"isuserdefined,omitempty"`
  467. Oscategoryid string `json:"oscategoryid,omitempty"`
  468. }
  469. type RemoveGuestOsParams struct {
  470. p map[string]interface{}
  471. }
  472. func (p *RemoveGuestOsParams) toURLValues() url.Values {
  473. u := url.Values{}
  474. if p.p == nil {
  475. return u
  476. }
  477. if v, found := p.p["id"]; found {
  478. u.Set("id", v.(string))
  479. }
  480. return u
  481. }
  482. func (p *RemoveGuestOsParams) SetId(v string) {
  483. if p.p == nil {
  484. p.p = make(map[string]interface{})
  485. }
  486. p.p["id"] = v
  487. return
  488. }
  489. // You should always use this function to get a new RemoveGuestOsParams instance,
  490. // as then you are sure you have configured all required params
  491. func (s *GuestOSService) NewRemoveGuestOsParams(id string) *RemoveGuestOsParams {
  492. p := &RemoveGuestOsParams{}
  493. p.p = make(map[string]interface{})
  494. p.p["id"] = id
  495. return p
  496. }
  497. // Removes a Guest OS from listing.
  498. func (s *GuestOSService) RemoveGuestOs(p *RemoveGuestOsParams) (*RemoveGuestOsResponse, error) {
  499. resp, err := s.cs.newRequest("removeGuestOs", p.toURLValues())
  500. if err != nil {
  501. return nil, err
  502. }
  503. var r RemoveGuestOsResponse
  504. if err := json.Unmarshal(resp, &r); err != nil {
  505. return nil, err
  506. }
  507. // If we have a async client, we need to wait for the async result
  508. if s.cs.async {
  509. b, err := s.cs.GetAsyncJobResult(r.JobID, s.cs.timeout)
  510. if err != nil {
  511. if err == AsyncTimeoutErr {
  512. return &r, err
  513. }
  514. return nil, err
  515. }
  516. if err := json.Unmarshal(b, &r); err != nil {
  517. return nil, err
  518. }
  519. }
  520. return &r, nil
  521. }
  522. type RemoveGuestOsResponse struct {
  523. JobID string `json:"jobid,omitempty"`
  524. Displaytext string `json:"displaytext,omitempty"`
  525. Success bool `json:"success,omitempty"`
  526. }
  527. type ListGuestOsMappingParams struct {
  528. p map[string]interface{}
  529. }
  530. func (p *ListGuestOsMappingParams) toURLValues() url.Values {
  531. u := url.Values{}
  532. if p.p == nil {
  533. return u
  534. }
  535. if v, found := p.p["hypervisor"]; found {
  536. u.Set("hypervisor", v.(string))
  537. }
  538. if v, found := p.p["hypervisorversion"]; found {
  539. u.Set("hypervisorversion", v.(string))
  540. }
  541. if v, found := p.p["id"]; found {
  542. u.Set("id", v.(string))
  543. }
  544. if v, found := p.p["keyword"]; found {
  545. u.Set("keyword", v.(string))
  546. }
  547. if v, found := p.p["ostypeid"]; found {
  548. u.Set("ostypeid", v.(string))
  549. }
  550. if v, found := p.p["page"]; found {
  551. vv := strconv.Itoa(v.(int))
  552. u.Set("page", vv)
  553. }
  554. if v, found := p.p["pagesize"]; found {
  555. vv := strconv.Itoa(v.(int))
  556. u.Set("pagesize", vv)
  557. }
  558. return u
  559. }
  560. func (p *ListGuestOsMappingParams) SetHypervisor(v string) {
  561. if p.p == nil {
  562. p.p = make(map[string]interface{})
  563. }
  564. p.p["hypervisor"] = v
  565. return
  566. }
  567. func (p *ListGuestOsMappingParams) SetHypervisorversion(v string) {
  568. if p.p == nil {
  569. p.p = make(map[string]interface{})
  570. }
  571. p.p["hypervisorversion"] = v
  572. return
  573. }
  574. func (p *ListGuestOsMappingParams) SetId(v string) {
  575. if p.p == nil {
  576. p.p = make(map[string]interface{})
  577. }
  578. p.p["id"] = v
  579. return
  580. }
  581. func (p *ListGuestOsMappingParams) SetKeyword(v string) {
  582. if p.p == nil {
  583. p.p = make(map[string]interface{})
  584. }
  585. p.p["keyword"] = v
  586. return
  587. }
  588. func (p *ListGuestOsMappingParams) SetOstypeid(v string) {
  589. if p.p == nil {
  590. p.p = make(map[string]interface{})
  591. }
  592. p.p["ostypeid"] = v
  593. return
  594. }
  595. func (p *ListGuestOsMappingParams) SetPage(v int) {
  596. if p.p == nil {
  597. p.p = make(map[string]interface{})
  598. }
  599. p.p["page"] = v
  600. return
  601. }
  602. func (p *ListGuestOsMappingParams) SetPagesize(v int) {
  603. if p.p == nil {
  604. p.p = make(map[string]interface{})
  605. }
  606. p.p["pagesize"] = v
  607. return
  608. }
  609. // You should always use this function to get a new ListGuestOsMappingParams instance,
  610. // as then you are sure you have configured all required params
  611. func (s *GuestOSService) NewListGuestOsMappingParams() *ListGuestOsMappingParams {
  612. p := &ListGuestOsMappingParams{}
  613. p.p = make(map[string]interface{})
  614. return p
  615. }
  616. // This is a courtesy helper function, which in some cases may not work as expected!
  617. func (s *GuestOSService) GetGuestOsMappingByID(id string, opts ...OptionFunc) (*GuestOsMapping, int, error) {
  618. p := &ListGuestOsMappingParams{}
  619. p.p = make(map[string]interface{})
  620. p.p["id"] = id
  621. for _, fn := range opts {
  622. if err := fn(s.cs, p); err != nil {
  623. return nil, -1, err
  624. }
  625. }
  626. l, err := s.ListGuestOsMapping(p)
  627. if err != nil {
  628. if strings.Contains(err.Error(), fmt.Sprintf(
  629. "Invalid parameter id value=%s due to incorrect long value format, "+
  630. "or entity does not exist", id)) {
  631. return nil, 0, fmt.Errorf("No match found for %s: %+v", id, l)
  632. }
  633. return nil, -1, err
  634. }
  635. if l.Count == 0 {
  636. return nil, l.Count, fmt.Errorf("No match found for %s: %+v", id, l)
  637. }
  638. if l.Count == 1 {
  639. return l.GuestOsMapping[0], l.Count, nil
  640. }
  641. return nil, l.Count, fmt.Errorf("There is more then one result for GuestOsMapping UUID: %s!", id)
  642. }
  643. // Lists all available OS mappings for given hypervisor
  644. func (s *GuestOSService) ListGuestOsMapping(p *ListGuestOsMappingParams) (*ListGuestOsMappingResponse, error) {
  645. resp, err := s.cs.newRequest("listGuestOsMapping", p.toURLValues())
  646. if err != nil {
  647. return nil, err
  648. }
  649. var r ListGuestOsMappingResponse
  650. if err := json.Unmarshal(resp, &r); err != nil {
  651. return nil, err
  652. }
  653. return &r, nil
  654. }
  655. type ListGuestOsMappingResponse struct {
  656. Count int `json:"count"`
  657. GuestOsMapping []*GuestOsMapping `json:"guestosmapping"`
  658. }
  659. type GuestOsMapping struct {
  660. Hypervisor string `json:"hypervisor,omitempty"`
  661. Hypervisorversion string `json:"hypervisorversion,omitempty"`
  662. Id string `json:"id,omitempty"`
  663. Isuserdefined string `json:"isuserdefined,omitempty"`
  664. Osdisplayname string `json:"osdisplayname,omitempty"`
  665. Osnameforhypervisor string `json:"osnameforhypervisor,omitempty"`
  666. Ostypeid string `json:"ostypeid,omitempty"`
  667. }
  668. type AddGuestOsMappingParams struct {
  669. p map[string]interface{}
  670. }
  671. func (p *AddGuestOsMappingParams) toURLValues() url.Values {
  672. u := url.Values{}
  673. if p.p == nil {
  674. return u
  675. }
  676. if v, found := p.p["hypervisor"]; found {
  677. u.Set("hypervisor", v.(string))
  678. }
  679. if v, found := p.p["hypervisorversion"]; found {
  680. u.Set("hypervisorversion", v.(string))
  681. }
  682. if v, found := p.p["osdisplayname"]; found {
  683. u.Set("osdisplayname", v.(string))
  684. }
  685. if v, found := p.p["osnameforhypervisor"]; found {
  686. u.Set("osnameforhypervisor", v.(string))
  687. }
  688. if v, found := p.p["ostypeid"]; found {
  689. u.Set("ostypeid", v.(string))
  690. }
  691. return u
  692. }
  693. func (p *AddGuestOsMappingParams) SetHypervisor(v string) {
  694. if p.p == nil {
  695. p.p = make(map[string]interface{})
  696. }
  697. p.p["hypervisor"] = v
  698. return
  699. }
  700. func (p *AddGuestOsMappingParams) SetHypervisorversion(v string) {
  701. if p.p == nil {
  702. p.p = make(map[string]interface{})
  703. }
  704. p.p["hypervisorversion"] = v
  705. return
  706. }
  707. func (p *AddGuestOsMappingParams) SetOsdisplayname(v string) {
  708. if p.p == nil {
  709. p.p = make(map[string]interface{})
  710. }
  711. p.p["osdisplayname"] = v
  712. return
  713. }
  714. func (p *AddGuestOsMappingParams) SetOsnameforhypervisor(v string) {
  715. if p.p == nil {
  716. p.p = make(map[string]interface{})
  717. }
  718. p.p["osnameforhypervisor"] = v
  719. return
  720. }
  721. func (p *AddGuestOsMappingParams) SetOstypeid(v string) {
  722. if p.p == nil {
  723. p.p = make(map[string]interface{})
  724. }
  725. p.p["ostypeid"] = v
  726. return
  727. }
  728. // You should always use this function to get a new AddGuestOsMappingParams instance,
  729. // as then you are sure you have configured all required params
  730. func (s *GuestOSService) NewAddGuestOsMappingParams(hypervisor string, hypervisorversion string, osnameforhypervisor string) *AddGuestOsMappingParams {
  731. p := &AddGuestOsMappingParams{}
  732. p.p = make(map[string]interface{})
  733. p.p["hypervisor"] = hypervisor
  734. p.p["hypervisorversion"] = hypervisorversion
  735. p.p["osnameforhypervisor"] = osnameforhypervisor
  736. return p
  737. }
  738. // Adds a guest OS name to hypervisor OS name mapping
  739. func (s *GuestOSService) AddGuestOsMapping(p *AddGuestOsMappingParams) (*AddGuestOsMappingResponse, error) {
  740. resp, err := s.cs.newRequest("addGuestOsMapping", p.toURLValues())
  741. if err != nil {
  742. return nil, err
  743. }
  744. var r AddGuestOsMappingResponse
  745. if err := json.Unmarshal(resp, &r); err != nil {
  746. return nil, err
  747. }
  748. // If we have a async client, we need to wait for the async result
  749. if s.cs.async {
  750. b, err := s.cs.GetAsyncJobResult(r.JobID, s.cs.timeout)
  751. if err != nil {
  752. if err == AsyncTimeoutErr {
  753. return &r, err
  754. }
  755. return nil, err
  756. }
  757. b, err = getRawValue(b)
  758. if err != nil {
  759. return nil, err
  760. }
  761. if err := json.Unmarshal(b, &r); err != nil {
  762. return nil, err
  763. }
  764. }
  765. return &r, nil
  766. }
  767. type AddGuestOsMappingResponse struct {
  768. JobID string `json:"jobid,omitempty"`
  769. Hypervisor string `json:"hypervisor,omitempty"`
  770. Hypervisorversion string `json:"hypervisorversion,omitempty"`
  771. Id string `json:"id,omitempty"`
  772. Isuserdefined string `json:"isuserdefined,omitempty"`
  773. Osdisplayname string `json:"osdisplayname,omitempty"`
  774. Osnameforhypervisor string `json:"osnameforhypervisor,omitempty"`
  775. Ostypeid string `json:"ostypeid,omitempty"`
  776. }
  777. type UpdateGuestOsMappingParams struct {
  778. p map[string]interface{}
  779. }
  780. func (p *UpdateGuestOsMappingParams) toURLValues() url.Values {
  781. u := url.Values{}
  782. if p.p == nil {
  783. return u
  784. }
  785. if v, found := p.p["id"]; found {
  786. u.Set("id", v.(string))
  787. }
  788. if v, found := p.p["osnameforhypervisor"]; found {
  789. u.Set("osnameforhypervisor", v.(string))
  790. }
  791. return u
  792. }
  793. func (p *UpdateGuestOsMappingParams) SetId(v string) {
  794. if p.p == nil {
  795. p.p = make(map[string]interface{})
  796. }
  797. p.p["id"] = v
  798. return
  799. }
  800. func (p *UpdateGuestOsMappingParams) SetOsnameforhypervisor(v string) {
  801. if p.p == nil {
  802. p.p = make(map[string]interface{})
  803. }
  804. p.p["osnameforhypervisor"] = v
  805. return
  806. }
  807. // You should always use this function to get a new UpdateGuestOsMappingParams instance,
  808. // as then you are sure you have configured all required params
  809. func (s *GuestOSService) NewUpdateGuestOsMappingParams(id string, osnameforhypervisor string) *UpdateGuestOsMappingParams {
  810. p := &UpdateGuestOsMappingParams{}
  811. p.p = make(map[string]interface{})
  812. p.p["id"] = id
  813. p.p["osnameforhypervisor"] = osnameforhypervisor
  814. return p
  815. }
  816. // Updates the information about Guest OS to Hypervisor specific name mapping
  817. func (s *GuestOSService) UpdateGuestOsMapping(p *UpdateGuestOsMappingParams) (*UpdateGuestOsMappingResponse, error) {
  818. resp, err := s.cs.newRequest("updateGuestOsMapping", p.toURLValues())
  819. if err != nil {
  820. return nil, err
  821. }
  822. var r UpdateGuestOsMappingResponse
  823. if err := json.Unmarshal(resp, &r); err != nil {
  824. return nil, err
  825. }
  826. // If we have a async client, we need to wait for the async result
  827. if s.cs.async {
  828. b, err := s.cs.GetAsyncJobResult(r.JobID, s.cs.timeout)
  829. if err != nil {
  830. if err == AsyncTimeoutErr {
  831. return &r, err
  832. }
  833. return nil, err
  834. }
  835. b, err = getRawValue(b)
  836. if err != nil {
  837. return nil, err
  838. }
  839. if err := json.Unmarshal(b, &r); err != nil {
  840. return nil, err
  841. }
  842. }
  843. return &r, nil
  844. }
  845. type UpdateGuestOsMappingResponse struct {
  846. JobID string `json:"jobid,omitempty"`
  847. Hypervisor string `json:"hypervisor,omitempty"`
  848. Hypervisorversion string `json:"hypervisorversion,omitempty"`
  849. Id string `json:"id,omitempty"`
  850. Isuserdefined string `json:"isuserdefined,omitempty"`
  851. Osdisplayname string `json:"osdisplayname,omitempty"`
  852. Osnameforhypervisor string `json:"osnameforhypervisor,omitempty"`
  853. Ostypeid string `json:"ostypeid,omitempty"`
  854. }
  855. type RemoveGuestOsMappingParams struct {
  856. p map[string]interface{}
  857. }
  858. func (p *RemoveGuestOsMappingParams) toURLValues() url.Values {
  859. u := url.Values{}
  860. if p.p == nil {
  861. return u
  862. }
  863. if v, found := p.p["id"]; found {
  864. u.Set("id", v.(string))
  865. }
  866. return u
  867. }
  868. func (p *RemoveGuestOsMappingParams) SetId(v string) {
  869. if p.p == nil {
  870. p.p = make(map[string]interface{})
  871. }
  872. p.p["id"] = v
  873. return
  874. }
  875. // You should always use this function to get a new RemoveGuestOsMappingParams instance,
  876. // as then you are sure you have configured all required params
  877. func (s *GuestOSService) NewRemoveGuestOsMappingParams(id string) *RemoveGuestOsMappingParams {
  878. p := &RemoveGuestOsMappingParams{}
  879. p.p = make(map[string]interface{})
  880. p.p["id"] = id
  881. return p
  882. }
  883. // Removes a Guest OS Mapping.
  884. func (s *GuestOSService) RemoveGuestOsMapping(p *RemoveGuestOsMappingParams) (*RemoveGuestOsMappingResponse, error) {
  885. resp, err := s.cs.newRequest("removeGuestOsMapping", p.toURLValues())
  886. if err != nil {
  887. return nil, err
  888. }
  889. var r RemoveGuestOsMappingResponse
  890. if err := json.Unmarshal(resp, &r); err != nil {
  891. return nil, err
  892. }
  893. // If we have a async client, we need to wait for the async result
  894. if s.cs.async {
  895. b, err := s.cs.GetAsyncJobResult(r.JobID, s.cs.timeout)
  896. if err != nil {
  897. if err == AsyncTimeoutErr {
  898. return &r, err
  899. }
  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 RemoveGuestOsMappingResponse struct {
  909. JobID string `json:"jobid,omitempty"`
  910. Displaytext string `json:"displaytext,omitempty"`
  911. Success bool `json:"success,omitempty"`
  912. }