Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2022-present Open Networking Foundation |
| 3 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | * you may not use this file except in compliance with the License. |
| 5 | * You may obtain a copy of the License at |
| 6 | * |
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | * |
| 9 | * Unless required by applicable law or agreed to in writing, software |
| 10 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | * See the License for the specific language governing permissions and |
| 13 | * limitations under the License. |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame^] | 14 | */ |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 15 | |
| 16 | package commands |
| 17 | |
| 18 | import ( |
| 19 | "fmt" |
| 20 | "log" |
| 21 | |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame^] | 22 | db "voltha-go-controller/database" |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 23 | "voltha-go-controller/voltha-go-controller/cli/database" |
| 24 | "voltha-go-controller/voltha-go-controller/cli/format" |
| 25 | "voltha-go-controller/voltha-go-controller/cli/models" |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame^] | 26 | |
| 27 | flags "github.com/jessevdk/go-flags" |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 28 | ) |
| 29 | |
| 30 | // RegisterVNETCommands to register vnet command |
| 31 | func RegisterVNETCommands(parser *flags.Parser) { |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 32 | if _, err := parser.AddCommand("vnet", "Lists configured VNET profiles", "Commands to display VNET configuration", &vnetCommand); err != nil { |
| 33 | log.Fatalf("Unexpected error while attempting to register vnet commands : %s", err) |
| 34 | } |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | // VNETCommand structure |
| 38 | type VNETCommand struct{} |
| 39 | |
| 40 | var vnetCommand VNETCommand |
| 41 | |
| 42 | // Execute for execution of vnet command |
| 43 | func (ic *VNETCommand) Execute(args []string) error { |
| 44 | rc, err := database.GetRedisClient() |
| 45 | if err != nil { |
| 46 | return fmt.Errorf("Failed to make connection to KV Store: %v ", err) |
| 47 | } |
| 48 | |
| 49 | switch len(args) { |
| 50 | case 0: |
| 51 | // Get all VNET profiles |
| 52 | profiles, err := rc.GetAll(db.GetKeyPath(db.VnetPath)) |
| 53 | if err != nil { |
| 54 | return fmt.Errorf("Error fetching the VNET profile: %s", err) |
| 55 | } |
| 56 | if len(profiles) == 0 { |
| 57 | return fmt.Errorf("No VNET profiles found") |
| 58 | } |
| 59 | // call the formating function and display it in a table |
| 60 | format.NewTable(models.AllVNET, models.Horizontal).MultipleEntries(profiles) |
| 61 | case 1: |
| 62 | // VNET ID provided in the command |
| 63 | vnetID := args[0] |
| 64 | profile, err := rc.Get(db.GetKeyPath(db.VnetPath), vnetID) |
| 65 | if err != nil { |
| 66 | return fmt.Errorf("Error fetching the VNET details: %s", err) |
| 67 | } |
| 68 | if profile == nil { |
| 69 | return fmt.Errorf("No VNET profile found with ID %s", vnetID) |
| 70 | } |
| 71 | // call the formating function and display it in a table |
| 72 | tableTitle := models.TableTitle(fmt.Sprintf(string(models.SingleVNET), vnetID)) |
| 73 | format.NewTable(tableTitle, models.Horizontal).SingleEntry(profile) |
| 74 | default: |
| 75 | return fmt.Errorf("Usage: %s", models.VNETUsage) |
| 76 | } |
| 77 | return nil |
| 78 | } |