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