member.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /*
  2. Copyright 2014 CoreOS, Inc.
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package migrate
  14. import (
  15. "crypto/sha1"
  16. "encoding/binary"
  17. "sort"
  18. "github.com/coreos/etcd/pkg/types"
  19. )
  20. type raftAttributes struct {
  21. PeerURLs []string `json:"peerURLs"`
  22. }
  23. type attributes struct {
  24. Name string `json:"name,omitempty"`
  25. ClientURLs []string `json:"clientURLs,omitempty"`
  26. }
  27. type member struct {
  28. ID types.ID `json:"id"`
  29. raftAttributes
  30. attributes
  31. }
  32. func NewMember(name string, peerURLs types.URLs, clusterName string) *member {
  33. m := &member{
  34. raftAttributes: raftAttributes{PeerURLs: peerURLs.StringSlice()},
  35. attributes: attributes{Name: name},
  36. }
  37. var b []byte
  38. sort.Strings(m.PeerURLs)
  39. for _, p := range m.PeerURLs {
  40. b = append(b, []byte(p)...)
  41. }
  42. b = append(b, []byte(clusterName)...)
  43. hash := sha1.Sum(b)
  44. m.ID = types.ID(binary.BigEndian.Uint64(hash[:8]))
  45. return m
  46. }