github.com/gophercloud/gophercloud@v1.11.0/internal/acceptance/openstack/workflow/v2/execution.go (about)

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