Skip to content

Commit b31f60b

Browse files
author
Ludovic Lamarche
committed
add barbican quota implementation
1 parent 8ddcaf3 commit b31f60b

File tree

7 files changed

+519
-0
lines changed

7 files changed

+519
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
Package quotas provides the ability to retrieve and manage Barbican quotas
3+
4+
Example to Get project quotas
5+
6+
projectID = "23d5d3f79dfa4f73b72b8b0b0063ec55"
7+
quotasInfo, err := quotas.Get(keyManagerClient, projectID).Extract()
8+
if err != nil {
9+
log.Fatal(err)
10+
}
11+
12+
fmt.Printf("quotas: %#v\n", quotasInfo)
13+
14+
Example to Update project quotas
15+
16+
projectID = "23d5d3f79dfa4f73b72b8b0b0063ec55"
17+
18+
updateOpts := quotas.UpdateOpts{
19+
Secrets: gophercloud.IntToPointer(10),
20+
Orders: gophercloud.IntToPointer(20),
21+
Containers: gophercloud.IntToPointer(10),
22+
Consumers: gophercloud.IntToPointer(-1),
23+
Cas: gophercloud.IntToPointer(5),
24+
}
25+
quotasInfo, err := quotas.Update(keyManagerClient, projectID)
26+
if err != nil {
27+
log.Fatal(err)
28+
}
29+
30+
fmt.Printf("quotas: %#v\n", quotasInfo)
31+
*/
32+
package quotas
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
package quotas
2+
3+
import (
4+
"github.com/gophercloud/gophercloud"
5+
"github.com/gophercloud/gophercloud/pagination"
6+
)
7+
8+
// Get the effective quotas for the project of the requester. The project id of the requester is derived from the authentication token provided in the X-Auth-Token header.
9+
func Get(client *gophercloud.ServiceClient) (r GetResult) {
10+
resp, err := client.Get(getURL(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+
ToOrderListQuery() (string, error)
19+
}
20+
21+
// ListOpts provides options to filter the List results.
22+
type ListOpts struct {
23+
// Limit is the amount of containers to retrieve.
24+
Limit int `q:"limit"`
25+
26+
// Offset is the index within the list to retrieve.
27+
Offset int `q:"offset"`
28+
}
29+
30+
// ToOrderListQuery formats a ListOpts into a query string.
31+
func (opts ListOpts) ToOrderListQuery() (string, error) {
32+
q, err := gophercloud.BuildQueryString(opts)
33+
return q.String(), err
34+
}
35+
36+
// List retrieves a list of orders.
37+
func List(client *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager {
38+
url := listURL(client)
39+
if opts != nil {
40+
query, err := opts.ToOrderListQuery()
41+
if err != nil {
42+
return pagination.Pager{Err: err}
43+
}
44+
url += query
45+
}
46+
return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
47+
return ProjectQuotaPage{pagination.LinkedPageBase{PageResult: r}}
48+
})
49+
}
50+
51+
// GetProjectQuota returns key manager Quotas for a project.
52+
func GetProjectQuota(client *gophercloud.ServiceClient, projectID string) (r GetProjectResult) {
53+
resp, err := client.Get(getProjectURL(client, projectID), &r.Body, nil)
54+
_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
55+
return
56+
}
57+
58+
// UpdateOptsBuilder allows extensions to add additional parameters to the
59+
// Update request.
60+
type UpdateOptsBuilder interface {
61+
ToQuotaUpdateMap() (map[string]interface{}, error)
62+
}
63+
64+
// UpdateOpts represents options used to update the key manager Quotas.
65+
type UpdateOpts struct {
66+
// Secrets represents the number of secrets. A "-1" value means no limit.
67+
Secrets *int `json:"secrets"`
68+
69+
// Orders represents the number of orders. A "-1" value means no limit.
70+
Orders *int `json:"orders"`
71+
72+
// Containers represents the number of containers. A "-1" value means no limit.
73+
Containers *int `json:"containers"`
74+
75+
// Consumers represents the number of consumers. A "-1" value means no limit.
76+
Consumers *int `json:"consumers"`
77+
78+
// CAS represents the number of cas. A "-1" value means no limit.
79+
CAS *int `json:"cas"`
80+
}
81+
82+
// ToQuotaUpdateMap builds a request body from UpdateOpts.
83+
func (opts UpdateOpts) ToQuotaUpdateMap() (map[string]interface{}, error) {
84+
return gophercloud.BuildRequestBody(opts, "project_quotas")
85+
}
86+
87+
// Update accepts a UpdateOpts struct and updates an existing key manager Quotas using the
88+
// values provided.
89+
func Update(c *gophercloud.ServiceClient, projectID string, opts UpdateOptsBuilder) (r gophercloud.Result) {
90+
b, err := opts.ToQuotaUpdateMap()
91+
if err != nil {
92+
r.Err = err
93+
return
94+
}
95+
resp, err := c.Put(updateProjectURL(c, projectID), b, &r.Body, &gophercloud.RequestOpts{
96+
OkCodes: []int{204},
97+
})
98+
_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
99+
return
100+
}
101+
102+
// Delete a key manager Quotas for a project.
103+
func Delete(c *gophercloud.ServiceClient, projectID string) (r gophercloud.Result) {
104+
resp, err := c.Delete(deleteProjectURL(c, projectID), &gophercloud.RequestOpts{
105+
OkCodes: []int{204},
106+
})
107+
_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
108+
return
109+
}
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package quotas
2+
3+
import (
4+
"github.com/gophercloud/gophercloud"
5+
"github.com/gophercloud/gophercloud/pagination"
6+
)
7+
8+
type commonResult struct {
9+
gophercloud.Result
10+
}
11+
12+
// Extract is a function that accepts a result and extracts a Quota resource.
13+
func (r commonResult) Extract() (*Quota, error) {
14+
var s struct {
15+
Quota *Quota `json:"quotas"`
16+
}
17+
err := r.ExtractInto(&s)
18+
return s.Quota, err
19+
}
20+
21+
// GetResult represents the result of a get operation. Call its Extract
22+
// method to interpret it as a Quota.
23+
type GetResult struct {
24+
commonResult
25+
}
26+
27+
// Quota contains key manager quotas for a project.
28+
type Quota struct {
29+
// Secrets represents the number of secrets. A "-1" value means no limit.
30+
Secrets *int `json:"secrets"`
31+
32+
// Orders represents the number of orders. A "-1" value means no limit.
33+
Orders *int `json:"orders"`
34+
35+
// Containers represents the number of containers. A "-1" value means no limit.
36+
Containers *int `json:"containers"`
37+
38+
// Consumers represents the number of consumers. A "-1" value means no limit.
39+
Consumers *int `json:"consumers"`
40+
41+
// CAS represents the number of cas. A "-1" value means no limit.
42+
CAS *int `json:"cas"`
43+
}
44+
45+
type commonProjectResult struct {
46+
gophercloud.Result
47+
}
48+
49+
// Extract is a function that accepts a result and extracts a Quota resource.
50+
func (r commonProjectResult) Extract() (*Quota, error) {
51+
var s struct {
52+
Quota *Quota `json:"project_quotas"`
53+
}
54+
err := r.ExtractInto(&s)
55+
return s.Quota, err
56+
}
57+
58+
// GetProjectResult represents the result of a get operation. Call its Extract
59+
// method to interpret it as a Quota.
60+
type GetProjectResult struct {
61+
commonProjectResult
62+
}
63+
64+
type ProjectQuota struct {
65+
ProjectID string `json:"project_id"`
66+
Quota Quota `json:"project_quotas"`
67+
}
68+
69+
// ProjectQuotaPage is a single page of quotas results.
70+
type ProjectQuotaPage struct {
71+
pagination.LinkedPageBase
72+
}
73+
74+
// IsEmpty determines whether or not a page of quotas contains any results.
75+
func (r ProjectQuotaPage) IsEmpty() (bool, error) {
76+
quotas, err := ExtractQuotas(r)
77+
return len(quotas) == 0, err
78+
}
79+
80+
// NextPageURL extracts the "next" link from the links section of the result.
81+
func (r ProjectQuotaPage) NextPageURL() (string, error) {
82+
var s struct {
83+
Next string `json:"next"`
84+
Previous string `json:"previous"`
85+
}
86+
err := r.ExtractInto(&s)
87+
if err != nil {
88+
return "", err
89+
}
90+
return s.Next, err
91+
}
92+
93+
// ExtractQuotas returns a slice of ProjectQuota contained in a single page of
94+
// results.
95+
func ExtractQuotas(r pagination.Page) ([]ProjectQuota, error) {
96+
var s struct {
97+
Quotas []ProjectQuota `json:"project_quotas"`
98+
}
99+
err := (r.(ProjectQuotaPage)).ExtractInto(&s)
100+
return s.Quotas, err
101+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// quotas unit tests
2+
package testing
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package testing
2+
3+
import (
4+
"github.com/gophercloud/gophercloud"
5+
"github.com/gophercloud/gophercloud/openstack/keymanager/v1/quotas"
6+
)
7+
8+
const GetResponseRaw_1 = `
9+
{
10+
"quotas": {
11+
"secrets": 10,
12+
"orders": 20,
13+
"containers": -1,
14+
"consumers": 10,
15+
"cas": 5
16+
}
17+
}
18+
`
19+
20+
var GetResponse = quotas.Quota{
21+
Secrets: gophercloud.IntToPointer(10),
22+
Orders: gophercloud.IntToPointer(20),
23+
Containers: gophercloud.IntToPointer(-1),
24+
Consumers: gophercloud.IntToPointer(10),
25+
CAS: gophercloud.IntToPointer(5),
26+
}
27+
28+
const GetProjectResponseRaw_1 = `
29+
{
30+
"project_quotas": {
31+
"secrets": 10,
32+
"orders": 20,
33+
"containers": -1,
34+
"consumers": 10,
35+
"cas": 5
36+
}
37+
}
38+
`
39+
40+
const ListResponseRaw_1 = `
41+
{
42+
"project_quotas": [
43+
{
44+
"project_id": "1234",
45+
"project_quotas": {
46+
"secrets": 2000,
47+
"orders": 0,
48+
"containers": -1,
49+
"consumers": null,
50+
"cas": null
51+
}
52+
},
53+
{
54+
"project_id": "5678",
55+
"project_quotas": {
56+
"secrets": 200,
57+
"orders": 100,
58+
"containers": -1,
59+
"consumers": null,
60+
"cas": null
61+
}
62+
}
63+
],
64+
"total" : 30
65+
}
66+
`
67+
68+
var ExpectedQuotasSlice = []quotas.ProjectQuota{
69+
{
70+
ProjectID: "1234",
71+
Quota: quotas.Quota{
72+
Secrets: gophercloud.IntToPointer(2000),
73+
Orders: gophercloud.IntToPointer(0),
74+
Containers: gophercloud.IntToPointer(-1),
75+
Consumers: nil,
76+
CAS: nil,
77+
},
78+
},
79+
{
80+
ProjectID: "5678",
81+
Quota: quotas.Quota{
82+
Secrets: gophercloud.IntToPointer(200),
83+
Orders: gophercloud.IntToPointer(100),
84+
Containers: gophercloud.IntToPointer(-1),
85+
Consumers: nil,
86+
CAS: nil,
87+
},
88+
},
89+
}

0 commit comments

Comments
 (0)