Address review feedback.

Switch ConnectTimeout to framework.TypeDurationSecond  with a default of 5. Remove own parsing code.
This commit is contained in:
Mark Paluch 2016-07-01 22:24:48 +02:00
parent 3859f7938a
commit ab63c938c4
4 changed files with 10 additions and 24 deletions

View file

@ -55,7 +55,7 @@ type sessionConfig struct {
PrivateKey string `json:"private_key" structs:"private_key"` PrivateKey string `json:"private_key" structs:"private_key"`
IssuingCA string `json:"issuing_ca" structs:"issuing_ca"` IssuingCA string `json:"issuing_ca" structs:"issuing_ca"`
ProtocolVersion int `json:"protocol_version" structs:"protocol_version"` ProtocolVersion int `json:"protocol_version" structs:"protocol_version"`
ConnectTimeout string `json:"connect_timeout" structs:"connect_timeout"` ConnectTimeout int `json:"connect_timeout" structs:"connect_timeout"`
} }
// DB returns the database connection. // DB returns the database connection.
@ -82,7 +82,7 @@ func (b *backend) DB(s logical.Storage) (*gocql.Session, error) {
return nil, err return nil, err
} }
return createSession(config, s, b.Logger()) return createSession(config, s)
} }
// ResetDB forces a connection next time DB() is called. // ResetDB forces a connection next time DB() is called.

View file

@ -62,8 +62,9 @@ take precedence.`,
}, },
"connect_timeout": &framework.FieldSchema{ "connect_timeout": &framework.FieldSchema{
Type: framework.TypeString, Type: framework.TypeDurationSecond,
Description: `The connection timeout to use. Defaults to 5s.`, Default: 5,
Description: `The connection timeout to use. Defaults to 5.`,
}, },
}, },
@ -124,7 +125,7 @@ func (b *backend) pathConnectionWrite(
TLS: data.Get("tls").(bool), TLS: data.Get("tls").(bool),
InsecureTLS: data.Get("insecure_tls").(bool), InsecureTLS: data.Get("insecure_tls").(bool),
ProtocolVersion: data.Get("protocol_version").(int), ProtocolVersion: data.Get("protocol_version").(int),
ConnectTimeout: data.Get("connect_timeout").(string), ConnectTimeout: data.Get("connect_timeout").(int),
} }
if config.InsecureTLS { if config.InsecureTLS {
@ -168,7 +169,7 @@ func (b *backend) pathConnectionWrite(
config.TLS = true config.TLS = true
} }
session, err := createSession(config, req.Storage, b.Logger()) session, err := createSession(config, req.Storage)
if err != nil { if err != nil {
return logical.ErrorResponse(err.Error()), nil return logical.ErrorResponse(err.Error()), nil
} }

View file

@ -3,7 +3,6 @@ package cassandra
import ( import (
"crypto/tls" "crypto/tls"
"fmt" "fmt"
"log"
"strings" "strings"
"time" "time"
@ -34,7 +33,7 @@ func substQuery(tpl string, data map[string]string) string {
return tpl return tpl
} }
func createSession(cfg *sessionConfig, s logical.Storage, logger *log.Logger) (*gocql.Session, error) { func createSession(cfg *sessionConfig, s logical.Storage) (*gocql.Session, error) {
clusterConfig := gocql.NewCluster(strings.Split(cfg.Hosts, ",")...) clusterConfig := gocql.NewCluster(strings.Split(cfg.Hosts, ",")...)
clusterConfig.Authenticator = gocql.PasswordAuthenticator{ clusterConfig.Authenticator = gocql.PasswordAuthenticator{
Username: cfg.Username, Username: cfg.Username,
@ -46,21 +45,7 @@ func createSession(cfg *sessionConfig, s logical.Storage, logger *log.Logger) (*
clusterConfig.ProtoVersion = 2 clusterConfig.ProtoVersion = 2
} }
if len(cfg.ConnectTimeout) != 0 { clusterConfig.Timeout = time.Duration(cfg.ConnectTimeout) * time.Second
d, err := time.ParseDuration(cfg.ConnectTimeout)
if err != nil {
return nil, err
}
if d < 1 {
return nil, fmt.Errorf("Cassandra connect_timeout must be greater than 0")
}
clusterConfig.Timeout = d
logger.Printf("[DEBUG]: cassandra: config connect_timeout set to %v", d)
} else {
clusterConfig.Timeout = 5 * time.Second
}
if cfg.TLS { if cfg.TLS {
tlsConfig := &tls.Config{ tlsConfig := &tls.Config{

View file

@ -185,7 +185,7 @@ subpath for interactive help output.
<li> <li>
<span class="param">connect_timeout</span> <span class="param">connect_timeout</span>
<span class="param-flags">optional</span> <span class="param-flags">optional</span>
The connection timeout to use. Defaults to 5s. The connection timeout to use. Defaults to 5 seconds.
</li> </li>
</ul> </ul>
</dd> </dd>