Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -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 | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 24 | ) |
| 25 | |
| 26 | const ( |
| 27 | DEFAULT_SERVICE_FORMAT = "table{{ .Name }}\t{{.Version}}\t{{.State}}" |
| 28 | ) |
| 29 | |
| 30 | type ServiceList struct { |
Scott Baker | a00418a | 2019-06-03 16:15:28 -0700 | [diff] [blame^] | 31 | ListOutputOptions |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | type ServiceListOutput struct { |
| 35 | Name string `json:"name"` |
| 36 | Version string `json:"version"` |
| 37 | State string `json:"state"` |
| 38 | } |
| 39 | |
| 40 | type ServiceOpts struct { |
| 41 | List ServiceList `command:"list"` |
| 42 | } |
| 43 | |
| 44 | var serviceOpts = ServiceOpts{} |
| 45 | |
| 46 | func RegisterServiceCommands(parser *flags.Parser) { |
| 47 | parser.AddCommand("service", "service commands", "Commands to query and manipulate dynamically loaded XOS Services", &serviceOpts) |
| 48 | } |
| 49 | |
| 50 | func (options *ServiceList) Execute(args []string) error { |
Scott Baker | f53bf15 | 2019-05-29 17:50:37 -0700 | [diff] [blame] | 51 | conn, descriptor, err := InitReflectionClient() |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 52 | if err != nil { |
| 53 | return err |
| 54 | } |
| 55 | defer conn.Close() |
| 56 | |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 57 | ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout) |
| 58 | defer cancel() |
| 59 | |
| 60 | headers := GenerateHeaders() |
| 61 | |
| 62 | h := &RpcEventHandler{} |
Scott Baker | f53bf15 | 2019-05-29 17:50:37 -0700 | [diff] [blame] | 63 | err = grpcurl.InvokeRPC(ctx, descriptor, conn, "xos.dynamicload.GetLoadStatus", headers, h, h.GetParams) |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 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 | items, err := d.TryGetFieldByName("services") |
| 78 | if err != nil { |
| 79 | return err |
| 80 | } |
| 81 | |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 82 | 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 Baker | a00418a | 2019-06-03 16:15:28 -0700 | [diff] [blame^] | 91 | FormatAndGenerateListOutput(&options.ListOutputOptions, DEFAULT_SERVICE_FORMAT, "{{.Name}}", data) |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 92 | |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 93 | return nil |
| 94 | } |