This repository has been archived by the owner on Aug 31, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 34
/
results.go
190 lines (168 loc) · 5.11 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package shares
import (
"encoding/json"
"time"
"github.com/huaweicloud/golangsdk"
"github.com/huaweicloud/golangsdk/pagination"
)
type Share struct {
//Details about the source backup
Backup Backup `json:"backup"`
//Backup ID
BackupID string `json:"backup_id"`
//Backup share ID
ID string `json:"id"`
//ID of the project with which the backup is shared
ToProjectID string `json:"to_project_id"`
//ID of the project that shares the backup
FromProjectID string `json:"from_project_id"`
//Creation time of the backup share
CreatedAt time.Time `json:"-"`
//Update time of the backup share
UpdatedAt time.Time `json:"-"`
//Whether the backup has been deleted
Deleted string `json:"deleted"`
//Deletion time
DeletedAt time.Time `json:"-"`
}
type Backup struct {
//Backup ID
ID string `json:"id"`
//Backup name
Name string `json:"name"`
//Backup status
Status string `json:"status"`
//Backup description
Description string `json:"description"`
//AZ where the backup resides
AvailabilityZone string `json:"availability_zone"`
//Source volume ID of the backup
VolumeID string `json:"volume_id"`
//Cause of the backup failure
FailReason string `json:"fail_reason"`
//Backup size
Size int `json:"size"`
//Number of objects on OBS for the disk data
ObjectCount int `json:"object_count"`
//Container of the backup
Container string `json:"container"`
//Backup creation time
CreatedAt time.Time `json:"-"`
//Backup metadata
ServiceMetadata string `json:"service_metadata"`
//Time when the backup was updated
UpdatedAt time.Time `json:"-"`
//Current time
DataTimeStamp time.Time `json:"-"`
//Whether a dependent backup exists
DependentBackups bool `json:"has_dependent_backups"`
//ID of the snapshot associated with the backup
SnapshotID string `json:"snapshot_id"`
//Whether the backup is an incremental backup
IsIncremental bool `json:"is_incremental"`
}
type commonResult struct {
golangsdk.Result
}
// SharePage is the page returned by a pager when traversing over a
// collection of Shares.
type SharePage struct {
pagination.LinkedPageBase
}
// Extract is a function that accepts a result and extracts shares.
func (r commonResult) Extract() ([]Share, error) {
var s struct {
Share []Share `json:"shared"`
}
err := r.ExtractInto(&s)
return s.Share, err
}
// ExtractShare is a function that accepts a result and extracts a share.
func (r commonResult) ExtractShare() (*Share, error) {
var s struct {
Share *Share `json:"shared"`
}
err := r.ExtractInto(&s)
return s.Share, err
}
// NextPageURL is invoked when a paginated collection of Shares has reached
// the end of a page and the pager seeks to traverse over a new one. In order
// to do this, it needs to construct the next page's URL.
func (r SharePage) NextPageURL() (string, error) {
var s struct {
Links []golangsdk.Link `json:"shared_links"`
}
err := r.ExtractInto(&s)
if err != nil {
return "", err
}
return golangsdk.ExtractNextURL(s.Links)
}
// IsEmpty checks whether a SharePage struct is empty.
func (r SharePage) IsEmpty() (bool, error) {
is, err := ExtractShareList(r)
return len(is) == 0, err
}
// ExtractShareList accepts a Page struct, specifically a SharePage struct,
// and extracts the elements into a slice of Shares struct. In other words,
// a generic collection is mapped into a relevant slice.
func ExtractShareList(r pagination.Page) ([]Share, error) {
var s struct {
Shares []Share `json:"shared"`
}
err := (r.(SharePage)).ExtractInto(&s)
return s.Shares, err
}
// CreateResult represents the result of a create operation. Call its Extract
// method to interpret it as a Share.
type CreateResult struct {
commonResult
}
// GetResult represents the result of a get operation. Call its ExtractShare
// method to interpret it as a Share.
type GetResult struct {
commonResult
}
// DeleteResult represents the result of a delete operation. Call its ExtractErr
// method to determine if the request succeeded or failed.
type DeleteResult struct {
golangsdk.ErrResult
}
// UnmarshalJSON overrides the default, to convert the JSON API response into our Backup struct
func (r *Backup) UnmarshalJSON(b []byte) error {
type tmp Backup
var s struct {
tmp
CreatedAt golangsdk.JSONRFC3339MilliNoZ `json:"created_at"`
UpdatedAt golangsdk.JSONRFC3339MilliNoZ `json:"updated_at"`
DataTimeStamp golangsdk.JSONRFC3339MilliNoZ `json:"data_timestamp"`
}
err := json.Unmarshal(b, &s)
if err != nil {
return err
}
*r = Backup(s.tmp)
r.CreatedAt = time.Time(s.CreatedAt)
r.UpdatedAt = time.Time(s.UpdatedAt)
r.DataTimeStamp = time.Time(s.DataTimeStamp)
return nil
}
// UnmarshalJSON overrides the default, to convert the JSON API response into our Share struct
func (r *Share) UnmarshalJSON(b []byte) error {
type tmp Share
var s struct {
tmp
CreatedAt golangsdk.JSONRFC3339MilliNoZ `json:"created_at"`
UpdatedAt golangsdk.JSONRFC3339MilliNoZ `json:"updated_at"`
DeletedAt golangsdk.JSONRFC3339MilliNoZ `json:"deleted_at"`
}
err := json.Unmarshal(b, &s)
if err != nil {
return err
}
*r = Share(s.tmp)
r.CreatedAt = time.Time(s.CreatedAt)
r.UpdatedAt = time.Time(s.UpdatedAt)
r.DeletedAt = time.Time(s.DeletedAt)
return nil
}