blob: e46dac6765257a9d7620ef744c394aa26c31dc31 [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 "context"
21 "github.com/fullstorydev/grpcurl"
22 flags "github.com/jessevdk/go-flags"
23 "github.com/jhump/protoreflect/dynamic"
Scott Baker2c0ebda2019-05-06 16:55:47 -070024)
25
26const (
27 DEFAULT_SERVICE_FORMAT = "table{{ .Name }}\t{{.Version}}\t{{.State}}"
28)
29
30type ServiceList struct {
31 OutputOptions
32}
33
34type ServiceListOutput struct {
35 Name string `json:"name"`
36 Version string `json:"version"`
37 State string `json:"state"`
38}
39
40type ServiceOpts struct {
41 List ServiceList `command:"list"`
42}
43
44var serviceOpts = ServiceOpts{}
45
46func RegisterServiceCommands(parser *flags.Parser) {
47 parser.AddCommand("service", "service commands", "Commands to query and manipulate dynamically loaded XOS Services", &serviceOpts)
48}
49
50func (options *ServiceList) Execute(args []string) error {
Scott Bakerf53bf152019-05-29 17:50:37 -070051 conn, descriptor, err := InitReflectionClient()
Scott Baker2c0ebda2019-05-06 16:55:47 -070052 if err != nil {
53 return err
54 }
55 defer conn.Close()
56
Scott Baker2c0ebda2019-05-06 16:55:47 -070057 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
58 defer cancel()
59
60 headers := GenerateHeaders()
61
62 h := &RpcEventHandler{}
Scott Bakerf53bf152019-05-29 17:50:37 -070063 err = grpcurl.InvokeRPC(ctx, descriptor, conn, "xos.dynamicload.GetLoadStatus", headers, h, h.GetParams)
Scott Baker2c0ebda2019-05-06 16:55:47 -070064 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 items, err := d.TryGetFieldByName("services")
78 if err != nil {
79 return err
80 }
81
Scott Baker2c0ebda2019-05-06 16:55:47 -070082 data := make([]ServiceListOutput, len(items.([]interface{})))
83
84 for i, item := range items.([]interface{}) {
85 val := item.(*dynamic.Message)
86 data[i].Name = val.GetFieldByName("name").(string)
87 data[i].Version = val.GetFieldByName("version").(string)
88 data[i].State = val.GetFieldByName("state").(string)
89 }
90
Scott Baker5281d002019-05-16 10:45:26 -070091 FormatAndGenerateOutput(&options.OutputOptions, DEFAULT_SERVICE_FORMAT, "{{.Name}}", data)
Scott Baker2c0ebda2019-05-06 16:55:47 -070092
Scott Baker2c0ebda2019-05-06 16:55:47 -070093 return nil
94}