flavors.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. )
  14. // Contains functionality for flavors API.
  15. type FlavorsAPI struct {
  16. client *Client
  17. }
  18. // Options used for find/get APIs
  19. type FlavorGetOptions struct {
  20. Name string `urlParam:"name"`
  21. Kind string `urlParam:"kind"`
  22. }
  23. var flavorUrl string = rootUrl + "/flavors"
  24. // Creates a flavor.
  25. func (api *FlavorsAPI) Create(spec *FlavorCreateSpec) (task *Task, err error) {
  26. body, err := json.Marshal(spec)
  27. if err != nil {
  28. return
  29. }
  30. res, err := api.client.restClient.Post(
  31. api.client.Endpoint+flavorUrl,
  32. "application/json",
  33. bytes.NewReader(body),
  34. api.client.options.TokenOptions)
  35. if err != nil {
  36. return
  37. }
  38. defer res.Body.Close()
  39. task, err = getTask(getError(res))
  40. return
  41. }
  42. // Gets details of flavor with specified ID.
  43. func (api *FlavorsAPI) Get(flavorID string) (flavor *Flavor, err error) {
  44. res, err := api.client.restClient.Get(api.client.Endpoint+flavorUrl+"/"+flavorID, api.client.options.TokenOptions)
  45. if err != nil {
  46. return
  47. }
  48. defer res.Body.Close()
  49. res, err = getError(res)
  50. if err != nil {
  51. return
  52. }
  53. flavor = &Flavor{}
  54. err = json.NewDecoder(res.Body).Decode(flavor)
  55. return
  56. }
  57. // Gets flavors using options to filter results. Returns all flavors if options is nil.
  58. func (api *FlavorsAPI) GetAll(options *FlavorGetOptions) (flavors *FlavorList, err error) {
  59. uri := api.client.Endpoint + flavorUrl
  60. if options != nil {
  61. uri += getQueryString(options)
  62. }
  63. res, err := api.client.restClient.GetList(api.client.Endpoint, uri, api.client.options.TokenOptions)
  64. if err != nil {
  65. return
  66. }
  67. flavors = &FlavorList{}
  68. err = json.Unmarshal(res, flavors)
  69. return
  70. }
  71. // Deletes flavor with specified ID.
  72. func (api *FlavorsAPI) Delete(flavorID string) (task *Task, err error) {
  73. res, err := api.client.restClient.Delete(api.client.Endpoint+flavorUrl+"/"+flavorID, api.client.options.TokenOptions)
  74. if err != nil {
  75. return
  76. }
  77. defer res.Body.Close()
  78. task, err = getTask(getError(res))
  79. return
  80. }
  81. // Gets all tasks with the specified flavor ID, using options to filter the results.
  82. // If options is nil, no filtering will occur.
  83. func (api *FlavorsAPI) GetTasks(id string, options *TaskGetOptions) (result *TaskList, err error) {
  84. uri := api.client.Endpoint + flavorUrl + "/" + id + "/tasks"
  85. if options != nil {
  86. uri += getQueryString(options)
  87. }
  88. res, err := api.client.restClient.GetList(api.client.Endpoint, uri, api.client.options.TokenOptions)
  89. if err != nil {
  90. return
  91. }
  92. result = &TaskList{}
  93. err = json.Unmarshal(res, result)
  94. return
  95. }