Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019-present Ciena Corporation |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package commands |
| 17 | |
| 18 | import ( |
| 19 | "encoding/json" |
| 20 | "fmt" |
Scott Baker | 2b0ad65 | 2019-08-21 14:57:07 -0700 | [diff] [blame] | 21 | "github.com/opencord/voltctl/pkg/filter" |
| 22 | "github.com/opencord/voltctl/pkg/format" |
| 23 | "github.com/opencord/voltctl/pkg/order" |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 24 | "google.golang.org/grpc" |
| 25 | "gopkg.in/yaml.v2" |
| 26 | "io/ioutil" |
| 27 | "log" |
| 28 | "os" |
| 29 | "path/filepath" |
| 30 | "strings" |
| 31 | "time" |
| 32 | ) |
| 33 | |
| 34 | type OutputType uint8 |
| 35 | |
| 36 | const ( |
| 37 | OUTPUT_TABLE OutputType = iota |
| 38 | OUTPUT_JSON |
| 39 | OUTPUT_YAML |
| 40 | ) |
| 41 | |
| 42 | type GrpcConfigSpec struct { |
| 43 | Timeout time.Duration `yaml:"timeout"` |
| 44 | } |
| 45 | |
| 46 | type TlsConfigSpec struct { |
| 47 | UseTls bool `yaml:"useTls"` |
| 48 | CACert string `yaml:"caCert"` |
| 49 | Cert string `yaml:"cert"` |
| 50 | Key string `yaml:"key"` |
| 51 | Verify string `yaml:"verify"` |
| 52 | } |
| 53 | |
| 54 | type GlobalConfigSpec struct { |
| 55 | ApiVersion string `yaml:"apiVersion"` |
| 56 | Server string `yaml:"server"` |
Scott Baker | ed4efab | 2020-01-13 19:12:25 -0800 | [diff] [blame] | 57 | Kafka string `yaml:"kafka"` |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 58 | Tls TlsConfigSpec `yaml:"tls"` |
| 59 | Grpc GrpcConfigSpec `yaml:"grpc"` |
| 60 | K8sConfig string `yaml:"-"` |
| 61 | } |
| 62 | |
| 63 | var ( |
| 64 | ParamNames = map[string]map[string]string{ |
| 65 | "v1": { |
| 66 | "ID": "voltha.ID", |
| 67 | }, |
| 68 | "v2": { |
| 69 | "ID": "common.ID", |
| 70 | }, |
kesavand | 12cd8eb | 2020-01-20 22:25:22 -0500 | [diff] [blame] | 71 | "v3": { |
| 72 | "ID": "common.ID", |
| 73 | "port": "voltha.Port", |
| 74 | }, |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | CharReplacer = strings.NewReplacer("\\t", "\t", "\\n", "\n") |
| 78 | |
| 79 | GlobalConfig = GlobalConfigSpec{ |
kesavand | 12cd8eb | 2020-01-20 22:25:22 -0500 | [diff] [blame] | 80 | ApiVersion: "v3", |
David Bainbridge | 0f758d4 | 2019-10-26 05:17:48 +0000 | [diff] [blame] | 81 | Server: "localhost:55555", |
Scott Baker | ed4efab | 2020-01-13 19:12:25 -0800 | [diff] [blame] | 82 | Kafka: "", |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 83 | Tls: TlsConfigSpec{ |
| 84 | UseTls: false, |
| 85 | }, |
| 86 | Grpc: GrpcConfigSpec{ |
| 87 | Timeout: time.Second * 10, |
| 88 | }, |
| 89 | } |
| 90 | |
David Bainbridge | a672234 | 2019-10-24 23:55:53 +0000 | [diff] [blame] | 91 | GlobalCommandOptions = make(map[string]map[string]string) |
| 92 | |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 93 | GlobalOptions struct { |
David Bainbridge | c4029aa | 2019-09-26 18:56:39 +0000 | [diff] [blame] | 94 | Config string `short:"c" long:"config" env:"VOLTCONFIG" value-name:"FILE" default:"" description:"Location of client config file"` |
| 95 | Server string `short:"s" long:"server" default:"" value-name:"SERVER:PORT" description:"IP/Host and port of VOLTHA"` |
Scott Baker | ed4efab | 2020-01-13 19:12:25 -0800 | [diff] [blame] | 96 | Kafka string `short:"k" long:"kafka" default:"" value-name:"SERVER:PORT" description:"IP/Host and port of Kafka"` |
David Bainbridge | c4029aa | 2019-09-26 18:56:39 +0000 | [diff] [blame] | 97 | // Do not set the default for the API version here, else it will override the value read in the config |
kesavand | 12cd8eb | 2020-01-20 22:25:22 -0500 | [diff] [blame] | 98 | ApiVersion string `short:"a" long:"apiversion" description:"API version" value-name:"VERSION" choice:"v1" choice:"v2" choice:"v3"` |
David Bainbridge | a672234 | 2019-10-24 23:55:53 +0000 | [diff] [blame] | 99 | Debug bool `short:"d" long:"debug" description:"Enable debug mode"` |
| 100 | UseTLS bool `long:"tls" description:"Use TLS"` |
| 101 | CACert string `long:"tlscacert" value-name:"CA_CERT_FILE" description:"Trust certs signed only by this CA"` |
| 102 | Cert string `long:"tlscert" value-name:"CERT_FILE" description:"Path to TLS vertificate file"` |
| 103 | Key string `long:"tlskey" value-name:"KEY_FILE" description:"Path to TLS key file"` |
| 104 | Verify bool `long:"tlsverify" description:"Use TLS and verify the remote"` |
| 105 | K8sConfig string `short:"8" long:"k8sconfig" env:"KUBECONFIG" value-name:"FILE" default:"" description:"Location of Kubernetes config file"` |
| 106 | CommandOptions string `short:"o" long:"command-options" env:"VOLTCTL_COMMAND_OPTIONS" value-name:"FILE" default:"" description:"Location of command options default configuration file"` |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 107 | } |
David Bainbridge | a672234 | 2019-10-24 23:55:53 +0000 | [diff] [blame] | 108 | |
| 109 | Debug = log.New(os.Stdout, "DEBUG: ", 0) |
| 110 | Info = log.New(os.Stdout, "INFO: ", 0) |
| 111 | Warn = log.New(os.Stderr, "WARN: ", 0) |
| 112 | Error = log.New(os.Stderr, "ERROR: ", 0) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 113 | ) |
| 114 | |
| 115 | type OutputOptions struct { |
| 116 | Format string `long:"format" value-name:"FORMAT" default:"" description:"Format to use to output structured data"` |
| 117 | Quiet bool `short:"q" long:"quiet" description:"Output only the IDs of the objects"` |
| 118 | OutputAs string `short:"o" long:"outputas" default:"table" choice:"table" choice:"json" choice:"yaml" description:"Type of output to generate"` |
| 119 | NameLimit int `short:"l" long:"namelimit" default:"-1" value-name:"LIMIT" description:"Limit the depth (length) in the table column name"` |
| 120 | } |
| 121 | |
| 122 | type ListOutputOptions struct { |
| 123 | OutputOptions |
| 124 | Filter string `short:"f" long:"filter" default:"" value-name:"FILTER" description:"Only display results that match filter"` |
| 125 | OrderBy string `short:"r" long:"orderby" default:"" value-name:"ORDER" description:"Specify the sort order of the results"` |
| 126 | } |
| 127 | |
| 128 | type OutputOptionsJson struct { |
| 129 | Format string `long:"format" value-name:"FORMAT" default:"" description:"Format to use to output structured data"` |
| 130 | Quiet bool `short:"q" long:"quiet" description:"Output only the IDs of the objects"` |
| 131 | OutputAs string `short:"o" long:"outputas" default:"json" choice:"table" choice:"json" choice:"yaml" description:"Type of output to generate"` |
| 132 | NameLimit int `short:"l" long:"namelimit" default:"-1" value-name:"LIMIT" description:"Limit the depth (length) in the table column name"` |
| 133 | } |
| 134 | |
| 135 | type ListOutputOptionsJson struct { |
| 136 | OutputOptionsJson |
| 137 | Filter string `short:"f" long:"filter" default:"" value-name:"FILTER" description:"Only display results that match filter"` |
| 138 | OrderBy string `short:"r" long:"orderby" default:"" value-name:"ORDER" description:"Specify the sort order of the results"` |
| 139 | } |
| 140 | |
| 141 | func toOutputType(in string) OutputType { |
| 142 | switch in { |
| 143 | case "table": |
| 144 | fallthrough |
| 145 | default: |
| 146 | return OUTPUT_TABLE |
| 147 | case "json": |
| 148 | return OUTPUT_JSON |
| 149 | case "yaml": |
| 150 | return OUTPUT_YAML |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | type CommandResult struct { |
| 155 | Format format.Format |
| 156 | Filter string |
| 157 | OrderBy string |
| 158 | OutputAs OutputType |
| 159 | NameLimit int |
| 160 | Data interface{} |
| 161 | } |
| 162 | |
David Bainbridge | a672234 | 2019-10-24 23:55:53 +0000 | [diff] [blame] | 163 | func GetCommandOptionWithDefault(name, option, defaultValue string) string { |
| 164 | if cmd, ok := GlobalCommandOptions[name]; ok { |
| 165 | if val, ok := cmd[option]; ok { |
| 166 | return CharReplacer.Replace(val) |
| 167 | } |
| 168 | } |
| 169 | return defaultValue |
| 170 | } |
| 171 | |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 172 | func ProcessGlobalOptions() { |
| 173 | if len(GlobalOptions.Config) == 0 { |
| 174 | home, err := os.UserHomeDir() |
| 175 | if err != nil { |
David Bainbridge | a672234 | 2019-10-24 23:55:53 +0000 | [diff] [blame] | 176 | Warn.Printf("Unable to discover the user's home directory: %s", err) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 177 | home = "~" |
| 178 | } |
| 179 | GlobalOptions.Config = filepath.Join(home, ".volt", "config") |
| 180 | } |
| 181 | |
David Bainbridge | a672234 | 2019-10-24 23:55:53 +0000 | [diff] [blame] | 182 | if info, err := os.Stat(GlobalOptions.Config); err == nil && !info.IsDir() { |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 183 | configFile, err := ioutil.ReadFile(GlobalOptions.Config) |
| 184 | if err != nil { |
David Bainbridge | a672234 | 2019-10-24 23:55:53 +0000 | [diff] [blame] | 185 | Error.Fatalf("Unable to read the configuration file '%s': %s", |
| 186 | GlobalOptions.Config, err.Error()) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 187 | } |
David Bainbridge | a672234 | 2019-10-24 23:55:53 +0000 | [diff] [blame] | 188 | if err = yaml.Unmarshal(configFile, &GlobalConfig); err != nil { |
| 189 | Error.Fatalf("Unable to parse the configuration file '%s': %s", |
| 190 | GlobalOptions.Config, err.Error()) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 191 | } |
| 192 | } |
| 193 | |
| 194 | // Override from command line |
| 195 | if GlobalOptions.Server != "" { |
| 196 | GlobalConfig.Server = GlobalOptions.Server |
| 197 | } |
Scott Baker | ed4efab | 2020-01-13 19:12:25 -0800 | [diff] [blame] | 198 | if GlobalOptions.Kafka != "" { |
| 199 | GlobalConfig.Kafka = GlobalOptions.Kafka |
| 200 | } |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 201 | if GlobalOptions.ApiVersion != "" { |
| 202 | GlobalConfig.ApiVersion = GlobalOptions.ApiVersion |
| 203 | } |
| 204 | |
| 205 | // If a k8s cert/key were not specified, then attempt to read it from |
| 206 | // any $HOME/.kube/config if it exists |
| 207 | if len(GlobalOptions.K8sConfig) == 0 { |
| 208 | home, err := os.UserHomeDir() |
| 209 | if err != nil { |
David Bainbridge | a672234 | 2019-10-24 23:55:53 +0000 | [diff] [blame] | 210 | Warn.Printf("Unable to discover the user's home directory: %s", err) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 211 | home = "~" |
| 212 | } |
| 213 | GlobalOptions.K8sConfig = filepath.Join(home, ".kube", "config") |
| 214 | } |
David Bainbridge | a672234 | 2019-10-24 23:55:53 +0000 | [diff] [blame] | 215 | |
| 216 | if len(GlobalOptions.CommandOptions) == 0 { |
| 217 | home, err := os.UserHomeDir() |
| 218 | if err != nil { |
| 219 | Warn.Printf("Unable to discover the user's home directory: %s", err) |
| 220 | home = "~" |
| 221 | } |
| 222 | GlobalOptions.CommandOptions = filepath.Join(home, ".volt", "command_options") |
| 223 | } |
| 224 | |
| 225 | if info, err := os.Stat(GlobalOptions.CommandOptions); err == nil && !info.IsDir() { |
| 226 | optionsFile, err := ioutil.ReadFile(GlobalOptions.CommandOptions) |
| 227 | if err != nil { |
| 228 | Error.Fatalf("Unable to read command options configuration file '%s' : %s", |
| 229 | GlobalOptions.CommandOptions, err.Error()) |
| 230 | } |
| 231 | if err = yaml.Unmarshal(optionsFile, &GlobalCommandOptions); err != nil { |
| 232 | Error.Fatalf("Unable to parse the command line options configuration file '%s': %s", |
| 233 | GlobalOptions.CommandOptions, err.Error()) |
| 234 | } |
| 235 | } |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | func NewConnection() (*grpc.ClientConn, error) { |
| 239 | ProcessGlobalOptions() |
| 240 | return grpc.Dial(GlobalConfig.Server, grpc.WithInsecure()) |
| 241 | } |
| 242 | |
| 243 | func GenerateOutput(result *CommandResult) { |
| 244 | if result != nil && result.Data != nil { |
| 245 | data := result.Data |
| 246 | if result.Filter != "" { |
| 247 | f, err := filter.Parse(result.Filter) |
| 248 | if err != nil { |
David Bainbridge | 0f758d4 | 2019-10-26 05:17:48 +0000 | [diff] [blame] | 249 | Error.Fatalf("Unable to parse specified output filter '%s': %s", result.Filter, err.Error()) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 250 | } |
| 251 | data, err = f.Process(data) |
| 252 | if err != nil { |
David Bainbridge | 0f758d4 | 2019-10-26 05:17:48 +0000 | [diff] [blame] | 253 | Error.Fatalf("Unexpected error while filtering command results: %s", err.Error()) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 254 | } |
| 255 | } |
| 256 | if result.OrderBy != "" { |
| 257 | s, err := order.Parse(result.OrderBy) |
| 258 | if err != nil { |
David Bainbridge | 0f758d4 | 2019-10-26 05:17:48 +0000 | [diff] [blame] | 259 | Error.Fatalf("Unable to parse specified sort specification '%s': %s", result.OrderBy, err.Error()) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 260 | } |
| 261 | data, err = s.Process(data) |
| 262 | if err != nil { |
David Bainbridge | 0f758d4 | 2019-10-26 05:17:48 +0000 | [diff] [blame] | 263 | Error.Fatalf("Unexpected error while sorting command result: %s", err.Error()) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 264 | } |
| 265 | } |
| 266 | if result.OutputAs == OUTPUT_TABLE { |
| 267 | tableFormat := format.Format(result.Format) |
David Bainbridge | 12f036f | 2019-10-15 22:09:04 +0000 | [diff] [blame] | 268 | if err := tableFormat.Execute(os.Stdout, true, result.NameLimit, data); err != nil { |
David Bainbridge | 0f758d4 | 2019-10-26 05:17:48 +0000 | [diff] [blame] | 269 | Error.Fatalf("Unexpected error while attempting to format results as table : %s", err.Error()) |
David Bainbridge | 12f036f | 2019-10-15 22:09:04 +0000 | [diff] [blame] | 270 | } |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 271 | } else if result.OutputAs == OUTPUT_JSON { |
| 272 | asJson, err := json.Marshal(&data) |
| 273 | if err != nil { |
David Bainbridge | 0f758d4 | 2019-10-26 05:17:48 +0000 | [diff] [blame] | 274 | Error.Fatalf("Unexpected error while processing command results to JSON: %s", err.Error()) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 275 | } |
| 276 | fmt.Printf("%s", asJson) |
| 277 | } else if result.OutputAs == OUTPUT_YAML { |
| 278 | asYaml, err := yaml.Marshal(&data) |
| 279 | if err != nil { |
David Bainbridge | 0f758d4 | 2019-10-26 05:17:48 +0000 | [diff] [blame] | 280 | Error.Fatalf("Unexpected error while processing command results to YAML: %s", err.Error()) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 281 | } |
| 282 | fmt.Printf("%s", asYaml) |
| 283 | } |
| 284 | } |
| 285 | } |