Skip to content
This repository was archived by the owner on Aug 31, 2021. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
42 changes: 42 additions & 0 deletions openstack/blockstorage/extensions/quotasets/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
Package quotasets enables retrieving and managing Block Storage quotas.

Example to Get a Quota Set

quotaset, err := quotasets.Get(blockStorageClient, "project-id").Extract()
if err != nil {
panic(err)
}

fmt.Printf("%+v\n", quotaset)

Example to Get Quota Set Usage

quotaset, err := quotasets.GetUsage(blockStorageClient, "project-id").Extract()
if err != nil {
panic(err)
}

fmt.Printf("%+v\n", quotaset)

Example to Update a Quota Set

updateOpts := quotasets.UpdateOpts{
Volumes: golangsdk.IntToPointer(100),
}

quotaset, err := quotasets.Update(blockStorageClient, "project-id", updateOpts).Extract()
if err != nil {
panic(err)
}

fmt.Printf("%+v\n", quotaset)

Example to Delete a Quota Set

err := quotasets.Delete(blockStorageClient, "project-id").ExtractErr()
if err != nil {
panic(err)
}
*/
package quotasets
94 changes: 94 additions & 0 deletions openstack/blockstorage/extensions/quotasets/requests.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package quotasets

import (
"fmt"

"github.com/huaweicloud/golangsdk"
)

// Get returns public data about a previously created QuotaSet.
func Get(client *golangsdk.ServiceClient, projectID string) (r GetResult) {
_, r.Err = client.Get(getURL(client, projectID), &r.Body, nil)
return
}

// GetDefaults returns public data about the project's default block storage quotas.
func GetDefaults(client *golangsdk.ServiceClient, projectID string) (r GetResult) {
_, r.Err = client.Get(getDefaultsURL(client, projectID), &r.Body, nil)
return
}

// GetUsage returns detailed public data about a previously created QuotaSet.
func GetUsage(client *golangsdk.ServiceClient, projectID string) (r GetUsageResult) {
u := fmt.Sprintf("%s?usage=true", getURL(client, projectID))
_, r.Err = client.Get(u, &r.Body, nil)
return
}

// Updates the quotas for the given projectID and returns the new QuotaSet.
func Update(client *golangsdk.ServiceClient, projectID string, opts UpdateOptsBuilder) (r UpdateResult) {
b, err := opts.ToBlockStorageQuotaUpdateMap()
if err != nil {
r.Err = err
return
}

_, r.Err = client.Put(updateURL(client, projectID), b, &r.Body, &golangsdk.RequestOpts{
OkCodes: []int{200},
})
return r
}

// UpdateOptsBuilder enables extensins to add parameters to the update request.
type UpdateOptsBuilder interface {
// Extra specific name to prevent collisions with interfaces for other quotas
// (e.g. neutron)
ToBlockStorageQuotaUpdateMap() (map[string]interface{}, error)
}

// ToBlockStorageQuotaUpdateMap builds the update options into a serializable
// format.
func (opts UpdateOpts) ToBlockStorageQuotaUpdateMap() (map[string]interface{}, error) {
return golangsdk.BuildRequestBody(opts, "quota_set")
}

// Options for Updating the quotas of a Tenant.
// All int-values are pointers so they can be nil if they are not needed.
// You can use gopercloud.IntToPointer() for convenience
type UpdateOpts struct {
// Volumes is the number of volumes that are allowed for each project.
Volumes *int `json:"volumes,omitempty"`

// Snapshots is the number of snapshots that are allowed for each project.
Snapshots *int `json:"snapshots,omitempty"`

// Gigabytes is the size (GB) of volumes and snapshots that are allowed for
// each project.
Gigabytes *int `json:"gigabytes,omitempty"`

// PerVolumeGigabytes is the size (GB) of volumes and snapshots that are
// allowed for each project and the specifed volume type.
PerVolumeGigabytes *int `json:"per_volume_gigabytes,omitempty"`

// Backups is the number of backups that are allowed for each project.
Backups *int `json:"backups,omitempty"`

// BackupGigabytes is the size (GB) of backups that are allowed for each
// project.
BackupGigabytes *int `json:"backup_gigabytes,omitempty"`

// Groups is the number of groups that are allowed for each project.
Groups *int `json:"groups,omitempty"`

// Force will update the quotaset even if the quota has already been used
// and the reserved quota exceeds the new quota.
Force bool `json:"force,omitempty"`
}

// Resets the quotas for the given tenant to their default values.
func Delete(client *golangsdk.ServiceClient, projectID string) (r DeleteResult) {
_, r.Err = client.Delete(updateURL(client, projectID), &golangsdk.RequestOpts{
OkCodes: []int{200},
})
return
}
165 changes: 165 additions & 0 deletions openstack/blockstorage/extensions/quotasets/results.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
package quotasets

import (
"github.com/huaweicloud/golangsdk"
"github.com/huaweicloud/golangsdk/pagination"
)

// QuotaSet is a set of operational limits that allow for control of block
// storage usage.
type QuotaSet struct {
// ID is project associated with this QuotaSet.
ID string `json:"id"`

// Volumes is the number of volumes that are allowed for each project.
Volumes int `json:"volumes"`

// Snapshots is the number of snapshots that are allowed for each project.
Snapshots int `json:"snapshots"`

// Gigabytes is the size (GB) of volumes and snapshots that are allowed for
// each project.
Gigabytes int `json:"gigabytes"`

// PerVolumeGigabytes is the size (GB) of volumes and snapshots that are
// allowed for each project and the specifed volume type.
PerVolumeGigabytes int `json:"per_volume_gigabytes"`

// Backups is the number of backups that are allowed for each project.
Backups int `json:"backups"`

// BackupGigabytes is the size (GB) of backups that are allowed for each
// project.
BackupGigabytes int `json:"backup_gigabytes"`
}

// QuotaUsageSet represents details of both operational limits of block
// storage resources and the current usage of those resources.
type QuotaUsageSet struct {
// ID is the project ID associated with this QuotaUsageSet.
ID string `json:"id"`

// Volumes is the volume usage information for this project, including
// in_use, limit, reserved and allocated attributes. Note: allocated
// attribute is available only when nested quota is enabled.
Volumes QuotaUsage `json:"volumes"`

// Snapshots is the snapshot usage information for this project, including
// in_use, limit, reserved and allocated attributes. Note: allocated
// attribute is available only when nested quota is enabled.
Snapshots QuotaUsage `json:"snapshots"`

// Gigabytes is the size (GB) usage information of volumes and snapshots
// for this project, including in_use, limit, reserved and allocated
// attributes. Note: allocated attribute is available only when nested
// quota is enabled.
Gigabytes QuotaUsage `json:"gigabytes"`

// PerVolumeGigabytes is the size (GB) usage information for each volume,
// including in_use, limit, reserved and allocated attributes. Note:
// allocated attribute is available only when nested quota is enabled and
// only limit is meaningful here.
PerVolumeGigabytes QuotaUsage `json:"per_volume_gigabytes"`

// Backups is the backup usage information for this project, including
// in_use, limit, reserved and allocated attributes. Note: allocated
// attribute is available only when nested quota is enabled.
Backups QuotaUsage `json:"backups"`

// BackupGigabytes is the size (GB) usage information of backup for this
// project, including in_use, limit, reserved and allocated attributes.
// Note: allocated attribute is available only when nested quota is
// enabled.
BackupGigabytes QuotaUsage `json:"backup_gigabytes"`
}

// QuotaUsage is a set of details about a single operational limit that allows
// for control of block storage usage.
type QuotaUsage struct {
// InUse is the current number of provisioned resources of the given type.
InUse int `json:"in_use"`

// Allocated is the current number of resources of a given type allocated
// for use. It is only available when nested quota is enabled.
Allocated int `json:"allocated"`

// Reserved is a transitional state when a claim against quota has been made
// but the resource is not yet fully online.
Reserved int `json:"reserved"`

// Limit is the maximum number of a given resource that can be
// allocated/provisioned. This is what "quota" usually refers to.
Limit int `json:"limit"`
}

// QuotaSetPage stores a single page of all QuotaSet results from a List call.
type QuotaSetPage struct {
pagination.SinglePageBase
}

// IsEmpty determines whether or not a QuotaSetsetPage is empty.
func (r QuotaSetPage) IsEmpty() (bool, error) {
ks, err := ExtractQuotaSets(r)
return len(ks) == 0, err
}

// ExtractQuotaSets interprets a page of results as a slice of QuotaSets.
func ExtractQuotaSets(r pagination.Page) ([]QuotaSet, error) {
var s struct {
QuotaSets []QuotaSet `json:"quotas"`
}
err := (r.(QuotaSetPage)).ExtractInto(&s)
return s.QuotaSets, err
}

type quotaResult struct {
golangsdk.Result
}

// Extract is a method that attempts to interpret any QuotaSet resource response
// as a QuotaSet struct.
func (r quotaResult) Extract() (*QuotaSet, error) {
var s struct {
QuotaSet *QuotaSet `json:"quota_set"`
}
err := r.ExtractInto(&s)
return s.QuotaSet, err
}

// GetResult is the response from a Get operation. Call its Extract method to
// interpret it as a QuotaSet.
type GetResult struct {
quotaResult
}

// UpdateResult is the response from a Update operation. Call its Extract method
// to interpret it as a QuotaSet.
type UpdateResult struct {
quotaResult
}

type quotaUsageResult struct {
golangsdk.Result
}

// GetUsageResult is the response from a Get operation. Call its Extract
// method to interpret it as a QuotaSet.
type GetUsageResult struct {
quotaUsageResult
}

// Extract is a method that attempts to interpret any QuotaUsageSet resource
// response as a set of QuotaUsageSet structs.
func (r quotaUsageResult) Extract() (QuotaUsageSet, error) {
var s struct {
QuotaUsageSet QuotaUsageSet `json:"quota_set"`
}
err := r.ExtractInto(&s)
return s.QuotaUsageSet, err
}

// DeleteResult is the response from a Delete operation. Call its ExtractErr
// method to determine if the request succeeded or failed.
type DeleteResult struct {
golangsdk.ErrResult
}
2 changes: 2 additions & 0 deletions openstack/blockstorage/extensions/quotasets/testing/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// quotasets unit tests
package testing
Loading