github.com/vnpaycloud-console/gophercloud/v2@v2.0.5/openstack/workflow/v2/crontriggers/requests.go (about) 1 package crontriggers 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 ToCronTriggerCreateMap() (map[string]any, error) 18 } 19 20 // CreateOpts specifies parameters used to create a cron trigger. 21 type CreateOpts struct { 22 // Name is the cron trigger name. 23 Name string `json:"name"` 24 25 // Pattern is a Unix crontab patterns format to execute the workflow. 26 Pattern string `json:"pattern"` 27 28 // RemainingExecutions sets the number of executions for the trigger. 29 RemainingExecutions int `json:"remaining_executions,omitempty"` 30 31 // WorkflowID is the unique id of the workflow. 32 WorkflowID string `json:"workflow_id,omitempty" or:"WorkflowName"` 33 34 // WorkflowName is the name of the workflow. 35 // It is recommended to refer to workflow by the WorkflowID parameter instead of WorkflowName. 36 WorkflowName string `json:"workflow_name,omitempty" or:"WorkflowID"` 37 38 // WorkflowParams defines workflow type specific parameters. 39 WorkflowParams map[string]any `json:"workflow_params,omitempty"` 40 41 // WorkflowInput defines workflow input values. 42 WorkflowInput map[string]any `json:"workflow_input,omitempty"` 43 44 // FirstExecutionTime defines the first execution time of the trigger. 45 FirstExecutionTime *time.Time `json:"-"` 46 } 47 48 // ToCronTriggerCreateMap constructs a request body from CreateOpts. 49 func (opts CreateOpts) ToCronTriggerCreateMap() (map[string]any, error) { 50 b, err := gophercloud.BuildRequestBody(opts, "") 51 if err != nil { 52 return nil, err 53 } 54 55 if opts.FirstExecutionTime != nil { 56 b["first_execution_time"] = opts.FirstExecutionTime.Format("2006-01-02 15:04") 57 } 58 59 return b, nil 60 } 61 62 // Create requests the creation of a new cron trigger. 63 func Create(ctx context.Context, client *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) { 64 b, err := opts.ToCronTriggerCreateMap() 65 if err != nil { 66 r.Err = err 67 return 68 } 69 70 resp, err := client.Post(ctx, createURL(client), b, &r.Body, nil) 71 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 72 return 73 } 74 75 // Delete deletes the specified cron trigger. 76 func Delete(ctx context.Context, client *gophercloud.ServiceClient, id string) (r DeleteResult) { 77 resp, err := client.Delete(ctx, deleteURL(client, id), nil) 78 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 79 return 80 } 81 82 // Get retrieves details of a single cron trigger. 83 // Use Extract to convert its result into an CronTrigger. 84 func Get(ctx context.Context, client *gophercloud.ServiceClient, id string) (r GetResult) { 85 resp, err := client.Get(ctx, getURL(client, id), &r.Body, nil) 86 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 87 return 88 } 89 90 // ListOptsBuilder allows extension to add additional parameters to the List request. 91 type ListOptsBuilder interface { 92 ToCronTriggerListQuery() (string, error) 93 } 94 95 // ListOpts filters the result returned by the List() function. 96 type ListOpts struct { 97 // WorkflowName allows to filter by workflow name. 98 WorkflowName *ListFilter `q:"-"` 99 // WorkflowID allows to filter by workflow id. 100 WorkflowID string `q:"workflow_id"` 101 // WorkflowInput allows to filter by specific workflow inputs. 102 WorkflowInput map[string]any `q:"-"` 103 // WorkflowParams allows to filter by specific workflow parameters. 104 WorkflowParams map[string]any `q:"-"` 105 // Scope filters by the trigger's scope. 106 // Values can be "private" or "public". 107 Scope string `q:"scope"` 108 // Name allows to filter by trigger name. 109 Name *ListFilter `q:"-"` 110 // Pattern allows to filter by pattern. 111 Pattern *ListFilter `q:"-"` 112 // RemainingExecutions allows to filter by remaining executions. 113 RemainingExecutions *ListIntFilter `q:"-"` 114 // FirstExecutionTime allows to filter by first execution time. 115 FirstExecutionTime *ListDateFilter `q:"-"` 116 // NextExecutionTime allows to filter by next execution time. 117 NextExecutionTime *ListDateFilter `q:"-"` 118 // CreatedAt allows to filter by trigger creation date. 119 CreatedAt *ListDateFilter `q:"-"` 120 // UpdatedAt allows to filter by trigger last update date. 121 UpdatedAt *ListDateFilter `q:"-"` 122 // ProjectID allows to filter by given project id. Admin required. 123 ProjectID string `q:"project_id"` 124 // AllProjects requests to get executions of all projects. Admin required. 125 AllProjects int `q:"all_projects"` 126 // SortDirs allows to select sort direction. 127 // It can be "asc" or "desc" (default). 128 SortDirs string `q:"sort_dirs"` 129 // SortKeys allows to sort by one of the cron trigger attributes. 130 SortKeys string `q:"sort_keys"` 131 // Marker and Limit control paging. 132 // Marker instructs List where to start listing from. 133 Marker string `q:"marker"` 134 // Limit instructs List to refrain from sending excessively large lists of 135 // cron triggers. 136 Limit int `q:"limit"` 137 } 138 139 // ListFilter allows to filter string parameters with different filters. 140 // Empty value for Filter checks for equality. 141 type ListFilter struct { 142 Filter FilterType 143 Value string 144 } 145 146 func (l ListFilter) String() string { 147 if l.Filter != "" { 148 return fmt.Sprintf("%s:%s", l.Filter, l.Value) 149 } 150 return l.Value 151 } 152 153 // ListDateFilter allows to filter date parameters with different filters. 154 // Empty value for Filter checks for equality. 155 type ListDateFilter struct { 156 Filter FilterType 157 Value time.Time 158 } 159 160 func (l ListDateFilter) String() string { 161 v := l.Value.Format(gophercloud.RFC3339ZNoTNoZ) 162 if l.Filter != "" { 163 return fmt.Sprintf("%s:%s", l.Filter, v) 164 } 165 return v 166 } 167 168 // ListIntFilter allows to filter integer parameters with different filters. 169 // Empty value for Filter checks for equality. 170 type ListIntFilter struct { 171 Filter FilterType 172 Value int 173 } 174 175 func (l ListIntFilter) String() string { 176 v := fmt.Sprintf("%d", l.Value) 177 if l.Filter != "" { 178 return fmt.Sprintf("%s:%s", l.Filter, v) 179 } 180 return v 181 } 182 183 // FilterType represents a valid filter to use for filtering executions. 184 type FilterType string 185 186 const ( 187 // FilterEQ checks equality. 188 FilterEQ = "eq" 189 // FilterNEQ checks non equality. 190 FilterNEQ = "neq" 191 // FilterIN checks for belonging in a list, comma separated. 192 FilterIN = "in" 193 // FilterNIN checks for values that does not belong from a list, comma separated. 194 FilterNIN = "nin" 195 // FilterGT checks for values strictly greater. 196 FilterGT = "gt" 197 // FilterGTE checks for values greater or equal. 198 FilterGTE = "gte" 199 // FilterLT checks for values strictly lower. 200 FilterLT = "lt" 201 // FilterLTE checks for values lower or equal. 202 FilterLTE = "lte" 203 // FilterHas checks for values that contains the requested parameter. 204 FilterHas = "has" 205 ) 206 207 // ToCronTriggerListQuery formats a ListOpts into a query string. 208 func (opts ListOpts) ToCronTriggerListQuery() (string, error) { 209 q, err := gophercloud.BuildQueryString(opts) 210 if err != nil { 211 return "", err 212 } 213 params := q.Query() 214 215 for queryParam, value := range map[string]map[string]any{"workflow_params": opts.WorkflowParams, "workflow_input": opts.WorkflowInput} { 216 if value != nil { 217 b, err := json.Marshal(value) 218 if err != nil { 219 return "", err 220 } 221 params.Add(queryParam, string(b)) 222 } 223 } 224 225 for queryParam, value := range map[string]fmt.Stringer{ 226 "workflow_name": opts.WorkflowName, 227 "name": opts.Name, 228 "pattern": opts.Pattern, 229 "remaining_executions": opts.RemainingExecutions, 230 "first_execution_time": opts.FirstExecutionTime, 231 "next_execution_time": opts.NextExecutionTime, 232 "created_at": opts.CreatedAt, 233 "updated_at": opts.UpdatedAt, 234 } { 235 if !reflect.ValueOf(value).IsNil() { 236 params.Add(queryParam, value.String()) 237 } 238 } 239 q = &url.URL{RawQuery: params.Encode()} 240 return q.String(), nil 241 } 242 243 // List performs a call to list cron triggers. 244 // You may provide options to filter the results. 245 func List(client *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager { 246 url := listURL(client) 247 if opts != nil { 248 query, err := opts.ToCronTriggerListQuery() 249 if err != nil { 250 return pagination.Pager{Err: err} 251 } 252 url += query 253 } 254 return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page { 255 return CronTriggerPage{pagination.LinkedPageBase{PageResult: r}} 256 }) 257 }