networks.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 networks API.
  15. type NetworksAPI struct {
  16. client *Client
  17. }
  18. // Options used for GetAll API
  19. type NetworkGetOptions struct {
  20. Name string `urlParam:"name"`
  21. }
  22. var networkUrl string = rootUrl + "/subnets"
  23. // Creates a network.
  24. func (api *NetworksAPI) Create(networkSpec *NetworkCreateSpec) (task *Task, err error) {
  25. body, err := json.Marshal(networkSpec)
  26. if err != nil {
  27. return
  28. }
  29. res, err := api.client.restClient.Post(
  30. api.client.Endpoint+networkUrl,
  31. "application/json",
  32. bytes.NewReader(body),
  33. api.client.options.TokenOptions)
  34. if err != nil {
  35. return
  36. }
  37. defer res.Body.Close()
  38. task, err = getTask(getError(res))
  39. return
  40. }
  41. // Deletes a network with specified ID.
  42. func (api *NetworksAPI) Delete(id string) (task *Task, err error) {
  43. res, err := api.client.restClient.Delete(api.client.Endpoint+networkUrl+"/"+id, api.client.options.TokenOptions)
  44. if err != nil {
  45. return
  46. }
  47. defer res.Body.Close()
  48. task, err = getTask(getError(res))
  49. return
  50. }
  51. // Gets a network with the specified ID.
  52. func (api *NetworksAPI) Get(id string) (network *Network, err error) {
  53. res, err := api.client.restClient.Get(api.client.Endpoint+networkUrl+"/"+id, api.client.options.TokenOptions)
  54. if err != nil {
  55. return
  56. }
  57. defer res.Body.Close()
  58. res, err = getError(res)
  59. if err != nil {
  60. return
  61. }
  62. var result Network
  63. err = json.NewDecoder(res.Body).Decode(&result)
  64. return &result, nil
  65. }
  66. // Returns all networks
  67. func (api *NetworksAPI) GetAll(options *NetworkGetOptions) (result *Networks, err error) {
  68. uri := api.client.Endpoint + networkUrl
  69. if options != nil {
  70. uri += getQueryString(options)
  71. }
  72. res, err := api.client.restClient.GetList(api.client.Endpoint, uri, api.client.options.TokenOptions)
  73. result = &Networks{}
  74. err = json.Unmarshal(res, result)
  75. return
  76. }
  77. // Sets default network.
  78. func (api *NetworksAPI) SetDefault(id string) (task *Task, err error) {
  79. res, err := api.client.restClient.Post(
  80. api.client.Endpoint+networkUrl+"/"+id+"/set_default",
  81. "application/json",
  82. bytes.NewReader([]byte("")),
  83. api.client.options.TokenOptions)
  84. if err != nil {
  85. return
  86. }
  87. defer res.Body.Close()
  88. task, err = getTask(getError(res))
  89. return
  90. }