virtual_network.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. package cfapi
  2. import (
  3. "fmt"
  4. "io"
  5. "net/http"
  6. "net/url"
  7. "path"
  8. "strconv"
  9. "time"
  10. "github.com/google/uuid"
  11. "github.com/pkg/errors"
  12. )
  13. type NewVirtualNetwork struct {
  14. Name string `json:"name"`
  15. Comment string `json:"comment"`
  16. IsDefault bool `json:"is_default"`
  17. }
  18. type VirtualNetwork struct {
  19. ID uuid.UUID `json:"id"`
  20. Comment string `json:"comment"`
  21. Name string `json:"name"`
  22. IsDefault bool `json:"is_default_network"`
  23. CreatedAt time.Time `json:"created_at"`
  24. DeletedAt time.Time `json:"deleted_at"`
  25. }
  26. type UpdateVirtualNetwork struct {
  27. Name *string `json:"name,omitempty"`
  28. Comment *string `json:"comment,omitempty"`
  29. IsDefault *bool `json:"is_default_network,omitempty"`
  30. }
  31. func (virtualNetwork VirtualNetwork) TableString() string {
  32. deletedColumn := "-"
  33. if !virtualNetwork.DeletedAt.IsZero() {
  34. deletedColumn = virtualNetwork.DeletedAt.Format(time.RFC3339)
  35. }
  36. return fmt.Sprintf(
  37. "%s\t%s\t%s\t%s\t%s\t%s\t",
  38. virtualNetwork.ID,
  39. virtualNetwork.Name,
  40. strconv.FormatBool(virtualNetwork.IsDefault),
  41. virtualNetwork.Comment,
  42. virtualNetwork.CreatedAt.Format(time.RFC3339),
  43. deletedColumn,
  44. )
  45. }
  46. func (r *RESTClient) CreateVirtualNetwork(newVnet NewVirtualNetwork) (VirtualNetwork, error) {
  47. resp, err := r.sendRequest("POST", r.baseEndpoints.accountVnets, newVnet)
  48. if err != nil {
  49. return VirtualNetwork{}, errors.Wrap(err, "REST request failed")
  50. }
  51. defer resp.Body.Close()
  52. if resp.StatusCode == http.StatusOK {
  53. return parseVnet(resp.Body)
  54. }
  55. return VirtualNetwork{}, r.statusCodeToError("add virtual network", resp)
  56. }
  57. func (r *RESTClient) ListVirtualNetworks(filter *VnetFilter) ([]*VirtualNetwork, error) {
  58. endpoint := r.baseEndpoints.accountVnets
  59. endpoint.RawQuery = filter.Encode()
  60. resp, err := r.sendRequest("GET", endpoint, nil)
  61. if err != nil {
  62. return nil, errors.Wrap(err, "REST request failed")
  63. }
  64. defer resp.Body.Close()
  65. if resp.StatusCode == http.StatusOK {
  66. return parseListVnets(resp.Body)
  67. }
  68. return nil, r.statusCodeToError("list virtual networks", resp)
  69. }
  70. func (r *RESTClient) DeleteVirtualNetwork(id uuid.UUID, force bool) error {
  71. endpoint := r.baseEndpoints.accountVnets
  72. endpoint.Path = path.Join(endpoint.Path, url.PathEscape(id.String()))
  73. queryParams := url.Values{}
  74. if force {
  75. queryParams.Set("force", strconv.FormatBool(force))
  76. }
  77. endpoint.RawQuery = queryParams.Encode()
  78. resp, err := r.sendRequest("DELETE", endpoint, nil)
  79. if err != nil {
  80. return errors.Wrap(err, "REST request failed")
  81. }
  82. defer resp.Body.Close()
  83. if resp.StatusCode == http.StatusOK {
  84. _, err := parseVnet(resp.Body)
  85. return err
  86. }
  87. return r.statusCodeToError("delete virtual network", resp)
  88. }
  89. func (r *RESTClient) UpdateVirtualNetwork(id uuid.UUID, updates UpdateVirtualNetwork) error {
  90. endpoint := r.baseEndpoints.accountVnets
  91. endpoint.Path = path.Join(endpoint.Path, url.PathEscape(id.String()))
  92. resp, err := r.sendRequest("PATCH", endpoint, updates)
  93. if err != nil {
  94. return errors.Wrap(err, "REST request failed")
  95. }
  96. defer resp.Body.Close()
  97. if resp.StatusCode == http.StatusOK {
  98. _, err := parseVnet(resp.Body)
  99. return err
  100. }
  101. return r.statusCodeToError("update virtual network", resp)
  102. }
  103. func parseListVnets(body io.ReadCloser) ([]*VirtualNetwork, error) {
  104. var vnets []*VirtualNetwork
  105. err := parseResponse(body, &vnets)
  106. return vnets, err
  107. }
  108. func parseVnet(body io.ReadCloser) (VirtualNetwork, error) {
  109. var vnet VirtualNetwork
  110. err := parseResponse(body, &vnet)
  111. return vnet, err
  112. }