example_cluster_test.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. "fmt"
  17. "log"
  18. "github.com/coreos/etcd/clientv3"
  19. "golang.org/x/net/context"
  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_memberRemove() {
  55. cli, err := clientv3.New(clientv3.Config{
  56. Endpoints: endpoints[1:],
  57. DialTimeout: dialTimeout,
  58. })
  59. if err != nil {
  60. log.Fatal(err)
  61. }
  62. defer cli.Close()
  63. resp, err := cli.MemberList(context.Background())
  64. if err != nil {
  65. log.Fatal(err)
  66. }
  67. _, err = cli.MemberRemove(context.Background(), resp.Members[0].ID)
  68. if err != nil {
  69. log.Fatal(err)
  70. }
  71. }
  72. func ExampleCluster_memberUpdate() {
  73. cli, err := clientv3.New(clientv3.Config{
  74. Endpoints: endpoints,
  75. DialTimeout: dialTimeout,
  76. })
  77. if err != nil {
  78. log.Fatal(err)
  79. }
  80. defer cli.Close()
  81. resp, err := cli.MemberList(context.Background())
  82. if err != nil {
  83. log.Fatal(err)
  84. }
  85. peerURLs := []string{"http://localhost:12380"}
  86. _, err = cli.MemberUpdate(context.Background(), resp.Members[0].ID, peerURLs)
  87. if err != nil {
  88. log.Fatal(err)
  89. }
  90. }