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

     1  package crontriggers
     2  
     3  import (
     4  	"encoding/json"
     5  	"time"
     6  
     7  	"github.com/gophercloud/gophercloud"
     8  	"github.com/gophercloud/gophercloud/pagination"
     9  )
    10  
    11  type commonResult struct {
    12  	gophercloud.Result
    13  }
    14  
    15  // CreateResult is the response of a Post operations. Call its Extract method to interpret it as a CronTrigger.
    16  type CreateResult struct {
    17  	commonResult
    18  }
    19  
    20  // GetResult is the response of Get operations. Call its Extract method to interpret it as a CronTrigger.
    21  type GetResult struct {
    22  	commonResult
    23  }
    24  
    25  // DeleteResult is the result from a Delete operation. Call its ExtractErr method to determine the success of the call.
    26  type DeleteResult struct {
    27  	gophercloud.ErrResult
    28  }
    29  
    30  // Extract helps to get a CronTrigger struct from a Get or a Create function.
    31  func (r commonResult) Extract() (*CronTrigger, error) {
    32  	var s CronTrigger
    33  	err := r.ExtractInto(&s)
    34  	return &s, err
    35  }
    36  
    37  // CronTrigger represents a workflow cron trigger on OpenStack mistral API.
    38  type CronTrigger struct {
    39  	// ID is the cron trigger's unique ID.
    40  	ID string `json:"id"`
    41  
    42  	// Name is the name of the cron trigger.
    43  	Name string `json:"name"`
    44  
    45  	// Pattern is the cron-like style pattern to execute the workflow.
    46  	// Example of value: "* * * * *"
    47  	Pattern string `json:"pattern"`
    48  
    49  	// ProjectID is the project id owner of the cron trigger.
    50  	ProjectID string `json:"project_id"`
    51  
    52  	// RemainingExecutions is the number of remaining executions of this trigger.
    53  	RemainingExecutions int `json:"remaining_executions"`
    54  
    55  	// Scope is the scope of the trigger.
    56  	// Values can be "private" or "public".
    57  	Scope string `json:"scope"`
    58  
    59  	// WorkflowID is the ID of the workflow linked to the trigger.
    60  	WorkflowID string `json:"workflow_id"`
    61  
    62  	// WorkflowName is the name of the workflow linked to the trigger.
    63  	WorkflowName string `json:"workflow_name"`
    64  
    65  	// WorkflowInput contains the workflow input values.
    66  	WorkflowInput map[string]interface{} `json:"-"`
    67  
    68  	// WorkflowParams contains workflow type specific parameters.
    69  	WorkflowParams map[string]interface{} `json:"-"`
    70  
    71  	// CreatedAt contains the cron trigger creation date.
    72  	CreatedAt time.Time `json:"-"`
    73  
    74  	// FirstExecutionTime is the date of the first execution of the trigger.
    75  	FirstExecutionTime *time.Time `json:"-"`
    76  
    77  	// NextExecutionTime is the date of the next execution of the trigger.
    78  	NextExecutionTime *time.Time `json:"-"`
    79  }
    80  
    81  // UnmarshalJSON implements unmarshalling custom types
    82  func (r *CronTrigger) UnmarshalJSON(b []byte) error {
    83  	type tmp CronTrigger
    84  	var s struct {
    85  		tmp
    86  		CreatedAt          gophercloud.JSONRFC3339ZNoTNoZ  `json:"created_at"`
    87  		FirstExecutionTime *gophercloud.JSONRFC3339ZNoTNoZ `json:"first_execution_time"`
    88  		NextExecutionTime  *gophercloud.JSONRFC3339ZNoTNoZ `json:"next_execution_time"`
    89  		WorkflowInput      string                          `json:"workflow_input"`
    90  		WorkflowParams     string                          `json:"workflow_params"`
    91  	}
    92  
    93  	err := json.Unmarshal(b, &s)
    94  	if err != nil {
    95  		return err
    96  	}
    97  
    98  	*r = CronTrigger(s.tmp)
    99  
   100  	r.CreatedAt = time.Time(s.CreatedAt)
   101  	if s.FirstExecutionTime != nil {
   102  		t := time.Time(*s.FirstExecutionTime)
   103  		r.FirstExecutionTime = &t
   104  	}
   105  
   106  	if s.NextExecutionTime != nil {
   107  		t := time.Time(*s.NextExecutionTime)
   108  		r.NextExecutionTime = &t
   109  	}
   110  
   111  	if s.WorkflowInput != "" {
   112  		if err := json.Unmarshal([]byte(s.WorkflowInput), &r.WorkflowInput); err != nil {
   113  			return err
   114  		}
   115  	}
   116  
   117  	if s.WorkflowParams != "" {
   118  		if err := json.Unmarshal([]byte(s.WorkflowParams), &r.WorkflowParams); err != nil {
   119  			return err
   120  		}
   121  	}
   122  
   123  	return nil
   124  }
   125  
   126  // CronTriggerPage contains a single page of all cron triggers from a List call.
   127  type CronTriggerPage struct {
   128  	pagination.LinkedPageBase
   129  }
   130  
   131  // IsEmpty checks if an CronTriggerPage contains any results.
   132  func (r CronTriggerPage) IsEmpty() (bool, error) {
   133  	if r.StatusCode == 204 {
   134  		return true, nil
   135  	}
   136  
   137  	exec, err := ExtractCronTriggers(r)
   138  	return len(exec) == 0, err
   139  }
   140  
   141  // NextPageURL finds the next page URL in a page in order to navigate to the next page of results.
   142  func (r CronTriggerPage) NextPageURL() (string, error) {
   143  	var s struct {
   144  		Next string `json:"next"`
   145  	}
   146  	err := r.ExtractInto(&s)
   147  	if err != nil {
   148  		return "", err
   149  	}
   150  	return s.Next, nil
   151  }
   152  
   153  // ExtractCronTriggers get the list of cron triggers from a page acquired from the List call.
   154  func ExtractCronTriggers(r pagination.Page) ([]CronTrigger, error) {
   155  	var s struct {
   156  		CronTriggers []CronTrigger `json:"cron_triggers"`
   157  	}
   158  	err := (r.(CronTriggerPage)).ExtractInto(&s)
   159  	return s.CronTriggers, err
   160  }