go.ligato.io/vpp-agent/v3@v3.5.0/cmd/agentctl/client/options.go (about) 1 package client 2 3 import ( 4 "crypto/tls" 5 "encoding/base64" 6 "net/http" 7 "time" 8 9 "google.golang.org/grpc" 10 11 "go.ligato.io/vpp-agent/v3/cmd/agentctl/client/tlsconfig" 12 ) 13 14 type Opt func(*Client) error 15 16 // WithHost overrides the client host with the specified one. 17 func WithHost(host string) Opt { 18 return func(c *Client) error { 19 hostURL, err := ParseHostURL(host) 20 if err != nil { 21 return err 22 } 23 c.host = host 24 c.proto = hostURL.Scheme 25 c.addr = hostURL.Host 26 c.basePath = hostURL.Path 27 return nil 28 } 29 } 30 31 // WithHTTPPort overrides port for HTTP connection. 32 func WithHTTPPort(p int) Opt { 33 return func(c *Client) error { 34 c.httpPort = p 35 return nil 36 } 37 } 38 39 // WithGrpcPort overrides port for GRPC connection. 40 func WithGrpcPort(p int) Opt { 41 return func(c *Client) error { 42 c.grpcPort = p 43 return nil 44 } 45 } 46 47 // WithEtcdEndpoints overrides endpoints for KVDB (etcd) connection. 48 func WithEtcdEndpoints(endpoints []string) Opt { 49 return func(c *Client) error { 50 if len(endpoints) != 0 { 51 c.kvdbEndpoints = endpoints 52 } 53 return nil 54 } 55 } 56 57 // WithEtcdDialTimeout overrides dial timeout for KVDB (etcd) connection. 58 func WithEtcdDialTimeout(t time.Duration) Opt { 59 return func(c *Client) error { 60 if t != 0 { 61 c.kvdbDialTimeout = t 62 } 63 return nil 64 } 65 } 66 67 func withTLS(cert, key, ca string, skipVerify bool) (*tls.Config, error) { 68 var options []tlsconfig.Option 69 if cert != "" && key != "" { 70 options = append(options, tlsconfig.CertKey(cert, key)) 71 } 72 if ca != "" { 73 options = append(options, tlsconfig.CA(ca)) 74 } 75 if skipVerify { 76 options = append(options, tlsconfig.SkipServerVerification()) 77 } 78 return tlsconfig.New(options...) 79 } 80 81 // WithGrpcTLS adds tls.Config for gRPC to Client. 82 func WithGrpcTLS(cert, key, ca string, skipVerify bool) Opt { 83 return func(c *Client) (err error) { 84 c.grpcTLS, err = withTLS(cert, key, ca, skipVerify) 85 return err 86 } 87 } 88 89 // WithHTTPTLS adds tls.Config for HTTP to Client. 90 func WithHTTPTLS(cert, key, ca string, skipVerify bool) Opt { 91 return func(c *Client) (err error) { 92 c.httpTLS, err = withTLS(cert, key, ca, skipVerify) 93 c.scheme = "https" 94 return err 95 } 96 } 97 98 // WithKvdbTLS adds tls.Config for KVDB to Client. 99 func WithKvdbTLS(cert, key, ca string, skipVerify bool) Opt { 100 return func(c *Client) (err error) { 101 c.kvdbTLS, err = withTLS(cert, key, ca, skipVerify) 102 return err 103 } 104 } 105 106 func WithServiceLabel(label string) Opt { 107 return func(c *Client) error { 108 if label != "" { 109 c.serviceLabel = label 110 } 111 return nil 112 } 113 } 114 115 // WithHTTPClient overrides the http client with the specified one 116 func WithHTTPClient(client *http.Client) Opt { 117 return func(c *Client) error { 118 if client != nil { 119 c.httpClient = client 120 } 121 return nil 122 } 123 } 124 125 // WithGRPCClient overrides the grpc client with the specified one 126 func WithGRPCClient(client *grpc.ClientConn) Opt { 127 return func(c *Client) error { 128 if client != nil { 129 c.grpcClient = client 130 } 131 return nil 132 } 133 } 134 135 // WithTimeout configures the time limit for requests made by the HTTP client 136 func WithTimeout(timeout time.Duration) Opt { 137 return func(c *Client) error { 138 c.HTTPClient().Timeout = timeout 139 return nil 140 } 141 } 142 143 // WithUserAgent sets User-Agent in the HTTP headers. 144 func WithUserAgent(a string) Opt { 145 return WithHTTPHeader("User-Agent", a) 146 } 147 148 // WithHTTPHeader adds header to HTTP headers. 149 func WithHTTPHeader(k, v string) Opt { 150 return func(c *Client) error { 151 if c.customHTTPHeaders == nil { 152 c.customHTTPHeaders = make(map[string]string) 153 } 154 c.customHTTPHeaders[k] = v 155 return nil 156 } 157 } 158 159 // WithHTTPBasicAuth adds basic auth header to HTTP headers. 160 func WithHTTPBasicAuth(s string) Opt { 161 // For empty string return empty option. 162 if s == "" { 163 return func(c *Client) error { return nil } 164 } 165 auth := base64.StdEncoding.EncodeToString([]byte(s)) 166 return WithHTTPHeader("Authorization", "Basic "+auth) 167 } 168 169 // WithVersion overrides the client version with the specified one. If an empty 170 // version is specified, the value will be ignored to allow version negotiation. 171 func WithVersion(version string) Opt { 172 return func(c *Client) error { 173 if version != "" { 174 c.version = version 175 c.manualOverride = true 176 } 177 return nil 178 } 179 } 180 181 // WithAPIVersionNegotiation enables automatic API version negotiation for the client. 182 // With this option enabled, the client automatically negotiates the API version 183 // to use when making requests. API version negotiation is performed on the first 184 // request; subsequent requests will not re-negotiate. 185 func WithAPIVersionNegotiation() Opt { 186 return func(c *Client) error { 187 c.negotiateVersion = true 188 return nil 189 } 190 }