member.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. // Copyright 2015 CoreOS, Inc.
  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 migrate
  15. import (
  16. "crypto/sha1"
  17. "encoding/binary"
  18. "sort"
  19. "github.com/coreos/etcd/pkg/types"
  20. )
  21. type raftAttributes struct {
  22. PeerURLs []string `json:"peerURLs"`
  23. }
  24. type attributes struct {
  25. Name string `json:"name,omitempty"`
  26. ClientURLs []string `json:"clientURLs,omitempty"`
  27. }
  28. type member struct {
  29. ID types.ID `json:"id"`
  30. raftAttributes
  31. attributes
  32. }
  33. func NewMember(name string, peerURLs types.URLs, clusterName string) *member {
  34. m := &member{
  35. raftAttributes: raftAttributes{PeerURLs: peerURLs.StringSlice()},
  36. attributes: attributes{Name: name},
  37. }
  38. var b []byte
  39. sort.Strings(m.PeerURLs)
  40. for _, p := range m.PeerURLs {
  41. b = append(b, []byte(p)...)
  42. }
  43. b = append(b, []byte(clusterName)...)
  44. hash := sha1.Sum(b)
  45. m.ID = types.ID(binary.BigEndian.Uint64(hash[:8]))
  46. return m
  47. }