github.com/vnpaycloud-console/gophercloud/v2@v2.0.5/openstack/blockstorage/v2/quotasets/requests.go (about)

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