VMGroupService.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  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 CreateInstanceGroupParams struct {
  25. p map[string]interface{}
  26. }
  27. func (p *CreateInstanceGroupParams) toURLValues() url.Values {
  28. u := url.Values{}
  29. if p.p == nil {
  30. return u
  31. }
  32. if v, found := p.p["account"]; found {
  33. u.Set("account", v.(string))
  34. }
  35. if v, found := p.p["domainid"]; found {
  36. u.Set("domainid", v.(string))
  37. }
  38. if v, found := p.p["name"]; found {
  39. u.Set("name", v.(string))
  40. }
  41. if v, found := p.p["projectid"]; found {
  42. u.Set("projectid", v.(string))
  43. }
  44. return u
  45. }
  46. func (p *CreateInstanceGroupParams) SetAccount(v string) {
  47. if p.p == nil {
  48. p.p = make(map[string]interface{})
  49. }
  50. p.p["account"] = v
  51. return
  52. }
  53. func (p *CreateInstanceGroupParams) SetDomainid(v string) {
  54. if p.p == nil {
  55. p.p = make(map[string]interface{})
  56. }
  57. p.p["domainid"] = v
  58. return
  59. }
  60. func (p *CreateInstanceGroupParams) SetName(v string) {
  61. if p.p == nil {
  62. p.p = make(map[string]interface{})
  63. }
  64. p.p["name"] = v
  65. return
  66. }
  67. func (p *CreateInstanceGroupParams) SetProjectid(v string) {
  68. if p.p == nil {
  69. p.p = make(map[string]interface{})
  70. }
  71. p.p["projectid"] = v
  72. return
  73. }
  74. // You should always use this function to get a new CreateInstanceGroupParams instance,
  75. // as then you are sure you have configured all required params
  76. func (s *VMGroupService) NewCreateInstanceGroupParams(name string) *CreateInstanceGroupParams {
  77. p := &CreateInstanceGroupParams{}
  78. p.p = make(map[string]interface{})
  79. p.p["name"] = name
  80. return p
  81. }
  82. // Creates a vm group
  83. func (s *VMGroupService) CreateInstanceGroup(p *CreateInstanceGroupParams) (*CreateInstanceGroupResponse, error) {
  84. resp, err := s.cs.newRequest("createInstanceGroup", p.toURLValues())
  85. if err != nil {
  86. return nil, err
  87. }
  88. var r CreateInstanceGroupResponse
  89. if err := json.Unmarshal(resp, &r); err != nil {
  90. return nil, err
  91. }
  92. return &r, nil
  93. }
  94. type CreateInstanceGroupResponse struct {
  95. Account string `json:"account,omitempty"`
  96. Created string `json:"created,omitempty"`
  97. Domain string `json:"domain,omitempty"`
  98. Domainid string `json:"domainid,omitempty"`
  99. Id string `json:"id,omitempty"`
  100. Name string `json:"name,omitempty"`
  101. Project string `json:"project,omitempty"`
  102. Projectid string `json:"projectid,omitempty"`
  103. }
  104. type DeleteInstanceGroupParams struct {
  105. p map[string]interface{}
  106. }
  107. func (p *DeleteInstanceGroupParams) toURLValues() url.Values {
  108. u := url.Values{}
  109. if p.p == nil {
  110. return u
  111. }
  112. if v, found := p.p["id"]; found {
  113. u.Set("id", v.(string))
  114. }
  115. return u
  116. }
  117. func (p *DeleteInstanceGroupParams) SetId(v string) {
  118. if p.p == nil {
  119. p.p = make(map[string]interface{})
  120. }
  121. p.p["id"] = v
  122. return
  123. }
  124. // You should always use this function to get a new DeleteInstanceGroupParams instance,
  125. // as then you are sure you have configured all required params
  126. func (s *VMGroupService) NewDeleteInstanceGroupParams(id string) *DeleteInstanceGroupParams {
  127. p := &DeleteInstanceGroupParams{}
  128. p.p = make(map[string]interface{})
  129. p.p["id"] = id
  130. return p
  131. }
  132. // Deletes a vm group
  133. func (s *VMGroupService) DeleteInstanceGroup(p *DeleteInstanceGroupParams) (*DeleteInstanceGroupResponse, error) {
  134. resp, err := s.cs.newRequest("deleteInstanceGroup", p.toURLValues())
  135. if err != nil {
  136. return nil, err
  137. }
  138. var r DeleteInstanceGroupResponse
  139. if err := json.Unmarshal(resp, &r); err != nil {
  140. return nil, err
  141. }
  142. return &r, nil
  143. }
  144. type DeleteInstanceGroupResponse struct {
  145. Displaytext string `json:"displaytext,omitempty"`
  146. Success string `json:"success,omitempty"`
  147. }
  148. type UpdateInstanceGroupParams struct {
  149. p map[string]interface{}
  150. }
  151. func (p *UpdateInstanceGroupParams) toURLValues() url.Values {
  152. u := url.Values{}
  153. if p.p == nil {
  154. return u
  155. }
  156. if v, found := p.p["id"]; found {
  157. u.Set("id", v.(string))
  158. }
  159. if v, found := p.p["name"]; found {
  160. u.Set("name", v.(string))
  161. }
  162. return u
  163. }
  164. func (p *UpdateInstanceGroupParams) SetId(v string) {
  165. if p.p == nil {
  166. p.p = make(map[string]interface{})
  167. }
  168. p.p["id"] = v
  169. return
  170. }
  171. func (p *UpdateInstanceGroupParams) SetName(v string) {
  172. if p.p == nil {
  173. p.p = make(map[string]interface{})
  174. }
  175. p.p["name"] = v
  176. return
  177. }
  178. // You should always use this function to get a new UpdateInstanceGroupParams instance,
  179. // as then you are sure you have configured all required params
  180. func (s *VMGroupService) NewUpdateInstanceGroupParams(id string) *UpdateInstanceGroupParams {
  181. p := &UpdateInstanceGroupParams{}
  182. p.p = make(map[string]interface{})
  183. p.p["id"] = id
  184. return p
  185. }
  186. // Updates a vm group
  187. func (s *VMGroupService) UpdateInstanceGroup(p *UpdateInstanceGroupParams) (*UpdateInstanceGroupResponse, error) {
  188. resp, err := s.cs.newRequest("updateInstanceGroup", p.toURLValues())
  189. if err != nil {
  190. return nil, err
  191. }
  192. var r UpdateInstanceGroupResponse
  193. if err := json.Unmarshal(resp, &r); err != nil {
  194. return nil, err
  195. }
  196. return &r, nil
  197. }
  198. type UpdateInstanceGroupResponse struct {
  199. Account string `json:"account,omitempty"`
  200. Created string `json:"created,omitempty"`
  201. Domain string `json:"domain,omitempty"`
  202. Domainid string `json:"domainid,omitempty"`
  203. Id string `json:"id,omitempty"`
  204. Name string `json:"name,omitempty"`
  205. Project string `json:"project,omitempty"`
  206. Projectid string `json:"projectid,omitempty"`
  207. }
  208. type ListInstanceGroupsParams struct {
  209. p map[string]interface{}
  210. }
  211. func (p *ListInstanceGroupsParams) toURLValues() url.Values {
  212. u := url.Values{}
  213. if p.p == nil {
  214. return u
  215. }
  216. if v, found := p.p["account"]; found {
  217. u.Set("account", v.(string))
  218. }
  219. if v, found := p.p["domainid"]; found {
  220. u.Set("domainid", v.(string))
  221. }
  222. if v, found := p.p["id"]; found {
  223. u.Set("id", v.(string))
  224. }
  225. if v, found := p.p["isrecursive"]; found {
  226. vv := strconv.FormatBool(v.(bool))
  227. u.Set("isrecursive", vv)
  228. }
  229. if v, found := p.p["keyword"]; found {
  230. u.Set("keyword", v.(string))
  231. }
  232. if v, found := p.p["listall"]; found {
  233. vv := strconv.FormatBool(v.(bool))
  234. u.Set("listall", vv)
  235. }
  236. if v, found := p.p["name"]; found {
  237. u.Set("name", v.(string))
  238. }
  239. if v, found := p.p["page"]; found {
  240. vv := strconv.Itoa(v.(int))
  241. u.Set("page", vv)
  242. }
  243. if v, found := p.p["pagesize"]; found {
  244. vv := strconv.Itoa(v.(int))
  245. u.Set("pagesize", vv)
  246. }
  247. if v, found := p.p["projectid"]; found {
  248. u.Set("projectid", v.(string))
  249. }
  250. return u
  251. }
  252. func (p *ListInstanceGroupsParams) SetAccount(v string) {
  253. if p.p == nil {
  254. p.p = make(map[string]interface{})
  255. }
  256. p.p["account"] = v
  257. return
  258. }
  259. func (p *ListInstanceGroupsParams) SetDomainid(v string) {
  260. if p.p == nil {
  261. p.p = make(map[string]interface{})
  262. }
  263. p.p["domainid"] = v
  264. return
  265. }
  266. func (p *ListInstanceGroupsParams) SetId(v string) {
  267. if p.p == nil {
  268. p.p = make(map[string]interface{})
  269. }
  270. p.p["id"] = v
  271. return
  272. }
  273. func (p *ListInstanceGroupsParams) SetIsrecursive(v bool) {
  274. if p.p == nil {
  275. p.p = make(map[string]interface{})
  276. }
  277. p.p["isrecursive"] = v
  278. return
  279. }
  280. func (p *ListInstanceGroupsParams) SetKeyword(v string) {
  281. if p.p == nil {
  282. p.p = make(map[string]interface{})
  283. }
  284. p.p["keyword"] = v
  285. return
  286. }
  287. func (p *ListInstanceGroupsParams) SetListall(v bool) {
  288. if p.p == nil {
  289. p.p = make(map[string]interface{})
  290. }
  291. p.p["listall"] = v
  292. return
  293. }
  294. func (p *ListInstanceGroupsParams) SetName(v string) {
  295. if p.p == nil {
  296. p.p = make(map[string]interface{})
  297. }
  298. p.p["name"] = v
  299. return
  300. }
  301. func (p *ListInstanceGroupsParams) SetPage(v int) {
  302. if p.p == nil {
  303. p.p = make(map[string]interface{})
  304. }
  305. p.p["page"] = v
  306. return
  307. }
  308. func (p *ListInstanceGroupsParams) SetPagesize(v int) {
  309. if p.p == nil {
  310. p.p = make(map[string]interface{})
  311. }
  312. p.p["pagesize"] = v
  313. return
  314. }
  315. func (p *ListInstanceGroupsParams) SetProjectid(v string) {
  316. if p.p == nil {
  317. p.p = make(map[string]interface{})
  318. }
  319. p.p["projectid"] = v
  320. return
  321. }
  322. // You should always use this function to get a new ListInstanceGroupsParams instance,
  323. // as then you are sure you have configured all required params
  324. func (s *VMGroupService) NewListInstanceGroupsParams() *ListInstanceGroupsParams {
  325. p := &ListInstanceGroupsParams{}
  326. p.p = make(map[string]interface{})
  327. return p
  328. }
  329. // This is a courtesy helper function, which in some cases may not work as expected!
  330. func (s *VMGroupService) GetInstanceGroupID(name string, opts ...OptionFunc) (string, int, error) {
  331. p := &ListInstanceGroupsParams{}
  332. p.p = make(map[string]interface{})
  333. p.p["name"] = name
  334. for _, fn := range opts {
  335. if err := fn(s.cs, p); err != nil {
  336. return "", -1, err
  337. }
  338. }
  339. l, err := s.ListInstanceGroups(p)
  340. if err != nil {
  341. return "", -1, err
  342. }
  343. if l.Count == 0 {
  344. return "", l.Count, fmt.Errorf("No match found for %s: %+v", name, l)
  345. }
  346. if l.Count == 1 {
  347. return l.InstanceGroups[0].Id, l.Count, nil
  348. }
  349. if l.Count > 1 {
  350. for _, v := range l.InstanceGroups {
  351. if v.Name == name {
  352. return v.Id, l.Count, nil
  353. }
  354. }
  355. }
  356. return "", l.Count, fmt.Errorf("Could not find an exact match for %s: %+v", name, l)
  357. }
  358. // This is a courtesy helper function, which in some cases may not work as expected!
  359. func (s *VMGroupService) GetInstanceGroupByName(name string, opts ...OptionFunc) (*InstanceGroup, int, error) {
  360. id, count, err := s.GetInstanceGroupID(name, opts...)
  361. if err != nil {
  362. return nil, count, err
  363. }
  364. r, count, err := s.GetInstanceGroupByID(id, opts...)
  365. if err != nil {
  366. return nil, count, err
  367. }
  368. return r, count, nil
  369. }
  370. // This is a courtesy helper function, which in some cases may not work as expected!
  371. func (s *VMGroupService) GetInstanceGroupByID(id string, opts ...OptionFunc) (*InstanceGroup, int, error) {
  372. p := &ListInstanceGroupsParams{}
  373. p.p = make(map[string]interface{})
  374. p.p["id"] = id
  375. for _, fn := range opts {
  376. if err := fn(s.cs, p); err != nil {
  377. return nil, -1, err
  378. }
  379. }
  380. l, err := s.ListInstanceGroups(p)
  381. if err != nil {
  382. if strings.Contains(err.Error(), fmt.Sprintf(
  383. "Invalid parameter id value=%s due to incorrect long value format, "+
  384. "or entity does not exist", id)) {
  385. return nil, 0, fmt.Errorf("No match found for %s: %+v", id, l)
  386. }
  387. return nil, -1, err
  388. }
  389. if l.Count == 0 {
  390. return nil, l.Count, fmt.Errorf("No match found for %s: %+v", id, l)
  391. }
  392. if l.Count == 1 {
  393. return l.InstanceGroups[0], l.Count, nil
  394. }
  395. return nil, l.Count, fmt.Errorf("There is more then one result for InstanceGroup UUID: %s!", id)
  396. }
  397. // Lists vm groups
  398. func (s *VMGroupService) ListInstanceGroups(p *ListInstanceGroupsParams) (*ListInstanceGroupsResponse, error) {
  399. resp, err := s.cs.newRequest("listInstanceGroups", p.toURLValues())
  400. if err != nil {
  401. return nil, err
  402. }
  403. var r ListInstanceGroupsResponse
  404. if err := json.Unmarshal(resp, &r); err != nil {
  405. return nil, err
  406. }
  407. return &r, nil
  408. }
  409. type ListInstanceGroupsResponse struct {
  410. Count int `json:"count"`
  411. InstanceGroups []*InstanceGroup `json:"instancegroup"`
  412. }
  413. type InstanceGroup struct {
  414. Account string `json:"account,omitempty"`
  415. Created string `json:"created,omitempty"`
  416. Domain string `json:"domain,omitempty"`
  417. Domainid string `json:"domainid,omitempty"`
  418. Id string `json:"id,omitempty"`
  419. Name string `json:"name,omitempty"`
  420. Project string `json:"project,omitempty"`
  421. Projectid string `json:"projectid,omitempty"`
  422. }