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 | a55e645 | 2019-06-25 11:10:30 -0700 | [diff] [blame] | 24 | corderrors "github.com/opencord/cordctl/error" |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 25 | ) |
| 26 | |
| 27 | const ( |
| 28 | DEFAULT_SERVICE_FORMAT = "table{{ .Name }}\t{{.Version}}\t{{.State}}" |
| 29 | ) |
| 30 | |
| 31 | type ServiceList struct { |
Scott Baker | a00418a | 2019-06-03 16:15:28 -0700 | [diff] [blame] | 32 | ListOutputOptions |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | type ServiceListOutput struct { |
| 36 | Name string `json:"name"` |
| 37 | Version string `json:"version"` |
| 38 | State string `json:"state"` |
| 39 | } |
| 40 | |
| 41 | type ServiceOpts struct { |
| 42 | List ServiceList `command:"list"` |
| 43 | } |
| 44 | |
| 45 | var serviceOpts = ServiceOpts{} |
| 46 | |
| 47 | func RegisterServiceCommands(parser *flags.Parser) { |
| 48 | parser.AddCommand("service", "service commands", "Commands to query and manipulate dynamically loaded XOS Services", &serviceOpts) |
| 49 | } |
| 50 | |
| 51 | func (options *ServiceList) Execute(args []string) error { |
Scott Baker | 867aa30 | 2019-06-19 13:18:45 -0700 | [diff] [blame] | 52 | conn, descriptor, err := InitClient(INIT_DEFAULT) |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 53 | if err != nil { |
| 54 | return err |
| 55 | } |
| 56 | defer conn.Close() |
| 57 | |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 58 | ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout) |
| 59 | defer cancel() |
| 60 | |
| 61 | headers := GenerateHeaders() |
| 62 | |
| 63 | h := &RpcEventHandler{} |
Scott Baker | f53bf15 | 2019-05-29 17:50:37 -0700 | [diff] [blame] | 64 | 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] | 65 | if err != nil { |
Scott Baker | a55e645 | 2019-06-25 11:10:30 -0700 | [diff] [blame] | 66 | return corderrors.RpcErrorToCordError(err) |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -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 | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | d, err := dynamic.AsDynamicMessage(h.Response) |
| 74 | if err != nil { |
| 75 | return err |
| 76 | } |
| 77 | |
| 78 | items, err := d.TryGetFieldByName("services") |
| 79 | if err != nil { |
| 80 | return err |
| 81 | } |
| 82 | |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 83 | data := make([]ServiceListOutput, len(items.([]interface{}))) |
| 84 | |
| 85 | for i, item := range items.([]interface{}) { |
| 86 | val := item.(*dynamic.Message) |
| 87 | data[i].Name = val.GetFieldByName("name").(string) |
| 88 | data[i].Version = val.GetFieldByName("version").(string) |
| 89 | data[i].State = val.GetFieldByName("state").(string) |
| 90 | } |
| 91 | |
Scott Baker | a00418a | 2019-06-03 16:15:28 -0700 | [diff] [blame] | 92 | FormatAndGenerateListOutput(&options.ListOutputOptions, DEFAULT_SERVICE_FORMAT, "{{.Name}}", data) |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 93 | |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 94 | return nil |
| 95 | } |