go.etcd.io/etcd@v3.3.27+incompatible/clientv3/example_test.go (about)

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