github.com/vnpaycloud-console/gophercloud/v2@v2.0.5/internal/acceptance/openstack/workflow/v2/execution.go (about)

     1  package v2
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"testing"
     7  
     8  	"github.com/vnpaycloud-console/gophercloud/v2"
     9  	"github.com/vnpaycloud-console/gophercloud/v2/internal/acceptance/tools"
    10  	"github.com/vnpaycloud-console/gophercloud/v2/openstack/workflow/v2/executions"
    11  	"github.com/vnpaycloud-console/gophercloud/v2/openstack/workflow/v2/workflows"
    12  	th "github.com/vnpaycloud-console/gophercloud/v2/testhelper"
    13  )
    14  
    15  // CreateExecution creates an execution for the given workflow.
    16  func CreateExecution(t *testing.T, client *gophercloud.ServiceClient, workflow *workflows.Workflow) (*executions.Execution, error) {
    17  	executionDescription := tools.RandomString("execution_", 5)
    18  
    19  	t.Logf("Attempting to create execution: %s", executionDescription)
    20  	createOpts := executions.CreateOpts{
    21  		ID:                executionDescription,
    22  		WorkflowID:        workflow.ID,
    23  		WorkflowNamespace: workflow.Namespace,
    24  		Description:       executionDescription,
    25  		Input: map[string]any{
    26  			"msg": "Hello World!",
    27  		},
    28  	}
    29  	execution, err := executions.Create(context.TODO(), client, createOpts).Extract()
    30  	if err != nil {
    31  		return execution, err
    32  	}
    33  
    34  	t.Logf("Execution created: %s", executionDescription)
    35  
    36  	th.AssertEquals(t, execution.Description, executionDescription)
    37  
    38  	t.Logf("Wait for execution status SUCCESS: %s", executionDescription)
    39  	th.AssertNoErr(t, tools.WaitFor(func(ctx context.Context) (bool, error) {
    40  		latest, err := executions.Get(ctx, client, execution.ID).Extract()
    41  		if err != nil {
    42  			return false, err
    43  		}
    44  
    45  		if latest.State == "SUCCESS" {
    46  			execution = latest
    47  			return true, nil
    48  		}
    49  
    50  		if latest.State == "ERROR" {
    51  			return false, fmt.Errorf("Execution in ERROR state")
    52  		}
    53  
    54  		return false, nil
    55  	}))
    56  	t.Logf("Execution success: %s", executionDescription)
    57  
    58  	return execution, nil
    59  }
    60  
    61  // DeleteExecution deletes an execution.
    62  func DeleteExecution(t *testing.T, client *gophercloud.ServiceClient, execution *executions.Execution) {
    63  	err := executions.Delete(context.TODO(), client, execution.ID).ExtractErr()
    64  	if err != nil {
    65  		t.Fatalf("Unable to delete executions %s: %v", execution.Description, err)
    66  	}
    67  	t.Logf("Deleted executions: %s", execution.Description)
    68  }
    69  
    70  // ListExecutions lists the executions.
    71  func ListExecutions(t *testing.T, client *gophercloud.ServiceClient, opts executions.ListOptsBuilder) ([]executions.Execution, error) {
    72  	allPages, err := executions.List(client, opts).AllPages(context.TODO())
    73  	if err != nil {
    74  		t.Fatalf("Unable to list executions: %v", err)
    75  	}
    76  
    77  	executionsList, err := executions.ExtractExecutions(allPages)
    78  	if err != nil {
    79  		t.Fatalf("Unable to extract executions: %v", err)
    80  	}
    81  
    82  	t.Logf("Executions list find, length: %d", len(executionsList))
    83  	return executionsList, err
    84  }