hosts.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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 hosts API.
  15. type HostsAPI struct {
  16. client *Client
  17. }
  18. var hostUrl string = rootUrl + "/hosts"
  19. // Creates a host.
  20. func (api *HostsAPI) Create(hostSpec *HostCreateSpec, deploymentId string) (task *Task, err error) {
  21. body, err := json.Marshal(hostSpec)
  22. if err != nil {
  23. return
  24. }
  25. res, err := api.client.restClient.Post(
  26. api.client.Endpoint+deploymentUrl+"/"+deploymentId+"/hosts",
  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 host with specified ID.
  38. func (api *HostsAPI) Delete(id string) (task *Task, err error) {
  39. res, err := api.client.restClient.Delete(api.client.Endpoint+hostUrl+"/"+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. // Returns all hosts
  48. func (api *HostsAPI) GetAll() (result *Hosts, err error) {
  49. res, err := api.client.restClient.Get(api.client.Endpoint+hostUrl, api.client.options.TokenOptions)
  50. if err != nil {
  51. return
  52. }
  53. defer res.Body.Close()
  54. res, err = getError(res)
  55. if err != nil {
  56. return
  57. }
  58. result = &Hosts{}
  59. err = json.NewDecoder(res.Body).Decode(result)
  60. return
  61. }
  62. // Gets a host with the specified ID.
  63. func (api *HostsAPI) Get(id string) (host *Host, err error) {
  64. res, err := api.client.restClient.Get(api.client.Endpoint+hostUrl+"/"+id, api.client.options.TokenOptions)
  65. if err != nil {
  66. return
  67. }
  68. defer res.Body.Close()
  69. res, err = getError(res)
  70. if err != nil {
  71. return
  72. }
  73. var result Host
  74. err = json.NewDecoder(res.Body).Decode(&result)
  75. return &result, nil
  76. }
  77. // Sets host's availability zone.
  78. func (api *HostsAPI) SetAvailabilityZone(id string, availabilityZone *HostSetAvailabilityZoneOperation) (task *Task, err error) {
  79. body, err := json.Marshal(availabilityZone)
  80. if err != nil {
  81. return
  82. }
  83. res, err := api.client.restClient.Post(
  84. api.client.Endpoint+hostUrl+"/"+id+"/set_availability_zone",
  85. "application/json",
  86. bytes.NewReader(body),
  87. api.client.options.TokenOptions)
  88. if err != nil {
  89. return
  90. }
  91. defer res.Body.Close()
  92. task, err = getTask(getError(res))
  93. return
  94. }
  95. // Gets all tasks with the specified host ID, using options to filter the results.
  96. // If options is nil, no filtering will occur.
  97. func (api *HostsAPI) GetTasks(id string, options *TaskGetOptions) (result *TaskList, err error) {
  98. uri := api.client.Endpoint + hostUrl + "/" + id + "/tasks"
  99. if options != nil {
  100. uri += getQueryString(options)
  101. }
  102. res, err := api.client.restClient.GetList(api.client.Endpoint, uri, api.client.options.TokenOptions)
  103. if err != nil {
  104. return
  105. }
  106. result = &TaskList{}
  107. err = json.Unmarshal(res, result)
  108. return
  109. }
  110. // Gets all the vms with the specified host ID.
  111. func (api *HostsAPI) GetVMs(id string) (result *VMs, err error) {
  112. res, err := api.client.restClient.Get(api.client.Endpoint+hostUrl+"/"+id+"/vms", api.client.options.TokenOptions)
  113. if err != nil {
  114. return
  115. }
  116. defer res.Body.Close()
  117. res, err = getError(res)
  118. if err != nil {
  119. return
  120. }
  121. result = &VMs{}
  122. err = json.NewDecoder(res.Body).Decode(result)
  123. return
  124. }
  125. // provision the host with the specified id
  126. func (api *HostsAPI) Provision(id string) (task *Task, err error) {
  127. body := []byte{}
  128. res, err := api.client.restClient.Post(
  129. api.client.Endpoint+hostUrl+"/"+id+"/provision",
  130. "application/json",
  131. bytes.NewReader(body),
  132. api.client.options.TokenOptions)
  133. if err != nil {
  134. return
  135. }
  136. defer res.Body.Close()
  137. task, err = getTask(getError(res))
  138. return
  139. }
  140. // Suspend the host with the specified id
  141. func (api *HostsAPI) Suspend(id string) (task *Task, err error) {
  142. body := []byte{}
  143. res, err := api.client.restClient.Post(
  144. api.client.Endpoint+hostUrl+"/"+id+"/suspend",
  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. // Resume the host with the specified id
  156. func (api *HostsAPI) Resume(id string) (task *Task, err error) {
  157. body := []byte{}
  158. res, err := api.client.restClient.Post(
  159. api.client.Endpoint+hostUrl+"/"+id+"/resume",
  160. "application/json",
  161. bytes.NewReader(body),
  162. api.client.options.TokenOptions)
  163. if err != nil {
  164. return
  165. }
  166. defer res.Body.Close()
  167. task, err = getTask(getError(res))
  168. return
  169. }
  170. // Host with the specified id enter maintenance mode
  171. func (api *HostsAPI) EnterMaintenanceMode(id string) (task *Task, err error) {
  172. body := []byte{}
  173. res, err := api.client.restClient.Post(
  174. api.client.Endpoint+hostUrl+"/"+id+"/enter_maintenance",
  175. "application/json",
  176. bytes.NewReader(body),
  177. api.client.options.TokenOptions)
  178. if err != nil {
  179. return
  180. }
  181. defer res.Body.Close()
  182. task, err = getTask(getError(res))
  183. return
  184. }
  185. // Host with the specified id exit maintenance mode
  186. func (api *HostsAPI) ExitMaintenanceMode(id string) (task *Task, err error) {
  187. body := []byte{}
  188. res, err := api.client.restClient.Post(
  189. api.client.Endpoint+hostUrl+"/"+id+"/exit_maintenance",
  190. "application/json",
  191. bytes.NewReader(body),
  192. api.client.options.TokenOptions)
  193. if err != nil {
  194. return
  195. }
  196. defer res.Body.Close()
  197. task, err = getTask(getError(res))
  198. return
  199. }