example_cluster_test.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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_test
  15. import (
  16. "context"
  17. "fmt"
  18. "log"
  19. "go.etcd.io/etcd/clientv3"
  20. )
  21. func ExampleCluster_memberList() {
  22. cli, err := clientv3.New(clientv3.Config{
  23. Endpoints: endpoints,
  24. DialTimeout: dialTimeout,
  25. })
  26. if err != nil {
  27. log.Fatal(err)
  28. }
  29. defer cli.Close()
  30. resp, err := cli.MemberList(context.Background())
  31. if err != nil {
  32. log.Fatal(err)
  33. }
  34. fmt.Println("members:", len(resp.Members))
  35. // Output: members: 3
  36. }
  37. func ExampleCluster_memberAdd() {
  38. cli, err := clientv3.New(clientv3.Config{
  39. Endpoints: endpoints[:2],
  40. DialTimeout: dialTimeout,
  41. })
  42. if err != nil {
  43. log.Fatal(err)
  44. }
  45. defer cli.Close()
  46. peerURLs := endpoints[2:]
  47. mresp, err := cli.MemberAdd(context.Background(), peerURLs)
  48. if err != nil {
  49. log.Fatal(err)
  50. }
  51. fmt.Println("added member.PeerURLs:", mresp.Member.PeerURLs)
  52. // added member.PeerURLs: [http://localhost:32380]
  53. }
  54. func ExampleCluster_memberAddAsLearner() {
  55. cli, err := clientv3.New(clientv3.Config{
  56. Endpoints: endpoints[:2],
  57. DialTimeout: dialTimeout,
  58. })
  59. if err != nil {
  60. log.Fatal(err)
  61. }
  62. defer cli.Close()
  63. peerURLs := endpoints[2:]
  64. mresp, err := cli.MemberAddAsLearner(context.Background(), peerURLs)
  65. if err != nil {
  66. log.Fatal(err)
  67. }
  68. fmt.Println("added member.PeerURLs:", mresp.Member.PeerURLs)
  69. fmt.Println("added member.IsLearner:", mresp.Member.IsLearner)
  70. // added member.PeerURLs: [http://localhost:32380]
  71. // added member.IsLearner: true
  72. }
  73. func ExampleCluster_memberRemove() {
  74. cli, err := clientv3.New(clientv3.Config{
  75. Endpoints: endpoints[1:],
  76. DialTimeout: dialTimeout,
  77. })
  78. if err != nil {
  79. log.Fatal(err)
  80. }
  81. defer cli.Close()
  82. resp, err := cli.MemberList(context.Background())
  83. if err != nil {
  84. log.Fatal(err)
  85. }
  86. _, err = cli.MemberRemove(context.Background(), resp.Members[0].ID)
  87. if err != nil {
  88. log.Fatal(err)
  89. }
  90. }
  91. func ExampleCluster_memberUpdate() {
  92. cli, err := clientv3.New(clientv3.Config{
  93. Endpoints: endpoints,
  94. DialTimeout: dialTimeout,
  95. })
  96. if err != nil {
  97. log.Fatal(err)
  98. }
  99. defer cli.Close()
  100. resp, err := cli.MemberList(context.Background())
  101. if err != nil {
  102. log.Fatal(err)
  103. }
  104. peerURLs := []string{"http://localhost:12380"}
  105. _, err = cli.MemberUpdate(context.Background(), resp.Members[0].ID, peerURLs)
  106. if err != nil {
  107. log.Fatal(err)
  108. }
  109. }