github.com/gophercloud/gophercloud@v1.11.0/openstack/compute/v2/flavors/requests.go (about) 1 package flavors 2 3 import ( 4 "github.com/gophercloud/gophercloud" 5 "github.com/gophercloud/gophercloud/pagination" 6 ) 7 8 // ListOptsBuilder allows extensions to add additional parameters to the 9 // List request. 10 type ListOptsBuilder interface { 11 ToFlavorListQuery() (string, error) 12 } 13 14 /* 15 AccessType maps to OpenStack's Flavor.is_public field. Although the is_public 16 field is boolean, the request options are ternary, which is why AccessType is 17 a string. The following values are allowed: 18 19 The AccessType arguement is optional, and if it is not supplied, OpenStack 20 returns the PublicAccess flavors. 21 */ 22 type AccessType string 23 24 const ( 25 // PublicAccess returns public flavors and private flavors associated with 26 // that project. 27 PublicAccess AccessType = "true" 28 29 // PrivateAccess (admin only) returns private flavors, across all projects. 30 PrivateAccess AccessType = "false" 31 32 // AllAccess (admin only) returns public and private flavors across all 33 // projects. 34 AllAccess AccessType = "None" 35 ) 36 37 /* 38 ListOpts filters the results returned by the List() function. 39 For example, a flavor with a minDisk field of 10 will not be returned if you 40 specify MinDisk set to 20. 41 42 Typically, software will use the last ID of the previous call to List to set 43 the Marker for the current call. 44 */ 45 type ListOpts struct { 46 // ChangesSince, if provided, instructs List to return only those things which 47 // have changed since the timestamp provided. 48 ChangesSince string `q:"changes-since"` 49 50 // MinDisk and MinRAM, if provided, elides flavors which do not meet your 51 // criteria. 52 MinDisk int `q:"minDisk"` 53 MinRAM int `q:"minRam"` 54 55 // SortDir allows to select sort direction. 56 // It can be "asc" or "desc" (default). 57 SortDir string `q:"sort_dir"` 58 59 // SortKey allows to sort by one of the flavors attributes. 60 // Default is flavorid. 61 SortKey string `q:"sort_key"` 62 63 // Marker and Limit control paging. 64 // Marker instructs List where to start listing from. 65 Marker string `q:"marker"` 66 67 // Limit instructs List to refrain from sending excessively large lists of 68 // flavors. 69 Limit int `q:"limit"` 70 71 // AccessType, if provided, instructs List which set of flavors to return. 72 // If IsPublic not provided, flavors for the current project are returned. 73 AccessType AccessType `q:"is_public"` 74 } 75 76 // ToFlavorListQuery formats a ListOpts into a query string. 77 func (opts ListOpts) ToFlavorListQuery() (string, error) { 78 q, err := gophercloud.BuildQueryString(opts) 79 return q.String(), err 80 } 81 82 // ListDetail instructs OpenStack to provide a list of flavors. 83 // You may provide criteria by which List curtails its results for easier 84 // processing. 85 func ListDetail(client *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager { 86 url := listURL(client) 87 if opts != nil { 88 query, err := opts.ToFlavorListQuery() 89 if err != nil { 90 return pagination.Pager{Err: err} 91 } 92 url += query 93 } 94 return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page { 95 return FlavorPage{pagination.LinkedPageBase{PageResult: r}} 96 }) 97 } 98 99 type CreateOptsBuilder interface { 100 ToFlavorCreateMap() (map[string]interface{}, error) 101 } 102 103 // CreateOpts specifies parameters used for creating a flavor. 104 type CreateOpts struct { 105 // Name is the name of the flavor. 106 Name string `json:"name" required:"true"` 107 108 // RAM is the memory of the flavor, measured in MB. 109 RAM int `json:"ram" required:"true"` 110 111 // VCPUs is the number of vcpus for the flavor. 112 VCPUs int `json:"vcpus" required:"true"` 113 114 // Disk the amount of root disk space, measured in GB. 115 Disk *int `json:"disk" required:"true"` 116 117 // ID is a unique ID for the flavor. 118 ID string `json:"id,omitempty"` 119 120 // Swap is the amount of swap space for the flavor, measured in MB. 121 Swap *int `json:"swap,omitempty"` 122 123 // RxTxFactor alters the network bandwidth of a flavor. 124 RxTxFactor float64 `json:"rxtx_factor,omitempty"` 125 126 // IsPublic flags a flavor as being available to all projects or not. 127 IsPublic *bool `json:"os-flavor-access:is_public,omitempty"` 128 129 // Ephemeral is the amount of ephemeral disk space, measured in GB. 130 Ephemeral *int `json:"OS-FLV-EXT-DATA:ephemeral,omitempty"` 131 132 // Description is a free form description of the flavor. Limited to 133 // 65535 characters in length. Only printable characters are allowed. 134 // New in version 2.55 135 Description string `json:"description,omitempty"` 136 } 137 138 // ToFlavorCreateMap constructs a request body from CreateOpts. 139 func (opts CreateOpts) ToFlavorCreateMap() (map[string]interface{}, error) { 140 return gophercloud.BuildRequestBody(opts, "flavor") 141 } 142 143 // Create requests the creation of a new flavor. 144 func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) { 145 b, err := opts.ToFlavorCreateMap() 146 if err != nil { 147 r.Err = err 148 return 149 } 150 resp, err := client.Post(createURL(client), b, &r.Body, &gophercloud.RequestOpts{ 151 OkCodes: []int{200, 201}, 152 }) 153 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 154 return 155 } 156 157 type UpdateOptsBuilder interface { 158 ToFlavorUpdateMap() (map[string]interface{}, error) 159 } 160 161 // UpdateOpts specifies parameters used for updating a flavor. 162 type UpdateOpts struct { 163 // Description is a free form description of the flavor. Limited to 164 // 65535 characters in length. Only printable characters are allowed. 165 // New in version 2.55 166 Description string `json:"description,omitempty"` 167 } 168 169 // ToFlavorUpdateMap constructs a request body from UpdateOpts. 170 func (opts UpdateOpts) ToFlavorUpdateMap() (map[string]interface{}, error) { 171 return gophercloud.BuildRequestBody(opts, "flavor") 172 } 173 174 // Update requests the update of a new flavor. 175 func Update(client *gophercloud.ServiceClient, id string, opts UpdateOptsBuilder) (r UpdateResult) { 176 b, err := opts.ToFlavorUpdateMap() 177 if err != nil { 178 r.Err = err 179 return 180 } 181 resp, err := client.Put(updateURL(client, id), b, &r.Body, &gophercloud.RequestOpts{ 182 OkCodes: []int{200}, 183 }) 184 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 185 return 186 } 187 188 // Get retrieves details of a single flavor. Use Extract to convert its 189 // result into a Flavor. 190 func Get(client *gophercloud.ServiceClient, id string) (r GetResult) { 191 resp, err := client.Get(getURL(client, id), &r.Body, nil) 192 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 193 return 194 } 195 196 // Delete deletes the specified flavor ID. 197 func Delete(client *gophercloud.ServiceClient, id string) (r DeleteResult) { 198 resp, err := client.Delete(deleteURL(client, id), nil) 199 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 200 return 201 } 202 203 // ListAccesses retrieves the tenants which have access to a flavor. 204 func ListAccesses(client *gophercloud.ServiceClient, id string) pagination.Pager { 205 url := accessURL(client, id) 206 207 return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page { 208 return AccessPage{pagination.SinglePageBase(r)} 209 }) 210 } 211 212 // AddAccessOptsBuilder allows extensions to add additional parameters to the 213 // AddAccess requests. 214 type AddAccessOptsBuilder interface { 215 ToFlavorAddAccessMap() (map[string]interface{}, error) 216 } 217 218 // AddAccessOpts represents options for adding access to a flavor. 219 type AddAccessOpts struct { 220 // Tenant is the project/tenant ID to grant access. 221 Tenant string `json:"tenant"` 222 } 223 224 // ToFlavorAddAccessMap constructs a request body from AddAccessOpts. 225 func (opts AddAccessOpts) ToFlavorAddAccessMap() (map[string]interface{}, error) { 226 return gophercloud.BuildRequestBody(opts, "addTenantAccess") 227 } 228 229 // AddAccess grants a tenant/project access to a flavor. 230 func AddAccess(client *gophercloud.ServiceClient, id string, opts AddAccessOptsBuilder) (r AddAccessResult) { 231 b, err := opts.ToFlavorAddAccessMap() 232 if err != nil { 233 r.Err = err 234 return 235 } 236 resp, err := client.Post(accessActionURL(client, id), b, &r.Body, &gophercloud.RequestOpts{ 237 OkCodes: []int{200}, 238 }) 239 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 240 return 241 } 242 243 // RemoveAccessOptsBuilder allows extensions to add additional parameters to the 244 // RemoveAccess requests. 245 type RemoveAccessOptsBuilder interface { 246 ToFlavorRemoveAccessMap() (map[string]interface{}, error) 247 } 248 249 // RemoveAccessOpts represents options for removing access to a flavor. 250 type RemoveAccessOpts struct { 251 // Tenant is the project/tenant ID to grant access. 252 Tenant string `json:"tenant"` 253 } 254 255 // ToFlavorRemoveAccessMap constructs a request body from RemoveAccessOpts. 256 func (opts RemoveAccessOpts) ToFlavorRemoveAccessMap() (map[string]interface{}, error) { 257 return gophercloud.BuildRequestBody(opts, "removeTenantAccess") 258 } 259 260 // RemoveAccess removes/revokes a tenant/project access to a flavor. 261 func RemoveAccess(client *gophercloud.ServiceClient, id string, opts RemoveAccessOptsBuilder) (r RemoveAccessResult) { 262 b, err := opts.ToFlavorRemoveAccessMap() 263 if err != nil { 264 r.Err = err 265 return 266 } 267 resp, err := client.Post(accessActionURL(client, id), b, &r.Body, &gophercloud.RequestOpts{ 268 OkCodes: []int{200}, 269 }) 270 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 271 return 272 } 273 274 // ExtraSpecs requests all the extra-specs for the given flavor ID. 275 func ListExtraSpecs(client *gophercloud.ServiceClient, flavorID string) (r ListExtraSpecsResult) { 276 resp, err := client.Get(extraSpecsListURL(client, flavorID), &r.Body, nil) 277 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 278 return 279 } 280 281 func GetExtraSpec(client *gophercloud.ServiceClient, flavorID string, key string) (r GetExtraSpecResult) { 282 resp, err := client.Get(extraSpecsGetURL(client, flavorID, key), &r.Body, nil) 283 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 284 return 285 } 286 287 // CreateExtraSpecsOptsBuilder allows extensions to add additional parameters to the 288 // CreateExtraSpecs requests. 289 type CreateExtraSpecsOptsBuilder interface { 290 ToFlavorExtraSpecsCreateMap() (map[string]interface{}, error) 291 } 292 293 // ExtraSpecsOpts is a map that contains key-value pairs. 294 type ExtraSpecsOpts map[string]string 295 296 // ToFlavorExtraSpecsCreateMap assembles a body for a Create request based on 297 // the contents of ExtraSpecsOpts. 298 func (opts ExtraSpecsOpts) ToFlavorExtraSpecsCreateMap() (map[string]interface{}, error) { 299 return map[string]interface{}{"extra_specs": opts}, nil 300 } 301 302 // CreateExtraSpecs will create or update the extra-specs key-value pairs for 303 // the specified Flavor. 304 func CreateExtraSpecs(client *gophercloud.ServiceClient, flavorID string, opts CreateExtraSpecsOptsBuilder) (r CreateExtraSpecsResult) { 305 b, err := opts.ToFlavorExtraSpecsCreateMap() 306 if err != nil { 307 r.Err = err 308 return 309 } 310 resp, err := client.Post(extraSpecsCreateURL(client, flavorID), b, &r.Body, &gophercloud.RequestOpts{ 311 OkCodes: []int{200}, 312 }) 313 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 314 return 315 } 316 317 // UpdateExtraSpecOptsBuilder allows extensions to add additional parameters to 318 // the Update request. 319 type UpdateExtraSpecOptsBuilder interface { 320 ToFlavorExtraSpecUpdateMap() (map[string]string, string, error) 321 } 322 323 // ToFlavorExtraSpecUpdateMap assembles a body for an Update request based on 324 // the contents of a ExtraSpecOpts. 325 func (opts ExtraSpecsOpts) ToFlavorExtraSpecUpdateMap() (map[string]string, string, error) { 326 if len(opts) != 1 { 327 err := gophercloud.ErrInvalidInput{} 328 err.Argument = "flavors.ExtraSpecOpts" 329 err.Info = "Must have 1 and only one key-value pair" 330 return nil, "", err 331 } 332 333 var key string 334 for k := range opts { 335 key = k 336 } 337 338 return opts, key, nil 339 } 340 341 // UpdateExtraSpec will updates the value of the specified flavor's extra spec 342 // for the key in opts. 343 func UpdateExtraSpec(client *gophercloud.ServiceClient, flavorID string, opts UpdateExtraSpecOptsBuilder) (r UpdateExtraSpecResult) { 344 b, key, err := opts.ToFlavorExtraSpecUpdateMap() 345 if err != nil { 346 r.Err = err 347 return 348 } 349 resp, err := client.Put(extraSpecUpdateURL(client, flavorID, key), b, &r.Body, &gophercloud.RequestOpts{ 350 OkCodes: []int{200}, 351 }) 352 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 353 return 354 } 355 356 // DeleteExtraSpec will delete the key-value pair with the given key for the given 357 // flavor ID. 358 func DeleteExtraSpec(client *gophercloud.ServiceClient, flavorID, key string) (r DeleteExtraSpecResult) { 359 resp, err := client.Delete(extraSpecDeleteURL(client, flavorID, key), &gophercloud.RequestOpts{ 360 OkCodes: []int{200}, 361 }) 362 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 363 return 364 }