Debian Codegraph @

photon/images.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 "io"
16 )
17
18 // Contains functionality for images API.
19 type ImagesAPI struct {
20 client *Client
21 }
22
23 // Options for GetImage API.
24 type ImageGetOptions struct {
25 Name string `urlParam:"name"`
26 }
27
28 var imageUrl string = rootUrl + "/images"
29
30 // Uploads a new image, reading from the specified image path.
31 // If options is nil, default options are used.
32 func (api *ImagesAPI) CreateFromFile(imagePath string, options *ImageCreateOptions) (task *Task, err error) {
33 params := imageCreateOptionsToMap(options)
34 res, err := api.client.restClient.MultipartUploadFile(api.client.Endpoint+imageUrl, imagePath, params, api.client.options.TokenOptions)
35 if err != nil {
36 return
37 }
38 defer res.Body.Close()
39 result, err := getTask(getError(res))
40 return result, err
41 }
42
43 // Uploads a new image, reading from the specified io.Reader.
44 // Name is a descriptive name of the image, it is used in the filename field of the Content-Disposition header,
45 // and does not need to be unique.
46 // If options is nil, default options are used.
47 func (api *ImagesAPI) Create(reader io.ReadSeeker, name string, options *ImageCreateOptions) (task *Task, err error) {
48 params := imageCreateOptionsToMap(options)
49 res, err := api.client.restClient.MultipartUpload(api.client.Endpoint+imageUrl, reader, name, params, api.client.options.TokenOptions)
50 if err != nil {
51 return
52 }
53 defer res.Body.Close()
54 result, err := getTask(getError(res))
55 return result, err
56 }
57
58 // Gets all images on this photon instance.
59 func (api *ImagesAPI) GetAll(options *ImageGetOptions) (images *Images, err error) {
60 uri := api.client.Endpoint + imageUrl
61 if options != nil {
62 uri += getQueryString(options)
63 }
64 res, err := api.client.restClient.GetList(api.client.Endpoint, uri, api.client.options.TokenOptions)
65 if err != nil {
66 return
67 }
68
69 images = &Images{}
70 err = json.Unmarshal(res, images)
71 return
72 }
73
74 // Gets details of image with the specified ID.
75 func (api *ImagesAPI) Get(imageID string) (image *Image, err error) {
76 res, err := api.client.restClient.Get(api.client.Endpoint+imageUrl+"/"+imageID, api.client.options.TokenOptions)
77 if err != nil {
78 return
79 }
80 defer res.Body.Close()
81 res, err = getError(res)
82 if err != nil {
83 return
84 }
85 var result Image
86 err = json.NewDecoder(res.Body).Decode(&result)
87 return &result, err
88 }
89
90 // Deletes image with the specified ID.
91 func (api *ImagesAPI) Delete(imageID string) (task *Task, err error) {
92 res, err := api.client.restClient.Delete(api.client.Endpoint+imageUrl+"/"+imageID, api.client.options.TokenOptions)
93 if err != nil {
94 return
95 }
96 defer res.Body.Close()
97 result, err := getTask(getError(res))
98 return result, err
99 }
100
101 // Gets all tasks with the specified image ID, using options to filter the results.
102 // If options is nil, no filtering will occur.
103 func (api *ImagesAPI) GetTasks(id string, options *TaskGetOptions) (result *TaskList, err error) {
104 uri := api.client.Endpoint + imageUrl + "/" + id + "/tasks"
105 if options != nil {
106 uri += getQueryString(options)
107 }
108
109 res, err := api.client.restClient.GetList(api.client.Endpoint, uri, api.client.options.TokenOptions)
110 if err != nil {
111 return
112 }
113
114 result = &TaskList{}
115 err = json.Unmarshal(res, result)
116 return
117 }
118
119 // Gets IAM Policy of an image.
120 func (api *ImagesAPI) GetIam(imageID string) (policy []*RoleBinding, err error) {
121 res, err := api.client.restClient.Get(
122 api.client.Endpoint+imageUrl+"/"+imageID+"/iam",
123 api.client.options.TokenOptions)
124 if err != nil {
125 return
126 }
127 defer res.Body.Close()
128 res, err = getError(res)
129 if err != nil {
130 return
131 }
132 err = json.NewDecoder(res.Body).Decode(&policy)
133 return policy, err
134 }
135
136 // Sets IAM Policy on an image.
137 func (api *ImagesAPI) SetIam(imageID string, policy []*RoleBinding) (task *Task, err error) {
138 body, err := json.Marshal(policy)
139 if err != nil {
140 return
141 }
142 res, err := api.client.restClient.Post(
143 api.client.Endpoint+imageUrl+"/"+imageID+"/iam",
144 "application/json",
145 bytes.NewReader(body),
146 api.client.options.TokenOptions)
147 if err != nil {
148 return
149 }
150 defer res.Body.Close()
151 task, err = getTask(getError(res))
152 return
153 }
154
155 // Modifies IAM Policy on an image.
156 func (api *ImagesAPI) ModifyIam(imageID string, policyDelta []*RoleBindingDelta) (task *Task, err error) {
157 body, err := json.Marshal(policyDelta)
158 if err != nil {
159 return
160 }
161 res, err := api.client.restClient.Patch(
162 api.client.Endpoint+imageUrl+"/"+imageID+"/iam",
163 "application/json",
164 bytes.NewReader(body),
165 api.client.options.TokenOptions)
166 if err != nil {
167 return
168 }
169 defer res.Body.Close()
170 task, err = getTask(getError(res))
171 return
172 }
173
174 func imageCreateOptionsToMap(opts *ImageCreateOptions) map[string]string {
175 if opts == nil {
176 return nil
177 }
178 return map[string]string{
179 "ImageReplication": opts.ReplicationType,
180 }
181 }