cluster.go 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // Copyright 2016 The etcd Authors
  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 clientv3
  15. import (
  16. "context"
  17. pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
  18. )
  19. type (
  20. Member pb.Member
  21. MemberListResponse pb.MemberListResponse
  22. MemberAddResponse pb.MemberAddResponse
  23. MemberRemoveResponse pb.MemberRemoveResponse
  24. MemberUpdateResponse pb.MemberUpdateResponse
  25. )
  26. type Cluster interface {
  27. // MemberList lists the current cluster membership.
  28. MemberList(ctx context.Context) (*MemberListResponse, error)
  29. // MemberAdd adds a new member into the cluster.
  30. MemberAdd(ctx context.Context, peerAddrs []string) (*MemberAddResponse, error)
  31. // MemberRemove removes an existing member from the cluster.
  32. MemberRemove(ctx context.Context, id uint64) (*MemberRemoveResponse, error)
  33. // MemberUpdate updates the peer addresses of the member.
  34. MemberUpdate(ctx context.Context, id uint64, peerAddrs []string) (*MemberUpdateResponse, error)
  35. }
  36. type cluster struct {
  37. remote pb.ClusterClient
  38. }
  39. func NewCluster(c *Client) Cluster {
  40. return &cluster{remote: RetryClusterClient(c)}
  41. }
  42. func NewClusterFromClusterClient(remote pb.ClusterClient) Cluster {
  43. return &cluster{remote: remote}
  44. }
  45. func (c *cluster) MemberAdd(ctx context.Context, peerAddrs []string) (*MemberAddResponse, error) {
  46. r := &pb.MemberAddRequest{PeerURLs: peerAddrs}
  47. resp, err := c.remote.MemberAdd(ctx, r)
  48. if err != nil {
  49. return nil, toErr(ctx, err)
  50. }
  51. return (*MemberAddResponse)(resp), nil
  52. }
  53. func (c *cluster) MemberRemove(ctx context.Context, id uint64) (*MemberRemoveResponse, error) {
  54. r := &pb.MemberRemoveRequest{ID: id}
  55. resp, err := c.remote.MemberRemove(ctx, r)
  56. if err != nil {
  57. return nil, toErr(ctx, err)
  58. }
  59. return (*MemberRemoveResponse)(resp), nil
  60. }
  61. func (c *cluster) MemberUpdate(ctx context.Context, id uint64, peerAddrs []string) (*MemberUpdateResponse, error) {
  62. // it is safe to retry on update.
  63. r := &pb.MemberUpdateRequest{ID: id, PeerURLs: peerAddrs}
  64. resp, err := c.remote.MemberUpdate(ctx, r)
  65. if err == nil {
  66. return (*MemberUpdateResponse)(resp), nil
  67. }
  68. return nil, toErr(ctx, err)
  69. }
  70. func (c *cluster) MemberList(ctx context.Context) (*MemberListResponse, error) {
  71. // it is safe to retry on list.
  72. resp, err := c.remote.MemberList(ctx, &pb.MemberListRequest{})
  73. if err == nil {
  74. return (*MemberListResponse)(resp), nil
  75. }
  76. return nil, toErr(ctx, err)
  77. }