tenants.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. "fmt"
  14. )
  15. // Contains functionality for tenants API.
  16. type TenantsAPI struct {
  17. client *Client
  18. }
  19. // Options for GetResourceTickets API.
  20. type ResourceTicketGetOptions struct {
  21. Name string `urlParam:"name"`
  22. }
  23. // Options for GetProjects API.
  24. type ProjectGetOptions struct {
  25. Name string `urlParam:"name"`
  26. }
  27. var tenantUrl string = rootUrl + "/tenants"
  28. // Returns all tenants on an photon instance.
  29. func (api *TenantsAPI) GetAll() (result *Tenants, err error) {
  30. uri := api.client.Endpoint + tenantUrl
  31. res, err := api.client.restClient.GetList(api.client.Endpoint, uri, api.client.options.TokenOptions)
  32. if err != nil {
  33. return
  34. }
  35. result = &Tenants{}
  36. err = json.Unmarshal(res, result)
  37. return
  38. }
  39. // Creates a tenant.
  40. func (api *TenantsAPI) Create(tenantSpec *TenantCreateSpec) (task *Task, err error) {
  41. body, err := json.Marshal(tenantSpec)
  42. if err != nil {
  43. return
  44. }
  45. res, err := api.client.restClient.Post(
  46. api.client.Endpoint+tenantUrl,
  47. "application/json",
  48. bytes.NewReader(body),
  49. api.client.options.TokenOptions)
  50. if err != nil {
  51. return
  52. }
  53. defer res.Body.Close()
  54. task, err = getTask(getError(res))
  55. return
  56. }
  57. // Deletes the tenant with specified ID. Any projects, VMs, disks, etc., owned by the tenant must be deleted first.
  58. func (api *TenantsAPI) Delete(id string) (task *Task, err error) {
  59. res, err := api.client.restClient.Delete(api.client.Endpoint+tenantUrl+"/"+id, api.client.options.TokenOptions)
  60. if err != nil {
  61. return
  62. }
  63. defer res.Body.Close()
  64. task, err = getTask(getError(res))
  65. return
  66. }
  67. // Creates a resource ticket on the specified tenant.
  68. func (api *TenantsAPI) CreateResourceTicket(tenantId string, spec *ResourceTicketCreateSpec) (task *Task, err error) {
  69. body, err := json.Marshal(spec)
  70. if err != nil {
  71. return
  72. }
  73. res, err := api.client.restClient.Post(
  74. api.client.Endpoint+tenantUrl+"/"+tenantId+"/resource-tickets",
  75. "application/json",
  76. bytes.NewReader(body),
  77. api.client.options.TokenOptions)
  78. if err != nil {
  79. return
  80. }
  81. defer res.Body.Close()
  82. task, err = getTask(getError(res))
  83. return
  84. }
  85. // Gets resource tickets for tenant with the specified ID, using options to filter the results.
  86. // If options is nil, no filtering will occur.
  87. func (api *TenantsAPI) GetResourceTickets(tenantId string, options *ResourceTicketGetOptions) (tickets *ResourceList, err error) {
  88. uri := api.client.Endpoint + tenantUrl + "/" + tenantId + "/resource-tickets"
  89. if options != nil {
  90. uri += getQueryString(options)
  91. }
  92. res, err := api.client.restClient.GetList(api.client.Endpoint, uri, api.client.options.TokenOptions)
  93. if err != nil {
  94. return
  95. }
  96. tickets = &ResourceList{}
  97. err = json.Unmarshal(res, tickets)
  98. return
  99. }
  100. // Creates a project on the specified tenant.
  101. func (api *TenantsAPI) CreateProject(tenantId string, spec *ProjectCreateSpec) (task *Task, err error) {
  102. body, err := json.Marshal(spec)
  103. if err != nil {
  104. return
  105. }
  106. res, err := api.client.restClient.Post(
  107. api.client.Endpoint+tenantUrl+"/"+tenantId+"/projects",
  108. "application/json",
  109. bytes.NewReader(body),
  110. api.client.options.TokenOptions)
  111. if err != nil {
  112. return
  113. }
  114. defer res.Body.Close()
  115. task, err = getTask(getError(res))
  116. return
  117. }
  118. // Gets the projects for tenant with the specified ID, using options to filter the results.
  119. // If options is nil, no filtering will occur.
  120. func (api *TenantsAPI) GetProjects(tenantId string, options *ProjectGetOptions) (result *ProjectList, err error) {
  121. uri := api.client.Endpoint + tenantUrl + "/" + tenantId + "/projects"
  122. if options != nil {
  123. uri += getQueryString(options)
  124. }
  125. res, err := api.client.restClient.GetList(api.client.Endpoint, uri, api.client.options.TokenOptions)
  126. if err != nil {
  127. return
  128. }
  129. result = &(ProjectList{})
  130. err = json.Unmarshal(res, result)
  131. return
  132. }
  133. // Gets all tasks with the specified tenant ID, using options to filter the results.
  134. // If options is nil, no filtering will occur.
  135. func (api *TenantsAPI) GetTasks(id string, options *TaskGetOptions) (result *TaskList, err error) {
  136. uri := api.client.Endpoint + tenantUrl + "/" + id + "/tasks"
  137. if options != nil {
  138. uri += getQueryString(options)
  139. }
  140. res, err := api.client.restClient.GetList(api.client.Endpoint, uri, api.client.options.TokenOptions)
  141. if err != nil {
  142. return
  143. }
  144. result = &TaskList{}
  145. err = json.Unmarshal(res, result)
  146. return
  147. }
  148. // Gets a tenant with the specified ID or name
  149. func (api *TenantsAPI) Get(identity string) (tenant *Tenant, err error) {
  150. res, err := api.client.restClient.Get(api.getEntityUrl(identity), api.client.options.TokenOptions)
  151. if err != nil {
  152. return
  153. }
  154. defer res.Body.Close()
  155. res, err = getError(res)
  156. tenant = &Tenant{}
  157. if res != nil {
  158. err = json.NewDecoder(res.Body).Decode(tenant)
  159. // ID corresponds to the tenant ID found, return tenant
  160. if err == nil {
  161. return
  162. }
  163. }
  164. // Find by Name
  165. uri := api.client.Endpoint + tenantUrl + "?name=" + identity
  166. res2, err := api.client.restClient.GetList(api.client.Endpoint, uri, api.client.options.TokenOptions)
  167. if err != nil {
  168. return
  169. }
  170. tenants := &Tenants{}
  171. err = json.Unmarshal(res2, tenants)
  172. if err != nil {
  173. return
  174. }
  175. if len(tenants.Items) < 1 {
  176. err = fmt.Errorf("Cannot find a tenant with id or name match %s", identity)
  177. return
  178. }
  179. tenant = &(tenants.Items[0])
  180. return
  181. }
  182. // Set security groups for this tenant, overwriting any existing ones.
  183. func (api *TenantsAPI) SetSecurityGroups(id string, securityGroups *SecurityGroupsSpec) (*Task, error) {
  184. return setSecurityGroups(api.client, api.getEntityUrl(id), securityGroups)
  185. }
  186. func (api *TenantsAPI) getEntityUrl(id string) (url string) {
  187. return api.client.Endpoint + tenantUrl + "/" + id
  188. }