From 3df61a46b667090864e3befa1e15d5e94db7434c Mon Sep 17 00:00:00 2001
From: Malte Bauch <malte.bauch@tbnet.works>
Date: Thu, 12 May 2022 15:42:23 +0000
Subject: [PATCH] Resolve "Allow to force push a SET from within the CLI"

See merge request danet/gosdn!310

Co-authored-by: Malte Bauch <malte.bauch@extern.h-da.de>
---
 cli/cmd/changeCommit.go             |  2 +-
 cli/cmd/changeConfirm.go            |  2 +-
 cli/cmd/deviceSet.go                | 11 ++++++----
 cli/cmd/prompt.go                   |  3 +--
 controller/northbound/server/pnd.go | 33 +++++++++++++++++------------
 5 files changed, 29 insertions(+), 22 deletions(-)

diff --git a/cli/cmd/changeCommit.go b/cli/cmd/changeCommit.go
index 0994ee2f6..2c271e9ca 100644
--- a/cli/cmd/changeCommit.go
+++ b/cli/cmd/changeCommit.go
@@ -55,7 +55,7 @@ Change UUID must be specified as positional argument.`,
 			pterm.Error.Println(err)
 		}
 		for _, r := range resp.GetResponses() {
-			pterm.Info.Println(r.String())
+			pterm.Info.Printfln("Change with ID: %s has been committed.", r.GetId())
 		}
 	},
 }
diff --git a/cli/cmd/changeConfirm.go b/cli/cmd/changeConfirm.go
index 20a49e536..cdc69f876 100644
--- a/cli/cmd/changeConfirm.go
+++ b/cli/cmd/changeConfirm.go
@@ -56,7 +56,7 @@ Change UUID must be specified as positional argument`,
 			pterm.Error.Println(err)
 		}
 		for _, r := range resp.GetResponses() {
-			pterm.Info.Println(r.String())
+			pterm.Info.Printfln("Change with ID: %s has been confirmed.", r.GetId())
 		}
 	},
 }
diff --git a/cli/cmd/deviceSet.go b/cli/cmd/deviceSet.go
index 8cd63cb2a..f64c2bd85 100644
--- a/cli/cmd/deviceSet.go
+++ b/cli/cmd/deviceSet.go
@@ -39,6 +39,7 @@ import (
 )
 
 var replace bool
+var forcePush bool
 
 // deviceSetCmd represents the set command
 var deviceSetCmd = &cobra.Command{
@@ -73,15 +74,16 @@ To enable replacing behaviour (destructive!), set the --replace flag."`,
 			args[1],
 			args[2],
 		)
+
 		if err != nil {
 			spinner.Fail(err)
 		}
 
 		for _, r := range resp.GetResponses() {
-			if r.Status == ppb.Status_STATUS_OK {
-				spinner.Success("A change for Device: ", did.String(), "has been created -> Change ID: ", r.GetId())
-			} else {
-				spinner.Fail("An error occured while creating a path set request for Device with ID: ", r.GetId(), r.GetStatus())
+			spinner.Success("A change for Device: ", did.String(), "has been created -> Change ID: ", r.GetId())
+			if forcePush {
+				executeFunc("change commit " + r.GetId())
+				executeFunc("change confirm " + r.GetId())
 			}
 		}
 	},
@@ -90,4 +92,5 @@ To enable replacing behaviour (destructive!), set the --replace flag."`,
 func init() {
 	deviceCmd.AddCommand(deviceSetCmd)
 	deviceSetCmd.Flags().BoolVarP(&replace, "replace", "r", false, "enables replace behaviour")
+	deviceSetCmd.Flags().BoolVarP(&forcePush, "force-push", "f", false, "enables the possibility to instantly push the set without commit/confirm")
 }
diff --git a/cli/cmd/prompt.go b/cli/cmd/prompt.go
index 20be885d3..747dccb3d 100644
--- a/cli/cmd/prompt.go
+++ b/cli/cmd/prompt.go
@@ -32,7 +32,6 @@ POSSIBILITY OF SUCH DAMAGE.
 package cmd
 
 import (
-	"fmt"
 	"os"
 	"strings"
 
@@ -104,7 +103,7 @@ func executeFunc(s string) {
 	err := rootCmd.Execute()
 
 	if err != nil {
-		fmt.Fprintln(os.Stderr, "Could not execute:", err)
+		pterm.Error.Println("Could not execute:", err)
 	}
 }
 
diff --git a/controller/northbound/server/pnd.go b/controller/northbound/server/pnd.go
index fbd7e7308..31f9386a6 100644
--- a/controller/northbound/server/pnd.go
+++ b/controller/northbound/server/pnd.go
@@ -541,7 +541,9 @@ func (p pndServer) SetChangeList(ctx context.Context, request *ppb.SetChangeList
 		return nil, handleRPCError(labels, err)
 	}
 
-	for _, r := range request.Change {
+	responses := make([]*ppb.SetResponse, len(request.Change))
+
+	for i, r := range request.Change {
 		cuid, err := uuid.Parse(r.Cuid)
 		if err != nil {
 			log.Error(err)
@@ -563,15 +565,16 @@ func (p pndServer) SetChangeList(ctx context.Context, request *ppb.SetChangeList
 				Param: r.Op,
 			}
 		}
+
+		responses[i] = &ppb.SetResponse{
+			Id:     cuid.String(),
+			Status: ppb.Status_STATUS_OK,
+		}
 	}
 	return &ppb.SetChangeListResponse{
 		Timestamp: time.Now().UnixNano(),
 		Status:    ppb.Status_STATUS_OK,
-		Responses: []*ppb.SetResponse{
-			{
-				Status: ppb.Status_STATUS_OK,
-			},
-		},
+		Responses: responses,
 	}, nil
 }
 
@@ -589,27 +592,29 @@ func (p pndServer) SetPathList(ctx context.Context, request *ppb.SetPathListRequ
 		return nil, handleRPCError(labels, err)
 	}
 
-	for _, r := range request.ChangeRequest {
+	responses := make([]*ppb.SetResponse, len(request.ChangeRequest))
+
+	for i, r := range request.ChangeRequest {
 		did, err := uuid.Parse(r.Did)
 		if err != nil {
 			log.Error(err)
 			return nil, status.Errorf(codes.Aborted, "%v", err)
 		}
-		// TODO: Return CUID in API
-		_, err = pnd.ChangeOND(did, r.ApiOp, r.Path, r.Value)
+		cid, err := pnd.ChangeOND(did, r.ApiOp, r.Path, r.Value)
 		if err != nil {
 			log.Error(err)
 			return nil, status.Errorf(codes.Aborted, "%v", err)
 		}
+
+		responses[i] = &ppb.SetResponse{
+			Status: ppb.Status_STATUS_OK,
+			Id:     cid.String(),
+		}
 	}
 	return &ppb.SetPathListResponse{
 		Timestamp: time.Now().UnixNano(),
 		Status:    ppb.Status_STATUS_OK,
-		Responses: []*ppb.SetResponse{
-			{
-				Status: ppb.Status_STATUS_OK,
-			},
-		},
+		Responses: responses,
 	}, nil
 }
 
-- 
GitLab