routers.go 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 routers API.
  15. type RoutersAPI struct {
  16. client *Client
  17. }
  18. var routerUrl string = rootUrl + "/routers/"
  19. // Gets a router with the specified ID.
  20. func (api *RoutersAPI) Get(id string) (router *Router, err error) {
  21. res, err := api.client.restClient.Get(api.client.Endpoint+routerUrl+id, api.client.options.TokenOptions)
  22. if err != nil {
  23. return
  24. }
  25. defer res.Body.Close()
  26. res, err = getError(res)
  27. if err != nil {
  28. return
  29. }
  30. var result Router
  31. err = json.NewDecoder(res.Body).Decode(&result)
  32. return &result, nil
  33. }
  34. // Updates router's attributes.
  35. func (api *RoutersAPI) UpdateRouter(id string, routerSpec *RouterUpdateSpec) (task *Task, err error) {
  36. body, err := json.Marshal(routerSpec)
  37. if err != nil {
  38. return
  39. }
  40. res, err := api.client.restClient.Put(
  41. api.client.Endpoint+routerUrl+id,
  42. "application/json",
  43. bytes.NewReader(body),
  44. api.client.options.TokenOptions)
  45. if err != nil {
  46. return
  47. }
  48. defer res.Body.Close()
  49. task, err = getTask(getError(res))
  50. return
  51. }
  52. // Deletes a router with specified ID.
  53. func (api *RoutersAPI) Delete(routerID string) (task *Task, err error) {
  54. res, err := api.client.restClient.Delete(api.client.Endpoint+routerUrl+routerID, 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. }
  62. // Creates a subnet on the specified router.
  63. func (api *RoutersAPI) CreateSubnet(routerID string, spec *SubnetCreateSpec) (task *Task, err error) {
  64. body, err := json.Marshal(spec)
  65. if err != nil {
  66. return
  67. }
  68. res, err := api.client.restClient.Post(
  69. api.client.Endpoint+routerUrl+routerID+"/subnets",
  70. "application/json",
  71. bytes.NewReader(body),
  72. api.client.options.TokenOptions)
  73. if err != nil {
  74. return
  75. }
  76. defer res.Body.Close()
  77. task, err = getTask(getError(res))
  78. return
  79. }