availabilityzones.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 availability zones API.
  15. type AvailabilityZonesAPI struct {
  16. client *Client
  17. }
  18. var availabilityzoneUrl string = rootUrl + "/availabilityzones"
  19. // Creates availability zone.
  20. func (api *AvailabilityZonesAPI) Create(availabilityzoneSpec *AvailabilityZoneCreateSpec) (task *Task, err error) {
  21. body, err := json.Marshal(availabilityzoneSpec)
  22. if err != nil {
  23. return
  24. }
  25. res, err := api.client.restClient.Post(
  26. api.client.Endpoint+availabilityzoneUrl,
  27. "application/json",
  28. bytes.NewReader(body),
  29. api.client.options.TokenOptions)
  30. if err != nil {
  31. return
  32. }
  33. defer res.Body.Close()
  34. task, err = getTask(getError(res))
  35. return
  36. }
  37. // Gets availability zone with the specified ID.
  38. func (api *AvailabilityZonesAPI) Get(id string) (availabilityzone *AvailabilityZone, err error) {
  39. res, err := api.client.restClient.Get(api.getEntityUrl(id), api.client.options.TokenOptions)
  40. if err != nil {
  41. return
  42. }
  43. defer res.Body.Close()
  44. res, err = getError(res)
  45. if err != nil {
  46. return
  47. }
  48. availabilityzone = &AvailabilityZone{}
  49. err = json.NewDecoder(res.Body).Decode(availabilityzone)
  50. return
  51. }
  52. // Returns all availability zones on an photon instance.
  53. func (api *AvailabilityZonesAPI) GetAll() (result *AvailabilityZones, err error) {
  54. uri := api.client.Endpoint + availabilityzoneUrl
  55. res, err := api.client.restClient.GetList(api.client.Endpoint, uri, api.client.options.TokenOptions)
  56. if err != nil {
  57. return
  58. }
  59. result = &AvailabilityZones{}
  60. err = json.Unmarshal(res, result)
  61. return
  62. }
  63. // Deletes the availability zone with specified ID.
  64. func (api *AvailabilityZonesAPI) Delete(id string) (task *Task, err error) {
  65. res, err := api.client.restClient.Delete(api.client.Endpoint+availabilityzoneUrl+"/"+id, api.client.options.TokenOptions)
  66. if err != nil {
  67. return
  68. }
  69. defer res.Body.Close()
  70. task, err = getTask(getError(res))
  71. return
  72. }
  73. // Gets all tasks with the specified availability zone ID, using options to filter the results.
  74. // If options is nil, no filtering will occur.
  75. func (api *AvailabilityZonesAPI) GetTasks(id string, options *TaskGetOptions) (result *TaskList, err error) {
  76. uri := api.client.Endpoint + availabilityzoneUrl + "/" + id + "/tasks"
  77. if options != nil {
  78. uri += getQueryString(options)
  79. }
  80. res, err := api.client.restClient.GetList(api.client.Endpoint, uri, api.client.options.TokenOptions)
  81. if err != nil {
  82. return
  83. }
  84. result = &TaskList{}
  85. err = json.Unmarshal(res, result)
  86. return
  87. }
  88. func (api *AvailabilityZonesAPI) getEntityUrl(id string) (url string) {
  89. return api.client.Endpoint + availabilityzoneUrl + "/" + id
  90. }