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

     1  package executions
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"net/url"
     7  	"reflect"
     8  	"time"
     9  
    10  	"github.com/gophercloud/gophercloud"
    11  	"github.com/gophercloud/gophercloud/pagination"
    12  )
    13  
    14  // CreateOptsBuilder allows extension to add additional parameters to the Create request.
    15  type CreateOptsBuilder interface {
    16  	ToExecutionCreateMap() (map[string]interface{}, error)
    17  }
    18  
    19  // CreateOpts specifies parameters used to create an execution.
    20  type CreateOpts struct {
    21  	// ID is the unique ID of the execution.
    22  	ID string `json:"id,omitempty"`
    23  
    24  	// SourceExecutionID can be set to create an execution based on another existing execution.
    25  	SourceExecutionID string `json:"source_execution_id,omitempty"`
    26  
    27  	// WorkflowID is the unique id of the workflow.
    28  	WorkflowID string `json:"workflow_id,omitempty" or:"WorkflowName"`
    29  
    30  	// WorkflowName is the name identifier of the workflow.
    31  	WorkflowName string `json:"workflow_name,omitempty" or:"WorkflowID"`
    32  
    33  	// WorkflowNamespace is the namespace of the workflow.
    34  	WorkflowNamespace string `json:"workflow_namespace,omitempty"`
    35  
    36  	// Input is a JSON structure containing workflow input values, serialized as string.
    37  	Input map[string]interface{} `json:"input,omitempty"`
    38  
    39  	// Params define workflow type specific parameters.
    40  	Params map[string]interface{} `json:"params,omitempty"`
    41  
    42  	// Description is the description of the workflow execution.
    43  	Description string `json:"description,omitempty"`
    44  }
    45  
    46  // ToExecutionCreateMap constructs a request body from CreateOpts.
    47  func (opts CreateOpts) ToExecutionCreateMap() (map[string]interface{}, error) {
    48  	return gophercloud.BuildRequestBody(opts, "")
    49  }
    50  
    51  // Create requests the creation of a new execution.
    52  func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
    53  	b, err := opts.ToExecutionCreateMap()
    54  	if err != nil {
    55  		r.Err = err
    56  		return
    57  	}
    58  
    59  	resp, err := client.Post(createURL(client), b, &r.Body, nil)
    60  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
    61  	return
    62  }
    63  
    64  // Get retrieves details of a single execution.
    65  // Use ExtractExecution to convert its result into an Execution.
    66  func Get(client *gophercloud.ServiceClient, id string) (r GetResult) {
    67  	resp, err := client.Get(getURL(client, id), &r.Body, nil)
    68  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
    69  	return
    70  }
    71  
    72  // Delete deletes the specified execution.
    73  func Delete(client *gophercloud.ServiceClient, id string) (r DeleteResult) {
    74  	resp, err := client.Delete(deleteURL(client, id), nil)
    75  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
    76  	return
    77  }
    78  
    79  // ListOptsBuilder allows extension to add additional parameters to the List request.
    80  type ListOptsBuilder interface {
    81  	ToExecutionListQuery() (string, error)
    82  }
    83  
    84  // ListOpts filters the result returned by the List() function.
    85  type ListOpts struct {
    86  	// WorkflowName allows to filter by workflow name.
    87  	WorkflowName *ListFilter `q:"-"`
    88  	// WorkflowID allows to filter by workflow id.
    89  	WorkflowID string `q:"workflow_id"`
    90  	// Description allows to filter by execution description.
    91  	Description *ListFilter `q:"-"`
    92  	// Params allows to filter by specific parameters.
    93  	Params map[string]interface{} `q:"-"`
    94  	// TaskExecutionID allows to filter with a specific task execution id.
    95  	TaskExecutionID string `q:"task_execution_id"`
    96  	// RootExecutionID allows to filter with a specific root execution id.
    97  	RootExecutionID string `q:"root_execution_id"`
    98  	// State allows to filter by execution state.
    99  	// Possible values are IDLE, RUNNING, PAUSED, SUCCESS, ERROR, CANCELLED.
   100  	State *ListFilter `q:"-"`
   101  	// StateInfo allows to filter by state info.
   102  	StateInfo *ListFilter `q:"-"`
   103  	// Input allows to filter by specific input.
   104  	Input map[string]interface{} `q:"-"`
   105  	// Output allows to filter by specific output.
   106  	Output map[string]interface{} `q:"-"`
   107  	// CreatedAt allows to filter by execution creation date.
   108  	CreatedAt *ListDateFilter `q:"-"`
   109  	// UpdatedAt allows to filter by last execution update date.
   110  	UpdatedAt *ListDateFilter `q:"-"`
   111  	// IncludeOutput requests to include the output for all executions in the list.
   112  	IncludeOutput bool `q:"-"`
   113  	// ProjectID allows to filter by given project id. Admin required.
   114  	ProjectID string `q:"project_id"`
   115  	// AllProjects requests to get executions of all projects. Admin required.
   116  	AllProjects int `q:"all_projects"`
   117  	// SortDir allows to select sort direction.
   118  	// It can be "asc" or "desc" (default).
   119  	SortDirs string `q:"sort_dirs"`
   120  	// SortKey allows to sort by one of the execution attributes.
   121  	SortKeys string `q:"sort_keys"`
   122  	// Marker and Limit control paging.
   123  	// Marker instructs List where to start listing from.
   124  	Marker string `q:"marker"`
   125  	// Limit instructs List to refrain from sending excessively large lists of
   126  	// executions.
   127  	Limit int `q:"limit"`
   128  }
   129  
   130  // ListFilter allows to filter string parameters with different filters.
   131  // Empty value for Filter checks for equality.
   132  type ListFilter struct {
   133  	Filter FilterType
   134  	Value  string
   135  }
   136  
   137  func (l ListFilter) String() string {
   138  	if l.Filter != "" {
   139  		return fmt.Sprintf("%s:%s", l.Filter, l.Value)
   140  	}
   141  
   142  	return l.Value
   143  }
   144  
   145  // ListDateFilter allows to filter date parameters with different filters.
   146  // Empty value for Filter checks for equality.
   147  type ListDateFilter struct {
   148  	Filter FilterType
   149  	Value  time.Time
   150  }
   151  
   152  func (l ListDateFilter) String() string {
   153  	v := l.Value.Format(gophercloud.RFC3339ZNoTNoZ)
   154  
   155  	if l.Filter != "" {
   156  		return fmt.Sprintf("%s:%s", l.Filter, v)
   157  	}
   158  
   159  	return v
   160  }
   161  
   162  // FilterType represents a valid filter to use for filtering executions.
   163  type FilterType string
   164  
   165  const (
   166  	// FilterEQ checks equality.
   167  	FilterEQ = "eq"
   168  	// FilterNEQ checks non equality.
   169  	FilterNEQ = "neq"
   170  	// FilterIN checks for belonging in a list, comma separated.
   171  	FilterIN = "in"
   172  	// FilterNIN checks for values that does not belong from a list, comma separated.
   173  	FilterNIN = "nin"
   174  	// FilterGT checks for values strictly greater.
   175  	FilterGT = "gt"
   176  	// FilterGTE checks for values greater or equal.
   177  	FilterGTE = "gte"
   178  	// FilterLT checks for values strictly lower.
   179  	FilterLT = "lt"
   180  	// FilterLTE checks for values lower or equal.
   181  	FilterLTE = "lte"
   182  	// FilterHas checks for values that contains the requested parameter.
   183  	FilterHas = "has"
   184  )
   185  
   186  // ToExecutionListQuery formats a ListOpts into a query string.
   187  func (opts ListOpts) ToExecutionListQuery() (string, error) {
   188  	q, err := gophercloud.BuildQueryString(opts)
   189  	if err != nil {
   190  		return "", err
   191  	}
   192  
   193  	params := q.Query()
   194  
   195  	if opts.IncludeOutput {
   196  		params.Add("include_output", "1")
   197  	}
   198  
   199  	for queryParam, value := range map[string]map[string]interface{}{"params": opts.Params, "input": opts.Input, "output": opts.Output} {
   200  		if value != nil {
   201  			b, err := json.Marshal(value)
   202  			if err != nil {
   203  				return "", err
   204  			}
   205  			params.Add(queryParam, string(b))
   206  		}
   207  	}
   208  
   209  	for queryParam, value := range map[string]fmt.Stringer{
   210  		"created_at":    opts.CreatedAt,
   211  		"updated_at":    opts.UpdatedAt,
   212  		"workflow_name": opts.WorkflowName,
   213  		"description":   opts.Description,
   214  		"state":         opts.State,
   215  		"state_info":    opts.StateInfo,
   216  	} {
   217  		if !reflect.ValueOf(value).IsNil() {
   218  			params.Add(queryParam, value.String())
   219  		}
   220  	}
   221  
   222  	q = &url.URL{RawQuery: params.Encode()}
   223  	return q.String(), nil
   224  }
   225  
   226  // List performs a call to list executions.
   227  // You may provide options to filter the executions.
   228  func List(client *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager {
   229  	url := listURL(client)
   230  	if opts != nil {
   231  		query, err := opts.ToExecutionListQuery()
   232  		if err != nil {
   233  			return pagination.Pager{Err: err}
   234  		}
   235  		url += query
   236  	}
   237  
   238  	return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
   239  		return ExecutionPage{pagination.LinkedPageBase{PageResult: r}}
   240  	})
   241  }