/cloudmock/openstack/mockloadbalancer/pools.go

https://github.com/kubernetes/kops · Go · 159 lines · 123 code · 20 blank · 16 comment · 32 complexity · dce7e5a616d2de0e0f2156fab15f84c0 MD5 · raw file

  1. /*
  2. Copyright 2020 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package mockloadbalancer
  14. import (
  15. "encoding/json"
  16. "fmt"
  17. "net/http"
  18. "net/url"
  19. "regexp"
  20. "github.com/google/uuid"
  21. "github.com/gophercloud/gophercloud/openstack/loadbalancer/v2/pools"
  22. )
  23. type poolListResponse struct {
  24. Pools []pools.Pool `json:"pools"`
  25. }
  26. type poolGetResponse struct {
  27. Pool pools.Pool `json:"pool"`
  28. }
  29. type poolCreateRequest struct {
  30. Pool pools.CreateOpts `json:"pool"`
  31. }
  32. func (m *MockClient) mockPools() {
  33. re := regexp.MustCompile(`/lbaas/pools/?`)
  34. handler := func(w http.ResponseWriter, r *http.Request) {
  35. m.mutex.Lock()
  36. defer m.mutex.Unlock()
  37. w.Header().Add("Content-Type", "application/json")
  38. poolID := re.ReplaceAllString(r.URL.Path, "")
  39. // TODO: handle /members subresource
  40. switch r.Method {
  41. case http.MethodGet:
  42. if poolID == "" {
  43. r.ParseForm()
  44. m.listPools(w, r.Form)
  45. } else {
  46. m.getPool(w, poolID)
  47. }
  48. case http.MethodPost:
  49. m.createPool(w, r)
  50. case http.MethodDelete:
  51. m.deletePool(w, poolID)
  52. default:
  53. w.WriteHeader(http.StatusBadRequest)
  54. }
  55. }
  56. m.Mux.HandleFunc("/lbaas/pools/", handler)
  57. m.Mux.HandleFunc("/lbaas/pools", handler)
  58. }
  59. func (m *MockClient) listPools(w http.ResponseWriter, vals url.Values) {
  60. w.WriteHeader(http.StatusOK)
  61. pools := make([]pools.Pool, 0)
  62. id := vals.Get("id")
  63. name := vals.Get("name")
  64. for _, p := range m.pools {
  65. if id != "" && id != p.ID {
  66. continue
  67. }
  68. if name != "" && name != p.Name {
  69. continue
  70. }
  71. pools = append(pools, p)
  72. }
  73. resp := poolListResponse{
  74. Pools: pools,
  75. }
  76. respB, err := json.Marshal(resp)
  77. if err != nil {
  78. panic(fmt.Sprintf("failed to marshal %+v", resp))
  79. }
  80. _, err = w.Write(respB)
  81. if err != nil {
  82. panic("failed to write body")
  83. }
  84. }
  85. func (m *MockClient) getPool(w http.ResponseWriter, poolID string) {
  86. if pool, ok := m.pools[poolID]; ok {
  87. resp := poolGetResponse{
  88. Pool: pool,
  89. }
  90. respB, err := json.Marshal(resp)
  91. if err != nil {
  92. panic(fmt.Sprintf("failed to marshal %+v", resp))
  93. }
  94. _, err = w.Write(respB)
  95. if err != nil {
  96. panic("failed to write body")
  97. }
  98. } else {
  99. w.WriteHeader(http.StatusNotFound)
  100. }
  101. }
  102. func (m *MockClient) deletePool(w http.ResponseWriter, poolID string) {
  103. if _, ok := m.pools[poolID]; ok {
  104. delete(m.pools, poolID)
  105. w.WriteHeader(http.StatusOK)
  106. } else {
  107. w.WriteHeader(http.StatusNotFound)
  108. }
  109. }
  110. func (m *MockClient) createPool(w http.ResponseWriter, r *http.Request) {
  111. var create poolCreateRequest
  112. err := json.NewDecoder(r.Body).Decode(&create)
  113. if err != nil {
  114. panic("error decoding create pool request")
  115. }
  116. w.WriteHeader(http.StatusAccepted)
  117. p := pools.Pool{
  118. ID: uuid.New().String(),
  119. Name: create.Pool.Name,
  120. LBMethod: string(create.Pool.LBMethod),
  121. Protocol: string(create.Pool.Protocol),
  122. Loadbalancers: []pools.LoadBalancerID{{ID: create.Pool.LoadbalancerID}},
  123. }
  124. m.pools[p.ID] = p
  125. resp := poolGetResponse{
  126. Pool: p,
  127. }
  128. respB, err := json.Marshal(resp)
  129. if err != nil {
  130. panic(fmt.Sprintf("failed to marshal %+v", resp))
  131. }
  132. _, err = w.Write(respB)
  133. if err != nil {
  134. panic("failed to write body")
  135. }
  136. }