github.com/gophercloud/gophercloud@v1.11.0/openstack/blockstorage/v3/qos/requests.go (about) 1 package qos 2 3 import ( 4 "github.com/gophercloud/gophercloud" 5 "github.com/gophercloud/gophercloud/pagination" 6 ) 7 8 type CreateOptsBuilder interface { 9 ToQoSCreateMap() (map[string]interface{}, error) 10 } 11 12 // ListOptsBuilder allows extensions to add additional parameters to the 13 // List request. 14 type ListOptsBuilder interface { 15 ToQoSListQuery() (string, error) 16 } 17 18 type QoSConsumer string 19 20 const ( 21 ConsumerFront QoSConsumer = "front-end" 22 ConsumerBack QoSConsumer = "back-end" 23 ConsumerBoth QoSConsumer = "both" 24 ) 25 26 // CreateOpts contains options for creating a QoS specification. 27 // This object is passed to the qos.Create function. 28 type CreateOpts struct { 29 // The name of the QoS spec 30 Name string `json:"name"` 31 // The consumer of the QoS spec. Possible values are 32 // both, front-end, back-end. 33 Consumer QoSConsumer `json:"consumer,omitempty"` 34 // Specs is a collection of miscellaneous key/values used to set 35 // specifications for the QoS 36 Specs map[string]string `json:"-"` 37 } 38 39 // ToQoSCreateMap assembles a request body based on the contents of a 40 // CreateOpts. 41 func (opts CreateOpts) ToQoSCreateMap() (map[string]interface{}, error) { 42 b, err := gophercloud.BuildRequestBody(opts, "qos_specs") 43 if err != nil { 44 return nil, err 45 } 46 47 if opts.Specs != nil { 48 if v, ok := b["qos_specs"].(map[string]interface{}); ok { 49 for key, value := range opts.Specs { 50 v[key] = value 51 } 52 } 53 } 54 55 return b, nil 56 } 57 58 // Create will create a new QoS based on the values in CreateOpts. To extract 59 // the QoS object from the response, call the Extract method on the 60 // CreateResult. 61 func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) { 62 b, err := opts.ToQoSCreateMap() 63 if err != nil { 64 r.Err = err 65 return 66 } 67 resp, err := client.Post(createURL(client), b, &r.Body, &gophercloud.RequestOpts{ 68 OkCodes: []int{200}, 69 }) 70 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 71 return 72 } 73 74 // DeleteOptsBuilder allows extensions to add additional parameters to the 75 // Delete request. 76 type DeleteOptsBuilder interface { 77 ToQoSDeleteQuery() (string, error) 78 } 79 80 // DeleteOpts contains options for deleting a QoS. This object is passed to 81 // the qos.Delete function. 82 type DeleteOpts struct { 83 // Delete a QoS specification even if it is in-use 84 Force bool `q:"force"` 85 } 86 87 // ToQoSDeleteQuery formats a DeleteOpts into a query string. 88 func (opts DeleteOpts) ToQoSDeleteQuery() (string, error) { 89 q, err := gophercloud.BuildQueryString(opts) 90 return q.String(), err 91 } 92 93 // Delete will delete the existing QoS with the provided ID. 94 func Delete(client *gophercloud.ServiceClient, id string, opts DeleteOptsBuilder) (r DeleteResult) { 95 url := deleteURL(client, id) 96 if opts != nil { 97 query, err := opts.ToQoSDeleteQuery() 98 if err != nil { 99 r.Err = err 100 return 101 } 102 url += query 103 } 104 resp, err := client.Delete(url, nil) 105 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 106 return 107 } 108 109 type ListOpts struct { 110 // Sort is Comma-separated list of sort keys and optional sort 111 // directions in the form of < key > [: < direction > ]. A valid 112 //direction is asc (ascending) or desc (descending). 113 Sort string `q:"sort"` 114 115 // Marker and Limit control paging. 116 // Marker instructs List where to start listing from. 117 Marker string `q:"marker"` 118 119 // Limit instructs List to refrain from sending excessively large lists of 120 // QoS. 121 Limit int `q:"limit"` 122 } 123 124 // ToQoSListQuery formats a ListOpts into a query string. 125 func (opts ListOpts) ToQoSListQuery() (string, error) { 126 q, err := gophercloud.BuildQueryString(opts) 127 return q.String(), err 128 } 129 130 // List instructs OpenStack to provide a list of QoS. 131 // You may provide criteria by which List curtails its results for easier 132 // processing. 133 func List(client *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager { 134 url := listURL(client) 135 if opts != nil { 136 query, err := opts.ToQoSListQuery() 137 if err != nil { 138 return pagination.Pager{Err: err} 139 } 140 url += query 141 } 142 return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page { 143 return QoSPage{pagination.LinkedPageBase{PageResult: r}} 144 }) 145 } 146 147 // Get retrieves details of a single qos. Use Extract to convert its 148 // result into a QoS. 149 func Get(client *gophercloud.ServiceClient, id string) (r GetResult) { 150 resp, err := client.Get(getURL(client, id), &r.Body, &gophercloud.RequestOpts{ 151 OkCodes: []int{200}, 152 }) 153 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 154 return 155 } 156 157 // CreateQosSpecsOptsBuilder allows extensions to add additional parameters to the 158 // CreateQosSpecs requests. 159 type CreateQosSpecsOptsBuilder interface { 160 ToQosSpecsCreateMap() (map[string]interface{}, error) 161 } 162 163 // UpdateOpts contains options for creating a QoS specification. 164 // This object is passed to the qos.Update function. 165 type UpdateOpts struct { 166 // The consumer of the QoS spec. Possible values are 167 // both, front-end, back-end. 168 Consumer QoSConsumer `json:"consumer,omitempty"` 169 // Specs is a collection of miscellaneous key/values used to set 170 // specifications for the QoS 171 Specs map[string]string `json:"-"` 172 } 173 174 type UpdateOptsBuilder interface { 175 ToQoSUpdateMap() (map[string]interface{}, error) 176 } 177 178 // ToQoSUpdateMap assembles a request body based on the contents of a 179 // UpdateOpts. 180 func (opts UpdateOpts) ToQoSUpdateMap() (map[string]interface{}, error) { 181 b, err := gophercloud.BuildRequestBody(opts, "qos_specs") 182 if err != nil { 183 return nil, err 184 } 185 186 if opts.Specs != nil { 187 if v, ok := b["qos_specs"].(map[string]interface{}); ok { 188 for key, value := range opts.Specs { 189 v[key] = value 190 } 191 } 192 } 193 194 return b, nil 195 } 196 197 // Update will update an existing QoS based on the values in UpdateOpts. 198 // To extract the QoS object from the response, call the Extract method 199 // on the UpdateResult. 200 func Update(client *gophercloud.ServiceClient, id string, opts UpdateOptsBuilder) (r updateResult) { 201 b, err := opts.ToQoSUpdateMap() 202 if err != nil { 203 r.Err = err 204 return 205 } 206 resp, err := client.Put(updateURL(client, id), b, &r.Body, &gophercloud.RequestOpts{ 207 OkCodes: []int{200}, 208 }) 209 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 210 return 211 } 212 213 // DeleteKeysOptsBuilder allows extensions to add additional parameters to the 214 // CreateExtraSpecs requests. 215 type DeleteKeysOptsBuilder interface { 216 ToDeleteKeysCreateMap() (map[string]interface{}, error) 217 } 218 219 // DeleteKeysOpts is a string slice that contains keys to be deleted. 220 type DeleteKeysOpts []string 221 222 // ToDeleteKeysCreateMap assembles a body for a Create request based on 223 // the contents of ExtraSpecsOpts. 224 func (opts DeleteKeysOpts) ToDeleteKeysCreateMap() (map[string]interface{}, error) { 225 return map[string]interface{}{"keys": opts}, nil 226 } 227 228 // DeleteKeys will delete the keys/specs from the specified QoS 229 func DeleteKeys(client *gophercloud.ServiceClient, qosID string, opts DeleteKeysOptsBuilder) (r DeleteResult) { 230 b, err := opts.ToDeleteKeysCreateMap() 231 if err != nil { 232 r.Err = err 233 return 234 } 235 resp, err := client.Put(deleteKeysURL(client, qosID), b, nil, &gophercloud.RequestOpts{ 236 OkCodes: []int{202}, 237 }) 238 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 239 return 240 } 241 242 // AssociateOpitsBuilder allows extensions to define volume type id 243 // to the associate query 244 type AssociateOptsBuilder interface { 245 ToQosAssociateQuery() (string, error) 246 } 247 248 // AssociateOpts contains options for associating a QoS with a 249 // volume type 250 type AssociateOpts struct { 251 VolumeTypeID string `q:"vol_type_id" required:"true"` 252 } 253 254 // ToQosAssociateQuery formats an AssociateOpts into a query string 255 func (opts AssociateOpts) ToQosAssociateQuery() (string, error) { 256 q, err := gophercloud.BuildQueryString(opts) 257 return q.String(), err 258 } 259 260 // Associate will associate a qos with a volute type 261 func Associate(client *gophercloud.ServiceClient, qosID string, opts AssociateOptsBuilder) (r AssociateResult) { 262 url := associateURL(client, qosID) 263 query, err := opts.ToQosAssociateQuery() 264 if err != nil { 265 r.Err = err 266 return 267 } 268 url += query 269 270 resp, err := client.Get(url, nil, &gophercloud.RequestOpts{ 271 OkCodes: []int{202}, 272 }) 273 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 274 return 275 } 276 277 // DisassociateOpitsBuilder allows extensions to define volume type id 278 // to the disassociate query 279 type DisassociateOptsBuilder interface { 280 ToQosDisassociateQuery() (string, error) 281 } 282 283 // DisassociateOpts contains options for disassociating a QoS from a 284 // volume type 285 type DisassociateOpts struct { 286 VolumeTypeID string `q:"vol_type_id" required:"true"` 287 } 288 289 // ToQosDisassociateQuery formats a DisassociateOpts into a query string 290 func (opts DisassociateOpts) ToQosDisassociateQuery() (string, error) { 291 q, err := gophercloud.BuildQueryString(opts) 292 return q.String(), err 293 } 294 295 // Disassociate will disassociate a qos from a volute type 296 func Disassociate(client *gophercloud.ServiceClient, qosID string, opts DisassociateOptsBuilder) (r DisassociateResult) { 297 url := disassociateURL(client, qosID) 298 query, err := opts.ToQosDisassociateQuery() 299 if err != nil { 300 r.Err = err 301 return 302 } 303 url += query 304 305 resp, err := client.Get(url, nil, &gophercloud.RequestOpts{ 306 OkCodes: []int{202}, 307 }) 308 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 309 return 310 } 311 312 // DisassociateAll will disassociate a qos from all volute types 313 func DisassociateAll(client *gophercloud.ServiceClient, qosID string) (r DisassociateAllResult) { 314 resp, err := client.Get(disassociateAllURL(client, qosID), nil, &gophercloud.RequestOpts{ 315 OkCodes: []int{202}, 316 }) 317 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 318 return 319 } 320 321 // ListAssociations retrieves the associations of a QoS. 322 func ListAssociations(client *gophercloud.ServiceClient, qosID string) pagination.Pager { 323 url := listAssociationsURL(client, qosID) 324 325 return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page { 326 return AssociationPage{pagination.SinglePageBase(r)} 327 }) 328 }