Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Portions copyright 2019-present Open Networking Foundation |
| 3 | * Original copyright 2019-present Ciena Corporation |
| 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | package commands |
| 18 | |
| 19 | import ( |
| 20 | "encoding/json" |
| 21 | "fmt" |
| 22 | "github.com/opencord/cordctl/format" |
Scott Baker | a00418a | 2019-06-03 16:15:28 -0700 | [diff] [blame] | 23 | "github.com/opencord/cordctl/order" |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 24 | "google.golang.org/grpc" |
| 25 | "gopkg.in/yaml.v2" |
Scott Baker | 14c8f18 | 2019-05-22 18:05:29 -0700 | [diff] [blame] | 26 | "io" |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 27 | "io/ioutil" |
| 28 | "log" |
| 29 | "os" |
| 30 | "strings" |
| 31 | "time" |
A R Karthick | efb1eef | 2019-07-16 12:43:24 -0700 | [diff] [blame^] | 32 | "net" |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 33 | ) |
| 34 | |
| 35 | type OutputType uint8 |
| 36 | |
| 37 | const ( |
| 38 | OUTPUT_TABLE OutputType = iota |
| 39 | OUTPUT_JSON |
| 40 | OUTPUT_YAML |
Scott Baker | 867aa30 | 2019-06-19 13:18:45 -0700 | [diff] [blame] | 41 | |
| 42 | CORE_VERSION_CONSTRAINT = ">= 3, < 4" // Support XOS major version 3 |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 43 | ) |
| 44 | |
Scott Baker | 14c8f18 | 2019-05-22 18:05:29 -0700 | [diff] [blame] | 45 | // Make it easy to override output stream for testing |
| 46 | var OutputStream io.Writer = os.Stdout |
| 47 | |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 48 | var CharReplacer = strings.NewReplacer("\\t", "\t", "\\n", "\n") |
| 49 | |
| 50 | type GrpcConfigSpec struct { |
| 51 | Timeout time.Duration `yaml:"timeout"` |
| 52 | } |
| 53 | |
| 54 | type TlsConfigSpec struct { |
| 55 | UseTls bool `yaml:"useTls"` |
| 56 | CACert string `yaml:"caCert"` |
| 57 | Cert string `yaml:"cert"` |
| 58 | Key string `yaml:"key"` |
| 59 | Verify string `yaml:"verify"` |
| 60 | } |
| 61 | |
| 62 | type GlobalConfigSpec struct { |
Scott Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 63 | Server string `yaml:"server"` |
| 64 | Username string `yaml:"username"` |
| 65 | Password string `yaml:"password"` |
Scott Baker | 14c8f18 | 2019-05-22 18:05:29 -0700 | [diff] [blame] | 66 | Protoset string `yaml:"protoset"` |
Scott Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 67 | Tls TlsConfigSpec `yaml:"tls"` |
| 68 | Grpc GrpcConfigSpec |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | var GlobalConfig = GlobalConfigSpec{ |
Scott Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 72 | Server: "localhost", |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 73 | Tls: TlsConfigSpec{ |
| 74 | UseTls: false, |
| 75 | }, |
| 76 | Grpc: GrpcConfigSpec{ |
| 77 | Timeout: time.Second * 10, |
| 78 | }, |
| 79 | } |
| 80 | |
| 81 | var GlobalOptions struct { |
Scott Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 82 | Config string `short:"c" long:"config" env:"CORDCONFIG" value-name:"FILE" default:"" description:"Location of client config file"` |
| 83 | Server string `short:"s" long:"server" default:"" value-name:"SERVER:PORT" description:"IP/Host and port of XOS"` |
| 84 | Username string `short:"u" long:"username" value-name:"USERNAME" default:"" description:"Username to authenticate with XOS"` |
| 85 | Password string `short:"p" long:"password" value-name:"PASSWORD" default:"" description:"Password to authenticate with XOS"` |
Scott Baker | 14c8f18 | 2019-05-22 18:05:29 -0700 | [diff] [blame] | 86 | Protoset string `long:"protoset" value-name:"FILENAME" description:"Load protobuf definitions from protoset instead of reflection api"` |
Scott Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 87 | Debug bool `short:"d" long:"debug" description:"Enable debug mode"` |
| 88 | UseTLS bool `long:"tls" description:"Use TLS"` |
| 89 | CACert string `long:"tlscacert" value-name:"CA_CERT_FILE" description:"Trust certs signed only by this CA"` |
| 90 | Cert string `long:"tlscert" value-name:"CERT_FILE" description:"Path to TLS vertificate file"` |
| 91 | Key string `long:"tlskey" value-name:"KEY_FILE" description:"Path to TLS key file"` |
| 92 | Verify bool `long:"tlsverify" description:"Use TLS and verify the remote"` |
Scott Baker | 5281d00 | 2019-05-16 10:45:26 -0700 | [diff] [blame] | 93 | Yes bool `short:"y" long:"yes" description:"answer yes to any confirmation prompts"` |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | type OutputOptions struct { |
| 97 | Format string `long:"format" value-name:"FORMAT" default:"" description:"Format to use to output structured data"` |
| 98 | Quiet bool `short:"q" long:"quiet" description:"Output only the IDs of the objects"` |
| 99 | OutputAs string `short:"o" long:"outputas" default:"table" choice:"table" choice:"json" choice:"yaml" description:"Type of output to generate"` |
| 100 | } |
| 101 | |
Scott Baker | a00418a | 2019-06-03 16:15:28 -0700 | [diff] [blame] | 102 | type ListOutputOptions struct { |
| 103 | OutputOptions |
| 104 | OrderBy string `short:"r" long:"orderby" default:"" description:"Specify the sort order of the results"` |
| 105 | } |
| 106 | |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 107 | func toOutputType(in string) OutputType { |
| 108 | switch in { |
| 109 | case "table": |
| 110 | fallthrough |
| 111 | default: |
| 112 | return OUTPUT_TABLE |
| 113 | case "json": |
| 114 | return OUTPUT_JSON |
| 115 | case "yaml": |
| 116 | return OUTPUT_YAML |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | type CommandResult struct { |
| 121 | Format format.Format |
Scott Baker | a00418a | 2019-06-03 16:15:28 -0700 | [diff] [blame] | 122 | OrderBy string |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 123 | OutputAs OutputType |
| 124 | Data interface{} |
| 125 | } |
| 126 | |
| 127 | type config struct { |
Scott Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 128 | Server string `yaml:"server"` |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 129 | } |
| 130 | |
Scott Baker | 63ce82e | 2019-05-15 09:01:42 -0700 | [diff] [blame] | 131 | func ProcessGlobalOptions() { |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 132 | if len(GlobalOptions.Config) == 0 { |
Scott Baker | 63ce82e | 2019-05-15 09:01:42 -0700 | [diff] [blame] | 133 | home, err := os.UserHomeDir() |
| 134 | if err != nil { |
| 135 | log.Printf("Unable to discover the users home directory: %s\n", err) |
| 136 | } |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 137 | GlobalOptions.Config = fmt.Sprintf("%s/.cord/config", home) |
| 138 | } |
| 139 | |
| 140 | info, err := os.Stat(GlobalOptions.Config) |
| 141 | if err == nil && !info.IsDir() { |
| 142 | configFile, err := ioutil.ReadFile(GlobalOptions.Config) |
| 143 | if err != nil { |
| 144 | log.Printf("configFile.Get err #%v ", err) |
| 145 | } |
| 146 | err = yaml.Unmarshal(configFile, &GlobalConfig) |
| 147 | if err != nil { |
| 148 | log.Fatalf("Unmarshal: %v", err) |
| 149 | } |
| 150 | } |
| 151 | |
Scott Baker | 14c8f18 | 2019-05-22 18:05:29 -0700 | [diff] [blame] | 152 | // Override from environment |
| 153 | // in particualr, for passing env vars via `go test` |
| 154 | env_server, present := os.LookupEnv("CORDCTL_SERVER") |
| 155 | if present { |
| 156 | GlobalConfig.Server = env_server |
| 157 | } |
| 158 | env_username, present := os.LookupEnv("CORDCTL_USERNAME") |
| 159 | if present { |
| 160 | GlobalConfig.Username = env_username |
| 161 | } |
| 162 | env_password, present := os.LookupEnv("CORDCTL_PASSWORD") |
| 163 | if present { |
| 164 | GlobalConfig.Password = env_password |
| 165 | } |
| 166 | env_protoset, present := os.LookupEnv("CORDCTL_PROTOSET") |
| 167 | if present { |
| 168 | GlobalConfig.Protoset = env_protoset |
| 169 | } |
| 170 | |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 171 | // Override from command line |
| 172 | if GlobalOptions.Server != "" { |
| 173 | GlobalConfig.Server = GlobalOptions.Server |
| 174 | } |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 175 | if GlobalOptions.Username != "" { |
| 176 | GlobalConfig.Username = GlobalOptions.Username |
| 177 | } |
| 178 | if GlobalOptions.Password != "" { |
| 179 | GlobalConfig.Password = GlobalOptions.Password |
| 180 | } |
Scott Baker | 14c8f18 | 2019-05-22 18:05:29 -0700 | [diff] [blame] | 181 | if GlobalOptions.Protoset != "" { |
| 182 | GlobalConfig.Protoset = GlobalOptions.Protoset |
| 183 | } |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 184 | |
Scott Baker | 5281d00 | 2019-05-16 10:45:26 -0700 | [diff] [blame] | 185 | // Generate error messages for required settings |
Scott Baker | 63ce82e | 2019-05-15 09:01:42 -0700 | [diff] [blame] | 186 | if GlobalConfig.Server == "" { |
| 187 | log.Fatal("Server is not set. Please update config file or use the -s option") |
| 188 | } |
| 189 | if GlobalConfig.Username == "" { |
| 190 | log.Fatal("Username is not set. Please update config file or use the -u option") |
| 191 | } |
| 192 | if GlobalConfig.Password == "" { |
| 193 | log.Fatal("Password is not set. Please update config file or use the -p option") |
| 194 | } |
A R Karthick | efb1eef | 2019-07-16 12:43:24 -0700 | [diff] [blame^] | 195 | //Try to resolve hostname if provided for the server |
| 196 | if host, port, err := net.SplitHostPort(GlobalConfig.Server); err == nil { |
| 197 | if addrs, err := net.LookupHost(host); err == nil { |
| 198 | GlobalConfig.Server = net.JoinHostPort(addrs[0], port) |
| 199 | } |
| 200 | } |
Scott Baker | 63ce82e | 2019-05-15 09:01:42 -0700 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | func NewConnection() (*grpc.ClientConn, error) { |
| 204 | ProcessGlobalOptions() |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 205 | return grpc.Dial(GlobalConfig.Server, grpc.WithInsecure()) |
| 206 | } |
| 207 | |
| 208 | func GenerateOutput(result *CommandResult) { |
| 209 | if result != nil && result.Data != nil { |
Scott Baker | a00418a | 2019-06-03 16:15:28 -0700 | [diff] [blame] | 210 | data := result.Data |
| 211 | if result.OrderBy != "" { |
| 212 | s, err := order.Parse(result.OrderBy) |
| 213 | if err != nil { |
| 214 | panic(err) |
| 215 | } |
| 216 | data, err = s.Process(data) |
| 217 | if err != nil { |
| 218 | panic(err) |
| 219 | } |
| 220 | } |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 221 | if result.OutputAs == OUTPUT_TABLE { |
| 222 | tableFormat := format.Format(result.Format) |
Scott Baker | a00418a | 2019-06-03 16:15:28 -0700 | [diff] [blame] | 223 | tableFormat.Execute(OutputStream, true, data) |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 224 | } else if result.OutputAs == OUTPUT_JSON { |
Scott Baker | a00418a | 2019-06-03 16:15:28 -0700 | [diff] [blame] | 225 | asJson, err := json.Marshal(&data) |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 226 | if err != nil { |
| 227 | panic(err) |
| 228 | } |
Scott Baker | 14c8f18 | 2019-05-22 18:05:29 -0700 | [diff] [blame] | 229 | fmt.Fprintf(OutputStream, "%s", asJson) |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 230 | } else if result.OutputAs == OUTPUT_YAML { |
Scott Baker | a00418a | 2019-06-03 16:15:28 -0700 | [diff] [blame] | 231 | asYaml, err := yaml.Marshal(&data) |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 232 | if err != nil { |
| 233 | panic(err) |
| 234 | } |
Scott Baker | 14c8f18 | 2019-05-22 18:05:29 -0700 | [diff] [blame] | 235 | fmt.Fprintf(OutputStream, "%s", asYaml) |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 236 | } |
| 237 | } |
| 238 | } |
Scott Baker | 5281d00 | 2019-05-16 10:45:26 -0700 | [diff] [blame] | 239 | |
| 240 | // Applies common output options to format and generate output |
| 241 | func FormatAndGenerateOutput(options *OutputOptions, default_format string, quiet_format string, data interface{}) { |
| 242 | outputFormat := CharReplacer.Replace(options.Format) |
| 243 | if outputFormat == "" { |
| 244 | outputFormat = default_format |
| 245 | } |
Scott Baker | 175cb40 | 2019-05-17 16:13:06 -0700 | [diff] [blame] | 246 | if options.Quiet { |
Scott Baker | 5281d00 | 2019-05-16 10:45:26 -0700 | [diff] [blame] | 247 | outputFormat = quiet_format |
| 248 | } |
| 249 | |
| 250 | result := CommandResult{ |
| 251 | Format: format.Format(outputFormat), |
| 252 | OutputAs: toOutputType(options.OutputAs), |
| 253 | Data: data, |
| 254 | } |
| 255 | |
| 256 | GenerateOutput(&result) |
| 257 | } |
Scott Baker | a00418a | 2019-06-03 16:15:28 -0700 | [diff] [blame] | 258 | |
| 259 | // Applies common output options to format and generate output |
| 260 | func FormatAndGenerateListOutput(options *ListOutputOptions, default_format string, quiet_format string, data interface{}) { |
| 261 | outputFormat := CharReplacer.Replace(options.Format) |
| 262 | if outputFormat == "" { |
| 263 | outputFormat = default_format |
| 264 | } |
| 265 | if options.Quiet { |
| 266 | outputFormat = quiet_format |
| 267 | } |
| 268 | |
| 269 | result := CommandResult{ |
| 270 | Format: format.Format(outputFormat), |
| 271 | OutputAs: toOutputType(options.OutputAs), |
| 272 | Data: data, |
| 273 | OrderBy: options.OrderBy, |
| 274 | } |
| 275 | |
| 276 | GenerateOutput(&result) |
| 277 | } |