Scott Baker | d847682 | 2019-06-07 15:12:09 -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 | "context" |
| 21 | "github.com/fullstorydev/grpcurl" |
| 22 | flags "github.com/jessevdk/go-flags" |
| 23 | "github.com/jhump/protoreflect/dynamic" |
Scott Baker | 28a3956 | 2019-07-12 09:34:15 -0700 | [diff] [blame] | 24 | corderrors "github.com/opencord/cordctl/internal/pkg/error" |
Scott Baker | d847682 | 2019-06-07 15:12:09 -0700 | [diff] [blame] | 25 | "strings" |
| 26 | ) |
| 27 | |
| 28 | const StatusListFormat = "table{{ .Component }}\t{{ .Name }}\t{{ .Version }}\t{{ .Connection }}\t{{ .Status }}" |
| 29 | |
| 30 | type StatusListOpts struct { |
| 31 | ListOutputOptions |
| 32 | Filter string `short:"f" long:"filter" description:"Comma-separated list of filters"` |
| 33 | } |
| 34 | |
| 35 | type StatusOpts struct { |
| 36 | List StatusListOpts `command:"list"` |
| 37 | } |
| 38 | |
| 39 | var statusOpts = StatusOpts{} |
| 40 | |
| 41 | func RegisterStatusCommands(parser *flags.Parser) { |
| 42 | parser.AddCommand("status", "status commands", "Commands to query status of various subsystems", &statusOpts) |
| 43 | } |
| 44 | |
| 45 | func (options *StatusListOpts) Execute(args []string) error { |
Scott Baker | 867aa30 | 2019-06-19 13:18:45 -0700 | [diff] [blame] | 46 | conn, descriptor, err := InitClient(INIT_DEFAULT) |
Scott Baker | d847682 | 2019-06-07 15:12:09 -0700 | [diff] [blame] | 47 | if err != nil { |
| 48 | return err |
| 49 | } |
| 50 | defer conn.Close() |
| 51 | |
Scott Baker | ce11c49 | 2019-07-30 15:53:34 -0700 | [diff] [blame] | 52 | ctx, cancel := GrpcTimeoutContext(context.Background()) |
Scott Baker | d847682 | 2019-06-07 15:12:09 -0700 | [diff] [blame] | 53 | 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 Baker | a55e645 | 2019-06-25 11:10:30 -0700 | [diff] [blame] | 66 | return corderrors.RpcErrorToCordError(err) |
Scott Baker | d847682 | 2019-06-07 15:12:09 -0700 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | if h.Status != nil && h.Status.Err() != nil { |
Scott Baker | a55e645 | 2019-06-25 11:10:30 -0700 | [diff] [blame] | 70 | return corderrors.RpcErrorToCordError(h.Status.Err()) |
Scott Baker | d847682 | 2019-06-07 15:12:09 -0700 | [diff] [blame] | 71 | } |
| 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 | } |