deployments.go 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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. )
  14. // Contains functionality for deployments API.
  15. type DeploymentsAPI struct {
  16. client *Client
  17. }
  18. var deploymentUrl string = rootUrl + "/deployments"
  19. // Creates a deployment
  20. func (api *DeploymentsAPI) Create(deploymentSpec *DeploymentCreateSpec) (task *Task, err error) {
  21. body, err := json.Marshal(deploymentSpec)
  22. if err != nil {
  23. return
  24. }
  25. res, err := api.client.restClient.Post(
  26. api.client.Endpoint+deploymentUrl,
  27. "application/json",
  28. bytes.NewReader(body),
  29. api.client.options.TokenOptions)
  30. if err != nil {
  31. return
  32. }
  33. defer res.Body.Close()
  34. task, err = getTask(getError(res))
  35. return
  36. }
  37. // Deletes a deployment with specified ID.
  38. func (api *DeploymentsAPI) Delete(id string) (task *Task, err error) {
  39. res, err := api.client.restClient.Delete(api.getEntityUrl(id), api.client.options.TokenOptions)
  40. if err != nil {
  41. return
  42. }
  43. defer res.Body.Close()
  44. task, err = getTask(getError(res))
  45. return
  46. }
  47. // Deploys a deployment with specified ID.
  48. func (api *DeploymentsAPI) Deploy(id string, config *DeploymentDeployOperation) (task *Task, err error) {
  49. body, err := json.Marshal(config)
  50. if err != nil {
  51. return
  52. }
  53. res, err := api.client.restClient.Post(
  54. api.getEntityUrl(id)+"/deploy",
  55. "application/json",
  56. bytes.NewReader(body),
  57. api.client.options.TokenOptions)
  58. if err != nil {
  59. return
  60. }
  61. defer res.Body.Close()
  62. task, err = getTask(getError(res))
  63. return
  64. }
  65. // Destroys a deployment with specified ID.
  66. func (api *DeploymentsAPI) Destroy(id string) (task *Task, err error) {
  67. res, err := api.client.restClient.Post(
  68. api.getEntityUrl(id)+"/destroy",
  69. "application/json",
  70. bytes.NewReader([]byte("")),
  71. api.client.options.TokenOptions)
  72. if err != nil {
  73. return
  74. }
  75. defer res.Body.Close()
  76. task, err = getTask(getError(res))
  77. return
  78. }
  79. // Returns all deployments.
  80. func (api *DeploymentsAPI) GetAll() (result *Deployments, err error) {
  81. res, err := api.client.restClient.Get(api.client.Endpoint+deploymentUrl, api.client.options.TokenOptions)
  82. if err != nil {
  83. return
  84. }
  85. defer res.Body.Close()
  86. res, err = getError(res)
  87. if err != nil {
  88. return
  89. }
  90. result = &Deployments{}
  91. err = json.NewDecoder(res.Body).Decode(result)
  92. return
  93. }
  94. // Gets a deployment with the specified ID.
  95. func (api *DeploymentsAPI) Get(id string) (deployment *Deployment, err error) {
  96. res, err := api.client.restClient.Get(api.getEntityUrl(id), api.client.options.TokenOptions)
  97. if err != nil {
  98. return
  99. }
  100. defer res.Body.Close()
  101. res, err = getError(res)
  102. if err != nil {
  103. return
  104. }
  105. var result Deployment
  106. err = json.NewDecoder(res.Body).Decode(&result)
  107. return &result, nil
  108. }
  109. // Gets all hosts with the specified deployment ID.
  110. func (api *DeploymentsAPI) GetHosts(id string) (result *Hosts, err error) {
  111. uri := api.getEntityUrl(id) + "/hosts"
  112. res, err := api.client.restClient.GetList(api.client.Endpoint, uri, api.client.options.TokenOptions)
  113. if err != nil {
  114. return
  115. }
  116. result = &Hosts{}
  117. err = json.Unmarshal(res, result)
  118. return
  119. }
  120. // Gets all the vms with the specified deployment ID.
  121. func (api *DeploymentsAPI) GetVms(id string) (result *VMs, err error) {
  122. uri := api.getEntityUrl(id) + "/vms"
  123. res, err := api.client.restClient.GetList(api.client.Endpoint, uri, api.client.options.TokenOptions)
  124. if err != nil {
  125. return
  126. }
  127. result = &VMs{}
  128. err = json.Unmarshal(res, result)
  129. return
  130. }
  131. // Initialize deployment migration from source to destination
  132. func (api *DeploymentsAPI) InitializeDeploymentMigration(sourceAddress *InitializeMigrationOperation, id string) (task *Task, err error) {
  133. body, err := json.Marshal(sourceAddress)
  134. if err != nil {
  135. return
  136. }
  137. res, err := api.client.restClient.Post(
  138. api.getEntityUrl(id)+"/initialize_migration",
  139. "application/json",
  140. bytes.NewReader(body),
  141. api.client.options.TokenOptions)
  142. if err != nil {
  143. return
  144. }
  145. defer res.Body.Close()
  146. task, err = getTask(getError(res))
  147. return
  148. }
  149. // Finalize deployment migration from source to destination
  150. func (api *DeploymentsAPI) FinalizeDeploymentMigration(sourceAddress *FinalizeMigrationOperation, id string) (task *Task, err error) {
  151. body, err := json.Marshal(sourceAddress)
  152. if err != nil {
  153. return
  154. }
  155. res, err := api.client.restClient.Post(
  156. api.getEntityUrl(id)+"/finalize_migration",
  157. "application/json",
  158. bytes.NewReader(body),
  159. api.client.options.TokenOptions)
  160. if err != nil {
  161. return
  162. }
  163. defer res.Body.Close()
  164. task, err = getTask(getError(res))
  165. return
  166. }
  167. func (api *DeploymentsAPI) SetSecurityGroups(id string, securityGroups *SecurityGroupsSpec) (*Task, error) {
  168. return setSecurityGroups(api.client, api.getEntityUrl(id), securityGroups)
  169. }
  170. // Update image datastores of a deployment.
  171. func (api *DeploymentsAPI) SetImageDatastores(id string, imageDatastores *ImageDatastores) (task *Task, err error) {
  172. body, err := json.Marshal(imageDatastores)
  173. if err != nil {
  174. return
  175. }
  176. res, err := api.client.restClient.Post(
  177. api.getEntityUrl(id)+"/set_image_datastores",
  178. "application/json",
  179. bytes.NewReader(body),
  180. api.client.options.TokenOptions)
  181. if err != nil {
  182. return
  183. }
  184. defer res.Body.Close()
  185. task, err = getTask(getError(res))
  186. return
  187. }
  188. // Synchronizes hosts configurations
  189. func (api *DeploymentsAPI) SyncHostsConfig(id string) (task *Task, err error) {
  190. res, err := api.client.restClient.Post(
  191. api.getEntityUrl(id)+"/sync_hosts_config",
  192. "application/json",
  193. bytes.NewReader([]byte("")),
  194. api.client.options.TokenOptions)
  195. if err != nil {
  196. return
  197. }
  198. defer res.Body.Close()
  199. task, err = getTask(getError(res))
  200. return
  201. }
  202. // Pause system with specified deployment ID.
  203. func (api *DeploymentsAPI) PauseSystem(id string) (task *Task, err error) {
  204. res, err := api.client.restClient.Post(
  205. api.getEntityUrl(id)+"/pause_system",
  206. "application/json",
  207. bytes.NewReader([]byte("")),
  208. api.client.options.TokenOptions)
  209. if err != nil {
  210. return
  211. }
  212. defer res.Body.Close()
  213. task, err = getTask(getError(res))
  214. return
  215. }
  216. // Pause background tasks of system with specified deployment ID.
  217. func (api *DeploymentsAPI) PauseBackgroundTasks(id string) (task *Task, err error) {
  218. res, err := api.client.restClient.Post(
  219. api.getEntityUrl(id)+"/pause_background_tasks",
  220. "application/json",
  221. bytes.NewReader([]byte("")),
  222. 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. // Pause background tasks of system with specified deployment ID.
  231. func (api *DeploymentsAPI) ResumeSystem(id string) (task *Task, err error) {
  232. res, err := api.client.restClient.Post(
  233. api.getEntityUrl(id)+"/resume_system",
  234. "application/json",
  235. bytes.NewReader([]byte("")),
  236. api.client.options.TokenOptions)
  237. if err != nil {
  238. return
  239. }
  240. defer res.Body.Close()
  241. task, err = getTask(getError(res))
  242. return
  243. }
  244. // Enable service type with specified deployment ID.
  245. func (api *DeploymentsAPI) EnableServiceType(id string, serviceConfigSpec *ServiceConfigurationSpec) (task *Task, err error) {
  246. body, err := json.Marshal(serviceConfigSpec)
  247. if err != nil {
  248. return
  249. }
  250. res, err := api.client.restClient.Post(
  251. api.getEntityUrl(id)+"/enable_service_type",
  252. "application/json",
  253. bytes.NewReader(body),
  254. api.client.options.TokenOptions)
  255. if err != nil {
  256. return
  257. }
  258. defer res.Body.Close()
  259. task, err = getTask(getError(res))
  260. return
  261. }
  262. // Disable service type with specified deployment ID.
  263. func (api *DeploymentsAPI) DisableServiceType(id string, serviceConfigSpec *ServiceConfigurationSpec) (task *Task, err error) {
  264. body, err := json.Marshal(serviceConfigSpec)
  265. if err != nil {
  266. return
  267. }
  268. res, err := api.client.restClient.Post(
  269. api.getEntityUrl(id)+"/disable_service_type",
  270. "application/json",
  271. bytes.NewReader(body),
  272. api.client.options.TokenOptions)
  273. if err != nil {
  274. return
  275. }
  276. defer res.Body.Close()
  277. task, err = getTask(getError(res))
  278. return
  279. }
  280. // Configure NSX.
  281. func (api *DeploymentsAPI) ConfigureNsx(id string, nsxConfigSpec *NsxConfigurationSpec) (task *Task, err error) {
  282. body, err := json.Marshal(nsxConfigSpec)
  283. if err != nil {
  284. return
  285. }
  286. res, err := api.client.restClient.Post(
  287. api.getEntityUrl(id)+"/configure_nsx",
  288. "application/json",
  289. bytes.NewReader(body),
  290. api.client.options.TokenOptions)
  291. if err != nil {
  292. return
  293. }
  294. defer res.Body.Close()
  295. task, err = getTask(getError(res))
  296. return
  297. }
  298. func (api *DeploymentsAPI) getEntityUrl(id string) (url string) {
  299. return api.client.Endpoint + deploymentUrl + "/" + id
  300. }