github.com/gophercloud/gophercloud@v1.11.0/openstack/identity/v3/groups/results.go (about) 1 package groups 2 3 import ( 4 "encoding/json" 5 6 "github.com/gophercloud/gophercloud" 7 "github.com/gophercloud/gophercloud/pagination" 8 ) 9 10 // Group helps manage related users. 11 type Group struct { 12 // Description describes the group purpose. 13 Description string `json:"description"` 14 15 // DomainID is the domain ID the group belongs to. 16 DomainID string `json:"domain_id"` 17 18 // ID is the unique ID of the group. 19 ID string `json:"id"` 20 21 // Extra is a collection of miscellaneous key/values. 22 Extra map[string]interface{} `json:"-"` 23 24 // Links contains referencing links to the group. 25 Links map[string]interface{} `json:"links"` 26 27 // Name is the name of the group. 28 Name string `json:"name"` 29 } 30 31 func (r *Group) UnmarshalJSON(b []byte) error { 32 type tmp Group 33 var s struct { 34 tmp 35 Extra map[string]interface{} `json:"extra"` 36 } 37 err := json.Unmarshal(b, &s) 38 if err != nil { 39 return err 40 } 41 *r = Group(s.tmp) 42 43 // Collect other fields and bundle them into Extra 44 // but only if a field titled "extra" wasn't sent. 45 if s.Extra != nil { 46 r.Extra = s.Extra 47 } else { 48 var result interface{} 49 err := json.Unmarshal(b, &result) 50 if err != nil { 51 return err 52 } 53 if resultMap, ok := result.(map[string]interface{}); ok { 54 r.Extra = gophercloud.RemainingKeys(Group{}, resultMap) 55 } 56 } 57 58 return err 59 } 60 61 type groupResult struct { 62 gophercloud.Result 63 } 64 65 // GetResult is the response from a Get operation. Call its Extract method 66 // to interpret it as a Group. 67 type GetResult struct { 68 groupResult 69 } 70 71 // CreateResult is the response from a Create operation. Call its Extract method 72 // to interpret it as a Group. 73 type CreateResult struct { 74 groupResult 75 } 76 77 // UpdateResult is the response from an Update operation. Call its Extract 78 // method to interpret it as a Group. 79 type UpdateResult struct { 80 groupResult 81 } 82 83 // DeleteResult is the response from a Delete operation. Call its ExtractErr to 84 // determine if the request succeeded or failed. 85 type DeleteResult struct { 86 gophercloud.ErrResult 87 } 88 89 // GroupPage is a single page of Group results. 90 type GroupPage struct { 91 pagination.LinkedPageBase 92 } 93 94 // IsEmpty determines whether or not a page of Groups contains any results. 95 func (r GroupPage) IsEmpty() (bool, error) { 96 if r.StatusCode == 204 { 97 return true, nil 98 } 99 100 groups, err := ExtractGroups(r) 101 return len(groups) == 0, err 102 } 103 104 // NextPageURL extracts the "next" link from the links section of the result. 105 func (r GroupPage) NextPageURL() (string, error) { 106 var s struct { 107 Links struct { 108 Next string `json:"next"` 109 Previous string `json:"previous"` 110 } `json:"links"` 111 } 112 err := r.ExtractInto(&s) 113 if err != nil { 114 return "", err 115 } 116 return s.Links.Next, err 117 } 118 119 // ExtractGroups returns a slice of Groups contained in a single page of results. 120 func ExtractGroups(r pagination.Page) ([]Group, error) { 121 var s struct { 122 Groups []Group `json:"groups"` 123 } 124 err := (r.(GroupPage)).ExtractInto(&s) 125 return s.Groups, err 126 } 127 128 // Extract interprets any group results as a Group. 129 func (r groupResult) Extract() (*Group, error) { 130 var s struct { 131 Group *Group `json:"group"` 132 } 133 err := r.ExtractInto(&s) 134 return s.Group, err 135 }