forked from infobloxopen/docker-infoblox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
135 lines (110 loc) · 4.95 KB
/
config.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
package main
import (
"flag"
"github.com/BurntSushi/toml"
"os"
)
type PluginConfig struct {
PluginDir string `toml:"plugin_dir"`
DriverName string `toml:"driver_name"`
}
type GridConfig struct {
GridHost string `toml:"grid_host"`
WapiVer string `toml:"wapi_version"`
WapiPort string `toml:"wapi_port"`
WapiUsername string `toml:"wapi_username"`
WapiPassword string `toml:"wapi_password"`
SslVerify string `toml:"ssl_verify"`
HttpRequestTimeout uint `toml:"http_request_timeout"`
HttpPoolConnections uint `toml:"http_pool_connections"`
}
type IpamConfig struct {
GlobalNetview string `toml:"global_view"`
GlobalNetworkContainer string `toml:"global_container"`
GlobalPrefixLength uint `toml:"global_prefix"`
LocalNetview string `toml:"local_view"`
LocalNetworkContainer string `toml:"local_container"`
LocalPrefixLength uint `toml:"local_prefix"`
}
type Config struct {
ConfigFile string `toml:`
PluginConfig `toml:"plugin_config"`
GridConfig `toml:"grid_config"`
IpamConfig `toml:"ipam_config"`
}
func NewConfig() *Config {
return &Config{
ConfigFile: "",
PluginConfig: PluginConfig{
PluginDir: "/run/docker/plugins",
DriverName: "infoblox",
},
GridConfig: GridConfig{
GridHost: "192.168.124.200",
WapiVer: "2.0",
WapiPort: "443",
WapiUsername: "",
WapiPassword: "",
SslVerify: "false",
HttpRequestTimeout: 60,
HttpPoolConnections: 10,
},
IpamConfig: IpamConfig{
GlobalNetview: "default",
GlobalNetworkContainer: "global-network-container",
GlobalPrefixLength: 24,
LocalNetview: "default",
LocalNetworkContainer: "192.168.0.0/16",
LocalPrefixLength: 24,
},
}
}
func LoadFromCommandLine(config *Config) (*Config, error) {
if config == nil {
config = NewConfig()
}
flagSet := flag.NewFlagSet(os.Args[0], flag.ExitOnError)
flagSet.StringVar(&config.ConfigFile, "conf-file", "", "File path of configuration file")
flagSet.StringVar(&config.PluginDir, "plugin-dir", config.PluginDir, "Docker plugin directory where driver socket is created")
flagSet.StringVar(&config.DriverName, "driver-name", config.DriverName, "Name of Infoblox IPAM driver")
flagSet.StringVar(&config.GridHost, "grid-host", config.GridHost, "IP of Infoblox Grid Host")
flagSet.StringVar(&config.WapiVer, "wapi-version", config.WapiVer, "Infoblox WAPI Version.")
flagSet.StringVar(&config.WapiPort, "wapi-port", config.WapiPort, "Infoblox WAPI Port.")
flagSet.StringVar(&config.WapiUsername, "wapi-username", config.WapiUsername, "Infoblox WAPI Username")
flagSet.StringVar(&config.WapiPassword, "wapi-password", config.WapiPassword, "Infoblox WAPI Password")
flagSet.StringVar(&config.SslVerify, "ssl-verify", config.SslVerify, "Specifies whether (true/false) to verify server certificate. If a file path is specified, it is assumed to be a certificate file and will be used to verify server certificate.")
flagSet.UintVar(&config.HttpRequestTimeout, "http-request-timeout", config.HttpRequestTimeout, "Infoblox WAPI request timeout in seconds.")
flagSet.UintVar(&config.HttpPoolConnections, "http-pool-connections", config.HttpPoolConnections, "Infoblox WAPI connection pool size.")
flagSet.StringVar(&config.GlobalNetview, "global-view", config.GlobalNetview, "Infoblox Network View for Global Address Space")
flagSet.StringVar(&config.GlobalNetworkContainer, "global-network-container", config.GlobalNetworkContainer, "Subnets will be allocated from this container when --subnet is not specified during network creation")
flagSet.UintVar(&config.GlobalPrefixLength, "global-prefix-length", config.GlobalPrefixLength, "The default CIDR prefix length when allocating a global subnet.")
flagSet.StringVar(&config.LocalNetview, "local-view", config.LocalNetview, "Infoblox Network View for Local Address Space")
flagSet.StringVar(&config.LocalNetworkContainer, "local-network-container", config.LocalNetworkContainer, "Subnets will be allocated from this container when --subnet is not specified during network creation")
flagSet.UintVar(&config.LocalPrefixLength, "local-prefix-length", config.LocalPrefixLength, "The default CIDR prefix length when allocating a local subnet.")
flagSet.Parse(os.Args[1:])
return config, nil
}
func LoadFromConfFile(config *Config) (*Config, error) {
// Just look for --conf-file flag
tmpConfig, err := LoadFromCommandLine(NewConfig())
if tmpConfig == nil || err != nil {
return tmpConfig, err
}
// Now load config file
if tmpConfig.ConfigFile != "" {
if _, err = toml.DecodeFile(tmpConfig.ConfigFile, config); err != nil {
return nil, err
}
}
return config, err
}
func LoadConfig() (config *Config, err error) {
config = NewConfig()
if config, err = LoadFromConfFile(config); config == nil || err != nil {
return
}
if config, err = LoadFromCommandLine(config); config == nil || err != nil {
return
}
return
}