virtualnetworks.go 2.9 KB

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