vms.go 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. // Copyright (c) 2016 VMware, Inc. All Rights Reserved.
  2. //
  3. // This product is licensed to you under the Apache License, Version 2.0 (the "License").
  4. // You may not use this product except in compliance with the License.
  5. //
  6. // This product may include a number of subcomponents with separate copyright notices and
  7. // license terms. Your use of these subcomponents is subject to the terms and conditions
  8. // of the subcomponent's license, as noted in the LICENSE file.
  9. package photon
  10. import (
  11. "bytes"
  12. "encoding/json"
  13. "io"
  14. )
  15. // Contains functionality for VMs API.
  16. type VmAPI struct {
  17. client *Client
  18. }
  19. var vmUrl string = rootUrl + "/vms/"
  20. func (api *VmAPI) Get(id string) (vm *VM, err error) {
  21. res, err := api.client.restClient.Get(api.client.Endpoint+vmUrl+id, api.client.options.TokenOptions)
  22. if err != nil {
  23. return
  24. }
  25. defer res.Body.Close()
  26. res, err = getError(res)
  27. if err != nil {
  28. return
  29. }
  30. vm = &VM{}
  31. err = json.NewDecoder(res.Body).Decode(vm)
  32. return
  33. }
  34. func (api *VmAPI) Delete(id string) (task *Task, err error) {
  35. res, err := api.client.restClient.Delete(api.client.Endpoint+vmUrl+id, api.client.options.TokenOptions)
  36. if err != nil {
  37. return
  38. }
  39. defer res.Body.Close()
  40. task, err = getTask(getError(res))
  41. return
  42. }
  43. func (api *VmAPI) AttachDisk(id string, op *VmDiskOperation) (task *Task, err error) {
  44. body, err := json.Marshal(op)
  45. if err != nil {
  46. return
  47. }
  48. res, err := api.client.restClient.Post(
  49. api.client.Endpoint+vmUrl+id+"/attach_disk",
  50. "application/json",
  51. bytes.NewReader(body),
  52. api.client.options.TokenOptions)
  53. if err != nil {
  54. return
  55. }
  56. defer res.Body.Close()
  57. task, err = getTask(getError(res))
  58. return
  59. }
  60. func (api *VmAPI) DetachDisk(id string, op *VmDiskOperation) (task *Task, err error) {
  61. body, err := json.Marshal(op)
  62. if err != nil {
  63. return
  64. }
  65. res, err := api.client.restClient.Post(
  66. api.client.Endpoint+vmUrl+id+"/detach_disk",
  67. "application/json",
  68. bytes.NewReader(body),
  69. api.client.options.TokenOptions)
  70. if err != nil {
  71. return
  72. }
  73. defer res.Body.Close()
  74. task, err = getTask(getError(res))
  75. return
  76. }
  77. func (api *VmAPI) AttachISO(id string, reader io.ReadSeeker, name string) (task *Task, err error) {
  78. res, err := api.client.restClient.MultipartUpload(
  79. api.client.Endpoint+vmUrl+id+"/attach_iso", reader, name, nil, api.client.options.TokenOptions)
  80. if err != nil {
  81. return
  82. }
  83. defer res.Body.Close()
  84. result, err := getTask(getError(res))
  85. return result, err
  86. }
  87. func (api *VmAPI) DetachISO(id string) (task *Task, err error) {
  88. body := []byte{}
  89. if err != nil {
  90. return
  91. }
  92. res, err := api.client.restClient.Post(
  93. api.client.Endpoint+vmUrl+id+"/detach_iso",
  94. "application/json",
  95. bytes.NewReader(body),
  96. api.client.options.TokenOptions)
  97. if err != nil {
  98. return
  99. }
  100. defer res.Body.Close()
  101. task, err = getTask(getError(res))
  102. return
  103. }
  104. func (api *VmAPI) Start(id string) (task *Task, err error) {
  105. body := []byte{}
  106. if err != nil {
  107. return
  108. }
  109. res, err := api.client.restClient.Post(
  110. api.client.Endpoint+vmUrl+id+"/start",
  111. "application/json",
  112. bytes.NewReader(body),
  113. api.client.options.TokenOptions)
  114. if err != nil {
  115. return
  116. }
  117. defer res.Body.Close()
  118. task, err = getTask(getError(res))
  119. return
  120. }
  121. func (api *VmAPI) Stop(id string) (task *Task, err error) {
  122. body := []byte{}
  123. if err != nil {
  124. return
  125. }
  126. res, err := api.client.restClient.Post(
  127. api.client.Endpoint+vmUrl+id+"/stop",
  128. "application/json",
  129. bytes.NewReader(body),
  130. api.client.options.TokenOptions)
  131. if err != nil {
  132. return
  133. }
  134. defer res.Body.Close()
  135. task, err = getTask(getError(res))
  136. return
  137. }
  138. func (api *VmAPI) Restart(id string) (task *Task, err error) {
  139. body := []byte{}
  140. if err != nil {
  141. return
  142. }
  143. res, err := api.client.restClient.Post(
  144. api.client.Endpoint+vmUrl+id+"/restart",
  145. "application/json",
  146. bytes.NewReader(body),
  147. api.client.options.TokenOptions)
  148. if err != nil {
  149. return
  150. }
  151. defer res.Body.Close()
  152. task, err = getTask(getError(res))
  153. return
  154. }
  155. func (api *VmAPI) Resume(id string) (task *Task, err error) {
  156. body := []byte{}
  157. if err != nil {
  158. return
  159. }
  160. res, err := api.client.restClient.Post(
  161. api.client.Endpoint+vmUrl+id+"/resume",
  162. "application/json",
  163. bytes.NewReader(body),
  164. api.client.options.TokenOptions)
  165. if err != nil {
  166. return
  167. }
  168. defer res.Body.Close()
  169. task, err = getTask(getError(res))
  170. return
  171. }
  172. func (api *VmAPI) Suspend(id string) (task *Task, err error) {
  173. body := []byte{}
  174. if err != nil {
  175. return
  176. }
  177. res, err := api.client.restClient.Post(
  178. api.client.Endpoint+vmUrl+id+"/suspend",
  179. "application/json",
  180. bytes.NewReader(body),
  181. api.client.options.TokenOptions)
  182. if err != nil {
  183. return
  184. }
  185. defer res.Body.Close()
  186. task, err = getTask(getError(res))
  187. return
  188. }
  189. func (api *VmAPI) SetMetadata(id string, metadata *VmMetadata) (task *Task, err error) {
  190. body, err := json.Marshal(metadata)
  191. if err != nil {
  192. return
  193. }
  194. res, err := api.client.restClient.Post(
  195. api.client.Endpoint+vmUrl+id+"/set_metadata",
  196. "application/json",
  197. bytes.NewReader(body),
  198. api.client.options.TokenOptions)
  199. if err != nil {
  200. return
  201. }
  202. defer res.Body.Close()
  203. task, err = getTask(getError(res))
  204. return
  205. }
  206. // Gets all tasks with the specified vm ID, using options to filter the results.
  207. // If options is nil, no filtering will occur.
  208. func (api *VmAPI) GetTasks(id string, options *TaskGetOptions) (result *TaskList, err error) {
  209. uri := api.client.Endpoint + vmUrl + id + "/tasks"
  210. if options != nil {
  211. uri += getQueryString(options)
  212. }
  213. res, err := api.client.restClient.GetList(api.client.Endpoint, uri, api.client.options.TokenOptions)
  214. if err != nil {
  215. return
  216. }
  217. result = &TaskList{}
  218. err = json.Unmarshal(res, result)
  219. return
  220. }
  221. func (api *VmAPI) GetNetworks(id string) (task *Task, err error) {
  222. res, err := api.client.restClient.Get(api.client.Endpoint+vmUrl+id+"/subnets", api.client.options.TokenOptions)
  223. if err != nil {
  224. return
  225. }
  226. defer res.Body.Close()
  227. task, err = getTask(getError(res))
  228. return
  229. }
  230. func (api *VmAPI) AcquireFloatingIp(id string, spec *VmFloatingIpSpec) (task *Task, err error) {
  231. body, err := json.Marshal(spec)
  232. if err != nil {
  233. return
  234. }
  235. res, err := api.client.restClient.Post(
  236. api.client.Endpoint+vmUrl+id+"/acquire_floating_ip",
  237. "application/json",
  238. bytes.NewReader(body),
  239. api.client.options.TokenOptions)
  240. if err != nil {
  241. return
  242. }
  243. defer res.Body.Close()
  244. task, err = getTask(getError(res))
  245. return
  246. }
  247. func (api *VmAPI) ReleaseFloatingIp(id string) (task *Task, err error) {
  248. res, err := api.client.restClient.Delete(
  249. api.client.Endpoint+vmUrl+id+"/release_floating_ip",
  250. api.client.options.TokenOptions)
  251. if err != nil {
  252. return
  253. }
  254. defer res.Body.Close()
  255. task, err = getTask(getError(res))
  256. return
  257. }
  258. func (api *VmAPI) GetMKSTicket(id string) (task *Task, err error) {
  259. res, err := api.client.restClient.Get(api.client.Endpoint+vmUrl+id+"/mks_ticket", api.client.options.TokenOptions)
  260. if err != nil {
  261. return
  262. }
  263. defer res.Body.Close()
  264. task, err = getTask(getError(res))
  265. return
  266. }
  267. func (api *VmAPI) SetTag(id string, tag *VmTag) (task *Task, err error) {
  268. body, err := json.Marshal(tag)
  269. if err != nil {
  270. return
  271. }
  272. res, err := api.client.restClient.Post(
  273. api.client.Endpoint+vmUrl+id+"/tags",
  274. "application/json",
  275. bytes.NewReader(body),
  276. api.client.options.TokenOptions)
  277. if err != nil {
  278. return
  279. }
  280. defer res.Body.Close()
  281. task, err = getTask(getError(res))
  282. return
  283. }
  284. func (api *VmAPI) CreateImage(id string, options *ImageCreateSpec) (task *Task, err error) {
  285. body, err := json.Marshal(options)
  286. if err != nil {
  287. return
  288. }
  289. res, err := api.client.restClient.Post(
  290. api.client.Endpoint+vmUrl+id+"/create_image",
  291. "application/json",
  292. bytes.NewReader(body),
  293. api.client.options.TokenOptions)
  294. if err != nil {
  295. return
  296. }
  297. defer res.Body.Close()
  298. task, err = getTask(getError(res))
  299. return
  300. }