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