images.go 5.1 KB

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