github.com/vnpaycloud-console/gophercloud/v2@v2.0.5/openstack/identity/v3/registeredlimits/requests.go (about)

     1  package registeredlimits
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/vnpaycloud-console/gophercloud/v2"
     7  	"github.com/vnpaycloud-console/gophercloud/v2/pagination"
     8  )
     9  
    10  // ListOptsBuilder allows extensions to add additional parameters to
    11  // the List request
    12  type ListOptsBuilder interface {
    13  	ToRegisteredLimitListQuery() (string, error)
    14  }
    15  
    16  // ListOpts provides options to filter the List results.
    17  type ListOpts struct {
    18  	// Filters the response by a region ID.
    19  	RegionID string `q:"region_id"`
    20  
    21  	// Filters the response by a service ID.
    22  	ServiceID string `q:"service_id"`
    23  
    24  	// Filters the response by a resource name.
    25  	ResourceName string `q:"resource_name"`
    26  }
    27  
    28  // ToRegisteredLimitListQuery formats a ListOpts into a query string.
    29  func (opts ListOpts) ToRegisteredLimitListQuery() (string, error) {
    30  	q, err := gophercloud.BuildQueryString(opts)
    31  	return q.String(), err
    32  }
    33  
    34  // List enumerates the registered limits.
    35  func List(client *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager {
    36  	url := rootURL(client)
    37  	if opts != nil {
    38  		query, err := opts.ToRegisteredLimitListQuery()
    39  		if err != nil {
    40  			return pagination.Pager{Err: err}
    41  		}
    42  		url += query
    43  	}
    44  	return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
    45  		return RegisteredLimitPage{pagination.LinkedPageBase{PageResult: r}}
    46  	})
    47  }
    48  
    49  // BatchCreateOptsBuilder allows extensions to add additional parameters to
    50  // the Create request.
    51  type BatchCreateOptsBuilder interface {
    52  	ToRegisteredLimitsCreateMap() (map[string]any, error)
    53  }
    54  
    55  type CreateOpts struct {
    56  	// RegionID is the ID of the region where the limit is applied.
    57  	RegionID string `json:"region_id,omitempty"`
    58  
    59  	// ServiceID is the ID of the service where the limit is applied.
    60  	ServiceID string `json:"service_id" required:"true"`
    61  
    62  	// Description of the limit.
    63  	Description string `json:"description,omitempty"`
    64  
    65  	// ResourceName is the name of the resource that the limit is applied to.
    66  	ResourceName string `json:"resource_name" required:"true"`
    67  
    68  	// DefaultLimit is the default limit.
    69  	DefaultLimit int `json:"default_limit" required:"true"`
    70  }
    71  
    72  // BatchCreateOpts provides options used to create limits.
    73  type BatchCreateOpts []CreateOpts
    74  
    75  // ToRegisteredLimitsCreateMap formats a BatchCreateOpts into a create request.
    76  func (opts BatchCreateOpts) ToRegisteredLimitsCreateMap() (map[string]any, error) {
    77  	registered_limits := make([]map[string]any, len(opts))
    78  	for i, registered_limit := range opts {
    79  		registeredLimitMap, err := registered_limit.ToMap()
    80  		if err != nil {
    81  			return nil, err
    82  		}
    83  		registered_limits[i] = registeredLimitMap
    84  	}
    85  	return map[string]any{"registered_limits": registered_limits}, nil
    86  }
    87  
    88  func (opts CreateOpts) ToMap() (map[string]any, error) {
    89  	return gophercloud.BuildRequestBody(opts, "")
    90  }
    91  
    92  // BatchCreate creates new Limits.
    93  func BatchCreate(ctx context.Context, client *gophercloud.ServiceClient, opts BatchCreateOptsBuilder) (r CreateResult) {
    94  	b, err := opts.ToRegisteredLimitsCreateMap()
    95  	if err != nil {
    96  		r.Err = err
    97  		return
    98  	}
    99  	resp, err := client.Post(ctx, rootURL(client), &b, &r.Body, &gophercloud.RequestOpts{
   100  		OkCodes: []int{201},
   101  	})
   102  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   103  	return
   104  }
   105  
   106  // Get retrieves details on a single registered_limit, by ID.
   107  func Get(ctx context.Context, client *gophercloud.ServiceClient, registeredLimitID string) (r GetResult) {
   108  	resp, err := client.Get(ctx, resourceURL(client, registeredLimitID), &r.Body, nil)
   109  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   110  	return
   111  }
   112  
   113  // UpdateOptsBuilder allows extensions to add additional parameters to
   114  // the Update request.
   115  type UpdateOptsBuilder interface {
   116  	ToRegisteredLimitUpdateMap() (map[string]any, error)
   117  }
   118  
   119  // UpdateOpts represents parameters to update a domain.
   120  type UpdateOpts struct {
   121  	// Description of the registered_limit.
   122  	Description *string `json:"description,omitempty"`
   123  
   124  	// DefaultLimit is the override limit.
   125  	DefaultLimit *int `json:"default_limit,omitempty"`
   126  
   127  	// RegionID is the ID of the region where the limit is applied.
   128  	RegionID string `json:"region_id,omitempty"`
   129  
   130  	// ServiceID is the ID of the service where the limit is applied.
   131  	ServiceID string `json:"service_id,omitempty"`
   132  
   133  	// ResourceName is the name of the resource that the limit is applied to.
   134  	ResourceName string `json:"resource_name,omitempty"`
   135  	//Either service_id, resource_name, or region_id must be different than existing value otherwise it will raise 409.
   136  }
   137  
   138  // ToRegisteredLimitUpdateMap formats UpdateOpts into an update request.
   139  func (opts UpdateOpts) ToRegisteredLimitUpdateMap() (map[string]any, error) {
   140  	return gophercloud.BuildRequestBody(opts, "registered_limit")
   141  }
   142  
   143  // Update modifies the attributes of a registered limit.
   144  func Update(ctx context.Context, client *gophercloud.ServiceClient, id string, opts UpdateOptsBuilder) (r UpdateResult) {
   145  	b, err := opts.ToRegisteredLimitUpdateMap()
   146  	if err != nil {
   147  		r.Err = err
   148  		return
   149  	}
   150  	resp, err := client.Patch(ctx, resourceURL(client, id), b, &r.Body, &gophercloud.RequestOpts{
   151  		OkCodes: []int{200},
   152  	})
   153  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   154  	return
   155  }
   156  
   157  // Delete deletes a registered_limit.
   158  func Delete(ctx context.Context, client *gophercloud.ServiceClient, registeredLimitID string) (r DeleteResult) {
   159  	resp, err := client.Delete(ctx, resourceURL(client, registeredLimitID), nil)
   160  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   161  	return
   162  }