| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
package photon |
| 11 |
|
| 12 |
import ( |
| 13 |
"bytes" |
| 14 |
"encoding/json" |
| 15 |
"io" |
| 16 |
) |
| 17 |
|
| 18 |
|
| 19 |
type ImagesAPI struct { |
| 20 |
client *Client |
| 21 |
} |
| 22 |
|
| 23 |
|
| 24 |
type ImageGetOptions struct { |
| 25 |
Name string `urlParam:"name"` |
| 26 |
} |
| 27 |
|
| 28 |
var imageUrl string = rootUrl + "/images" |
| 29 |
|
| 30 |
|
| 31 |
|
| 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 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 102 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
} |