blob: b2ebc98984052e0bd0a9a5029f947b513cb16fb9 [file] [log] [blame]
khenaidooab1f7bd2019-11-14 14:00:27 -05001// 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
15package v3rpc
16
17import (
18 "context"
19 "time"
20
21 "go.etcd.io/etcd/etcdserver"
22 "go.etcd.io/etcd/etcdserver/api"
23 "go.etcd.io/etcd/etcdserver/api/membership"
24 "go.etcd.io/etcd/etcdserver/api/v3rpc/rpctypes"
25 pb "go.etcd.io/etcd/etcdserver/etcdserverpb"
26 "go.etcd.io/etcd/pkg/types"
27)
28
29type ClusterServer struct {
30 cluster api.Cluster
31 server etcdserver.ServerV3
32}
33
34func NewClusterServer(s etcdserver.ServerV3) *ClusterServer {
35 return &ClusterServer{
36 cluster: s.Cluster(),
37 server: s,
38 }
39}
40
41func (cs *ClusterServer) MemberAdd(ctx context.Context, r *pb.MemberAddRequest) (*pb.MemberAddResponse, error) {
42 urls, err := types.NewURLs(r.PeerURLs)
43 if err != nil {
44 return nil, rpctypes.ErrGRPCMemberBadURLs
45 }
46
47 now := time.Now()
48 var m *membership.Member
49 if r.IsLearner {
50 m = membership.NewMemberAsLearner("", urls, "", &now)
51 } else {
52 m = membership.NewMember("", urls, "", &now)
53 }
54 membs, merr := cs.server.AddMember(ctx, *m)
55 if merr != nil {
56 return nil, togRPCError(merr)
57 }
58
59 return &pb.MemberAddResponse{
60 Header: cs.header(),
61 Member: &pb.Member{
62 ID: uint64(m.ID),
63 PeerURLs: m.PeerURLs,
64 IsLearner: m.IsLearner,
65 },
66 Members: membersToProtoMembers(membs),
67 }, nil
68}
69
70func (cs *ClusterServer) MemberRemove(ctx context.Context, r *pb.MemberRemoveRequest) (*pb.MemberRemoveResponse, error) {
71 membs, err := cs.server.RemoveMember(ctx, r.ID)
72 if err != nil {
73 return nil, togRPCError(err)
74 }
75 return &pb.MemberRemoveResponse{Header: cs.header(), Members: membersToProtoMembers(membs)}, nil
76}
77
78func (cs *ClusterServer) MemberUpdate(ctx context.Context, r *pb.MemberUpdateRequest) (*pb.MemberUpdateResponse, error) {
79 m := membership.Member{
80 ID: types.ID(r.ID),
81 RaftAttributes: membership.RaftAttributes{PeerURLs: r.PeerURLs},
82 }
83 membs, err := cs.server.UpdateMember(ctx, m)
84 if err != nil {
85 return nil, togRPCError(err)
86 }
87 return &pb.MemberUpdateResponse{Header: cs.header(), Members: membersToProtoMembers(membs)}, nil
88}
89
90func (cs *ClusterServer) MemberList(ctx context.Context, r *pb.MemberListRequest) (*pb.MemberListResponse, error) {
91 membs := membersToProtoMembers(cs.cluster.Members())
92 return &pb.MemberListResponse{Header: cs.header(), Members: membs}, nil
93}
94
95func (cs *ClusterServer) MemberPromote(ctx context.Context, r *pb.MemberPromoteRequest) (*pb.MemberPromoteResponse, error) {
96 membs, err := cs.server.PromoteMember(ctx, r.ID)
97 if err != nil {
98 return nil, togRPCError(err)
99 }
100 return &pb.MemberPromoteResponse{Header: cs.header(), Members: membersToProtoMembers(membs)}, nil
101}
102
103func (cs *ClusterServer) header() *pb.ResponseHeader {
104 return &pb.ResponseHeader{ClusterId: uint64(cs.cluster.ID()), MemberId: uint64(cs.server.ID()), RaftTerm: cs.server.Term()}
105}
106
107func membersToProtoMembers(membs []*membership.Member) []*pb.Member {
108 protoMembs := make([]*pb.Member, len(membs))
109 for i := range membs {
110 protoMembs[i] = &pb.Member{
111 Name: membs[i].Name,
112 ID: uint64(membs[i].ID),
113 PeerURLs: membs[i].PeerURLs,
114 ClientURLs: membs[i].ClientURLs,
115 IsLearner: membs[i].IsLearner,
116 }
117 }
118 return protoMembs
119}