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