-
Notifications
You must be signed in to change notification settings - Fork 800
/
handler.go
221 lines (198 loc) · 7 KB
/
handler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
// Copyright (c) 2017 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package cassandra
import (
"fmt"
"github.com/urfave/cli"
"log"
)
// setupSchema executes the setupSchemaTask
// using the given command line arguments
// as input
func setupSchema(cli *cli.Context) error {
config, err := newSetupSchemaConfig(cli)
if err != nil {
return handleErr(newConfigError(err.Error()))
}
if err := handleSetupSchema(config); err != nil {
return handleErr(err)
}
return nil
}
// updateSchema executes the updateSchemaTask
// using the given command lien args as input
func updateSchema(cli *cli.Context) error {
config, err := newUpdateSchemaConfig(cli)
if err != nil {
return handleErr(newConfigError(err.Error()))
}
if err := handleUpdateSchema(config); err != nil {
return handleErr(err)
}
return nil
}
// createKeyspace creates a cassandra keyspace
func createKeyspace(cli *cli.Context) error {
config, err := newCreateKeyspaceConfig(cli)
if err != nil {
return handleErr(err)
}
client, err := newCQLClient(config.CassHosts, config.CassPort, config.CassUser, config.CassPassword, "system")
if err != nil {
return handleErr(fmt.Errorf("error creating cql client:%v", err))
}
err = client.CreateKeyspace(config.CassKeyspace, config.ReplicationFactor)
if err != nil {
return handleErr(fmt.Errorf("error creating keyspace:%v", err))
}
return nil
}
func handleUpdateSchema(config *UpdateSchemaConfig) error {
task, err := NewUpdateSchemaTask(config)
if err != nil {
return fmt.Errorf("error creating task, err=%v", err)
}
if err := task.run(); err != nil {
return fmt.Errorf("error setting up schema, err=%v", err)
}
return nil
}
func handleSetupSchema(config *SetupSchemaConfig) error {
task, err := newSetupSchemaTask(config)
if err != nil {
return fmt.Errorf("error creating task, err=%v", err)
}
if err := task.run(); err != nil {
return fmt.Errorf("error setting up schema, err=%v", err)
}
return nil
}
func validateSetupSchemaConfig(config *SetupSchemaConfig) error {
if len(config.CassHosts) == 0 {
return newConfigError("missing cassandra endpoint argument " + flag(cliOptEndpoint))
}
if config.CassPort == 0 {
config.CassPort = defaultCassandraPort
}
if len(config.CassKeyspace) == 0 {
return newConfigError("missing " + flag(cliOptKeyspace) + " argument ")
}
if len(config.SchemaFilePath) == 0 && config.DisableVersioning {
return newConfigError("missing schemaFilePath " + flag(cliOptSchemaFile))
}
if (config.DisableVersioning && len(config.InitialVersion) > 0) ||
(!config.DisableVersioning && len(config.InitialVersion) == 0) {
return newConfigError("either " + flag(cliOptDisableVersioning) + " or " +
flag(cliOptVersion) + " but not both must be specified")
}
if !config.DisableVersioning {
ver, err := parseValidateVersion(config.InitialVersion)
if err != nil {
return newConfigError("invalid " + flag(cliOptVersion) + " argument:" + err.Error())
}
config.InitialVersion = ver
}
return nil
}
func newSetupSchemaConfig(cli *cli.Context) (*SetupSchemaConfig, error) {
config := new(SetupSchemaConfig)
config.CassHosts = cli.GlobalString(cliOptEndpoint)
config.CassPort = cli.GlobalInt(cliOptPort)
config.CassUser = cli.GlobalString(cliOptUser)
config.CassPassword = cli.GlobalString(cliOptPassword)
config.CassKeyspace = cli.GlobalString(cliOptKeyspace)
config.SchemaFilePath = cli.String(cliOptSchemaFile)
config.InitialVersion = cli.String(cliOptVersion)
config.DisableVersioning = cli.Bool(cliOptDisableVersioning)
config.Overwrite = cli.Bool(cliOptOverwrite)
if err := validateSetupSchemaConfig(config); err != nil {
return nil, err
}
return config, nil
}
func validateUpdateSchemaConfig(config *UpdateSchemaConfig) error {
if len(config.CassHosts) == 0 {
return newConfigError("missing cassandra endpoint argument " + flag(cliOptEndpoint))
}
if config.CassPort == 0 {
config.CassPort = defaultCassandraPort
}
if len(config.CassKeyspace) == 0 {
return newConfigError("missing " + flag(cliOptKeyspace) + " argument ")
}
if len(config.SchemaDir) == 0 {
return newConfigError("missing " + flag(cliOptSchemaDir) + " argument ")
}
if len(config.TargetVersion) > 0 {
ver, err := parseValidateVersion(config.TargetVersion)
if err != nil {
return newConfigError("invalid " + flag(cliOptTargetVersion) + " argument:" + err.Error())
}
config.TargetVersion = ver
}
return nil
}
func newUpdateSchemaConfig(cli *cli.Context) (*UpdateSchemaConfig, error) {
config := new(UpdateSchemaConfig)
config.CassHosts = cli.GlobalString(cliOptEndpoint)
config.CassPort = cli.GlobalInt(cliOptPort)
config.CassUser = cli.GlobalString(cliOptUser)
config.CassPassword = cli.GlobalString(cliOptPassword)
config.CassKeyspace = cli.GlobalString(cliOptKeyspace)
config.SchemaDir = cli.String(cliOptSchemaDir)
config.IsDryRun = cli.Bool(cliOptDryrun)
config.TargetVersion = cli.String(cliOptTargetVersion)
if err := validateUpdateSchemaConfig(config); err != nil {
return nil, err
}
return config, nil
}
func newCreateKeyspaceConfig(cli *cli.Context) (*CreateKeyspaceConfig, error) {
config := new(CreateKeyspaceConfig)
config.CassHosts = cli.GlobalString(cliOptEndpoint)
config.CassPort = cli.GlobalInt(cliOptPort)
config.CassUser = cli.GlobalString(cliOptUser)
config.CassPassword = cli.GlobalString(cliOptPassword)
config.CassKeyspace = cli.String(cliOptKeyspace)
config.ReplicationFactor = cli.Int(cliOptReplicationFactor)
if err := validateCreateKeyspaceConfig(config); err != nil {
return nil, err
}
return config, nil
}
func validateCreateKeyspaceConfig(config *CreateKeyspaceConfig) error {
if len(config.CassHosts) == 0 {
return newConfigError("missing cassandra endpoint argument " + flag(cliOptEndpoint))
}
if config.CassPort == 0 {
config.CassPort = defaultCassandraPort
}
if len(config.CassKeyspace) == 0 {
return newConfigError("missing " + flag(cliOptKeyspace) + " argument ")
}
return nil
}
func flag(opt string) string {
return "(-" + opt + ")"
}
func handleErr(err error) error {
log.Println(err)
return err
}