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