From 8dac05dca112de97c00172dc251e64a5e8f2f2ab Mon Sep 17 00:00:00 2001 From: Malte Bauch <malte.bauch@extern.h-da.de> Date: Wed, 20 Apr 2022 18:58:20 +0200 Subject: [PATCH] WIP --- cli/adapter/PndAdapter.go | 2 +- cli/cmd/deviceCreate.go | 14 ++++++++------ cli/cmd/deviceDelete.go | 22 ++++++++++++++-------- cli/cmd/deviceGet.go | 28 ++++++++++++++++++++++------ 4 files changed, 45 insertions(+), 21 deletions(-) diff --git a/cli/adapter/PndAdapter.go b/cli/adapter/PndAdapter.go index 3f4908a24..601e2d681 100644 --- a/cli/adapter/PndAdapter.go +++ b/cli/adapter/PndAdapter.go @@ -119,7 +119,7 @@ func (p *PndAdapter) ChangeOND(duid uuid.UUID, operation ppb.ApiOperation, path // Request sends an API call to the controller requesting the specified path // for the specified device -func (p *PndAdapter) Request(did uuid.UUID, path string) (proto.Message, error) { +func (p *PndAdapter) Request(did uuid.UUID, path string) (*ppb.GetPathResponse, error) { resp, err := api.GetPath(p.endpoint, p.id.String(), did.String(), path) if err != nil { return nil, err diff --git a/cli/cmd/deviceCreate.go b/cli/cmd/deviceCreate.go index 15980f113..19dc2dd35 100644 --- a/cli/cmd/deviceCreate.go +++ b/cli/cmd/deviceCreate.go @@ -53,6 +53,7 @@ if they diverge from the default credentials (user:'admin' and pw:'arista').`, spinner, _ := pterm.DefaultSpinner.Start("Creating new device") err := checkIPPort(address) if err != nil { + spinner.Fail(err) return err } @@ -74,6 +75,7 @@ if they diverge from the default credentials (user:'admin' and pw:'arista').`, } sid, err := uuid.Parse(cliSbi) if err != nil { + spinner.Fail(err) return err } @@ -82,13 +84,13 @@ if they diverge from the default credentials (user:'admin' and pw:'arista').`, return err } - if resp.GetStatus() == pnd.Status_STATUS_OK { - // TODO: we should return a ID here, changes needed for AddDevice() - spinner.Success("Device has been created with ID: ") - return nil + for _, r := range resp.GetResponses() { + if r.GetStatus() == pnd.Status_STATUS_OK { + spinner.Success("Device has been created with ID: ", r.GetId()) + } else { + spinner.Fail("An error occured while creating Device with ID: ", r.GetId(), r.GetStatus()) + } } - spinner.Warning("Creating new device: ", resp.GetStatus()) - return nil }, } diff --git a/cli/cmd/deviceDelete.go b/cli/cmd/deviceDelete.go index 1dc333899..0f939a697 100644 --- a/cli/cmd/deviceDelete.go +++ b/cli/cmd/deviceDelete.go @@ -34,7 +34,7 @@ package cmd import ( ppb "code.fbi.h-da.de/danet/gosdn/api/go/gosdn/pnd" "github.com/google/uuid" - log "github.com/sirupsen/logrus" + "github.com/pterm/pterm" "github.com/spf13/cobra" ) @@ -46,16 +46,22 @@ var deviceDeleteCmd = &cobra.Command{ Long: `Delete a path for a given orchestrated network device. The device UUID and request path must be specified as a positional arguments.`, - Run: func(cmd *cobra.Command, args []string) { + RunE: func(cmd *cobra.Command, args []string) error { + spinner, _ := pterm.DefaultSpinner.Start("Create a path deletion request.") did, err := uuid.Parse(args[0]) if err != nil { - log.Fatal(err) + spinner.Fail(err) + return err } - log.Info(pndAdapter.ChangeOND( - did, - ppb.ApiOperation_API_OPERATION_DELETE, - args[1], - )) + resp, err := pndAdapter.ChangeOND(did, ppb.ApiOperation_API_OPERATION_DELETE, args[1]) + for _, r := range resp.Responses { + if r.Status == ppb.Status_STATUS_OK { + spinner.Success("A change for path deletion for Device: ", did.String(), "has been created -> Change ID: ", r.GetId()) + } else { + spinner.Fail("An error occured while creating a path deletion request for Device with ID: ", r.GetId(), r.GetStatus()) + } + } + return nil }, } diff --git a/cli/cmd/deviceGet.go b/cli/cmd/deviceGet.go index feb2c5c2d..322c17f6c 100644 --- a/cli/cmd/deviceGet.go +++ b/cli/cmd/deviceGet.go @@ -33,7 +33,7 @@ package cmd import ( "github.com/google/uuid" - log "github.com/sirupsen/logrus" + "github.com/pterm/pterm" "github.com/spf13/cobra" "google.golang.org/protobuf/encoding/protojson" ) @@ -46,21 +46,37 @@ var deviceGetCmd = &cobra.Command{ Long: `Requests a path from a specified orchestrated network device on the controller. The device UUID and request path must be specified as a positional arguments.`, - Run: func(cmd *cobra.Command, args []string) { + RunE: func(cmd *cobra.Command, args []string) error { did, err := uuid.Parse(args[0]) if err != nil { - log.Fatal(err) + pterm.Error.Println(err) + return err } - message, err := pndAdapter.Request( + res, err := pndAdapter.Request( did, args[1], ) if err != nil { - log.Error(err) + pterm.Error.Println(err) + return err } - log.Info(protojson.Format(message)) + for _, n := range res.Device { + title := pterm.Sprintf("gNMI Notification for device with ID: ", did) + panel1 := pterm.DefaultBox.WithTitle("Timestamp:").Sprint(n.GetTimestamp()) + panel2 := pterm.DefaultBox.WithTitle("Requested Path:").Sprint(args[1]) + panel3 := pterm.DefaultBox.WithTitle("Result:").Sprint(protojson.Format(n.Update[0])) + + panels, _ := pterm.DefaultPanel.WithPanels(pterm.Panels{ + {{Data: panel1}}, + {{Data: panel2}}, + {{Data: panel3}}, + }).Srender() + + pterm.DefaultBox.WithTitle(title).WithTitleTopCenter().WithRightPadding(0).WithBottomPadding(0).Println(panels) + } + return nil }, } -- GitLab