2015-01-25 06:19:16 +03:00
|
|
|
// Copyright 2015 CoreOS, Inc.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
2014-10-25 20:49:35 +04:00
|
|
|
|
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
2014-10-30 00:35:31 +03:00
|
|
|
"bytes"
|
2014-10-25 20:49:35 +04:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"path"
|
2014-10-27 03:28:47 +03:00
|
|
|
|
2014-11-20 21:02:52 +03:00
|
|
|
"github.com/coreos/etcd/Godeps/_workspace/src/golang.org/x/net/context"
|
2015-01-29 02:22:27 +03:00
|
|
|
|
2014-10-30 02:14:26 +03:00
|
|
|
"github.com/coreos/etcd/pkg/types"
|
2014-10-25 20:49:35 +04:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2015-01-27 22:26:20 +03:00
|
|
|
defaultV2MembersPrefix = "/v2/members"
|
2015-12-21 11:46:41 +03:00
|
|
|
defaultLeaderSuffix = "/leader"
|
2014-10-25 20:49:35 +04:00
|
|
|
)
|
|
|
|
|
2015-01-29 02:22:27 +03:00
|
|
|
type Member struct {
|
2015-01-29 03:10:20 +03:00
|
|
|
// ID is the unique identifier of this Member.
|
|
|
|
ID string `json:"id"`
|
|
|
|
|
|
|
|
// Name is a human-readable, non-unique identifier of this Member.
|
|
|
|
Name string `json:"name"`
|
|
|
|
|
|
|
|
// PeerURLs represents the HTTP(S) endpoints this Member uses to
|
|
|
|
// participate in etcd's consensus protocol.
|
|
|
|
PeerURLs []string `json:"peerURLs"`
|
|
|
|
|
|
|
|
// ClientURLs represents the HTTP(S) endpoints on which this Member
|
|
|
|
// serves it's client-facing APIs.
|
2015-01-29 02:22:27 +03:00
|
|
|
ClientURLs []string `json:"clientURLs"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type memberCollection []Member
|
|
|
|
|
|
|
|
func (c *memberCollection) UnmarshalJSON(data []byte) error {
|
|
|
|
d := struct {
|
|
|
|
Members []Member
|
|
|
|
}{}
|
|
|
|
|
|
|
|
if err := json.Unmarshal(data, &d); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if d.Members == nil {
|
|
|
|
*c = make([]Member, 0)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
*c = d.Members
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-07-01 23:38:03 +03:00
|
|
|
type memberCreateOrUpdateRequest struct {
|
2015-01-29 02:22:27 +03:00
|
|
|
PeerURLs types.URLs
|
|
|
|
}
|
|
|
|
|
2015-07-01 23:38:03 +03:00
|
|
|
func (m *memberCreateOrUpdateRequest) MarshalJSON() ([]byte, error) {
|
2015-01-29 02:22:27 +03:00
|
|
|
s := struct {
|
|
|
|
PeerURLs []string `json:"peerURLs"`
|
|
|
|
}{
|
|
|
|
PeerURLs: make([]string, len(m.PeerURLs)),
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, u := range m.PeerURLs {
|
|
|
|
s.PeerURLs[i] = u.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
return json.Marshal(&s)
|
|
|
|
}
|
2015-01-28 03:55:13 +03:00
|
|
|
|
2015-01-28 00:51:07 +03:00
|
|
|
// NewMembersAPI constructs a new MembersAPI that uses HTTP to
|
|
|
|
// interact with etcd's membership API.
|
2015-01-27 22:23:23 +03:00
|
|
|
func NewMembersAPI(c Client) MembersAPI {
|
2014-10-31 21:01:34 +03:00
|
|
|
return &httpMembersAPI{
|
2014-10-31 23:59:25 +03:00
|
|
|
client: c,
|
2014-10-25 20:49:35 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type MembersAPI interface {
|
2015-01-29 03:10:20 +03:00
|
|
|
// List enumerates the current cluster membership.
|
2015-01-28 03:55:13 +03:00
|
|
|
List(ctx context.Context) ([]Member, error)
|
2015-01-28 04:10:30 +03:00
|
|
|
|
2015-01-29 03:10:20 +03:00
|
|
|
// Add instructs etcd to accept a new Member into the cluster.
|
2015-01-28 03:55:13 +03:00
|
|
|
Add(ctx context.Context, peerURL string) (*Member, error)
|
2015-01-28 04:10:30 +03:00
|
|
|
|
2015-01-29 03:10:20 +03:00
|
|
|
// Remove demotes an existing Member out of the cluster.
|
2014-10-31 23:59:25 +03:00
|
|
|
Remove(ctx context.Context, mID string) error
|
2015-07-01 23:38:03 +03:00
|
|
|
|
|
|
|
// Update instructs etcd to update an existing Member in the cluster.
|
|
|
|
Update(ctx context.Context, mID string, peerURLs []string) error
|
2015-12-21 11:46:41 +03:00
|
|
|
|
|
|
|
// Leader gets current leader of the cluster
|
|
|
|
Leader(ctx context.Context) (*Member, error)
|
2014-10-25 20:49:35 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
type httpMembersAPI struct {
|
2015-01-27 22:21:30 +03:00
|
|
|
client httpClient
|
2014-10-25 20:49:35 +04:00
|
|
|
}
|
|
|
|
|
2015-01-28 03:55:13 +03:00
|
|
|
func (m *httpMembersAPI) List(ctx context.Context) ([]Member, error) {
|
2014-10-31 01:42:34 +03:00
|
|
|
req := &membersAPIActionList{}
|
2014-11-01 00:24:14 +03:00
|
|
|
resp, body, err := m.client.Do(ctx, req)
|
2014-10-25 20:49:35 +04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2014-11-06 23:31:24 +03:00
|
|
|
if err := assertStatusCode(resp.StatusCode, http.StatusOK); err != nil {
|
2014-10-25 20:49:35 +04:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-01-29 02:22:27 +03:00
|
|
|
var mCollection memberCollection
|
2014-10-27 19:44:41 +03:00
|
|
|
if err := json.Unmarshal(body, &mCollection); err != nil {
|
2014-10-25 20:49:35 +04:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-01-29 02:22:27 +03:00
|
|
|
return []Member(mCollection), nil
|
2014-10-25 20:49:35 +04:00
|
|
|
}
|
|
|
|
|
2015-01-28 03:55:13 +03:00
|
|
|
func (m *httpMembersAPI) Add(ctx context.Context, peerURL string) (*Member, error) {
|
2014-10-30 02:14:26 +03:00
|
|
|
urls, err := types.NewURLs([]string{peerURL})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
req := &membersAPIActionAdd{peerURLs: urls}
|
2014-11-01 00:24:14 +03:00
|
|
|
resp, body, err := m.client.Do(ctx, req)
|
2014-10-30 00:35:31 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2014-11-06 23:31:24 +03:00
|
|
|
if err := assertStatusCode(resp.StatusCode, http.StatusCreated, http.StatusConflict); err != nil {
|
2014-10-30 00:35:31 +03:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2014-11-06 23:31:24 +03:00
|
|
|
if resp.StatusCode != http.StatusCreated {
|
2015-01-29 03:46:58 +03:00
|
|
|
var merr membersError
|
|
|
|
if err := json.Unmarshal(body, &merr); err != nil {
|
2014-11-06 23:31:24 +03:00
|
|
|
return nil, err
|
|
|
|
}
|
2015-01-29 03:46:58 +03:00
|
|
|
return nil, merr
|
2014-11-06 23:31:24 +03:00
|
|
|
}
|
|
|
|
|
2015-01-28 03:55:13 +03:00
|
|
|
var memb Member
|
2014-10-30 00:35:31 +03:00
|
|
|
if err := json.Unmarshal(body, &memb); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &memb, nil
|
2014-10-25 20:49:35 +04:00
|
|
|
}
|
|
|
|
|
2015-07-01 23:38:03 +03:00
|
|
|
func (m *httpMembersAPI) Update(ctx context.Context, memberID string, peerURLs []string) error {
|
|
|
|
urls, err := types.NewURLs(peerURLs)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
req := &membersAPIActionUpdate{peerURLs: urls, memberID: memberID}
|
|
|
|
resp, body, err := m.client.Do(ctx, req)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := assertStatusCode(resp.StatusCode, http.StatusNoContent, http.StatusNotFound, http.StatusConflict); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp.StatusCode != http.StatusNoContent {
|
|
|
|
var merr membersError
|
|
|
|
if err := json.Unmarshal(body, &merr); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return merr
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-10-31 23:59:25 +03:00
|
|
|
func (m *httpMembersAPI) Remove(ctx context.Context, memberID string) error {
|
2014-10-30 00:35:31 +03:00
|
|
|
req := &membersAPIActionRemove{memberID: memberID}
|
2014-11-01 00:24:14 +03:00
|
|
|
resp, _, err := m.client.Do(ctx, req)
|
2014-10-30 00:35:31 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2014-10-25 20:49:35 +04:00
|
|
|
}
|
2014-10-30 00:35:31 +03:00
|
|
|
|
2015-04-25 02:59:29 +03:00
|
|
|
return assertStatusCode(resp.StatusCode, http.StatusNoContent, http.StatusGone)
|
2014-10-25 20:49:35 +04:00
|
|
|
}
|
|
|
|
|
2015-12-21 11:46:41 +03:00
|
|
|
func (m *httpMembersAPI) Leader(ctx context.Context) (*Member, error) {
|
|
|
|
req := &membersAPIActionLeader{}
|
|
|
|
resp, body, err := m.client.Do(ctx, req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := assertStatusCode(resp.StatusCode, http.StatusOK); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var leader Member
|
|
|
|
if err := json.Unmarshal(body, &leader); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &leader, nil
|
|
|
|
}
|
|
|
|
|
2014-10-25 20:49:35 +04:00
|
|
|
type membersAPIActionList struct{}
|
|
|
|
|
2014-11-01 00:24:14 +03:00
|
|
|
func (l *membersAPIActionList) HTTPRequest(ep url.URL) *http.Request {
|
2014-10-31 21:44:28 +03:00
|
|
|
u := v2MembersURL(ep)
|
|
|
|
req, _ := http.NewRequest("GET", u.String(), nil)
|
2014-10-25 20:49:35 +04:00
|
|
|
return req
|
|
|
|
}
|
2014-10-30 00:35:31 +03:00
|
|
|
|
|
|
|
type membersAPIActionRemove struct {
|
|
|
|
memberID string
|
|
|
|
}
|
|
|
|
|
2014-11-01 00:24:14 +03:00
|
|
|
func (d *membersAPIActionRemove) HTTPRequest(ep url.URL) *http.Request {
|
2014-10-31 21:44:28 +03:00
|
|
|
u := v2MembersURL(ep)
|
|
|
|
u.Path = path.Join(u.Path, d.memberID)
|
|
|
|
req, _ := http.NewRequest("DELETE", u.String(), nil)
|
2014-10-30 00:35:31 +03:00
|
|
|
return req
|
|
|
|
}
|
|
|
|
|
|
|
|
type membersAPIActionAdd struct {
|
2014-10-30 02:14:26 +03:00
|
|
|
peerURLs types.URLs
|
2014-10-30 00:35:31 +03:00
|
|
|
}
|
|
|
|
|
2014-11-01 00:24:14 +03:00
|
|
|
func (a *membersAPIActionAdd) HTTPRequest(ep url.URL) *http.Request {
|
2014-10-31 21:44:28 +03:00
|
|
|
u := v2MembersURL(ep)
|
2015-07-01 23:38:03 +03:00
|
|
|
m := memberCreateOrUpdateRequest{PeerURLs: a.peerURLs}
|
2014-10-30 00:35:31 +03:00
|
|
|
b, _ := json.Marshal(&m)
|
2014-10-31 21:44:28 +03:00
|
|
|
req, _ := http.NewRequest("POST", u.String(), bytes.NewReader(b))
|
2014-10-30 00:35:31 +03:00
|
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
return req
|
|
|
|
}
|
|
|
|
|
2015-07-01 23:38:03 +03:00
|
|
|
type membersAPIActionUpdate struct {
|
|
|
|
memberID string
|
|
|
|
peerURLs types.URLs
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *membersAPIActionUpdate) HTTPRequest(ep url.URL) *http.Request {
|
|
|
|
u := v2MembersURL(ep)
|
|
|
|
m := memberCreateOrUpdateRequest{PeerURLs: a.peerURLs}
|
|
|
|
u.Path = path.Join(u.Path, a.memberID)
|
|
|
|
b, _ := json.Marshal(&m)
|
|
|
|
req, _ := http.NewRequest("PUT", u.String(), bytes.NewReader(b))
|
|
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
return req
|
|
|
|
}
|
|
|
|
|
2014-11-06 23:31:24 +03:00
|
|
|
func assertStatusCode(got int, want ...int) (err error) {
|
|
|
|
for _, w := range want {
|
|
|
|
if w == got {
|
|
|
|
return nil
|
|
|
|
}
|
2014-10-30 00:35:31 +03:00
|
|
|
}
|
2014-11-06 23:31:24 +03:00
|
|
|
return fmt.Errorf("unexpected status code %d", got)
|
2014-10-30 00:35:31 +03:00
|
|
|
}
|
2014-10-31 21:44:28 +03:00
|
|
|
|
2015-12-21 11:46:41 +03:00
|
|
|
type membersAPIActionLeader struct{}
|
|
|
|
|
|
|
|
func (l *membersAPIActionLeader) HTTPRequest(ep url.URL) *http.Request {
|
|
|
|
u := v2MembersURL(ep)
|
|
|
|
u.Path = path.Join(u.Path, defaultLeaderSuffix)
|
|
|
|
req, _ := http.NewRequest("GET", u.String(), nil)
|
|
|
|
return req
|
|
|
|
}
|
|
|
|
|
2014-10-31 21:44:28 +03:00
|
|
|
// v2MembersURL add the necessary path to the provided endpoint
|
|
|
|
// to route requests to the default v2 members API.
|
|
|
|
func v2MembersURL(ep url.URL) *url.URL {
|
2015-01-27 22:26:20 +03:00
|
|
|
ep.Path = path.Join(ep.Path, defaultV2MembersPrefix)
|
2014-10-31 21:44:28 +03:00
|
|
|
return &ep
|
|
|
|
}
|
2015-01-29 03:46:58 +03:00
|
|
|
|
|
|
|
type membersError struct {
|
|
|
|
Message string `json:"message"`
|
|
|
|
Code int `json:"-"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e membersError) Error() string {
|
|
|
|
return e.Message
|
|
|
|
}
|