github.com/vnpaycloud-console/gophercloud/v2@v2.0.5/openstack/compute/v2/flavors/results.go (about) 1 package flavors 2 3 import ( 4 "encoding/json" 5 "strconv" 6 7 "github.com/vnpaycloud-console/gophercloud/v2" 8 "github.com/vnpaycloud-console/gophercloud/v2/pagination" 9 ) 10 11 type commonResult struct { 12 gophercloud.Result 13 } 14 15 // CreateResult is the response of a Get operations. Call its Extract method to 16 // interpret it as a Flavor. 17 type CreateResult struct { 18 commonResult 19 } 20 21 // UpdateResult is the response of a Put operation. Call its Extract method to 22 // interpret it as a Flavor. 23 type UpdateResult struct { 24 commonResult 25 } 26 27 // GetResult is the response of a Get operations. Call its Extract method to 28 // interpret it as a Flavor. 29 type GetResult struct { 30 commonResult 31 } 32 33 // DeleteResult is the result from a Delete operation. Call its ExtractErr 34 // method to determine if the call succeeded or failed. 35 type DeleteResult struct { 36 gophercloud.ErrResult 37 } 38 39 // Extract provides access to the individual Flavor returned by the Get and 40 // Create functions. 41 func (r commonResult) Extract() (*Flavor, error) { 42 var s struct { 43 Flavor *Flavor `json:"flavor"` 44 } 45 err := r.ExtractInto(&s) 46 return s.Flavor, err 47 } 48 49 // Flavor represent (virtual) hardware configurations for server resources 50 // in a region. 51 type Flavor struct { 52 // ID is the flavor's unique ID. 53 ID string `json:"id"` 54 55 // Disk is the amount of root disk, measured in GB. 56 Disk int `json:"disk"` 57 58 // RAM is the amount of memory, measured in MB. 59 RAM int `json:"ram"` 60 61 // Name is the name of the flavor. 62 Name string `json:"name"` 63 64 // RxTxFactor describes bandwidth alterations of the flavor. 65 RxTxFactor float64 `json:"rxtx_factor"` 66 67 // Swap is the amount of swap space, measured in MB. 68 Swap int `json:"-"` 69 70 // VCPUs indicates how many (virtual) CPUs are available for this flavor. 71 VCPUs int `json:"vcpus"` 72 73 // IsPublic indicates whether the flavor is public. 74 IsPublic bool `json:"os-flavor-access:is_public"` 75 76 // Ephemeral is the amount of ephemeral disk space, measured in GB. 77 Ephemeral int `json:"OS-FLV-EXT-DATA:ephemeral"` 78 79 // Description is a free form description of the flavor. Limited to 80 // 65535 characters in length. Only printable characters are allowed. 81 // New in version 2.55 82 Description string `json:"description"` 83 84 // Properties is a dictionary of the flavor’s extra-specs key-and-value 85 // pairs. This will only be included if the user is allowed by policy to 86 // index flavor extra_specs 87 // New in version 2.61 88 ExtraSpecs map[string]string `json:"extra_specs"` 89 } 90 91 func (r *Flavor) UnmarshalJSON(b []byte) error { 92 type tmp Flavor 93 var s struct { 94 tmp 95 Swap any `json:"swap"` 96 } 97 err := json.Unmarshal(b, &s) 98 if err != nil { 99 return err 100 } 101 102 *r = Flavor(s.tmp) 103 104 switch t := s.Swap.(type) { 105 case float64: 106 r.Swap = int(t) 107 case string: 108 switch t { 109 case "": 110 r.Swap = 0 111 default: 112 swap, err := strconv.ParseFloat(t, 64) 113 if err != nil { 114 return err 115 } 116 r.Swap = int(swap) 117 } 118 } 119 120 return nil 121 } 122 123 // FlavorPage contains a single page of all flavors from a ListDetails call. 124 type FlavorPage struct { 125 pagination.LinkedPageBase 126 } 127 128 // IsEmpty determines if a FlavorPage contains any results. 129 func (page FlavorPage) IsEmpty() (bool, error) { 130 if page.StatusCode == 204 { 131 return true, nil 132 } 133 134 flavors, err := ExtractFlavors(page) 135 return len(flavors) == 0, err 136 } 137 138 // NextPageURL uses the response's embedded link reference to navigate to the 139 // next page of results. 140 func (page FlavorPage) NextPageURL() (string, error) { 141 var s struct { 142 Links []gophercloud.Link `json:"flavors_links"` 143 } 144 err := page.ExtractInto(&s) 145 if err != nil { 146 return "", err 147 } 148 return gophercloud.ExtractNextURL(s.Links) 149 } 150 151 // ExtractFlavors provides access to the list of flavors in a page acquired 152 // from the ListDetail operation. 153 func ExtractFlavors(r pagination.Page) ([]Flavor, error) { 154 var s struct { 155 Flavors []Flavor `json:"flavors"` 156 } 157 err := (r.(FlavorPage)).ExtractInto(&s) 158 return s.Flavors, err 159 } 160 161 // AccessPage contains a single page of all FlavorAccess entries for a flavor. 162 type AccessPage struct { 163 pagination.SinglePageBase 164 } 165 166 // IsEmpty indicates whether an AccessPage is empty. 167 func (page AccessPage) IsEmpty() (bool, error) { 168 if page.StatusCode == 204 { 169 return true, nil 170 } 171 172 v, err := ExtractAccesses(page) 173 return len(v) == 0, err 174 } 175 176 // ExtractAccesses interprets a page of results as a slice of FlavorAccess. 177 func ExtractAccesses(r pagination.Page) ([]FlavorAccess, error) { 178 var s struct { 179 FlavorAccesses []FlavorAccess `json:"flavor_access"` 180 } 181 err := (r.(AccessPage)).ExtractInto(&s) 182 return s.FlavorAccesses, err 183 } 184 185 type accessResult struct { 186 gophercloud.Result 187 } 188 189 // AddAccessResult is the response of an AddAccess operation. Call its 190 // Extract method to interpret it as a slice of FlavorAccess. 191 type AddAccessResult struct { 192 accessResult 193 } 194 195 // RemoveAccessResult is the response of a RemoveAccess operation. Call its 196 // Extract method to interpret it as a slice of FlavorAccess. 197 type RemoveAccessResult struct { 198 accessResult 199 } 200 201 // Extract provides access to the result of an access create or delete. 202 // The result will be all accesses that the flavor has. 203 func (r accessResult) Extract() ([]FlavorAccess, error) { 204 var s struct { 205 FlavorAccesses []FlavorAccess `json:"flavor_access"` 206 } 207 err := r.ExtractInto(&s) 208 return s.FlavorAccesses, err 209 } 210 211 // FlavorAccess represents an ACL of tenant access to a specific Flavor. 212 type FlavorAccess struct { 213 // FlavorID is the unique ID of the flavor. 214 FlavorID string `json:"flavor_id"` 215 216 // TenantID is the unique ID of the tenant. 217 TenantID string `json:"tenant_id"` 218 } 219 220 // Extract interprets any extraSpecsResult as ExtraSpecs, if possible. 221 func (r extraSpecsResult) Extract() (map[string]string, error) { 222 var s struct { 223 ExtraSpecs map[string]string `json:"extra_specs"` 224 } 225 err := r.ExtractInto(&s) 226 return s.ExtraSpecs, err 227 } 228 229 // extraSpecsResult contains the result of a call for (potentially) multiple 230 // key-value pairs. Call its Extract method to interpret it as a 231 // map[string]interface. 232 type extraSpecsResult struct { 233 gophercloud.Result 234 } 235 236 // ListExtraSpecsResult contains the result of a Get operation. Call its Extract 237 // method to interpret it as a map[string]interface. 238 type ListExtraSpecsResult struct { 239 extraSpecsResult 240 } 241 242 // CreateExtraSpecResult contains the result of a Create operation. Call its 243 // Extract method to interpret it as a map[string]interface. 244 type CreateExtraSpecsResult struct { 245 extraSpecsResult 246 } 247 248 // extraSpecResult contains the result of a call for individual a single 249 // key-value pair. 250 type extraSpecResult struct { 251 gophercloud.Result 252 } 253 254 // GetExtraSpecResult contains the result of a Get operation. Call its Extract 255 // method to interpret it as a map[string]interface. 256 type GetExtraSpecResult struct { 257 extraSpecResult 258 } 259 260 // UpdateExtraSpecResult contains the result of an Update operation. Call its 261 // Extract method to interpret it as a map[string]interface. 262 type UpdateExtraSpecResult struct { 263 extraSpecResult 264 } 265 266 // DeleteExtraSpecResult contains the result of a Delete operation. Call its 267 // ExtractErr method to determine if the call succeeded or failed. 268 type DeleteExtraSpecResult struct { 269 gophercloud.ErrResult 270 } 271 272 // Extract interprets any extraSpecResult as an ExtraSpec, if possible. 273 func (r extraSpecResult) Extract() (map[string]string, error) { 274 var s map[string]string 275 err := r.ExtractInto(&s) 276 return s, err 277 }