github.com/gophercloud/gophercloud@v1.14.1/openstack/identity/v3/limits/requests.go (about)

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