Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019-present Ciena Corporation |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package commands |
| 17 | |
| 18 | import ( |
| 19 | "context" |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 20 | "github.com/fullstorydev/grpcurl" |
| 21 | flags "github.com/jessevdk/go-flags" |
| 22 | "github.com/jhump/protoreflect/dynamic" |
Scott Baker | 2b0ad65 | 2019-08-21 14:57:07 -0700 | [diff] [blame] | 23 | "github.com/opencord/voltctl/pkg/format" |
| 24 | "github.com/opencord/voltctl/pkg/model" |
David Bainbridge | 12f036f | 2019-10-15 22:09:04 +0000 | [diff] [blame^] | 25 | "log" |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 26 | ) |
| 27 | |
| 28 | const ( |
| 29 | DEFAULT_DEVICE_GROUP_FORMAT = "table{{ .Id }}\t{{.LogicalDevices}}\t{{.Devices}}" |
| 30 | ) |
| 31 | |
| 32 | type DeviceGroupList struct { |
| 33 | ListOutputOptions |
| 34 | } |
| 35 | |
| 36 | type DeviceGroupOpts struct { |
| 37 | List DeviceGroupList `command:"list"` |
| 38 | } |
| 39 | |
| 40 | var deviceGroupOpts = DeviceGroupOpts{} |
| 41 | |
| 42 | func RegisterDeviceGroupCommands(parser *flags.Parser) { |
David Bainbridge | 12f036f | 2019-10-15 22:09:04 +0000 | [diff] [blame^] | 43 | if _, err := parser.AddCommand("devicegroup", "device group commands", "Commands to query and manipulate VOLTHA device groups", |
| 44 | &deviceGroupOpts); err != nil { |
| 45 | log.Fatalf("Unexpected error while attempting to register device group commands : %s", err) |
| 46 | } |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | func (options *DeviceGroupList) Execute(args []string) error { |
| 50 | |
| 51 | conn, err := NewConnection() |
| 52 | if err != nil { |
| 53 | return err |
| 54 | } |
| 55 | defer conn.Close() |
| 56 | |
| 57 | descriptor, method, err := GetMethod("devicegroup-list") |
| 58 | if err != nil { |
| 59 | return err |
| 60 | } |
| 61 | |
| 62 | ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout) |
| 63 | defer cancel() |
| 64 | |
| 65 | h := &RpcEventHandler{} |
| 66 | err = grpcurl.InvokeRPC(ctx, descriptor, conn, method, []string{}, h, h.GetParams) |
| 67 | if err != nil { |
| 68 | return err |
| 69 | } |
| 70 | |
| 71 | if h.Status != nil && h.Status.Err() != nil { |
| 72 | return h.Status.Err() |
| 73 | } |
| 74 | |
| 75 | d, err := dynamic.AsDynamicMessage(h.Response) |
| 76 | if err != nil { |
| 77 | return err |
| 78 | } |
| 79 | |
| 80 | items, err := d.TryGetFieldByName("items") |
| 81 | if err != nil { |
| 82 | return err |
| 83 | } |
| 84 | |
| 85 | outputFormat := CharReplacer.Replace(options.Format) |
| 86 | if outputFormat == "" { |
| 87 | outputFormat = DEFAULT_DEVICE_GROUP_FORMAT |
| 88 | } |
| 89 | if options.Quiet { |
| 90 | outputFormat = "{{.Id}}" |
| 91 | } |
| 92 | |
| 93 | data := make([]model.DeviceGroup, len(items.([]interface{}))) |
| 94 | for i, item := range items.([]interface{}) { |
| 95 | val := item.(*dynamic.Message) |
| 96 | data[i].PopulateFrom(val) |
| 97 | } |
| 98 | |
| 99 | result := CommandResult{ |
| 100 | Format: format.Format(outputFormat), |
| 101 | Filter: options.Filter, |
| 102 | OrderBy: options.OrderBy, |
| 103 | OutputAs: toOutputType(options.OutputAs), |
| 104 | NameLimit: options.NameLimit, |
| 105 | Data: data, |
| 106 | } |
| 107 | |
| 108 | GenerateOutput(&result) |
| 109 | return nil |
| 110 | } |