1package actions
2
3import (
4	"encoding/json"
5	"time"
6
7	"github.com/gophercloud/gophercloud"
8	"github.com/gophercloud/gophercloud/pagination"
9)
10
11// Action represents a detailed Action.
12type Action struct {
13	Action       string                 `json:"action"`
14	Cause        string                 `json:"cause"`
15	CreatedAt    time.Time              `json:"-"`
16	Data         map[string]interface{} `json:"data"`
17	DependedBy   []string               `json:"depended_by"`
18	DependsOn    []string               `json:"depends_on"`
19	StartTime    float64                `json:"start_time"`
20	EndTime      float64                `json:"end_time"`
21	ID           string                 `json:"id"`
22	Inputs       map[string]interface{} `json:"inputs"`
23	Interval     int                    `json:"interval"`
24	Name         string                 `json:"name"`
25	Outputs      map[string]interface{} `json:"outputs"`
26	Owner        string                 `json:"owner"`
27	Project      string                 `json:"project"`
28	Status       string                 `json:"status"`
29	StatusReason string                 `json:"status_reason"`
30	Target       string                 `json:"target"`
31	Timeout      int                    `json:"timeout"`
32	UpdatedAt    time.Time              `json:"-"`
33	User         string                 `json:"user"`
34}
35
36func (r *Action) UnmarshalJSON(b []byte) error {
37	type tmp Action
38	var s struct {
39		tmp
40		CreatedAt string `json:"created_at"`
41		UpdatedAt string `json:"updated_at"`
42	}
43
44	err := json.Unmarshal(b, &s)
45	if err != nil {
46		return err
47	}
48
49	*r = Action(s.tmp)
50
51	if s.CreatedAt != "" {
52		r.CreatedAt, err = time.Parse(time.RFC3339, s.CreatedAt)
53		if err != nil {
54			return err
55		}
56	}
57
58	if s.UpdatedAt != "" {
59		r.UpdatedAt, err = time.Parse(time.RFC3339, s.UpdatedAt)
60		if err != nil {
61			return err
62		}
63	}
64
65	return nil
66}
67
68// commonResult is the response of a base result.
69type commonResult struct {
70	gophercloud.Result
71}
72
73// Extract interprets any commonResult-based result as an Action.
74func (r commonResult) Extract() (*Action, error) {
75	var s struct {
76		Action *Action `json:"action"`
77	}
78	err := r.ExtractInto(&s)
79	return s.Action, err
80}
81
82// GetResult is the response of a Get operations. Call its Extract method to
83// interpret it as an Action.
84type GetResult struct {
85	commonResult
86}
87
88// ActionPage contains a single page of all actions from a List call.
89type ActionPage struct {
90	pagination.LinkedPageBase
91}
92
93// IsEmpty determines if a ActionPage contains any results.
94func (r ActionPage) IsEmpty() (bool, error) {
95	actions, err := ExtractActions(r)
96	return len(actions) == 0, err
97}
98
99// ExtractActions returns a slice of Actions from the List operation.
100func ExtractActions(r pagination.Page) ([]Action, error) {
101	var s struct {
102		Actions []Action `json:"actions"`
103	}
104	err := (r.(ActionPage)).ExtractInto(&s)
105	return s.Actions, err
106}
107