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