blob: 329fee1a653c72d1a1dae93ab62d33fccf14b1c3 [file] [log] [blame]
Scott Baker2c0ebda2019-05-06 16:55:47 -07001/*
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 */
17package commands
18
19import (
20 "encoding/json"
21 "fmt"
22 "github.com/opencord/cordctl/format"
23 "google.golang.org/grpc"
24 "gopkg.in/yaml.v2"
25 "io/ioutil"
26 "log"
27 "os"
28 "strings"
29 "time"
30)
31
32type OutputType uint8
33
34const (
35 OUTPUT_TABLE OutputType = iota
36 OUTPUT_JSON
37 OUTPUT_YAML
38)
39
40var CharReplacer = strings.NewReplacer("\\t", "\t", "\\n", "\n")
41
42type GrpcConfigSpec struct {
43 Timeout time.Duration `yaml:"timeout"`
44}
45
46type 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
54type GlobalConfigSpec struct {
55 ApiVersion string `yaml:"apiVersion"`
56 Server string `yaml:"server"`
57 Username string `yaml:"username"`
58 Password string `yaml:"password"`
59 Tls TlsConfigSpec `yaml:"tls"`
60 Grpc GrpcConfigSpec
61}
62
63var GlobalConfig = GlobalConfigSpec{
64 ApiVersion: "v1",
65 Server: "localhost",
66 Tls: TlsConfigSpec{
67 UseTls: false,
68 },
69 Grpc: GrpcConfigSpec{
70 Timeout: time.Second * 10,
71 },
72}
73
74var GlobalOptions struct {
75 Config string `short:"c" long:"config" env:"CORDCONFIG" value-name:"FILE" default:"" description:"Location of client config file"`
76 Server string `short:"s" long:"server" default:"" value-name:"SERVER:PORT" description:"IP/Host and port of XOS"`
77 Username string `short:"u" long:"username" value-name:"USERNAME" default:"" description:"Username to authenticate with XOS"`
78 Password string `short:"p" long:"password" value-name:"PASSWORD" default:"" description:"Password to authenticate with XOS"`
79 ApiVersion string `short:"a" long:"apiversion" description:"API version" value-name:"VERSION" choice:"v1" choice:"v2"`
80 Debug bool `short:"d" long:"debug" description:"Enable debug mode"`
81 UseTLS bool `long:"tls" description:"Use TLS"`
82 CACert string `long:"tlscacert" value-name:"CA_CERT_FILE" description:"Trust certs signed only by this CA"`
83 Cert string `long:"tlscert" value-name:"CERT_FILE" description:"Path to TLS vertificate file"`
84 Key string `long:"tlskey" value-name:"KEY_FILE" description:"Path to TLS key file"`
85 Verify bool `long:"tlsverify" description:"Use TLS and verify the remote"`
86}
87
88type OutputOptions struct {
89 Format string `long:"format" value-name:"FORMAT" default:"" description:"Format to use to output structured data"`
90 Quiet bool `short:"q" long:"quiet" description:"Output only the IDs of the objects"`
91 OutputAs string `short:"o" long:"outputas" default:"table" choice:"table" choice:"json" choice:"yaml" description:"Type of output to generate"`
92}
93
94func toOutputType(in string) OutputType {
95 switch in {
96 case "table":
97 fallthrough
98 default:
99 return OUTPUT_TABLE
100 case "json":
101 return OUTPUT_JSON
102 case "yaml":
103 return OUTPUT_YAML
104 }
105}
106
107type CommandResult struct {
108 Format format.Format
109 OutputAs OutputType
110 Data interface{}
111}
112
113type config struct {
114 ApiVersion string `yaml:"apiVersion"`
115 Server string `yaml:"server"`
116}
117
118func NewConnection() (*grpc.ClientConn, error) {
119 if len(GlobalOptions.Config) == 0 {
120 home := os.Getenv("HOME")
121 // TODO: Replace after Jenkins updated to go 1.12
122 /*
123 home, err := os.UserHomeDir()
124 if err != nil {
125 log.Printf("Unable to discover they users home directory: %s\n", err)
126 }
127 */
128 GlobalOptions.Config = fmt.Sprintf("%s/.cord/config", home)
129 }
130
131 info, err := os.Stat(GlobalOptions.Config)
132 if err == nil && !info.IsDir() {
133 configFile, err := ioutil.ReadFile(GlobalOptions.Config)
134 if err != nil {
135 log.Printf("configFile.Get err #%v ", err)
136 }
137 err = yaml.Unmarshal(configFile, &GlobalConfig)
138 if err != nil {
139 log.Fatalf("Unmarshal: %v", err)
140 }
141 }
142
143 // Override from command line
144 if GlobalOptions.Server != "" {
145 GlobalConfig.Server = GlobalOptions.Server
146 }
147 if GlobalOptions.ApiVersion != "" {
148 GlobalConfig.ApiVersion = GlobalOptions.ApiVersion
149 }
150 if GlobalOptions.Username != "" {
151 GlobalConfig.Username = GlobalOptions.Username
152 }
153 if GlobalOptions.Password != "" {
154 GlobalConfig.Password = GlobalOptions.Password
155 }
156
157 return grpc.Dial(GlobalConfig.Server, grpc.WithInsecure())
158}
159
160func GenerateOutput(result *CommandResult) {
161 if result != nil && result.Data != nil {
162 if result.OutputAs == OUTPUT_TABLE {
163 tableFormat := format.Format(result.Format)
164 tableFormat.Execute(os.Stdout, true, result.Data)
165 } else if result.OutputAs == OUTPUT_JSON {
166 asJson, err := json.Marshal(&result.Data)
167 if err != nil {
168 panic(err)
169 }
170 fmt.Printf("%s", asJson)
171 } else if result.OutputAs == OUTPUT_YAML {
172 asYaml, err := yaml.Marshal(&result.Data)
173 if err != nil {
174 panic(err)
175 }
176 fmt.Printf("%s", asYaml)
177 }
178 }
179}