From c4fadaba6b46029c4c94ef7779eac73345cde07a Mon Sep 17 00:00:00 2001
From: haircommander <pehunt@redhat.com>
Date: Thu, 16 Aug 2018 11:41:53 -0400
Subject: [PATCH] Added helper function for libpod pod api calls

Signed-off-by: haircommander <pehunt@redhat.com>

Closes: #1275
Approved by: mheon
---
 cmd/podman/shared/pod.go |  2 +-
 pkg/varlinkapi/pods.go   | 90 ++++++++++------------------------------
 pkg/varlinkapi/util.go   | 16 +++++++
 3 files changed, 38 insertions(+), 70 deletions(-)

diff --git a/cmd/podman/shared/pod.go b/cmd/podman/shared/pod.go
index 50c642d592..c660bcf9e2 100644
--- a/cmd/podman/shared/pod.go
+++ b/cmd/podman/shared/pod.go
@@ -1,7 +1,7 @@
 package shared
 
 import (
-	"github.com/projectatomic/libpod/libpod"
+	"github.com/containers/libpod/libpod"
 )
 
 const (
diff --git a/pkg/varlinkapi/pods.go b/pkg/varlinkapi/pods.go
index a987574b2c..640dd665ef 100644
--- a/pkg/varlinkapi/pods.go
+++ b/pkg/varlinkapi/pods.go
@@ -4,9 +4,9 @@ import (
 	"encoding/json"
 	"syscall"
 
-	"github.com/projectatomic/libpod/cmd/podman/shared"
-	"github.com/projectatomic/libpod/cmd/podman/varlink"
-	"github.com/projectatomic/libpod/libpod"
+	"github.com/containers/libpod/cmd/podman/shared"
+	"github.com/containers/libpod/cmd/podman/varlink"
+	"github.com/containers/libpod/libpod"
 )
 
 // CreatePod ...
@@ -91,18 +91,10 @@ func (i *LibpodAPI) StartPod(call iopodman.VarlinkCall, name string) error {
 		return call.ReplyPodNotFound(name)
 	}
 	ctrErrs, err := pod.Start(getContext())
-	if err != nil && ctrErrs == nil {
-		return call.ReplyErrorOccurred(err.Error())
+	callErr := handlePodCall(call, pod, ctrErrs, err)
+	if callErr != nil {
+		return err
 	}
-	if ctrErrs != nil {
-		containerErrs := make([]iopodman.PodContainerErrorData, len(ctrErrs))
-		for ctr, reason := range ctrErrs {
-			ctrErr := iopodman.PodContainerErrorData{Containerid: ctr, Reason: reason.Error()}
-			containerErrs = append(containerErrs, ctrErr)
-		}
-		return call.ReplyPodContainerError(pod.ID(), containerErrs)
-	}
-
 	return call.ReplyStartPod(pod.ID())
 }
 
@@ -113,18 +105,10 @@ func (i *LibpodAPI) StopPod(call iopodman.VarlinkCall, name string) error {
 		return call.ReplyPodNotFound(name)
 	}
 	ctrErrs, err := pod.Stop(true)
-	if err != nil && ctrErrs == nil {
-		return call.ReplyErrorOccurred(err.Error())
+	callErr := handlePodCall(call, pod, ctrErrs, err)
+	if callErr != nil {
+		return err
 	}
-	if ctrErrs != nil {
-		containerErrs := make([]iopodman.PodContainerErrorData, len(ctrErrs))
-		for ctr, reason := range ctrErrs {
-			ctrErr := iopodman.PodContainerErrorData{Containerid: ctr, Reason: reason.Error()}
-			containerErrs = append(containerErrs, ctrErr)
-		}
-		return call.ReplyPodContainerError(pod.ID(), containerErrs)
-	}
-
 	return call.ReplyStopPod(pod.ID())
 }
 
@@ -135,18 +119,10 @@ func (i *LibpodAPI) RestartPod(call iopodman.VarlinkCall, name string) error {
 		return call.ReplyPodNotFound(name)
 	}
 	ctrErrs, err := pod.Restart(getContext())
-	if err != nil && ctrErrs == nil {
-		return call.ReplyErrorOccurred(err.Error())
+	callErr := handlePodCall(call, pod, ctrErrs, err)
+	if callErr != nil {
+		return err
 	}
-	if ctrErrs != nil {
-		containerErrs := make([]iopodman.PodContainerErrorData, len(ctrErrs))
-		for ctr, reason := range ctrErrs {
-			ctrErr := iopodman.PodContainerErrorData{Containerid: ctr, Reason: reason.Error()}
-			containerErrs = append(containerErrs, ctrErr)
-		}
-		return call.ReplyPodContainerError(pod.ID(), containerErrs)
-	}
-
 	return call.ReplyRestartPod(pod.ID())
 }
 
@@ -163,18 +139,10 @@ func (i *LibpodAPI) KillPod(call iopodman.VarlinkCall, name string, signal int64
 		return call.ReplyPodNotFound(name)
 	}
 	ctrErrs, err := pod.Kill(killSignal)
-	if err != nil && ctrErrs == nil {
-		return call.ReplyErrorOccurred(err.Error())
+	callErr := handlePodCall(call, pod, ctrErrs, err)
+	if callErr != nil {
+		return err
 	}
-	if ctrErrs != nil {
-		containerErrs := make([]iopodman.PodContainerErrorData, len(ctrErrs))
-		for ctr, reason := range ctrErrs {
-			ctrErr := iopodman.PodContainerErrorData{Containerid: ctr, Reason: reason.Error()}
-			containerErrs = append(containerErrs, ctrErr)
-		}
-		return call.ReplyPodContainerError(pod.ID(), containerErrs)
-	}
-
 	return call.ReplyKillPod(pod.ID())
 }
 
@@ -185,18 +153,10 @@ func (i *LibpodAPI) PausePod(call iopodman.VarlinkCall, name string) error {
 		return call.ReplyPodNotFound(name)
 	}
 	ctrErrs, err := pod.Pause()
-	if err != nil && ctrErrs == nil {
-		return call.ReplyErrorOccurred(err.Error())
+	callErr := handlePodCall(call, pod, ctrErrs, err)
+	if callErr != nil {
+		return err
 	}
-	if ctrErrs != nil {
-		containerErrs := make([]iopodman.PodContainerErrorData, len(ctrErrs))
-		for ctr, reason := range ctrErrs {
-			ctrErr := iopodman.PodContainerErrorData{Containerid: ctr, Reason: reason.Error()}
-			containerErrs = append(containerErrs, ctrErr)
-		}
-		return call.ReplyPodContainerError(pod.ID(), containerErrs)
-	}
-
 	return call.ReplyPausePod(pod.ID())
 }
 
@@ -207,18 +167,10 @@ func (i *LibpodAPI) UnpausePod(call iopodman.VarlinkCall, name string) error {
 		return call.ReplyPodNotFound(name)
 	}
 	ctrErrs, err := pod.Unpause()
-	if err != nil && ctrErrs == nil {
-		return call.ReplyErrorOccurred(err.Error())
+	callErr := handlePodCall(call, pod, ctrErrs, err)
+	if callErr != nil {
+		return err
 	}
-	if ctrErrs != nil {
-		containerErrs := make([]iopodman.PodContainerErrorData, len(ctrErrs))
-		for ctr, reason := range ctrErrs {
-			ctrErr := iopodman.PodContainerErrorData{Containerid: ctr, Reason: reason.Error()}
-			containerErrs = append(containerErrs, ctrErr)
-		}
-		return call.ReplyPodContainerError(pod.ID(), containerErrs)
-	}
-
 	return call.ReplyUnpausePod(pod.ID())
 }
 
diff --git a/pkg/varlinkapi/util.go b/pkg/varlinkapi/util.go
index e0b6799349..a80c8db416 100644
--- a/pkg/varlinkapi/util.go
+++ b/pkg/varlinkapi/util.go
@@ -117,3 +117,19 @@ func makeListPod(pod *libpod.Pod, batchInfo shared.PsOptions) (iopodman.ListPodD
 	}
 	return listPod, nil
 }
+
+func handlePodCall(call iopodman.VarlinkCall, pod *libpod.Pod, ctrErrs map[string]error, err error) error {
+	if err != nil && ctrErrs == nil {
+		return call.ReplyErrorOccurred(err.Error())
+	}
+	if ctrErrs != nil {
+		containerErrs := make([]iopodman.PodContainerErrorData, len(ctrErrs))
+		for ctr, reason := range ctrErrs {
+			ctrErr := iopodman.PodContainerErrorData{Containerid: ctr, Reason: reason.Error()}
+			containerErrs = append(containerErrs, ctrErr)
+		}
+		return call.ReplyPodContainerError(pod.ID(), containerErrs)
+	}
+
+	return nil
+}