subnets.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. // Copyright (c) 2017 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 subnets API.
  15. type SubnetsAPI struct {
  16. client *Client
  17. }
  18. var subnetUrl string = "/temp-subnets/"
  19. // Deletes a subnet with the specified ID.
  20. func (api *SubnetsAPI) Delete(id string) (task *Task, err error) {
  21. res, err := api.client.restClient.Delete(api.client.Endpoint+subnetUrl+id, api.client.options.TokenOptions)
  22. if err != nil {
  23. return
  24. }
  25. defer res.Body.Close()
  26. task, err = getTask(getError(res))
  27. return
  28. }
  29. // Gets a subnet with the specified ID.
  30. func (api *SubnetsAPI) Get(id string) (subnet *Subnet, err error) {
  31. res, err := api.client.restClient.Get(api.client.Endpoint+subnetUrl+id, api.client.options.TokenOptions)
  32. if err != nil {
  33. return
  34. }
  35. defer res.Body.Close()
  36. res, err = getError(res)
  37. if err != nil {
  38. return
  39. }
  40. var result Subnet
  41. err = json.NewDecoder(res.Body).Decode(&result)
  42. return &result, nil
  43. }
  44. // Updates subnet's attributes.
  45. func (api *SubnetsAPI) Update(id string, subnetSpec *SubnetUpdateSpec) (task *Task, err error) {
  46. body, err := json.Marshal(subnetSpec)
  47. if err != nil {
  48. return
  49. }
  50. res, err := api.client.restClient.Put(
  51. api.client.Endpoint+subnetUrl+id,
  52. "application/json",
  53. bytes.NewReader(body),
  54. api.client.options.TokenOptions)
  55. if err != nil {
  56. return
  57. }
  58. defer res.Body.Close()
  59. task, err = getTask(getError(res))
  60. return
  61. }