blob: 0924db4abf42ecb7b981be317778981d196c7f30 [file] [log] [blame]
Scott Bakerd8476822019-06-07 15:12:09 -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 "context"
21 "github.com/fullstorydev/grpcurl"
22 flags "github.com/jessevdk/go-flags"
23 "github.com/jhump/protoreflect/dynamic"
24 "strings"
25)
26
27const StatusListFormat = "table{{ .Component }}\t{{ .Name }}\t{{ .Version }}\t{{ .Connection }}\t{{ .Status }}"
28
29type StatusListOpts struct {
30 ListOutputOptions
31 Filter string `short:"f" long:"filter" description:"Comma-separated list of filters"`
32}
33
34type StatusOpts struct {
35 List StatusListOpts `command:"list"`
36}
37
38var statusOpts = StatusOpts{}
39
40func RegisterStatusCommands(parser *flags.Parser) {
41 parser.AddCommand("status", "status commands", "Commands to query status of various subsystems", &statusOpts)
42}
43
44func (options *StatusListOpts) Execute(args []string) error {
Scott Baker867aa302019-06-19 13:18:45 -070045 conn, descriptor, err := InitClient(INIT_DEFAULT)
Scott Bakerd8476822019-06-07 15:12:09 -070046 if err != nil {
47 return err
48 }
49 defer conn.Close()
50
51 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
52 defer cancel()
53
54 headers := GenerateHeaders()
55
56 var components []map[string]string
57
58 // TODO(smbaker): Consider using David's client-side filtering can be used so we get filtering
59 // by other fields (status, etc) for free.
60
61 if options.Filter == "" || strings.Contains(strings.ToLower(options.Filter), "database") {
62 h := &RpcEventHandler{}
63 err = grpcurl.InvokeRPC(ctx, descriptor, conn, "xos.utility.GetDatabaseInfo", headers, h, h.GetParams)
64 if err != nil {
65 return err
66 }
67
68 if h.Status != nil && h.Status.Err() != nil {
69 return h.Status.Err()
70 }
71
72 d, err := dynamic.AsDynamicMessage(h.Response)
73 if err != nil {
74 return err
75 }
76
77 db_map := make(map[string]string)
78 db_map["Component"] = "Database"
79 db_map["Name"] = d.GetFieldByName("name").(string)
80 db_map["Version"] = d.GetFieldByName("version").(string)
81 db_map["Connection"] = d.GetFieldByName("connection").(string)
82 db_map["Status"] = GetEnumValue(d, "status")
83
84 components = append(components, db_map)
85 }
86
87 FormatAndGenerateListOutput(&options.ListOutputOptions, StatusListFormat, StatusListFormat, components)
88
89 return nil
90}