1package workflows
2
3import (
4	"encoding/json"
5	"time"
6
7	"github.com/gophercloud/gophercloud"
8	"github.com/gophercloud/gophercloud/pagination"
9)
10
11// CreateResult is the response of a Post operations. Call its Extract method to interpret it as a list of Workflows.
12type CreateResult struct {
13	gophercloud.Result
14}
15
16// DeleteResult is the result from a Delete operation. Call its ExtractErr method to determine the success of the call.
17type DeleteResult struct {
18	gophercloud.ErrResult
19}
20
21// Extract helps to get created Workflow struct from a Create function.
22func (r CreateResult) Extract() ([]Workflow, error) {
23	var s struct {
24		Workflows []Workflow `json:"workflows"`
25	}
26	err := r.ExtractInto(&s)
27	return s.Workflows, err
28}
29
30// GetResult is the response of Get operations. Call its Extract method to interpret it as a Workflow.
31type GetResult struct {
32	gophercloud.Result
33}
34
35// Extract helps to get a Workflow struct from a Get function.
36func (r GetResult) Extract() (*Workflow, error) {
37	var s Workflow
38	err := r.ExtractInto(&s)
39	return &s, err
40}
41
42// Workflow represents a workflow execution on OpenStack mistral API.
43type Workflow struct {
44	// ID is the workflow's unique ID.
45	ID string `json:"id"`
46
47	// Definition is the workflow definition in Mistral v2 DSL.
48	Definition string `json:"definition"`
49
50	// Name is the name of the workflow.
51	Name string `json:"name"`
52
53	// Namespace is the namespace of the workflow.
54	Namespace string `json:"namespace"`
55
56	// Input represents the needed input to execute the workflow.
57	// This parameter is a list of each input, comma separated.
58	Input string `json:"input"`
59
60	// ProjectID is the project id owner of the workflow.
61	ProjectID string `json:"project_id"`
62
63	// Scope is the scope of the workflow.
64	// Values can be "private" or "public".
65	Scope string `json:"scope"`
66
67	// Tags is a list of tags associated to the workflow.
68	Tags []string `json:"tags"`
69
70	// CreatedAt is the creation date of the workflow.
71	CreatedAt time.Time `json:"-"`
72
73	// UpdatedAt is the last update date of the workflow.
74	UpdatedAt *time.Time `json:"-"`
75}
76
77// UnmarshalJSON implements unmarshalling custom types
78func (r *Workflow) UnmarshalJSON(b []byte) error {
79	type tmp Workflow
80	var s struct {
81		tmp
82		CreatedAt gophercloud.JSONRFC3339ZNoTNoZ  `json:"created_at"`
83		UpdatedAt *gophercloud.JSONRFC3339ZNoTNoZ `json:"updated_at"`
84	}
85
86	err := json.Unmarshal(b, &s)
87	if err != nil {
88		return err
89	}
90
91	*r = Workflow(s.tmp)
92
93	r.CreatedAt = time.Time(s.CreatedAt)
94	if s.UpdatedAt != nil {
95		t := time.Time(*s.UpdatedAt)
96		r.UpdatedAt = &t
97	}
98
99	return nil
100}
101
102// WorkflowPage contains a single page of all workflows from a List call.
103type WorkflowPage struct {
104	pagination.LinkedPageBase
105}
106
107// IsEmpty checks if an WorkflowPage contains any results.
108func (r WorkflowPage) IsEmpty() (bool, error) {
109	exec, err := ExtractWorkflows(r)
110	return len(exec) == 0, err
111}
112
113// NextPageURL finds the next page URL in a page in order to navigate to the next page of results.
114func (r WorkflowPage) NextPageURL() (string, error) {
115	var s struct {
116		Next string `json:"next"`
117	}
118	err := r.ExtractInto(&s)
119	if err != nil {
120		return "", err
121	}
122	return s.Next, nil
123}
124
125// ExtractWorkflows get the list of cron triggers from a page acquired from the List call.
126func ExtractWorkflows(r pagination.Page) ([]Workflow, error) {
127	var s struct {
128		Workflows []Workflow `json:"workflows"`
129	}
130	err := (r.(WorkflowPage)).ExtractInto(&s)
131	return s.Workflows, err
132}
133