2014-10-25 20:49:35 +04:00
|
|
|
/*
|
|
|
|
Copyright 2014 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
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-10-31 03:00:16 +03:00
|
|
|
"github.com/coreos/etcd/Godeps/_workspace/src/code.google.com/p/go.net/context"
|
2014-10-27 03:28:47 +03:00
|
|
|
"github.com/coreos/etcd/etcdserver/etcdhttp/httptypes"
|
2014-10-30 02:14:26 +03:00
|
|
|
"github.com/coreos/etcd/pkg/types"
|
2014-10-25 20:49:35 +04:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2014-10-29 22:11:26 +03:00
|
|
|
DefaultV2MembersPrefix = "/v2/members"
|
2014-10-25 20:49:35 +04:00
|
|
|
)
|
|
|
|
|
2014-11-01 00:53:59 +03:00
|
|
|
func NewMembersAPI(c HTTPClient) 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 {
|
2014-10-31 23:59:25 +03:00
|
|
|
List(ctx context.Context) ([]httptypes.Member, error)
|
|
|
|
Add(ctx context.Context, peerURL string) (*httptypes.Member, error)
|
|
|
|
Remove(ctx context.Context, mID string) error
|
2014-10-25 20:49:35 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
type httpMembersAPI struct {
|
2014-11-01 00:53:59 +03:00
|
|
|
client HTTPClient
|
2014-10-25 20:49:35 +04:00
|
|
|
}
|
|
|
|
|
2014-10-31 23:59:25 +03:00
|
|
|
func (m *httpMembersAPI) List(ctx context.Context) ([]httptypes.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
|
|
|
|
}
|
|
|
|
|
2014-10-27 03:28:47 +03:00
|
|
|
var mCollection httptypes.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
|
|
|
|
}
|
|
|
|
|
2014-10-27 19:37:43 +03:00
|
|
|
return []httptypes.Member(mCollection), nil
|
2014-10-25 20:49:35 +04:00
|
|
|
}
|
|
|
|
|
2014-10-31 23:59:25 +03:00
|
|
|
func (m *httpMembersAPI) Add(ctx context.Context, peerURL string) (*httptypes.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 {
|
|
|
|
var httperr httptypes.HTTPError
|
|
|
|
if err := json.Unmarshal(body, &httperr); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return nil, httperr
|
|
|
|
}
|
|
|
|
|
2014-10-30 00:35:31 +03:00
|
|
|
var memb httptypes.Member
|
|
|
|
if err := json.Unmarshal(body, &memb); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &memb, nil
|
2014-10-25 20:49:35 +04:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2014-11-06 23:31:24 +03:00
|
|
|
return assertStatusCode(resp.StatusCode, http.StatusNoContent)
|
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)
|
2014-10-30 02:14:26 +03:00
|
|
|
m := httptypes.MemberCreateRequest{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
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
// 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 {
|
|
|
|
ep.Path = path.Join(ep.Path, DefaultV2MembersPrefix)
|
|
|
|
return &ep
|
|
|
|
}
|