github.com/gophercloud/gophercloud@v1.11.0/openstack/identity/v3/registeredlimits/requests.go (about)

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