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