1package receivers
2
3import (
4	"encoding/json"
5	"time"
6
7	"github.com/gophercloud/gophercloud"
8	"github.com/gophercloud/gophercloud/pagination"
9)
10
11// Receiver represent a detailed receiver
12type Receiver struct {
13	Action    string                 `json:"action"`
14	Actor     map[string]interface{} `json:"actor"`
15	Channel   map[string]interface{} `json:"channel"`
16	ClusterID string                 `json:"cluster_id"`
17	CreatedAt time.Time              `json:"-"`
18	Domain    string                 `json:"domain"`
19	ID        string                 `json:"id"`
20	Name      string                 `json:"name"`
21	Params    map[string]interface{} `json:"params"`
22	Project   string                 `json:"project"`
23	Type      string                 `json:"type"`
24	UpdatedAt time.Time              `json:"-"`
25	User      string                 `json:"user"`
26}
27
28func (r *Receiver) UnmarshalJSON(b []byte) error {
29	type tmp Receiver
30	var s struct {
31		tmp
32		CreatedAt string `json:"created_at"`
33		UpdatedAt string `json:"updated_at"`
34	}
35	err := json.Unmarshal(b, &s)
36	if err != nil {
37		return err
38	}
39	*r = Receiver(s.tmp)
40
41	if s.CreatedAt != "" {
42		r.CreatedAt, err = time.Parse(time.RFC3339, s.CreatedAt)
43		if err != nil {
44			return err
45		}
46	}
47
48	if s.UpdatedAt != "" {
49		r.UpdatedAt, err = time.Parse(time.RFC3339, s.UpdatedAt)
50		if err != nil {
51			return err
52		}
53	}
54
55	return nil
56}
57
58// commonResult is the response of a base result.
59type commonResult struct {
60	gophercloud.Result
61}
62
63// Extract interprets any commonResult-based result as a Receiver.
64func (r commonResult) Extract() (*Receiver, error) {
65	var s struct {
66		Receiver *Receiver `json:"receiver"`
67	}
68	err := r.ExtractInto(&s)
69	return s.Receiver, err
70}
71
72// CreateResult is the result of a Create operation. Call its Extract method
73// to interpret it as a Receiver.
74type CreateResult struct {
75	commonResult
76}
77
78// GetResult is the result for of a Get operation. Call its Extract method
79// to interpret it as a Receiver.
80type GetResult struct {
81	commonResult
82}
83
84// UpdateResult is the result of a Update operation. Call its Extract method
85// to interpret it as a Receiver.
86type UpdateResult struct {
87	commonResult
88}
89
90// DeleteResult is the result from a Delete operation. Call its ExtractErr
91// method to determine if the call succeeded or failed.
92type DeleteResult struct {
93	gophercloud.ErrResult
94}
95
96// NotifyResult is the result from a Notify operation. Call its Extract
97// method to determine if the call succeeded or failed.
98type NotifyResult struct {
99	commonResult
100}
101
102// ReceiverPage contains a single page of all nodes from a List operation.
103type ReceiverPage struct {
104	pagination.LinkedPageBase
105}
106
107// IsEmpty determines if a ReceiverPage contains any results.
108func (page ReceiverPage) IsEmpty() (bool, error) {
109	receivers, err := ExtractReceivers(page)
110	return len(receivers) == 0, err
111}
112
113// ExtractReceivers returns a slice of Receivers from the List operation.
114func ExtractReceivers(r pagination.Page) ([]Receiver, error) {
115	var s struct {
116		Receivers []Receiver `json:"receivers"`
117	}
118	err := (r.(ReceiverPage)).ExtractInto(&s)
119	return s.Receivers, err
120}
121
122// Extract returns action for notify receivers
123func (r NotifyResult) Extract() (string, error) {
124	requestID := r.Header.Get("X-Openstack-Request-Id")
125	return requestID, r.Err
126}
127