forked from funktionio/funktion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
edit.go
288 lines (260 loc) · 7.1 KB
/
edit.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
// Copyright 2016 Red Hat, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package cmd
import (
"bufio"
"bytes"
"fmt"
"strings"
"github.com/magiconair/properties"
"github.com/muter3000/funktion/pkg/funktion"
"github.com/muter3000/funktion/pkg/spec"
"github.com/spf13/cobra"
"strconv"
"k8s.io/client-go/1.5/kubernetes"
)
type editConnectorCmd struct {
kubeclient *kubernetes.Clientset
cmd *cobra.Command
kubeConfigPath string
namespace string
name string
listProperties bool
configMaps map[string]*v1.ConfigMap
schema *spec.ConnectorSchema
applicationProperties *properties.Properties
setProperties map[string]string
}
func init() {
RootCmd.AddCommand(newEditCmd())
}
func newEditCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "edit KIND [NAME] [flags]",
Short: "edits a resources",
Long: `This command will create edit a resource`,
}
cmd.AddCommand(newEditConnectorCmd())
return cmd
}
func newEditConnectorCmd() *cobra.Command {
p := &editConnectorCmd{}
cmd := &cobra.Command{
Use: "connector NAME [flags] [prop1=value1] [prop2=value2]",
Short: "edits a connectors configuration",
Long: `This command will edit a connector resource`,
Run: func(cmd *cobra.Command, args []string) {
p.cmd = cmd
if len(args) == 0 {
fmt.Printf("You must specify the name of the connector as an argument!")
return
}
p.name = args[0]
if len(args) > 1 {
sp, err := parseProperties(args[1:])
if err != nil {
handleError(err)
return
}
p.setProperties = sp
}
err := createKubernetesClient(cmd, p.kubeConfigPath, &p.kubeclient, &p.namespace)
if err != nil {
handleError(err)
return
}
handleError(p.run())
},
}
f := cmd.Flags()
f.StringVar(&p.kubeConfigPath, "kubeconfig", "", "the directory to look for the kubernetes configuration")
f.StringVar(&p.namespace, "namespace", "", "the namespace to query")
f.BoolVarP(&p.listProperties, "list", "l", false, "list the properties to edit")
return cmd
}
func parseProperties(args []string) (map[string]string, error) {
m := map[string]string{}
for _, arg := range args {
values := strings.SplitN(arg, "=", 2)
if len(values) != 2 {
return nil, fmt.Errorf("Argument does not contain `=` but was `%s`", arg)
}
m[values[0]] = values[1]
}
return m, nil
}
func (p *editConnectorCmd) run() error {
listOpts, err := funktion.CreateConnectorListOptions()
if err != nil {
return err
}
kubeclient := p.kubeclient
cms := kubeclient.ConfigMaps(p.namespace)
resources, err := cms.List(*listOpts)
if err != nil {
return err
}
var connector *v1.ConfigMap
for _, resource := range resources.Items {
if resource.Name == p.name {
connector = &resource
break
}
}
name := p.name
if connector == nil {
return fmt.Errorf("No Connector called `%s` exists in namespace %s", name, p.namespace)
}
err = p.loadConnectorSchema(name, connector)
if err != nil {
return err
}
if p.listProperties {
err = p.listConnectorProperties(name, connector)
} else {
err = p.editConnector(name, connector)
}
if err != nil {
return err
}
return err
}
func (p *editConnectorCmd) loadConnectorSchema(name string, connector *v1.ConfigMap) error {
// lets load the connector model
if connector.Data == nil {
return fmt.Errorf("No data in the Connector %s", name)
}
schemaYaml := connector.Data[funktion.SchemaYmlProperty]
if len(schemaYaml) == 0 {
return fmt.Errorf("No YAML for data key %s in Connector %s", funktion.SchemaYmlProperty, name)
}
schema, err := funktion.LoadConnectorSchema([]byte(schemaYaml))
if err != nil {
return err
}
p.schema = schema
propertiesText := connector.Data[funktion.ApplicationPropertiesProperty]
if len(propertiesText) > 0 {
props, err := properties.LoadString(propertiesText)
if err != nil {
return err
}
p.applicationProperties = props
} else {
p.applicationProperties = properties.NewProperties()
}
return nil
}
func (p *editConnectorCmd) listConnectorProperties(name string, connector *v1.ConfigMap) error {
compProps := p.schema.ComponentProperties
if len(compProps) == 0 {
fmt.Printf("The connector `%s` has no properties to configure!", name)
return nil
}
maxLen := 1
for k, _ := range compProps {
l := len(k)
if l > maxLen {
maxLen = l
}
}
colText := strconv.Itoa(maxLen)
fmt.Printf(" %-"+colText+"s VALUE\n", "NAME")
for k, cp := range compProps {
propertyKey := "camel.component." + name + "." + funktion.ToSpringBootPropertyName(k)
value := p.applicationProperties.GetString(propertyKey, "")
prompt := "?"
if cp.Required {
prompt = "*"
}
fmt.Printf("%s %-"+colText+"s %s\n", prompt, k, value)
}
return nil
}
func (p *editConnectorCmd) editConnector(name string, connector *v1.ConfigMap) error {
compProps := p.schema.ComponentProperties
if len(compProps) == 0 {
fmt.Printf("The connector `%s` has no properties to configure!", name)
return nil
}
updated := false
if len(p.setProperties) > 0 {
for k, v := range p.setProperties {
propertyKey := springPropertiesKey(name, k)
p.applicationProperties.Set(propertyKey, v)
}
updated = true
} else {
for k, cp := range compProps {
label := funktion.HumanizeString(k)
propertyKey := springPropertiesKey(name, k)
value := p.applicationProperties.GetString(propertyKey, "")
valueText := ""
boolType := cp.Type == "boolean"
if len(value) > 0 {
if boolType {
if value == "true" {
valueText = "[Y/n]"
} else {
valueText = "[y/N]"
}
} else {
valueText = "[" + value + "]"
}
}
prompt := "?"
if cp.Required {
prompt = "*"
}
fmt.Printf("%s %s%s: ", prompt, label, valueText)
var input string
fmt.Scanln(&input)
if len(input) > 0 {
// convert boolean to true/false values
if boolType {
lower := strings.TrimSpace(strings.ToLower(input))
if lower[0] == 't' {
input = "true"
} else {
input = "false"
}
}
p.applicationProperties.Set(propertyKey, input)
updated = true
}
}
}
if updated {
var b bytes.Buffer
w := bufio.NewWriter(&b)
p.applicationProperties.Write(w, properties.UTF8)
w.Flush()
propText := b.String()
cms := p.kubeclient.ConfigMaps(p.namespace)
latestCon, err := cms.Get(name)
if err != nil {
return err
}
latestCon.Data[funktion.ApplicationPropertiesProperty] = propText
_, err = cms.Update(latestCon)
if err == nil {
fmt.Printf("Connector %s updated\n", name)
}
return err
}
return nil
}
func springPropertiesKey(name string, k string) string {
return "camel.component." + name + "." + funktion.ToSpringBootPropertyName(k)
}