Debian Codegraph @

photon/disks.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 )
16
17 // Contains functionality for disks API.
18 type DisksAPI struct {
19 client *Client
20 }
21
22 var diskUrl string = rootUrl + "/disks/"
23
24 // Gets a PersistentDisk for the disk with specified ID.
25 func (api *DisksAPI) Get(diskID string) (disk *PersistentDisk, err error) {
26 res, err := api.client.restClient.Get(api.client.Endpoint+diskUrl+diskID, api.client.options.TokenOptions)
27 if err != nil {
28 return
29 }
30 defer res.Body.Close()
31 res, err = getError(res)
32 if err != nil {
33 return
34 }
35 disk = &PersistentDisk{}
36 err = json.NewDecoder(res.Body).Decode(disk)
37 return
38 }
39
40 // Deletes a disk with the specified ID.
41 func (api *DisksAPI) Delete(diskID string) (task *Task, err error) {
42 res, err := api.client.restClient.Delete(api.client.Endpoint+diskUrl+diskID, api.client.options.TokenOptions)
43 if err != nil {
44 return
45 }
46 defer res.Body.Close()
47 task, err = getTask(getError(res))
48 return
49 }
50
51 // Gets all tasks with the specified disk ID, using options to filter the results.
52 // If options is nil, no filtering will occur.
53 func (api *DisksAPI) GetTasks(id string, options *TaskGetOptions) (result *TaskList, err error) {
54 uri := api.client.Endpoint + diskUrl + id + "/tasks"
55 if options != nil {
56 uri += getQueryString(options)
57 }
58 res, err := api.client.restClient.GetList(api.client.Endpoint, uri, api.client.options.TokenOptions)
59 if err != nil {
60 return
61 }
62
63 result = &TaskList{}
64 err = json.Unmarshal(res, result)
65 return
66 }
67
68 // Gets IAM Policy on a disk.
69 func (api *DisksAPI) GetIam(id string) (policy []*RoleBinding, err error) {
70 res, err := api.client.restClient.Get(
71 api.client.Endpoint+diskUrl+id+"/iam",
72 api.client.options.TokenOptions)
73 if err != nil {
74 return
75 }
76 defer res.Body.Close()
77 res, err = getError(res)
78 if err != nil {
79 return
80 }
81 err = json.NewDecoder(res.Body).Decode(&policy)
82 return policy, err
83 }
84
85 // Sets IAM Policy on a disk.
86 func (api *DisksAPI) SetIam(id string, policy []*RoleBinding) (task *Task, err error) {
87 body, err := json.Marshal(policy)
88 if err != nil {
89 return
90 }
91 res, err := api.client.restClient.Post(
92 api.client.Endpoint+diskUrl+id+"/iam",
93 "application/json",
94 bytes.NewReader(body),
95 api.client.options.TokenOptions)
96 if err != nil {
97 return
98 }
99 defer res.Body.Close()
100 task, err = getTask(getError(res))
101 return
102 }
103
104 // Modifies IAM Policy on a disk.
105 func (api *DisksAPI) ModifyIam(id string, policyDelta []*RoleBindingDelta) (task *Task, err error) {
106 body, err := json.Marshal(policyDelta)
107 if err != nil {
108 return
109 }
110 res, err := api.client.restClient.Patch(
111 api.client.Endpoint+diskUrl+id+"/iam",
112 "application/json",
113 bytes.NewReader(body),
114 api.client.options.TokenOptions)
115 if err != nil {
116 return
117 }
118 defer res.Body.Close()
119 task, err = getTask(getError(res))
120 return
121 }