From a3ce8cfe54173bcf0d9ba22f57b6b3d27bc8cc3a Mon Sep 17 00:00:00 2001
From: Povilas Versockas <p.versockas@gmail.com>
Date: Mon, 3 Jun 2024 11:16:46 +0300
Subject: [PATCH] feat: [TKC-2074] delete worklows by name

---
 api/v1/testkube.yaml                          |  8 +++++
 pkg/tcl/apitcl/v1/testworkflows.go            | 29 +++++++++++++++++--
 .../cloudtcl/data/testworkflow/commands.go    | 11 ++++---
 .../data/testworkflow/execution_models.go     |  7 +++++
 pkg/tcl/cloudtcl/data/testworkflow/output.go  |  5 ++++
 .../repositorytcl/testworkflow/interface.go   |  2 ++
 .../testworkflow/minio_output_repository.go   | 11 +++++++
 7 files changed, 66 insertions(+), 7 deletions(-)

diff --git a/api/v1/testkube.yaml b/api/v1/testkube.yaml
index bc9238ede93..f8277353690 100644
--- a/api/v1/testkube.yaml
+++ b/api/v1/testkube.yaml
@@ -3346,6 +3346,7 @@ paths:
         - pro
       parameters:
         - $ref: "#/components/parameters/Selector"
+        - $ref: "#/components/parameters/TestWorkflowNames"
       summary: Delete test workflows
       description: Delete test workflows from the kubernetes cluster
       operationId: deleteTestWorkflows
@@ -9983,6 +9984,13 @@ components:
       schema:
         type: string
         description: Labels to filter by
+    TestWorkflowNames:
+      in: query
+      name: testWorkflowNames
+      schema:
+        items:
+          type: string
+        type: array
     ExecutionSelector:
       in: query
       name: executionSelector
diff --git a/pkg/tcl/apitcl/v1/testworkflows.go b/pkg/tcl/apitcl/v1/testworkflows.go
index 4420b71c57c..bc8ed013d84 100644
--- a/pkg/tcl/apitcl/v1/testworkflows.go
+++ b/pkg/tcl/apitcl/v1/testworkflows.go
@@ -86,9 +86,27 @@ func (s *apiTCL) DeleteTestWorkflowsHandler() fiber.Handler {
 	errPrefix := "failed to delete test workflows"
 	return func(c *fiber.Ctx) error {
 		selector := c.Query("selector")
-		workflows, err := s.TestWorkflowsClient.List(selector)
-		if err != nil {
-			return s.BadGateway(c, errPrefix, "client problem", err)
+
+		var (
+			workflows *testworkflowsv1.TestWorkflowList
+			err       error
+		)
+		testWorkflowNames := c.Query("testWorkflowNames")
+		if testWorkflowNames != "" {
+			names := strings.Split(testWorkflowNames, ",")
+			workflows = &testworkflowsv1.TestWorkflowList{}
+			for _, name := range names {
+				workflow, err := s.TestWorkflowsClient.Get(name)
+				if err != nil {
+					return s.ClientError(c, errPrefix, err)
+				}
+				workflows.Items = append(workflows.Items, *workflow)
+			}
+		} else {
+			workflows, err = s.TestWorkflowsClient.List(selector)
+			if err != nil {
+				return s.BadGateway(c, errPrefix, "client problem", err)
+			}
 		}
 
 		// Delete
@@ -108,6 +126,11 @@ func (s *apiTCL) DeleteTestWorkflowsHandler() fiber.Handler {
 			names := common.MapSlice(workflows.Items, func(t testworkflowsv1.TestWorkflow) string {
 				return t.Name
 			})
+
+			err = s.TestWorkflowOutput.DeleteOutputForTestWorkflows(context.Background(), names)
+			if err != nil {
+				return s.ClientError(c, "deleting executions output", err)
+			}
 			err = s.TestWorkflowResults.DeleteByTestWorkflows(context.Background(), names)
 			if err != nil {
 				return s.ClientError(c, "deleting executions", err)
diff --git a/pkg/tcl/cloudtcl/data/testworkflow/commands.go b/pkg/tcl/cloudtcl/data/testworkflow/commands.go
index a91ff0a7156..fdad7352624 100644
--- a/pkg/tcl/cloudtcl/data/testworkflow/commands.go
+++ b/pkg/tcl/cloudtcl/data/testworkflow/commands.go
@@ -29,10 +29,11 @@ const (
 	CmdTestWorkflowExecutionDeleteByWorkflows    executor.Command = "workflow_execution_delete_by_workflows"
 	CmdTestWorkflowExecutionGetWorkflowMetrics   executor.Command = "workflow_execution_get_workflow_metrics"
 
-	CmdTestWorkflowOutputPresignSaveLog       executor.Command = "workflow_output_presign_save_log"
-	CmdTestWorkflowOutputPresignReadLog       executor.Command = "workflow_output_presign_read_log"
-	CmdTestWorkflowOutputHasLog               executor.Command = "workflow_output_has_log"
-	CmdTestWorkflowOutputDeleteByTestWorkflow executor.Command = "workflow_output_delete_by_test_workflow"
+	CmdTestWorkflowOutputPresignSaveLog         executor.Command = "workflow_output_presign_save_log"
+	CmdTestWorkflowOutputPresignReadLog         executor.Command = "workflow_output_presign_read_log"
+	CmdTestWorkflowOutputHasLog                 executor.Command = "workflow_output_has_log"
+	CmdTestWorkflowOutputDeleteByTestWorkflow   executor.Command = "workflow_output_delete_by_test_workflow"
+	CmdTestworkflowOutputDeleteForTestWorkflows executor.Command = "workflow_output_delete_for_test_workflows"
 )
 
 func command(v interface{}) executor.Command {
@@ -78,6 +79,8 @@ func command(v interface{}) executor.Command {
 		return CmdTestWorkflowOutputHasLog
 	case ExecutionDeleteOutputByWorkflowRequest:
 		return CmdTestWorkflowOutputDeleteByTestWorkflow
+	case ExecutionDeleteOutputForTestWorkflowsRequest:
+		return CmdTestworkflowOutputDeleteForTestWorkflows
 	}
 	panic("unknown test workflows Cloud request")
 }
diff --git a/pkg/tcl/cloudtcl/data/testworkflow/execution_models.go b/pkg/tcl/cloudtcl/data/testworkflow/execution_models.go
index 5d72a96be04..2da0ab51994 100644
--- a/pkg/tcl/cloudtcl/data/testworkflow/execution_models.go
+++ b/pkg/tcl/cloudtcl/data/testworkflow/execution_models.go
@@ -121,6 +121,13 @@ type ExecutionDeleteOutputByWorkflowRequest struct {
 type ExecutionDeleteOutputByWorkflowResponse struct {
 }
 
+type ExecutionDeleteOutputForTestWorkflowsRequest struct {
+	WorkflowNames []string `json:"workflowNames"`
+}
+
+type ExecutionDeleteOutputForTestWorkflowsResponse struct {
+}
+
 type ExecutionDeleteAllRequest struct {
 }
 
diff --git a/pkg/tcl/cloudtcl/data/testworkflow/output.go b/pkg/tcl/cloudtcl/data/testworkflow/output.go
index 40122b87176..c3ff8239599 100644
--- a/pkg/tcl/cloudtcl/data/testworkflow/output.go
+++ b/pkg/tcl/cloudtcl/data/testworkflow/output.go
@@ -111,3 +111,8 @@ func (r *CloudOutputRepository) DeleteOutputByTestWorkflow(ctx context.Context,
 	req := ExecutionDeleteOutputByWorkflowRequest{WorkflowName: workflowName}
 	return passNoContent(r.executor, ctx, req)
 }
+
+func (r *CloudOutputRepository) DeleteOutputForTestWorkflows(ctx context.Context, workflowNames []string) (err error) {
+	req := ExecutionDeleteOutputForTestWorkflowsRequest{WorkflowNames: workflowNames}
+	return passNoContent(r.executor, ctx, req)
+}
diff --git a/pkg/tcl/repositorytcl/testworkflow/interface.go b/pkg/tcl/repositorytcl/testworkflow/interface.go
index 265e8742047..c5e2ee3b0f1 100644
--- a/pkg/tcl/repositorytcl/testworkflow/interface.go
+++ b/pkg/tcl/repositorytcl/testworkflow/interface.go
@@ -89,4 +89,6 @@ type OutputRepository interface {
 
 	// DeleteOutputByTestWorkflow deletes execution output by test workflow
 	DeleteOutputByTestWorkflow(ctx context.Context, testWorkflowName string) error
+	// DeleteOutputForTestWorkflows deletes execution output by test workflows
+	DeleteOutputForTestWorkflows(ctx context.Context, workflowNames []string) error
 }
diff --git a/pkg/tcl/repositorytcl/testworkflow/minio_output_repository.go b/pkg/tcl/repositorytcl/testworkflow/minio_output_repository.go
index 12fc71cefb3..273a299be18 100644
--- a/pkg/tcl/repositorytcl/testworkflow/minio_output_repository.go
+++ b/pkg/tcl/repositorytcl/testworkflow/minio_output_repository.go
@@ -95,6 +95,17 @@ func (m *MinioRepository) DeleteOutputByTestWorkflow(ctx context.Context, testWo
 	return nil
 }
 
+func (m *MinioRepository) DeleteOutputForTestWorkflows(ctx context.Context, workflowNames []string) error {
+	log.DefaultLogger.Debugw("deleting output for testWorkflows", "testWorkflowNames", workflowNames)
+	for _, testName := range workflowNames {
+		err := m.DeleteOutputByTestWorkflow(ctx, testName)
+		if err != nil {
+			return err
+		}
+	}
+	return nil
+}
+
 func (m *MinioRepository) DeleteOutput(ctx context.Context, id string) error {
 	log.DefaultLogger.Debugw("deleting test workflow output", "id", id)
 	return m.storage.DeleteFileFromBucket(ctx, m.bucket, bucketFolder, id)