members.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. // Copyright 2015 CoreOS, Inc.
  2. //
  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. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package client
  15. import (
  16. "bytes"
  17. "encoding/json"
  18. "fmt"
  19. "net/http"
  20. "net/url"
  21. "path"
  22. "github.com/coreos/etcd/Godeps/_workspace/src/golang.org/x/net/context"
  23. "github.com/coreos/etcd/etcdserver/etcdhttp/httptypes"
  24. "github.com/coreos/etcd/pkg/types"
  25. )
  26. var (
  27. DefaultV2MembersPrefix = "/v2/members"
  28. )
  29. func NewMembersAPI(c HTTPClient) MembersAPI {
  30. return &httpMembersAPI{
  31. client: c,
  32. }
  33. }
  34. type MembersAPI interface {
  35. List(ctx context.Context) ([]httptypes.Member, error)
  36. Add(ctx context.Context, peerURL string) (*httptypes.Member, error)
  37. Remove(ctx context.Context, mID string) error
  38. }
  39. type httpMembersAPI struct {
  40. client HTTPClient
  41. }
  42. func (m *httpMembersAPI) List(ctx context.Context) ([]httptypes.Member, error) {
  43. req := &membersAPIActionList{}
  44. resp, body, err := m.client.Do(ctx, req)
  45. if err != nil {
  46. return nil, err
  47. }
  48. if err := assertStatusCode(resp.StatusCode, http.StatusOK); err != nil {
  49. return nil, err
  50. }
  51. var mCollection httptypes.MemberCollection
  52. if err := json.Unmarshal(body, &mCollection); err != nil {
  53. return nil, err
  54. }
  55. return []httptypes.Member(mCollection), nil
  56. }
  57. func (m *httpMembersAPI) Add(ctx context.Context, peerURL string) (*httptypes.Member, error) {
  58. urls, err := types.NewURLs([]string{peerURL})
  59. if err != nil {
  60. return nil, err
  61. }
  62. req := &membersAPIActionAdd{peerURLs: urls}
  63. resp, body, err := m.client.Do(ctx, req)
  64. if err != nil {
  65. return nil, err
  66. }
  67. if err := assertStatusCode(resp.StatusCode, http.StatusCreated, http.StatusConflict); err != nil {
  68. return nil, err
  69. }
  70. if resp.StatusCode != http.StatusCreated {
  71. var httperr httptypes.HTTPError
  72. if err := json.Unmarshal(body, &httperr); err != nil {
  73. return nil, err
  74. }
  75. return nil, httperr
  76. }
  77. var memb httptypes.Member
  78. if err := json.Unmarshal(body, &memb); err != nil {
  79. return nil, err
  80. }
  81. return &memb, nil
  82. }
  83. func (m *httpMembersAPI) Remove(ctx context.Context, memberID string) error {
  84. req := &membersAPIActionRemove{memberID: memberID}
  85. resp, _, err := m.client.Do(ctx, req)
  86. if err != nil {
  87. return err
  88. }
  89. return assertStatusCode(resp.StatusCode, http.StatusNoContent, http.StatusGone)
  90. }
  91. type membersAPIActionList struct{}
  92. func (l *membersAPIActionList) HTTPRequest(ep url.URL) *http.Request {
  93. u := v2MembersURL(ep)
  94. req, _ := http.NewRequest("GET", u.String(), nil)
  95. return req
  96. }
  97. type membersAPIActionRemove struct {
  98. memberID string
  99. }
  100. func (d *membersAPIActionRemove) HTTPRequest(ep url.URL) *http.Request {
  101. u := v2MembersURL(ep)
  102. u.Path = path.Join(u.Path, d.memberID)
  103. req, _ := http.NewRequest("DELETE", u.String(), nil)
  104. return req
  105. }
  106. type membersAPIActionAdd struct {
  107. peerURLs types.URLs
  108. }
  109. func (a *membersAPIActionAdd) HTTPRequest(ep url.URL) *http.Request {
  110. u := v2MembersURL(ep)
  111. m := httptypes.MemberCreateRequest{PeerURLs: a.peerURLs}
  112. b, _ := json.Marshal(&m)
  113. req, _ := http.NewRequest("POST", u.String(), bytes.NewReader(b))
  114. req.Header.Set("Content-Type", "application/json")
  115. return req
  116. }
  117. func assertStatusCode(got int, want ...int) (err error) {
  118. for _, w := range want {
  119. if w == got {
  120. return nil
  121. }
  122. }
  123. return fmt.Errorf("unexpected status code %d", got)
  124. }
  125. // v2MembersURL add the necessary path to the provided endpoint
  126. // to route requests to the default v2 members API.
  127. func v2MembersURL(ep url.URL) *url.URL {
  128. ep.Path = path.Join(ep.Path, DefaultV2MembersPrefix)
  129. return &ep
  130. }