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" |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 25 | ) |
| 26 | |
| 27 | const ( |
| 28 | DEFAULT_DEVICE_GROUP_FORMAT = "table{{ .Id }}\t{{.LogicalDevices}}\t{{.Devices}}" |
| 29 | ) |
| 30 | |
| 31 | type DeviceGroupList struct { |
| 32 | ListOutputOptions |
| 33 | } |
| 34 | |
| 35 | type DeviceGroupOpts struct { |
| 36 | List DeviceGroupList `command:"list"` |
| 37 | } |
| 38 | |
| 39 | var deviceGroupOpts = DeviceGroupOpts{} |
| 40 | |
| 41 | func RegisterDeviceGroupCommands(parser *flags.Parser) { |
David Bainbridge | 12f036f | 2019-10-15 22:09:04 +0000 | [diff] [blame] | 42 | if _, err := parser.AddCommand("devicegroup", "device group commands", "Commands to query and manipulate VOLTHA device groups", |
| 43 | &deviceGroupOpts); err != nil { |
David Bainbridge | a672234 | 2019-10-24 23:55:53 +0000 | [diff] [blame] | 44 | Error.Fatalf("Unexpected error while attempting to register device group commands : %s", err) |
David Bainbridge | 12f036f | 2019-10-15 22:09:04 +0000 | [diff] [blame] | 45 | } |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | func (options *DeviceGroupList) Execute(args []string) error { |
| 49 | |
| 50 | conn, err := NewConnection() |
| 51 | if err != nil { |
| 52 | return err |
| 53 | } |
| 54 | defer conn.Close() |
| 55 | |
David Bainbridge | a672234 | 2019-10-24 23:55:53 +0000 | [diff] [blame] | 56 | descriptor, method, err := GetMethod("device-group-list") |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 57 | if err != nil { |
| 58 | return err |
| 59 | } |
| 60 | |
| 61 | ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout) |
| 62 | defer cancel() |
| 63 | |
| 64 | h := &RpcEventHandler{} |
| 65 | err = grpcurl.InvokeRPC(ctx, descriptor, conn, method, []string{}, h, h.GetParams) |
| 66 | if err != nil { |
| 67 | return err |
| 68 | } |
| 69 | |
| 70 | if h.Status != nil && h.Status.Err() != nil { |
| 71 | return h.Status.Err() |
| 72 | } |
| 73 | |
| 74 | d, err := dynamic.AsDynamicMessage(h.Response) |
| 75 | if err != nil { |
| 76 | return err |
| 77 | } |
| 78 | |
| 79 | items, err := d.TryGetFieldByName("items") |
| 80 | if err != nil { |
| 81 | return err |
| 82 | } |
| 83 | |
| 84 | outputFormat := CharReplacer.Replace(options.Format) |
| 85 | if outputFormat == "" { |
David Bainbridge | a672234 | 2019-10-24 23:55:53 +0000 | [diff] [blame] | 86 | outputFormat = GetCommandOptionWithDefault("device-group-list", "format", DEFAULT_DEVICE_GROUP_FORMAT) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 87 | } |
| 88 | if options.Quiet { |
| 89 | outputFormat = "{{.Id}}" |
| 90 | } |
David Bainbridge | a672234 | 2019-10-24 23:55:53 +0000 | [diff] [blame] | 91 | orderBy := options.OrderBy |
| 92 | if orderBy == "" { |
| 93 | orderBy = GetCommandOptionWithDefault("device-group-list", "order", "") |
| 94 | } |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 95 | |
| 96 | data := make([]model.DeviceGroup, len(items.([]interface{}))) |
| 97 | for i, item := range items.([]interface{}) { |
| 98 | val := item.(*dynamic.Message) |
| 99 | data[i].PopulateFrom(val) |
| 100 | } |
| 101 | |
| 102 | result := CommandResult{ |
| 103 | Format: format.Format(outputFormat), |
| 104 | Filter: options.Filter, |
David Bainbridge | a672234 | 2019-10-24 23:55:53 +0000 | [diff] [blame] | 105 | OrderBy: orderBy, |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 106 | OutputAs: toOutputType(options.OutputAs), |
| 107 | NameLimit: options.NameLimit, |
| 108 | Data: data, |
| 109 | } |
| 110 | |
| 111 | GenerateOutput(&result) |
| 112 | return nil |
| 113 | } |