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