member.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 v3rpc
  15. import (
  16. "context"
  17. "time"
  18. "github.com/coreos/etcd/etcdserver"
  19. "github.com/coreos/etcd/etcdserver/api"
  20. "github.com/coreos/etcd/etcdserver/api/v3rpc/rpctypes"
  21. pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
  22. "github.com/coreos/etcd/etcdserver/membership"
  23. "github.com/coreos/etcd/pkg/types"
  24. )
  25. type ClusterServer struct {
  26. cluster api.Cluster
  27. server etcdserver.ServerV3
  28. }
  29. func NewClusterServer(s etcdserver.ServerV3) *ClusterServer {
  30. return &ClusterServer{
  31. cluster: s.Cluster(),
  32. server: s,
  33. }
  34. }
  35. func (cs *ClusterServer) MemberAdd(ctx context.Context, r *pb.MemberAddRequest) (*pb.MemberAddResponse, error) {
  36. urls, err := types.NewURLs(r.PeerURLs)
  37. if err != nil {
  38. return nil, rpctypes.ErrGRPCMemberBadURLs
  39. }
  40. now := time.Now()
  41. m := membership.NewMember("", urls, "", &now)
  42. membs, merr := cs.server.AddMember(ctx, *m)
  43. if merr != nil {
  44. return nil, togRPCError(merr)
  45. }
  46. return &pb.MemberAddResponse{
  47. Header: cs.header(),
  48. Member: &pb.Member{ID: uint64(m.ID), PeerURLs: m.PeerURLs},
  49. Members: membersToProtoMembers(membs),
  50. }, nil
  51. }
  52. func (cs *ClusterServer) MemberRemove(ctx context.Context, r *pb.MemberRemoveRequest) (*pb.MemberRemoveResponse, error) {
  53. membs, err := cs.server.RemoveMember(ctx, r.ID)
  54. if err != nil {
  55. return nil, togRPCError(err)
  56. }
  57. return &pb.MemberRemoveResponse{Header: cs.header(), Members: membersToProtoMembers(membs)}, nil
  58. }
  59. func (cs *ClusterServer) MemberUpdate(ctx context.Context, r *pb.MemberUpdateRequest) (*pb.MemberUpdateResponse, error) {
  60. m := membership.Member{
  61. ID: types.ID(r.ID),
  62. RaftAttributes: membership.RaftAttributes{PeerURLs: r.PeerURLs},
  63. }
  64. membs, err := cs.server.UpdateMember(ctx, m)
  65. if err != nil {
  66. return nil, togRPCError(err)
  67. }
  68. return &pb.MemberUpdateResponse{Header: cs.header(), Members: membersToProtoMembers(membs)}, nil
  69. }
  70. func (cs *ClusterServer) MemberList(ctx context.Context, r *pb.MemberListRequest) (*pb.MemberListResponse, error) {
  71. membs := membersToProtoMembers(cs.cluster.Members())
  72. return &pb.MemberListResponse{Header: cs.header(), Members: membs}, nil
  73. }
  74. func (cs *ClusterServer) header() *pb.ResponseHeader {
  75. return &pb.ResponseHeader{ClusterId: uint64(cs.cluster.ID()), MemberId: uint64(cs.server.ID()), RaftTerm: cs.server.Term()}
  76. }
  77. func membersToProtoMembers(membs []*membership.Member) []*pb.Member {
  78. protoMembs := make([]*pb.Member, len(membs))
  79. for i := range membs {
  80. protoMembs[i] = &pb.Member{
  81. Name: membs[i].Name,
  82. ID: uint64(membs[i].ID),
  83. PeerURLs: membs[i].PeerURLs,
  84. ClientURLs: membs[i].ClientURLs,
  85. }
  86. }
  87. return protoMembs
  88. }