github.com/gophercloud/gophercloud@v1.11.0/openstack/blockstorage/extensions/quotasets/requests.go (about)

     1  package quotasets
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/gophercloud/gophercloud"
     7  )
     8  
     9  // Get returns public data about a previously created QuotaSet.
    10  func Get(client *gophercloud.ServiceClient, projectID string) (r GetResult) {
    11  	resp, err := client.Get(getURL(client, projectID), &r.Body, nil)
    12  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
    13  	return
    14  }
    15  
    16  // GetDefaults returns public data about the project's default block storage quotas.
    17  func GetDefaults(client *gophercloud.ServiceClient, projectID string) (r GetResult) {
    18  	resp, err := client.Get(getDefaultsURL(client, projectID), &r.Body, nil)
    19  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
    20  	return
    21  }
    22  
    23  // GetUsage returns detailed public data about a previously created QuotaSet.
    24  func GetUsage(client *gophercloud.ServiceClient, projectID string) (r GetUsageResult) {
    25  	u := fmt.Sprintf("%s?usage=true", getURL(client, projectID))
    26  	resp, err := client.Get(u, &r.Body, nil)
    27  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
    28  	return
    29  }
    30  
    31  // Updates the quotas for the given projectID and returns the new QuotaSet.
    32  func Update(client *gophercloud.ServiceClient, projectID string, opts UpdateOptsBuilder) (r UpdateResult) {
    33  	b, err := opts.ToBlockStorageQuotaUpdateMap()
    34  	if err != nil {
    35  		r.Err = err
    36  		return
    37  	}
    38  
    39  	resp, err := client.Put(updateURL(client, projectID), b, &r.Body, &gophercloud.RequestOpts{
    40  		OkCodes: []int{200},
    41  	})
    42  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
    43  	return
    44  }
    45  
    46  // UpdateOptsBuilder enables extensions to add parameters to the update request.
    47  type UpdateOptsBuilder interface {
    48  	// Extra specific name to prevent collisions with interfaces for other quotas
    49  	// (e.g. neutron)
    50  	ToBlockStorageQuotaUpdateMap() (map[string]interface{}, error)
    51  }
    52  
    53  // ToBlockStorageQuotaUpdateMap builds the update options into a serializable
    54  // format.
    55  func (opts UpdateOpts) ToBlockStorageQuotaUpdateMap() (map[string]interface{}, error) {
    56  	b, err := gophercloud.BuildRequestBody(opts, "quota_set")
    57  	if err != nil {
    58  		return nil, err
    59  	}
    60  
    61  	if opts.Extra != nil {
    62  		if v, ok := b["quota_set"].(map[string]interface{}); ok {
    63  			for key, value := range opts.Extra {
    64  				v[key] = value
    65  			}
    66  		}
    67  	}
    68  
    69  	return b, nil
    70  }
    71  
    72  // Options for Updating the quotas of a Tenant.
    73  // All int-values are pointers so they can be nil if they are not needed.
    74  // You can use gopercloud.IntToPointer() for convenience
    75  type UpdateOpts struct {
    76  	// Volumes is the number of volumes that are allowed for each project.
    77  	Volumes *int `json:"volumes,omitempty"`
    78  
    79  	// Snapshots is the number of snapshots that are allowed for each project.
    80  	Snapshots *int `json:"snapshots,omitempty"`
    81  
    82  	// Gigabytes is the size (GB) of volumes and snapshots that are allowed for
    83  	// each project.
    84  	Gigabytes *int `json:"gigabytes,omitempty"`
    85  
    86  	// PerVolumeGigabytes is the size (GB) of volumes and snapshots that are
    87  	// allowed for each project and the specifed volume type.
    88  	PerVolumeGigabytes *int `json:"per_volume_gigabytes,omitempty"`
    89  
    90  	// Backups is the number of backups that are allowed for each project.
    91  	Backups *int `json:"backups,omitempty"`
    92  
    93  	// BackupGigabytes is the size (GB) of backups that are allowed for each
    94  	// project.
    95  	BackupGigabytes *int `json:"backup_gigabytes,omitempty"`
    96  
    97  	// Groups is the number of groups that are allowed for each project.
    98  	Groups *int `json:"groups,omitempty"`
    99  
   100  	// Force will update the quotaset even if the quota has already been used
   101  	// and the reserved quota exceeds the new quota.
   102  	Force bool `json:"force,omitempty"`
   103  
   104  	// Extra is a collection of miscellaneous key/values used to set
   105  	// quota per volume_type
   106  	Extra map[string]interface{} `json:"-"`
   107  }
   108  
   109  // Resets the quotas for the given tenant to their default values.
   110  func Delete(client *gophercloud.ServiceClient, projectID string) (r DeleteResult) {
   111  	resp, err := client.Delete(updateURL(client, projectID), &gophercloud.RequestOpts{
   112  		OkCodes: []int{200},
   113  	})
   114  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   115  	return
   116  }