github.com/vnpaycloud-console/gophercloud/v2@v2.0.5/openstack/workflow/v2/workflows/requests.go (about) 1 package workflows 2 3 import ( 4 "context" 5 "fmt" 6 "io" 7 "net/url" 8 "reflect" 9 "strings" 10 "time" 11 12 "github.com/vnpaycloud-console/gophercloud/v2" 13 "github.com/vnpaycloud-console/gophercloud/v2/pagination" 14 ) 15 16 // CreateOptsBuilder allows extension to add additional parameters to the Create request. 17 type CreateOptsBuilder interface { 18 ToWorkflowCreateParams() (io.Reader, string, error) 19 } 20 21 // CreateOpts specifies parameters used to create a cron trigger. 22 type CreateOpts struct { 23 // Scope is the scope of the workflow. 24 // Allowed values are "private" and "public". 25 Scope string `q:"scope"` 26 27 // Namespace will define the namespace of the workflow. 28 Namespace string `q:"namespace"` 29 30 // Definition is the workflow definition written in Mistral Workflow Language v2. 31 Definition io.Reader 32 } 33 34 // ToWorkflowCreateParams constructs a request query string from CreateOpts. 35 func (opts CreateOpts) ToWorkflowCreateParams() (io.Reader, string, error) { 36 q, err := gophercloud.BuildQueryString(opts) 37 return opts.Definition, q.String(), err 38 } 39 40 // Create requests the creation of a new execution. 41 func Create(ctx context.Context, client *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) { 42 url := createURL(client) 43 var b io.Reader 44 if opts != nil { 45 tmpB, query, err := opts.ToWorkflowCreateParams() 46 if err != nil { 47 r.Err = err 48 return 49 } 50 url += query 51 b = tmpB 52 } 53 54 resp, err := client.Post(ctx, url, b, &r.Body, &gophercloud.RequestOpts{ 55 MoreHeaders: map[string]string{ 56 "Content-Type": "text/plain", 57 "Accept": "", // Drop default JSON Accept header 58 }, 59 }) 60 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 61 return 62 } 63 64 // Delete deletes the specified execution. 65 func Delete(ctx context.Context, client *gophercloud.ServiceClient, id string) (r DeleteResult) { 66 resp, err := client.Delete(ctx, deleteURL(client, id), nil) 67 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 68 return 69 } 70 71 // Get retrieves details of a single execution. 72 // Use Extract to convert its result into an Workflow. 73 func Get(ctx context.Context, client *gophercloud.ServiceClient, id string) (r GetResult) { 74 resp, err := client.Get(ctx, getURL(client, id), &r.Body, 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 ToWorkflowListQuery() (string, error) 82 } 83 84 // ListOpts filters the result returned by the List() function. 85 type ListOpts struct { 86 // Scope filters by the workflow's scope. 87 // Values can be "private" or "public". 88 Scope string `q:"scope"` 89 // CreatedAt allows to filter by workflow creation date. 90 CreatedAt *ListDateFilter `q:"-"` 91 // UpdatedAt allows to filter by last execution update date. 92 UpdatedAt *ListDateFilter `q:"-"` 93 // Name allows to filter by workflow name. 94 Name *ListFilter `q:"-"` 95 // Tags allows to filter by tags. 96 Tags []string 97 // Definition allows to filter by workflow definition. 98 Definition *ListFilter `q:"-"` 99 // Namespace allows to filter by workflow namespace. 100 Namespace *ListFilter `q:"-"` 101 // SortDirs allows to select sort direction. 102 // It can be "asc" or "desc" (default). 103 SortDirs string `q:"sort_dirs"` 104 // SortKeys allows to sort by one of the cron trigger attributes. 105 SortKeys string `q:"sort_keys"` 106 // Marker and Limit control paging. 107 // Marker instructs List where to start listing from. 108 Marker string `q:"marker"` 109 // Limit instructs List to refrain from sending excessively large lists of 110 // cron triggers. 111 Limit int `q:"limit"` 112 // ProjectID allows to filter by given project id. Admin required. 113 ProjectID string `q:"project_id"` 114 // AllProjects requests to get executions of all projects. Admin required. 115 AllProjects int `q:"all_projects"` 116 } 117 118 // ListFilter allows to filter string parameters with different filters. 119 // Empty value for Filter checks for equality. 120 type ListFilter struct { 121 Filter FilterType 122 Value string 123 } 124 125 func (l ListFilter) String() string { 126 if l.Filter != "" { 127 return fmt.Sprintf("%s:%s", l.Filter, l.Value) 128 } 129 return l.Value 130 } 131 132 // ListDateFilter allows to filter date parameters with different filters. 133 // Empty value for Filter checks for equality. 134 type ListDateFilter struct { 135 Filter FilterType 136 Value time.Time 137 } 138 139 func (l ListDateFilter) String() string { 140 v := l.Value.Format(gophercloud.RFC3339ZNoTNoZ) 141 if l.Filter != "" { 142 return fmt.Sprintf("%s:%s", l.Filter, v) 143 } 144 return v 145 } 146 147 // FilterType represents a valid filter to use for filtering executions. 148 type FilterType string 149 150 const ( 151 // FilterEQ checks equality. 152 FilterEQ = "eq" 153 // FilterNEQ checks non equality. 154 FilterNEQ = "neq" 155 // FilterIN checks for belonging in a list, comma separated. 156 FilterIN = "in" 157 // FilterNIN checks for values that does not belong from a list, comma separated. 158 FilterNIN = "nin" 159 // FilterGT checks for values strictly greater. 160 FilterGT = "gt" 161 // FilterGTE checks for values greater or equal. 162 FilterGTE = "gte" 163 // FilterLT checks for values strictly lower. 164 FilterLT = "lt" 165 // FilterLTE checks for values lower or equal. 166 FilterLTE = "lte" 167 // FilterHas checks for values that contains the requested parameter. 168 FilterHas = "has" 169 ) 170 171 // ToWorkflowListQuery formats a ListOpts into a query string. 172 func (opts ListOpts) ToWorkflowListQuery() (string, error) { 173 q, err := gophercloud.BuildQueryString(opts) 174 if err != nil { 175 return "", err 176 } 177 params := q.Query() 178 179 if opts.Tags != nil { 180 params.Add("tags", strings.Join(opts.Tags, ",")) 181 } 182 183 for queryParam, value := range map[string]fmt.Stringer{ 184 "created_at": opts.CreatedAt, 185 "updated_at": opts.UpdatedAt, 186 "name": opts.Name, 187 "definition": opts.Definition, 188 "namespace": opts.Namespace, 189 } { 190 if !reflect.ValueOf(value).IsNil() { 191 params.Add(queryParam, value.String()) 192 } 193 } 194 195 q = &url.URL{RawQuery: params.Encode()} 196 197 return q.String(), nil 198 } 199 200 // List performs a call to list cron triggers. 201 // You may provide options to filter the results. 202 func List(client *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager { 203 url := listURL(client) 204 if opts != nil { 205 query, err := opts.ToWorkflowListQuery() 206 if err != nil { 207 return pagination.Pager{Err: err} 208 } 209 url += query 210 } 211 return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page { 212 return WorkflowPage{pagination.LinkedPageBase{PageResult: r}} 213 }) 214 }