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