UCSService.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  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 AddUcsManagerParams struct {
  25. p map[string]interface{}
  26. }
  27. func (p *AddUcsManagerParams) toURLValues() url.Values {
  28. u := url.Values{}
  29. if p.p == nil {
  30. return u
  31. }
  32. if v, found := p.p["name"]; found {
  33. u.Set("name", v.(string))
  34. }
  35. if v, found := p.p["password"]; found {
  36. u.Set("password", v.(string))
  37. }
  38. if v, found := p.p["url"]; found {
  39. u.Set("url", v.(string))
  40. }
  41. if v, found := p.p["username"]; found {
  42. u.Set("username", v.(string))
  43. }
  44. if v, found := p.p["zoneid"]; found {
  45. u.Set("zoneid", v.(string))
  46. }
  47. return u
  48. }
  49. func (p *AddUcsManagerParams) SetName(v string) {
  50. if p.p == nil {
  51. p.p = make(map[string]interface{})
  52. }
  53. p.p["name"] = v
  54. return
  55. }
  56. func (p *AddUcsManagerParams) SetPassword(v string) {
  57. if p.p == nil {
  58. p.p = make(map[string]interface{})
  59. }
  60. p.p["password"] = v
  61. return
  62. }
  63. func (p *AddUcsManagerParams) SetUrl(v string) {
  64. if p.p == nil {
  65. p.p = make(map[string]interface{})
  66. }
  67. p.p["url"] = v
  68. return
  69. }
  70. func (p *AddUcsManagerParams) SetUsername(v string) {
  71. if p.p == nil {
  72. p.p = make(map[string]interface{})
  73. }
  74. p.p["username"] = v
  75. return
  76. }
  77. func (p *AddUcsManagerParams) SetZoneid(v string) {
  78. if p.p == nil {
  79. p.p = make(map[string]interface{})
  80. }
  81. p.p["zoneid"] = v
  82. return
  83. }
  84. // You should always use this function to get a new AddUcsManagerParams instance,
  85. // as then you are sure you have configured all required params
  86. func (s *UCSService) NewAddUcsManagerParams(password string, url string, username string, zoneid string) *AddUcsManagerParams {
  87. p := &AddUcsManagerParams{}
  88. p.p = make(map[string]interface{})
  89. p.p["password"] = password
  90. p.p["url"] = url
  91. p.p["username"] = username
  92. p.p["zoneid"] = zoneid
  93. return p
  94. }
  95. // Adds a Ucs manager
  96. func (s *UCSService) AddUcsManager(p *AddUcsManagerParams) (*AddUcsManagerResponse, error) {
  97. resp, err := s.cs.newRequest("addUcsManager", p.toURLValues())
  98. if err != nil {
  99. return nil, err
  100. }
  101. var r AddUcsManagerResponse
  102. if err := json.Unmarshal(resp, &r); err != nil {
  103. return nil, err
  104. }
  105. return &r, nil
  106. }
  107. type AddUcsManagerResponse struct {
  108. Id string `json:"id,omitempty"`
  109. Name string `json:"name,omitempty"`
  110. Url string `json:"url,omitempty"`
  111. Zoneid string `json:"zoneid,omitempty"`
  112. }
  113. type ListUcsManagersParams struct {
  114. p map[string]interface{}
  115. }
  116. func (p *ListUcsManagersParams) toURLValues() url.Values {
  117. u := url.Values{}
  118. if p.p == nil {
  119. return u
  120. }
  121. if v, found := p.p["id"]; found {
  122. u.Set("id", v.(string))
  123. }
  124. if v, found := p.p["keyword"]; found {
  125. u.Set("keyword", v.(string))
  126. }
  127. if v, found := p.p["page"]; found {
  128. vv := strconv.Itoa(v.(int))
  129. u.Set("page", vv)
  130. }
  131. if v, found := p.p["pagesize"]; found {
  132. vv := strconv.Itoa(v.(int))
  133. u.Set("pagesize", vv)
  134. }
  135. if v, found := p.p["zoneid"]; found {
  136. u.Set("zoneid", v.(string))
  137. }
  138. return u
  139. }
  140. func (p *ListUcsManagersParams) SetId(v string) {
  141. if p.p == nil {
  142. p.p = make(map[string]interface{})
  143. }
  144. p.p["id"] = v
  145. return
  146. }
  147. func (p *ListUcsManagersParams) SetKeyword(v string) {
  148. if p.p == nil {
  149. p.p = make(map[string]interface{})
  150. }
  151. p.p["keyword"] = v
  152. return
  153. }
  154. func (p *ListUcsManagersParams) SetPage(v int) {
  155. if p.p == nil {
  156. p.p = make(map[string]interface{})
  157. }
  158. p.p["page"] = v
  159. return
  160. }
  161. func (p *ListUcsManagersParams) SetPagesize(v int) {
  162. if p.p == nil {
  163. p.p = make(map[string]interface{})
  164. }
  165. p.p["pagesize"] = v
  166. return
  167. }
  168. func (p *ListUcsManagersParams) SetZoneid(v string) {
  169. if p.p == nil {
  170. p.p = make(map[string]interface{})
  171. }
  172. p.p["zoneid"] = v
  173. return
  174. }
  175. // You should always use this function to get a new ListUcsManagersParams instance,
  176. // as then you are sure you have configured all required params
  177. func (s *UCSService) NewListUcsManagersParams() *ListUcsManagersParams {
  178. p := &ListUcsManagersParams{}
  179. p.p = make(map[string]interface{})
  180. return p
  181. }
  182. // This is a courtesy helper function, which in some cases may not work as expected!
  183. func (s *UCSService) GetUcsManagerID(keyword string, opts ...OptionFunc) (string, int, error) {
  184. p := &ListUcsManagersParams{}
  185. p.p = make(map[string]interface{})
  186. p.p["keyword"] = keyword
  187. for _, fn := range opts {
  188. if err := fn(s.cs, p); err != nil {
  189. return "", -1, err
  190. }
  191. }
  192. l, err := s.ListUcsManagers(p)
  193. if err != nil {
  194. return "", -1, err
  195. }
  196. if l.Count == 0 {
  197. return "", l.Count, fmt.Errorf("No match found for %s: %+v", keyword, l)
  198. }
  199. if l.Count == 1 {
  200. return l.UcsManagers[0].Id, l.Count, nil
  201. }
  202. if l.Count > 1 {
  203. for _, v := range l.UcsManagers {
  204. if v.Name == keyword {
  205. return v.Id, l.Count, nil
  206. }
  207. }
  208. }
  209. return "", l.Count, fmt.Errorf("Could not find an exact match for %s: %+v", keyword, l)
  210. }
  211. // This is a courtesy helper function, which in some cases may not work as expected!
  212. func (s *UCSService) GetUcsManagerByName(name string, opts ...OptionFunc) (*UcsManager, int, error) {
  213. id, count, err := s.GetUcsManagerID(name, opts...)
  214. if err != nil {
  215. return nil, count, err
  216. }
  217. r, count, err := s.GetUcsManagerByID(id, opts...)
  218. if err != nil {
  219. return nil, count, err
  220. }
  221. return r, count, nil
  222. }
  223. // This is a courtesy helper function, which in some cases may not work as expected!
  224. func (s *UCSService) GetUcsManagerByID(id string, opts ...OptionFunc) (*UcsManager, int, error) {
  225. p := &ListUcsManagersParams{}
  226. p.p = make(map[string]interface{})
  227. p.p["id"] = id
  228. for _, fn := range opts {
  229. if err := fn(s.cs, p); err != nil {
  230. return nil, -1, err
  231. }
  232. }
  233. l, err := s.ListUcsManagers(p)
  234. if err != nil {
  235. if strings.Contains(err.Error(), fmt.Sprintf(
  236. "Invalid parameter id value=%s due to incorrect long value format, "+
  237. "or entity does not exist", id)) {
  238. return nil, 0, fmt.Errorf("No match found for %s: %+v", id, l)
  239. }
  240. return nil, -1, err
  241. }
  242. if l.Count == 0 {
  243. return nil, l.Count, fmt.Errorf("No match found for %s: %+v", id, l)
  244. }
  245. if l.Count == 1 {
  246. return l.UcsManagers[0], l.Count, nil
  247. }
  248. return nil, l.Count, fmt.Errorf("There is more then one result for UcsManager UUID: %s!", id)
  249. }
  250. // List ucs manager
  251. func (s *UCSService) ListUcsManagers(p *ListUcsManagersParams) (*ListUcsManagersResponse, error) {
  252. resp, err := s.cs.newRequest("listUcsManagers", p.toURLValues())
  253. if err != nil {
  254. return nil, err
  255. }
  256. var r ListUcsManagersResponse
  257. if err := json.Unmarshal(resp, &r); err != nil {
  258. return nil, err
  259. }
  260. return &r, nil
  261. }
  262. type ListUcsManagersResponse struct {
  263. Count int `json:"count"`
  264. UcsManagers []*UcsManager `json:"ucsmanager"`
  265. }
  266. type UcsManager struct {
  267. Id string `json:"id,omitempty"`
  268. Name string `json:"name,omitempty"`
  269. Url string `json:"url,omitempty"`
  270. Zoneid string `json:"zoneid,omitempty"`
  271. }
  272. type ListUcsProfilesParams struct {
  273. p map[string]interface{}
  274. }
  275. func (p *ListUcsProfilesParams) toURLValues() url.Values {
  276. u := url.Values{}
  277. if p.p == nil {
  278. return u
  279. }
  280. if v, found := p.p["keyword"]; found {
  281. u.Set("keyword", v.(string))
  282. }
  283. if v, found := p.p["page"]; found {
  284. vv := strconv.Itoa(v.(int))
  285. u.Set("page", vv)
  286. }
  287. if v, found := p.p["pagesize"]; found {
  288. vv := strconv.Itoa(v.(int))
  289. u.Set("pagesize", vv)
  290. }
  291. if v, found := p.p["ucsmanagerid"]; found {
  292. u.Set("ucsmanagerid", v.(string))
  293. }
  294. return u
  295. }
  296. func (p *ListUcsProfilesParams) SetKeyword(v string) {
  297. if p.p == nil {
  298. p.p = make(map[string]interface{})
  299. }
  300. p.p["keyword"] = v
  301. return
  302. }
  303. func (p *ListUcsProfilesParams) SetPage(v int) {
  304. if p.p == nil {
  305. p.p = make(map[string]interface{})
  306. }
  307. p.p["page"] = v
  308. return
  309. }
  310. func (p *ListUcsProfilesParams) SetPagesize(v int) {
  311. if p.p == nil {
  312. p.p = make(map[string]interface{})
  313. }
  314. p.p["pagesize"] = v
  315. return
  316. }
  317. func (p *ListUcsProfilesParams) SetUcsmanagerid(v string) {
  318. if p.p == nil {
  319. p.p = make(map[string]interface{})
  320. }
  321. p.p["ucsmanagerid"] = v
  322. return
  323. }
  324. // You should always use this function to get a new ListUcsProfilesParams instance,
  325. // as then you are sure you have configured all required params
  326. func (s *UCSService) NewListUcsProfilesParams(ucsmanagerid string) *ListUcsProfilesParams {
  327. p := &ListUcsProfilesParams{}
  328. p.p = make(map[string]interface{})
  329. p.p["ucsmanagerid"] = ucsmanagerid
  330. return p
  331. }
  332. // List profile in ucs manager
  333. func (s *UCSService) ListUcsProfiles(p *ListUcsProfilesParams) (*ListUcsProfilesResponse, error) {
  334. resp, err := s.cs.newRequest("listUcsProfiles", p.toURLValues())
  335. if err != nil {
  336. return nil, err
  337. }
  338. var r ListUcsProfilesResponse
  339. if err := json.Unmarshal(resp, &r); err != nil {
  340. return nil, err
  341. }
  342. return &r, nil
  343. }
  344. type ListUcsProfilesResponse struct {
  345. Count int `json:"count"`
  346. UcsProfiles []*UcsProfile `json:"ucsprofile"`
  347. }
  348. type UcsProfile struct {
  349. Ucsdn string `json:"ucsdn,omitempty"`
  350. }
  351. type ListUcsBladesParams struct {
  352. p map[string]interface{}
  353. }
  354. func (p *ListUcsBladesParams) toURLValues() url.Values {
  355. u := url.Values{}
  356. if p.p == nil {
  357. return u
  358. }
  359. if v, found := p.p["keyword"]; found {
  360. u.Set("keyword", v.(string))
  361. }
  362. if v, found := p.p["page"]; found {
  363. vv := strconv.Itoa(v.(int))
  364. u.Set("page", vv)
  365. }
  366. if v, found := p.p["pagesize"]; found {
  367. vv := strconv.Itoa(v.(int))
  368. u.Set("pagesize", vv)
  369. }
  370. if v, found := p.p["ucsmanagerid"]; found {
  371. u.Set("ucsmanagerid", v.(string))
  372. }
  373. return u
  374. }
  375. func (p *ListUcsBladesParams) SetKeyword(v string) {
  376. if p.p == nil {
  377. p.p = make(map[string]interface{})
  378. }
  379. p.p["keyword"] = v
  380. return
  381. }
  382. func (p *ListUcsBladesParams) SetPage(v int) {
  383. if p.p == nil {
  384. p.p = make(map[string]interface{})
  385. }
  386. p.p["page"] = v
  387. return
  388. }
  389. func (p *ListUcsBladesParams) SetPagesize(v int) {
  390. if p.p == nil {
  391. p.p = make(map[string]interface{})
  392. }
  393. p.p["pagesize"] = v
  394. return
  395. }
  396. func (p *ListUcsBladesParams) SetUcsmanagerid(v string) {
  397. if p.p == nil {
  398. p.p = make(map[string]interface{})
  399. }
  400. p.p["ucsmanagerid"] = v
  401. return
  402. }
  403. // You should always use this function to get a new ListUcsBladesParams instance,
  404. // as then you are sure you have configured all required params
  405. func (s *UCSService) NewListUcsBladesParams(ucsmanagerid string) *ListUcsBladesParams {
  406. p := &ListUcsBladesParams{}
  407. p.p = make(map[string]interface{})
  408. p.p["ucsmanagerid"] = ucsmanagerid
  409. return p
  410. }
  411. // List ucs blades
  412. func (s *UCSService) ListUcsBlades(p *ListUcsBladesParams) (*ListUcsBladesResponse, error) {
  413. resp, err := s.cs.newRequest("listUcsBlades", p.toURLValues())
  414. if err != nil {
  415. return nil, err
  416. }
  417. var r ListUcsBladesResponse
  418. if err := json.Unmarshal(resp, &r); err != nil {
  419. return nil, err
  420. }
  421. return &r, nil
  422. }
  423. type ListUcsBladesResponse struct {
  424. Count int `json:"count"`
  425. UcsBlades []*UcsBlade `json:"ucsblade"`
  426. }
  427. type UcsBlade struct {
  428. Bladedn string `json:"bladedn,omitempty"`
  429. Hostid string `json:"hostid,omitempty"`
  430. Id string `json:"id,omitempty"`
  431. Profiledn string `json:"profiledn,omitempty"`
  432. Ucsmanagerid string `json:"ucsmanagerid,omitempty"`
  433. }
  434. type AssociateUcsProfileToBladeParams struct {
  435. p map[string]interface{}
  436. }
  437. func (p *AssociateUcsProfileToBladeParams) toURLValues() url.Values {
  438. u := url.Values{}
  439. if p.p == nil {
  440. return u
  441. }
  442. if v, found := p.p["bladeid"]; found {
  443. u.Set("bladeid", v.(string))
  444. }
  445. if v, found := p.p["profiledn"]; found {
  446. u.Set("profiledn", v.(string))
  447. }
  448. if v, found := p.p["ucsmanagerid"]; found {
  449. u.Set("ucsmanagerid", v.(string))
  450. }
  451. return u
  452. }
  453. func (p *AssociateUcsProfileToBladeParams) SetBladeid(v string) {
  454. if p.p == nil {
  455. p.p = make(map[string]interface{})
  456. }
  457. p.p["bladeid"] = v
  458. return
  459. }
  460. func (p *AssociateUcsProfileToBladeParams) SetProfiledn(v string) {
  461. if p.p == nil {
  462. p.p = make(map[string]interface{})
  463. }
  464. p.p["profiledn"] = v
  465. return
  466. }
  467. func (p *AssociateUcsProfileToBladeParams) SetUcsmanagerid(v string) {
  468. if p.p == nil {
  469. p.p = make(map[string]interface{})
  470. }
  471. p.p["ucsmanagerid"] = v
  472. return
  473. }
  474. // You should always use this function to get a new AssociateUcsProfileToBladeParams instance,
  475. // as then you are sure you have configured all required params
  476. func (s *UCSService) NewAssociateUcsProfileToBladeParams(bladeid string, profiledn string, ucsmanagerid string) *AssociateUcsProfileToBladeParams {
  477. p := &AssociateUcsProfileToBladeParams{}
  478. p.p = make(map[string]interface{})
  479. p.p["bladeid"] = bladeid
  480. p.p["profiledn"] = profiledn
  481. p.p["ucsmanagerid"] = ucsmanagerid
  482. return p
  483. }
  484. // associate a profile to a blade
  485. func (s *UCSService) AssociateUcsProfileToBlade(p *AssociateUcsProfileToBladeParams) (*AssociateUcsProfileToBladeResponse, error) {
  486. resp, err := s.cs.newRequest("associateUcsProfileToBlade", p.toURLValues())
  487. if err != nil {
  488. return nil, err
  489. }
  490. var r AssociateUcsProfileToBladeResponse
  491. if err := json.Unmarshal(resp, &r); err != nil {
  492. return nil, err
  493. }
  494. // If we have a async client, we need to wait for the async result
  495. if s.cs.async {
  496. b, err := s.cs.GetAsyncJobResult(r.JobID, s.cs.timeout)
  497. if err != nil {
  498. if err == AsyncTimeoutErr {
  499. return &r, err
  500. }
  501. return nil, err
  502. }
  503. b, err = getRawValue(b)
  504. if err != nil {
  505. return nil, err
  506. }
  507. if err := json.Unmarshal(b, &r); err != nil {
  508. return nil, err
  509. }
  510. }
  511. return &r, nil
  512. }
  513. type AssociateUcsProfileToBladeResponse struct {
  514. JobID string `json:"jobid,omitempty"`
  515. Bladedn string `json:"bladedn,omitempty"`
  516. Hostid string `json:"hostid,omitempty"`
  517. Id string `json:"id,omitempty"`
  518. Profiledn string `json:"profiledn,omitempty"`
  519. Ucsmanagerid string `json:"ucsmanagerid,omitempty"`
  520. }