Debian Codegraph @

photon/tenants.go

raw symbols SCIP
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
10 package photon
11
12 import (
13 "bytes"
14 "encoding/json"
15 "fmt"
16 "io/ioutil"
17 )
18
19 // Contains functionality for tenants API.
20 type TenantsAPI struct {
21 client *Client
22 }
23
24 // Options for GetResourceTickets API.
25 type ResourceTicketGetOptions struct {
26 Name string `urlParam:"name"`
27 }
28
29 // Options for GetProjects API.
30 type ProjectGetOptions struct {
31 Name string `urlParam:"name"`
32 }
33
34 var tenantUrl string = rootUrl + "/tenants"
35
36 // Returns all tenants on an photon instance.
37 func (api *TenantsAPI) GetAll() (result *Tenants, err error) {
38 uri := api.client.Endpoint + tenantUrl
39 res, err := api.client.restClient.GetList(api.client.Endpoint, uri, api.client.options.TokenOptions)
40 if err != nil {
41 return
42 }
43
44 result = &Tenants{}
45 err = json.Unmarshal(res, result)
46 return
47 }
48
49 // Creates a tenant.
50 func (api *TenantsAPI) Create(tenantSpec *TenantCreateSpec) (task *Task, err error) {
51 body, err := json.Marshal(tenantSpec)
52 if err != nil {
53 return
54 }
55 res, err := api.client.restClient.Post(
56 api.client.Endpoint+tenantUrl,
57 "application/json",
58 bytes.NewReader(body),
59 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
68 // Deletes the tenant with specified ID. Any projects, VMs, disks, etc., owned by the tenant must be deleted first.
69 func (api *TenantsAPI) Delete(id string) (task *Task, err error) {
70 res, err := api.client.restClient.Delete(api.client.Endpoint+tenantUrl+"/"+id, api.client.options.TokenOptions)
71 if err != nil {
72 return
73 }
74 defer res.Body.Close()
75 task, err = getTask(getError(res))
76 return
77 }
78
79 // Creates a project on the specified tenant.
80 func (api *TenantsAPI) CreateProject(tenantId string, spec *ProjectCreateSpec) (task *Task, err error) {
81 body, err := json.Marshal(spec)
82 if err != nil {
83 return
84 }
85 res, err := api.client.restClient.Post(
86 api.client.Endpoint+tenantUrl+"/"+tenantId+"/projects",
87 "application/json",
88 bytes.NewReader(body),
89 api.client.options.TokenOptions)
90 if err != nil {
91 return
92 }
93 defer res.Body.Close()
94 task, err = getTask(getError(res))
95 return
96 }
97
98 // Gets the projects for tenant with the specified ID, using options to filter the results.
99 // If options is nil, no filtering will occur.
100 func (api *TenantsAPI) GetProjects(tenantId string, options *ProjectGetOptions) (result *ProjectList, err error) {
101 uri := api.client.Endpoint + tenantUrl + "/" + tenantId + "/projects"
102 if options != nil {
103 uri += getQueryString(options)
104 }
105 res, err := api.client.restClient.GetList(api.client.Endpoint, uri, api.client.options.TokenOptions)
106 if err != nil {
107 return
108 }
109
110 result = &(ProjectList{})
111 err = json.Unmarshal(res, result)
112 return
113 }
114
115 // Gets all tasks with the specified tenant ID, using options to filter the results.
116 // If options is nil, no filtering will occur.
117 func (api *TenantsAPI) GetTasks(id string, options *TaskGetOptions) (result *TaskList, err error) {
118 uri := api.client.Endpoint + tenantUrl + "/" + id + "/tasks"
119 if options != nil {
120 uri += getQueryString(options)
121 }
122 res, err := api.client.restClient.GetList(api.client.Endpoint, uri, api.client.options.TokenOptions)
123 if err != nil {
124 return
125 }
126
127 result = &TaskList{}
128 err = json.Unmarshal(res, result)
129 return
130 }
131
132 // Gets a tenant with the specified ID or name
133 func (api *TenantsAPI) Get(identity string) (tenant *Tenant, err error) {
134 res, err := api.client.restClient.Get(api.getEntityUrl(identity), api.client.options.TokenOptions)
135 if err != nil {
136 return
137 }
138 defer res.Body.Close()
139 res, err = getError(res)
140 if err != nil {
141 return
142 }
143 tenant = &Tenant{}
144 if res != nil {
145 err = json.NewDecoder(res.Body).Decode(tenant)
146 // ID corresponds to the tenant ID found, return tenant
147 if err == nil {
148 return
149 }
150 }
151 // Find by Name
152 uri := api.client.Endpoint + tenantUrl + "?name=" + identity
153 res2, err := api.client.restClient.GetList(api.client.Endpoint, uri, api.client.options.TokenOptions)
154
155 if err != nil {
156 return
157 }
158
159 tenants := &Tenants{}
160 err = json.Unmarshal(res2, tenants)
161 if err != nil {
162 return
163 }
164
165 if len(tenants.Items) < 1 {
166 err = fmt.Errorf("Cannot find a tenant with id or name match %s", identity)
167 return
168 }
169
170 tenant = &(tenants.Items[0])
171 return
172 }
173
174 // Set security groups for this tenant, overwriting any existing ones.
175 func (api *TenantsAPI) SetSecurityGroups(id string, securityGroups *SecurityGroupsSpec) (*Task, error) {
176 return setSecurityGroups(api.client, api.getEntityUrl(id), securityGroups)
177 }
178
179 func (api *TenantsAPI) getEntityUrl(id string) (url string) {
180 return api.client.Endpoint + tenantUrl + "/" + id
181 }
182
183 // Get quota for project with the specified ID.
184 func (api *TenantsAPI) GetQuota(tenantId string) (quota *Quota, err error) {
185 uri := api.client.Endpoint + tenantUrl + "/" + tenantId + "/quota"
186 res, err := api.client.restClient.Get(uri, api.client.options.TokenOptions)
187
188 if err != nil {
189 return
190 }
191
192 defer res.Body.Close()
193 res, err = getError(res)
194 if err != nil {
195 return
196 }
197
198 body, err := ioutil.ReadAll(res.Body)
199 if err != nil {
200 return
201 }
202
203 quota = &Quota{}
204 err = json.Unmarshal(body, quota)
205 return
206 }
207
208 // Set (replace) the whole project quota with the quota line items specified in quota spec.
209 func (api *TenantsAPI) SetQuota(tenantId string, spec *QuotaSpec) (task *Task, err error) {
210 task, err = api.modifyQuota("PUT", tenantId, spec)
211 return
212 }
213
214 // Update portion of the project quota with the quota line items specified in quota spec.
215 func (api *TenantsAPI) UpdateQuota(tenantId string, spec *QuotaSpec) (task *Task, err error) {
216 task, err = api.modifyQuota("PATCH", tenantId, spec)
217 return
218 }
219
220 // Exclude project quota line items from the specific quota spec.
221 func (api *TenantsAPI) ExcludeQuota(tenantId string, spec *QuotaSpec) (task *Task, err error) {
222 task, err = api.modifyQuota("DELETE", tenantId, spec)
223 return
224 }
225
226 // A private common function for modifying quota for the specified project with the quota line items specified
227 // in quota spec.
228 func (api *TenantsAPI) modifyQuota(method string, tenantId string, spec *QuotaSpec) (task *Task, err error) {
229 body, err := json.Marshal(spec)
230 if err != nil {
231 return
232 }
233 res, err := api.client.restClient.SendRequestCommon(
234 method,
235 api.client.Endpoint+tenantUrl+"/"+tenantId+"/quota",
236 "application/json",
237 bytes.NewReader(body),
238 api.client.options.TokenOptions)
239 if err != nil {
240 return
241 }
242 defer res.Body.Close()
243 task, err = getTask(getError(res))
244 return
245 }
246
247 // Gets IAM Policy of a tenant.
248 func (api *TenantsAPI) GetIam(tenantId string) (policy []*RoleBinding, err error) {
249 res, err := api.client.restClient.Get(
250 api.client.Endpoint+tenantUrl+"/"+tenantId+"/iam",
251 api.client.options.TokenOptions)
252 if err != nil {
253 return
254 }
255 defer res.Body.Close()
256 res, err = getError(res)
257 if err != nil {
258 return
259 }
260 err = json.NewDecoder(res.Body).Decode(&policy)
261 return policy, err
262 }
263
264 // Sets IAM Policy on a tenant.
265 func (api *TenantsAPI) SetIam(tenantId string, policy []*RoleBinding) (task *Task, err error) {
266 body, err := json.Marshal(policy)
267 if err != nil {
268 return
269 }
270 res, err := api.client.restClient.Post(
271 api.client.Endpoint+tenantUrl+"/"+tenantId+"/iam",
272 "application/json",
273 bytes.NewReader(body),
274 api.client.options.TokenOptions)
275 if err != nil {
276 return
277 }
278 defer res.Body.Close()
279 task, err = getTask(getError(res))
280 return
281 }
282
283 // Modifies IAM Policy on a tenant.
284 func (api *TenantsAPI) ModifyIam(tenantId string, policyDelta []*RoleBindingDelta) (task *Task, err error) {
285 body, err := json.Marshal(policyDelta)
286 if err != nil {
287 return
288 }
289 res, err := api.client.restClient.Patch(
290 api.client.Endpoint+tenantUrl+"/"+tenantId+"/iam",
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 }