jrperritt | c5c590a | 2016-11-04 14:41:15 -0500 | [diff] [blame] | 1 | package members |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "time" |
| 6 | |
| 7 | "github.com/gophercloud/gophercloud" |
| 8 | "github.com/gophercloud/gophercloud/pagination" |
| 9 | ) |
| 10 | |
| 11 | // Member model |
| 12 | type Member struct { |
| 13 | CreatedAt time.Time `json:"-"` |
| 14 | ImageID string `json:"image_id"` |
| 15 | MemberID string `json:"member_id"` |
| 16 | Schema string `json:"schema"` |
| 17 | Status string `json:"status"` |
| 18 | UpdatedAt time.Time `json:"-"` |
| 19 | } |
| 20 | |
| 21 | func (s *Member) UnmarshalJSON(b []byte) error { |
| 22 | type tmp Member |
| 23 | var p *struct { |
| 24 | tmp |
| 25 | CreatedAt string `json:"created_at"` |
| 26 | UpdatedAt string `json:"updated_at"` |
| 27 | } |
| 28 | err := json.Unmarshal(b, &p) |
| 29 | if err != nil { |
| 30 | return err |
| 31 | } |
| 32 | |
| 33 | *s = Member(p.tmp) |
| 34 | s.CreatedAt, err = time.Parse(time.RFC3339, p.CreatedAt) |
| 35 | if err != nil { |
| 36 | return err |
| 37 | } |
| 38 | s.UpdatedAt, err = time.Parse(time.RFC3339, p.UpdatedAt) |
| 39 | return err |
| 40 | } |
| 41 | |
| 42 | // Extract Member model from request if possible |
| 43 | func (r commonResult) Extract() (*Member, error) { |
| 44 | var s *Member |
| 45 | err := r.ExtractInto(&s) |
| 46 | return s, err |
| 47 | } |
| 48 | |
| 49 | // MemberPage is a single page of Members results. |
| 50 | type MemberPage struct { |
| 51 | pagination.SinglePageBase |
| 52 | } |
| 53 | |
| 54 | // ExtractMembers returns a slice of Members contained in a single page of results. |
| 55 | func ExtractMembers(r pagination.Page) ([]Member, error) { |
| 56 | var s struct { |
| 57 | Members []Member `json:"members"` |
| 58 | } |
| 59 | err := r.(MemberPage).ExtractInto(&s) |
| 60 | return s.Members, err |
| 61 | } |
| 62 | |
| 63 | // IsEmpty determines whether or not a page of Members contains any results. |
| 64 | func (r MemberPage) IsEmpty() (bool, error) { |
| 65 | members, err := ExtractMembers(r) |
| 66 | return len(members) == 0, err |
| 67 | } |
| 68 | |
| 69 | type commonResult struct { |
| 70 | gophercloud.Result |
| 71 | } |
| 72 | |
| 73 | // CreateResult result model |
| 74 | type CreateResult struct { |
| 75 | commonResult |
| 76 | } |
| 77 | |
| 78 | // DetailsResult model |
| 79 | type DetailsResult struct { |
| 80 | commonResult |
| 81 | } |
| 82 | |
| 83 | // UpdateResult model |
| 84 | type UpdateResult struct { |
| 85 | commonResult |
| 86 | } |
| 87 | |
| 88 | // DeleteResult model |
| 89 | type DeleteResult struct { |
| 90 | gophercloud.ErrResult |
| 91 | } |