Skip to content
This repository has been archived by the owner on Feb 18, 2021. It is now read-only.

Commit

Permalink
Add authentication and port to cassandra tool (#278)
Browse files Browse the repository at this point in the history
  • Loading branch information
kobeyang authored Aug 17, 2017
1 parent 39ddbc0 commit 0e6a2b9
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 5 deletions.
3 changes: 3 additions & 0 deletions tools/cassandra/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ type (
// for the schema update task
SchemaUpdaterConfig struct {
Keyspace string
Username string
Password string
HostsCsv string
Port int
IsDryRun bool
SkipVersionCheck bool
ManifestFilePath string
Expand Down
17 changes: 13 additions & 4 deletions tools/cassandra/cqlclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,16 +66,25 @@ const (
)

// newCQLClient returns a new instance of CQLClient
func newCQLClient(hostsCsv string, keyspace string, protoVersion int) (CQLClient, error) {
hosts := parseHosts(hostsCsv)
func newCQLClient(config *SchemaUpdaterConfig) (CQLClient, error) {
hosts := parseHosts(config.HostsCsv)
if len(hosts) == 0 {
return nil, errNoHosts
}
clusterCfg := gocql.NewCluster(hosts...)
clusterCfg.Keyspace = keyspace
clusterCfg.Port = config.Port
clusterCfg.Keyspace = config.Keyspace
clusterCfg.Timeout = defaultTimeout
clusterCfg.ProtoVersion = protoVersion
clusterCfg.ProtoVersion = config.ProtoVersion
clusterCfg.Consistency = gocql.ParseConsistency(defaultConsistency)

if config.Username != "" && config.Password != "" {
clusterCfg.Authenticator = gocql.PasswordAuthenticator{
Username: config.Username,
Password: config.Password,
}
}

cqlClient := new(cqlClient)
cqlClient.clusterConfig = clusterCfg
var err error
Expand Down
9 changes: 9 additions & 0 deletions tools/cassandra/updateschema.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ func parseSchemaUpadaterConfig(args []string) *SchemaUpdaterConfig {
var config SchemaUpdaterConfig
cmd := flag.NewFlagSet("update-schema", flag.ExitOnError)
cmd.StringVar(&config.HostsCsv, "h", "", "Path to json file containing cassandra hosts")
cmd.IntVar(&config.Port, "port", 9042, "Port to cassandra hosts")
cmd.StringVar(&config.Username, "u", "", "Cassandra username")
cmd.StringVar(&config.Password, "pw", "", "Cassandra password")
cmd.StringVar(&config.Keyspace, "k", "", "Cassandra keyspace")
cmd.BoolVar(&config.IsDryRun, "d", true, "Dry run")
cmd.BoolVar(&config.SkipVersionCheck, "v", false, "Skip previous version check")
Expand Down Expand Up @@ -77,6 +80,12 @@ func printHelp() {
helpMessage := `updateSchema -h [cassandra_host1,host2..] -k [keyspace] -m [/tmp/manifest.json] [-d false] [-v false]
-h
List of cassandra hosts to connect to
-port
Port to cassandra cluster, 9042 by default
-u
Cassandra username
-pw
Cassandra password
-k
Cassandra keyspace
-m
Expand Down
2 changes: 1 addition & 1 deletion tools/cassandra/updatetask.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ var (

// NewSchemaUpdateTask creates and returns a new SchemaUpdateTask
func NewSchemaUpdateTask(config *SchemaUpdaterConfig) (*SchemaUpdateTask, error) {
cqlClient, err := newCQLClient(config.HostsCsv, config.Keyspace, config.ProtoVersion)
cqlClient, err := newCQLClient(config)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 0e6a2b9

Please sign in to comment.