blob: 469353902147c60bf67c08f655e74a4adc8c94af [file] [log] [blame]
Zack Williamse940c7a2019-08-21 14:25:39 -07001/*
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 */
16package commands
17
18import (
19 "encoding/json"
20 "fmt"
Scott Baker2b0ad652019-08-21 14:57:07 -070021 "github.com/opencord/voltctl/pkg/filter"
22 "github.com/opencord/voltctl/pkg/format"
23 "github.com/opencord/voltctl/pkg/order"
Zack Williamse940c7a2019-08-21 14:25:39 -070024 "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
34type OutputType uint8
35
36const (
37 OUTPUT_TABLE OutputType = iota
38 OUTPUT_JSON
39 OUTPUT_YAML
40)
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 Tls TlsConfigSpec `yaml:"tls"`
58 Grpc GrpcConfigSpec `yaml:"grpc"`
59 K8sConfig string `yaml:"-"`
60}
61
62var (
63 ParamNames = map[string]map[string]string{
64 "v1": {
65 "ID": "voltha.ID",
66 },
67 "v2": {
68 "ID": "common.ID",
69 },
70 }
71
72 CharReplacer = strings.NewReplacer("\\t", "\t", "\\n", "\n")
73
74 GlobalConfig = GlobalConfigSpec{
David Bainbridgec4029aa2019-09-26 18:56:39 +000075 ApiVersion: "v2",
Zack Williamse940c7a2019-08-21 14:25:39 -070076 Server: "localhost",
77 Tls: TlsConfigSpec{
78 UseTls: false,
79 },
80 Grpc: GrpcConfigSpec{
81 Timeout: time.Second * 10,
82 },
83 }
84
85 GlobalOptions struct {
David Bainbridgec4029aa2019-09-26 18:56:39 +000086 Config string `short:"c" long:"config" env:"VOLTCONFIG" value-name:"FILE" default:"" description:"Location of client config file"`
87 Server string `short:"s" long:"server" default:"" value-name:"SERVER:PORT" description:"IP/Host and port of VOLTHA"`
88 // Do not set the default for the API version here, else it will override the value read in the config
Zack Williamse940c7a2019-08-21 14:25:39 -070089 ApiVersion string `short:"a" long:"apiversion" description:"API version" value-name:"VERSION" choice:"v1" choice:"v2"`
90 Debug bool `short:"d" long:"debug" description:"Enable debug mode"`
91 UseTLS bool `long:"tls" description:"Use TLS"`
92 CACert string `long:"tlscacert" value-name:"CA_CERT_FILE" description:"Trust certs signed only by this CA"`
93 Cert string `long:"tlscert" value-name:"CERT_FILE" description:"Path to TLS vertificate file"`
94 Key string `long:"tlskey" value-name:"KEY_FILE" description:"Path to TLS key file"`
95 Verify bool `long:"tlsverify" description:"Use TLS and verify the remote"`
96 K8sConfig string `short:"8" long:"k8sconfig" env:"KUBECONFIG" value-name:"FILE" default:"" description:"Location of Kubernetes config file"`
97 }
98)
99
100type OutputOptions struct {
101 Format string `long:"format" value-name:"FORMAT" default:"" description:"Format to use to output structured data"`
102 Quiet bool `short:"q" long:"quiet" description:"Output only the IDs of the objects"`
103 OutputAs string `short:"o" long:"outputas" default:"table" choice:"table" choice:"json" choice:"yaml" description:"Type of output to generate"`
104 NameLimit int `short:"l" long:"namelimit" default:"-1" value-name:"LIMIT" description:"Limit the depth (length) in the table column name"`
105}
106
107type ListOutputOptions struct {
108 OutputOptions
109 Filter string `short:"f" long:"filter" default:"" value-name:"FILTER" description:"Only display results that match filter"`
110 OrderBy string `short:"r" long:"orderby" default:"" value-name:"ORDER" description:"Specify the sort order of the results"`
111}
112
113type OutputOptionsJson struct {
114 Format string `long:"format" value-name:"FORMAT" default:"" description:"Format to use to output structured data"`
115 Quiet bool `short:"q" long:"quiet" description:"Output only the IDs of the objects"`
116 OutputAs string `short:"o" long:"outputas" default:"json" choice:"table" choice:"json" choice:"yaml" description:"Type of output to generate"`
117 NameLimit int `short:"l" long:"namelimit" default:"-1" value-name:"LIMIT" description:"Limit the depth (length) in the table column name"`
118}
119
120type ListOutputOptionsJson struct {
121 OutputOptionsJson
122 Filter string `short:"f" long:"filter" default:"" value-name:"FILTER" description:"Only display results that match filter"`
123 OrderBy string `short:"r" long:"orderby" default:"" value-name:"ORDER" description:"Specify the sort order of the results"`
124}
125
126func toOutputType(in string) OutputType {
127 switch in {
128 case "table":
129 fallthrough
130 default:
131 return OUTPUT_TABLE
132 case "json":
133 return OUTPUT_JSON
134 case "yaml":
135 return OUTPUT_YAML
136 }
137}
138
139type CommandResult struct {
140 Format format.Format
141 Filter string
142 OrderBy string
143 OutputAs OutputType
144 NameLimit int
145 Data interface{}
146}
147
148type config struct {
149 ApiVersion string `yaml:"apiVersion"`
150 Server string `yaml:"server"`
151}
152
153func ProcessGlobalOptions() {
154 if len(GlobalOptions.Config) == 0 {
155 home, err := os.UserHomeDir()
156 if err != nil {
157 log.Printf("Unable to discover they users home directory: %s\n", err)
158 home = "~"
159 }
160 GlobalOptions.Config = filepath.Join(home, ".volt", "config")
161 }
162
163 info, err := os.Stat(GlobalOptions.Config)
164 if err == nil && !info.IsDir() {
165 configFile, err := ioutil.ReadFile(GlobalOptions.Config)
166 if err != nil {
167 log.Printf("configFile.Get err #%v ", err)
168 }
169 err = yaml.Unmarshal(configFile, &GlobalConfig)
170 if err != nil {
171 log.Fatalf("Unmarshal: %v", err)
172 }
173 }
174
175 // Override from command line
176 if GlobalOptions.Server != "" {
177 GlobalConfig.Server = GlobalOptions.Server
178 }
179 if GlobalOptions.ApiVersion != "" {
180 GlobalConfig.ApiVersion = GlobalOptions.ApiVersion
181 }
182
183 // If a k8s cert/key were not specified, then attempt to read it from
184 // any $HOME/.kube/config if it exists
185 if len(GlobalOptions.K8sConfig) == 0 {
186 home, err := os.UserHomeDir()
187 if err != nil {
188 log.Printf("Unable to discover the user's home directory: %s\n", err)
189 home = "~"
190 }
191 GlobalOptions.K8sConfig = filepath.Join(home, ".kube", "config")
192 }
193}
194
195func NewConnection() (*grpc.ClientConn, error) {
196 ProcessGlobalOptions()
197 return grpc.Dial(GlobalConfig.Server, grpc.WithInsecure())
198}
199
200func GenerateOutput(result *CommandResult) {
201 if result != nil && result.Data != nil {
202 data := result.Data
203 if result.Filter != "" {
204 f, err := filter.Parse(result.Filter)
205 if err != nil {
206 panic(err)
207 }
208 data, err = f.Process(data)
209 if err != nil {
210 panic(err)
211 }
212 }
213 if result.OrderBy != "" {
214 s, err := order.Parse(result.OrderBy)
215 if err != nil {
216 panic(err)
217 }
218 data, err = s.Process(data)
219 if err != nil {
220 panic(err)
221 }
222 }
223 if result.OutputAs == OUTPUT_TABLE {
224 tableFormat := format.Format(result.Format)
225 tableFormat.Execute(os.Stdout, true, result.NameLimit, data)
226 } else if result.OutputAs == OUTPUT_JSON {
227 asJson, err := json.Marshal(&data)
228 if err != nil {
229 panic(err)
230 }
231 fmt.Printf("%s", asJson)
232 } else if result.OutputAs == OUTPUT_YAML {
233 asYaml, err := yaml.Marshal(&data)
234 if err != nil {
235 panic(err)
236 }
237 fmt.Printf("%s", asYaml)
238 }
239 }
240}