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