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

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