-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
124 lines (109 loc) · 2.91 KB
/
utils.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
package nodedriver
import (
"fmt"
"net/rpc"
"reflect"
"strconv"
"strings"
"github.com/docker/machine/libmachine/drivers/plugin/localbinary"
"github.com/docker/machine/libmachine/drivers/rpc"
cli "github.com/docker/machine/libmachine/mcnflag"
"github.com/rancher/types/apis/management.cattle.io/v3"
"github.com/sirupsen/logrus"
)
var (
// secretFields lists all the hard-coded fields that should hidden as password
secretFields = map[string]struct{}{
// ec2
"secretKey": {},
// digitalOcean
"accessToken": {},
// azure
"clientSecret": {},
// packet, rackspace, softlayer
"apiKey": {},
// vSphere, openstack, vmwarevcloudair
"password": {},
// exoscale
"apiSecretKey": {},
// otc
"accessKeySecret": {},
}
)
func flagToField(flag cli.Flag) (string, v3.Field, error) {
field := v3.Field{
Create: true,
Update: true,
Type: "string",
}
name, err := toLowerCamelCase(flag.String())
if err != nil {
return name, field, err
}
switch v := flag.(type) {
case *cli.StringFlag:
if isPassword(name) {
field.Type = "password"
}
field.Description = v.Usage
field.Default.StringValue = v.Value
case *cli.IntFlag:
field.Description = v.Usage
field.Default.StringValue = strconv.Itoa(v.Value)
case *cli.BoolFlag:
field.Type = "boolean"
field.Description = v.Usage
case *cli.StringSliceFlag:
field.Type = "array[string]"
field.Description = v.Usage
field.Default.StringSliceValue = v.Value
default:
return name, field, fmt.Errorf("unknown type of flag %v: %v", flag, reflect.TypeOf(flag))
}
return name, field, nil
}
func isPassword(key string) bool {
_, ok := secretFields[key]
return ok
}
func toLowerCamelCase(nodeFlagName string) (string, error) {
parts := strings.SplitN(nodeFlagName, "-", 2)
if len(parts) != 2 {
return "", fmt.Errorf("parameter %s does not follow expected naming convention [DRIVER]-[FLAG-NAME]", nodeFlagName)
}
flagNameParts := strings.Split(parts[1], "-")
flagName := flagNameParts[0]
for _, flagNamePart := range flagNameParts[1:] {
flagName = flagName + strings.ToUpper(flagNamePart[:1]) + flagNamePart[1:]
}
return flagName, nil
}
func getCreateFlagsForDriver(driver string) ([]cli.Flag, error) {
logrus.Debug("Starting binary ", driver)
p, err := localbinary.NewPlugin(driver)
if err != nil {
return nil, err
}
go func() {
err := p.Serve()
if err != nil {
logrus.Debugf("Error serving plugin server for driver=%s, err=%v", driver, err)
}
}()
defer p.Close()
addr, err := p.Address()
if err != nil {
return nil, err
}
rpcclient, err := rpc.DialHTTP("tcp", addr)
if err != nil {
return nil, fmt.Errorf("Error dialing to plugin server's address(%v), err=%v", addr, err)
}
defer rpcclient.Close()
c := rpcdriver.NewInternalClient(rpcclient)
var flags []cli.Flag
if err := c.Call(".GetCreateFlags", struct{}{}, &flags); err != nil {
return nil, fmt.Errorf("Error getting flags err=%v", err)
}
return flags, nil
}