example_test.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. "log"
  17. "time"
  18. "github.com/coreos/etcd/clientv3"
  19. "github.com/coreos/etcd/pkg/transport"
  20. "github.com/coreos/pkg/capnslog"
  21. "golang.org/x/net/context"
  22. )
  23. var (
  24. dialTimeout = 5 * time.Second
  25. requestTimeout = 1 * time.Second
  26. endpoints = []string{"localhost:2379", "localhost:22379", "localhost:32379"}
  27. )
  28. func Example() {
  29. var plog = capnslog.NewPackageLogger("github.com/coreos/etcd", "clientv3")
  30. clientv3.SetLogger(plog)
  31. cli, err := clientv3.New(clientv3.Config{
  32. Endpoints: endpoints,
  33. DialTimeout: dialTimeout,
  34. })
  35. if err != nil {
  36. log.Fatal(err)
  37. }
  38. defer cli.Close() // make sure to close the client
  39. _, err = cli.Put(context.TODO(), "foo", "bar")
  40. if err != nil {
  41. log.Fatal(err)
  42. }
  43. }
  44. func ExampleConfig_withTLS() {
  45. tlsInfo := transport.TLSInfo{
  46. CertFile: "/tmp/test-certs/test-name-1.pem",
  47. KeyFile: "/tmp/test-certs/test-name-1-key.pem",
  48. TrustedCAFile: "/tmp/test-certs/trusted-ca.pem",
  49. }
  50. tlsConfig, err := tlsInfo.ClientConfig()
  51. if err != nil {
  52. log.Fatal(err)
  53. }
  54. cli, err := clientv3.New(clientv3.Config{
  55. Endpoints: endpoints,
  56. DialTimeout: dialTimeout,
  57. TLS: tlsConfig,
  58. })
  59. if err != nil {
  60. log.Fatal(err)
  61. }
  62. defer cli.Close() // make sure to close the client
  63. _, err = cli.Put(context.TODO(), "foo", "bar")
  64. if err != nil {
  65. log.Fatal(err)
  66. }
  67. }