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"
|
|
|
|
"time"
|
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
|
|
|
)
|
|
|
|
|
|
|
|
func NewMembersAPI(tr *http.Transport, ep string, to time.Duration) (MembersAPI, error) {
|
|
|
|
u, err := url.Parse(ep)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
c := &httpClient{
|
|
|
|
transport: tr,
|
|
|
|
endpoint: *u,
|
|
|
|
}
|
|
|
|
|
|
|
|
mAPI := httpMembersAPI{
|
2014-10-31 03:00:16 +03:00
|
|
|
client: c,
|
|
|
|
timeout: to,
|
2014-10-25 20:49:35 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return &mAPI, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type MembersAPI interface {
|
2014-10-27 03:28:47 +03:00
|
|
|
List() ([]httptypes.Member, error)
|
2014-10-30 00:35:31 +03:00
|
|
|
Add(peerURL string) (*httptypes.Member, error)
|
|
|
|
Remove(mID string) error
|
2014-10-25 20:49:35 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
type httpMembersAPI struct {
|
2014-10-31 03:00:16 +03:00
|
|
|
client *httpClient
|
|
|
|
timeout time.Duration
|
2014-10-25 20:49:35 +04:00
|
|
|
}
|
|
|
|
|
2014-10-27 03:28:47 +03:00
|
|
|
func (m *httpMembersAPI) List() ([]httptypes.Member, error) {
|
2014-10-31 01:42:34 +03:00
|
|
|
req := &membersAPIActionList{}
|
2014-10-31 03:00:16 +03:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), m.timeout)
|
|
|
|
code, body, err := m.client.do(ctx, req)
|
|
|
|
cancel()
|
2014-10-25 20:49:35 +04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2014-10-30 01:11:37 +03:00
|
|
|
if err := assertStatusCode(http.StatusOK, code); 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-30 00:35:31 +03:00
|
|
|
func (m *httpMembersAPI) Add(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-10-31 03:00:16 +03:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), m.timeout)
|
|
|
|
code, body, err := m.client.do(ctx, req)
|
|
|
|
cancel()
|
2014-10-30 00:35:31 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2014-10-30 01:11:37 +03:00
|
|
|
if err := assertStatusCode(http.StatusCreated, code); err != nil {
|
2014-10-30 00:35:31 +03:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
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-30 00:35:31 +03:00
|
|
|
func (m *httpMembersAPI) Remove(memberID string) error {
|
|
|
|
req := &membersAPIActionRemove{memberID: memberID}
|
2014-10-31 03:00:16 +03:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), m.timeout)
|
|
|
|
code, _, err := m.client.do(ctx, req)
|
|
|
|
cancel()
|
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-10-30 01:11:37 +03:00
|
|
|
return assertStatusCode(http.StatusNoContent, code)
|
2014-10-25 20:49:35 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
type membersAPIActionList struct{}
|
|
|
|
|
|
|
|
func (l *membersAPIActionList) httpRequest(ep url.URL) *http.Request {
|
2014-10-31 01:42:34 +03:00
|
|
|
ep.Path = path.Join(ep.Path, DefaultV2MembersPrefix)
|
2014-10-25 20:49:35 +04:00
|
|
|
req, _ := http.NewRequest("GET", ep.String(), nil)
|
|
|
|
return req
|
|
|
|
}
|
2014-10-30 00:35:31 +03:00
|
|
|
|
|
|
|
type membersAPIActionRemove struct {
|
|
|
|
memberID string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *membersAPIActionRemove) httpRequest(ep url.URL) *http.Request {
|
2014-10-31 01:42:34 +03:00
|
|
|
ep.Path = path.Join(ep.Path, DefaultV2MembersPrefix, d.memberID)
|
2014-10-30 00:35:31 +03:00
|
|
|
req, _ := http.NewRequest("DELETE", ep.String(), nil)
|
|
|
|
return req
|
|
|
|
}
|
|
|
|
|
|
|
|
type membersAPIActionAdd struct {
|
2014-10-30 02:14:26 +03:00
|
|
|
peerURLs types.URLs
|
2014-10-30 00:35:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *membersAPIActionAdd) httpRequest(ep url.URL) *http.Request {
|
2014-10-31 01:42:34 +03:00
|
|
|
ep.Path = path.Join(ep.Path, DefaultV2MembersPrefix)
|
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)
|
|
|
|
req, _ := http.NewRequest("POST", ep.String(), bytes.NewReader(b))
|
|
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
return req
|
|
|
|
}
|
|
|
|
|
|
|
|
func assertStatusCode(want, got int) (err error) {
|
|
|
|
if want != got {
|
|
|
|
err = fmt.Errorf("unexpected status code %d", got)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|