projects.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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 projects API.
  15. type ProjectsAPI struct {
  16. client *Client
  17. }
  18. // Options for GetDisks API.
  19. type DiskGetOptions struct {
  20. Name string `urlParam:"name"`
  21. }
  22. // Options for GetDisks API.
  23. type VmGetOptions struct {
  24. Name string `urlParam:"name"`
  25. }
  26. var projectUrl string = rootUrl + "/projects/"
  27. // Deletes the project with specified ID. Any VMs, disks, etc., owned by the project must be deleted first.
  28. func (api *ProjectsAPI) Delete(projectID string) (task *Task, err error) {
  29. res, err := api.client.restClient.Delete(api.client.Endpoint+projectUrl+projectID, 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. // Creates a disk on the specified project.
  38. func (api *ProjectsAPI) CreateDisk(projectID string, spec *DiskCreateSpec) (task *Task, err error) {
  39. body, err := json.Marshal(spec)
  40. if err != nil {
  41. return
  42. }
  43. res, err := api.client.restClient.Post(
  44. api.client.Endpoint+projectUrl+projectID+"/disks",
  45. "application/json",
  46. bytes.NewReader(body),
  47. api.client.options.TokenOptions)
  48. if err != nil {
  49. return
  50. }
  51. defer res.Body.Close()
  52. task, err = getTask(getError(res))
  53. return
  54. }
  55. // Gets disks for project with the specified ID, using options to filter the results.
  56. // If options is nil, no filtering will occur.
  57. func (api *ProjectsAPI) GetDisks(projectID string, options *DiskGetOptions) (result *DiskList, err error) {
  58. uri := api.client.Endpoint + projectUrl + projectID + "/disks"
  59. if options != nil {
  60. uri += getQueryString(options)
  61. }
  62. res, err := api.client.restClient.GetList(api.client.Endpoint, uri, api.client.options.TokenOptions)
  63. if err != nil {
  64. return
  65. }
  66. result = &DiskList{}
  67. err = json.Unmarshal(res, result)
  68. return
  69. }
  70. // Creates a VM on the specified project.
  71. func (api *ProjectsAPI) CreateVM(projectID string, spec *VmCreateSpec) (task *Task, err error) {
  72. body, err := json.Marshal(spec)
  73. if err != nil {
  74. return
  75. }
  76. res, err := api.client.restClient.Post(
  77. api.client.Endpoint+projectUrl+projectID+"/vms",
  78. "application/json",
  79. bytes.NewReader(body),
  80. api.client.options.TokenOptions)
  81. if err != nil {
  82. return
  83. }
  84. defer res.Body.Close()
  85. task, err = getTask(getError(res))
  86. return
  87. }
  88. // Gets all tasks with the specified project ID, using options to filter the results.
  89. // If options is nil, no filtering will occur.
  90. func (api *ProjectsAPI) GetTasks(id string, options *TaskGetOptions) (result *TaskList, err error) {
  91. uri := api.client.Endpoint + projectUrl + id + "/tasks"
  92. if options != nil {
  93. uri += getQueryString(options)
  94. }
  95. res, err := api.client.restClient.GetList(api.client.Endpoint, uri, api.client.options.TokenOptions)
  96. if err != nil {
  97. return
  98. }
  99. result = &TaskList{}
  100. err = json.Unmarshal(res, result)
  101. return
  102. }
  103. // Gets vms for project with the specified ID, using options to filter the results.
  104. // If options is nil, no filtering will occur.
  105. func (api *ProjectsAPI) GetVMs(projectID string, options *VmGetOptions) (result *VMs, err error) {
  106. uri := api.client.Endpoint + projectUrl + projectID + "/vms"
  107. if options != nil {
  108. uri += getQueryString(options)
  109. }
  110. res, err := api.client.restClient.GetList(api.client.Endpoint, uri, api.client.options.TokenOptions)
  111. if err != nil {
  112. return
  113. }
  114. result = &VMs{}
  115. err = json.Unmarshal(res, result)
  116. return
  117. }
  118. // Creates a service on the specified project.
  119. func (api *ProjectsAPI) CreateService(projectID string, spec *ServiceCreateSpec) (task *Task, err error) {
  120. body, err := json.Marshal(spec)
  121. if err != nil {
  122. return
  123. }
  124. res, err := api.client.restClient.Post(
  125. api.client.Endpoint+projectUrl+projectID+"/services",
  126. "application/json",
  127. bytes.NewReader(body),
  128. api.client.options.TokenOptions)
  129. if err != nil {
  130. return
  131. }
  132. defer res.Body.Close()
  133. task, err = getTask(getError(res))
  134. return
  135. }
  136. // Gets services for project with the specified ID
  137. func (api *ProjectsAPI) GetServices(projectID string) (result *Services, err error) {
  138. uri := api.client.Endpoint + projectUrl + projectID + "/services"
  139. res, err := api.client.restClient.GetList(api.client.Endpoint, uri, api.client.options.TokenOptions)
  140. if err != nil {
  141. return
  142. }
  143. result = &Services{}
  144. err = json.Unmarshal(res, result)
  145. return
  146. }
  147. // Gets the project with a specified ID.
  148. func (api *ProjectsAPI) Get(id string) (project *ProjectCompact, err error) {
  149. res, err := api.client.restClient.Get(api.getEntityUrl(id), api.client.options.TokenOptions)
  150. if err != nil {
  151. return
  152. }
  153. defer res.Body.Close()
  154. res, err = getError(res)
  155. if err != nil {
  156. return
  157. }
  158. project = &ProjectCompact{}
  159. err = json.NewDecoder(res.Body).Decode(project)
  160. return
  161. }
  162. // Set security groups for this project, overwriting any existing ones.
  163. func (api *ProjectsAPI) SetSecurityGroups(projectID string, securityGroups *SecurityGroupsSpec) (*Task, error) {
  164. return setSecurityGroups(api.client, api.getEntityUrl(projectID), securityGroups)
  165. }
  166. func (api *ProjectsAPI) getEntityUrl(id string) string {
  167. return api.client.Endpoint + projectUrl + id
  168. }
  169. // Creates a router on the specified project.
  170. func (api *ProjectsAPI) CreateRouter(projectID string, spec *RouterCreateSpec) (task *Task, err error) {
  171. body, err := json.Marshal(spec)
  172. if err != nil {
  173. return
  174. }
  175. res, err := api.client.restClient.Post(
  176. api.client.Endpoint+projectUrl+projectID+"/routers",
  177. "application/json",
  178. bytes.NewReader(body),
  179. api.client.options.TokenOptions)
  180. if err != nil {
  181. return
  182. }
  183. defer res.Body.Close()
  184. task, err = getTask(getError(res))
  185. return
  186. }