github.com/gophercloud/gophercloud@v1.11.0/openstack/workflow/v2/executions/results.go (about)

     1  package executions
     2  
     3  import (
     4  	"encoding/json"
     5  	"time"
     6  
     7  	"github.com/gophercloud/gophercloud"
     8  	"github.com/gophercloud/gophercloud/pagination"
     9  )
    10  
    11  type commonResult struct {
    12  	gophercloud.Result
    13  }
    14  
    15  // CreateResult is the response of a Post operations. Call its Extract method to interpret it as an Execution.
    16  type CreateResult struct {
    17  	commonResult
    18  }
    19  
    20  // GetResult is the response of Get operations. Call its Extract method to interpret it as an Execution.
    21  type GetResult struct {
    22  	commonResult
    23  }
    24  
    25  // Extract helps to get an Execution struct from a Get or a Create function.
    26  func (r commonResult) Extract() (*Execution, error) {
    27  	var s Execution
    28  	err := r.ExtractInto(&s)
    29  	return &s, err
    30  }
    31  
    32  // DeleteResult is the result from a Delete operation. Call its ExtractErr method to determine the success of the call.
    33  type DeleteResult struct {
    34  	gophercloud.ErrResult
    35  }
    36  
    37  // Execution represents a workflow execution on OpenStack mistral API.
    38  type Execution struct {
    39  	// ID is the execution's unique ID.
    40  	ID string `json:"id"`
    41  
    42  	// CreatedAt contains the execution creation date.
    43  	CreatedAt time.Time `json:"-"`
    44  
    45  	// UpdatedAt is the last update of the execution.
    46  	UpdatedAt time.Time `json:"-"`
    47  
    48  	// RootExecutionID is the parent execution ID.
    49  	RootExecutionID *string `json:"root_execution_id"`
    50  
    51  	// TaskExecutionID is the task execution ID.
    52  	TaskExecutionID *string `json:"task_execution_id"`
    53  
    54  	// Description is the description of the execution.
    55  	Description string `json:"description"`
    56  
    57  	// Input contains the workflow input values.
    58  	Input map[string]interface{} `json:"-"`
    59  
    60  	// Ouput contains the workflow output values.
    61  	Output map[string]interface{} `json:"-"`
    62  
    63  	// Params contains workflow type specific parameters.
    64  	Params map[string]interface{} `json:"-"`
    65  
    66  	// ProjectID is the project id owner of the execution.
    67  	ProjectID string `json:"project_id"`
    68  
    69  	// State is the current state of the execution. State can be one of: IDLE, RUNNING, SUCCESS, ERROR, PAUSED, CANCELLED.
    70  	State string `json:"state"`
    71  
    72  	// StateInfo contains an optional state information string.
    73  	StateInfo *string `json:"state_info"`
    74  
    75  	// WorkflowID is the ID of the workflow linked to the execution.
    76  	WorkflowID string `json:"workflow_id"`
    77  
    78  	// WorkflowName is the name of the workflow linked to the execution.
    79  	WorkflowName string `json:"workflow_name"`
    80  
    81  	// WorkflowNamespace is the namespace of the workflow linked to the execution.
    82  	WorkflowNamespace string `json:"workflow_namespace"`
    83  }
    84  
    85  // UnmarshalJSON implements unmarshalling custom types
    86  func (r *Execution) UnmarshalJSON(b []byte) error {
    87  	type tmp Execution
    88  	var s struct {
    89  		tmp
    90  		CreatedAt gophercloud.JSONRFC3339ZNoTNoZ `json:"created_at"`
    91  		UpdatedAt gophercloud.JSONRFC3339ZNoTNoZ `json:"updated_at"`
    92  		Input     string                         `json:"input"`
    93  		Output    string                         `json:"output"`
    94  		Params    string                         `json:"params"`
    95  	}
    96  
    97  	err := json.Unmarshal(b, &s)
    98  	if err != nil {
    99  		return err
   100  	}
   101  
   102  	*r = Execution(s.tmp)
   103  
   104  	r.CreatedAt = time.Time(s.CreatedAt)
   105  	r.UpdatedAt = time.Time(s.UpdatedAt)
   106  
   107  	if s.Input != "" {
   108  		if err := json.Unmarshal([]byte(s.Input), &r.Input); err != nil {
   109  			return err
   110  		}
   111  	}
   112  
   113  	if s.Output != "" {
   114  		if err := json.Unmarshal([]byte(s.Output), &r.Output); err != nil {
   115  			return err
   116  		}
   117  	}
   118  
   119  	if s.Params != "" {
   120  		if err := json.Unmarshal([]byte(s.Params), &r.Params); err != nil {
   121  			return err
   122  		}
   123  	}
   124  
   125  	return nil
   126  }
   127  
   128  // ExecutionPage contains a single page of all executions from a List call.
   129  type ExecutionPage struct {
   130  	pagination.LinkedPageBase
   131  }
   132  
   133  // IsEmpty checks if an ExecutionPage contains any results.
   134  func (r ExecutionPage) IsEmpty() (bool, error) {
   135  	if r.StatusCode == 204 {
   136  		return true, nil
   137  	}
   138  
   139  	exec, err := ExtractExecutions(r)
   140  	return len(exec) == 0, err
   141  }
   142  
   143  // NextPageURL finds the next page URL in a page in order to navigate to the next page of results.
   144  func (r ExecutionPage) NextPageURL() (string, error) {
   145  	var s struct {
   146  		Next string `json:"next"`
   147  	}
   148  	err := r.ExtractInto(&s)
   149  	if err != nil {
   150  		return "", err
   151  	}
   152  	return s.Next, nil
   153  }
   154  
   155  // ExtractExecutions get the list of executions from a page acquired from the List call.
   156  func ExtractExecutions(r pagination.Page) ([]Execution, error) {
   157  	var s struct {
   158  		Executions []Execution `json:"executions"`
   159  	}
   160  	err := (r.(ExecutionPage)).ExtractInto(&s)
   161  	return s.Executions, err
   162  }