Skip to content
This repository was archived by the owner on Aug 1, 2023. It is now read-only.

Commit ceb8409

Browse files
author
Dan Kirkwood
committed
initial os-quota-set api; GET unit test
1 parent f3d0534 commit ceb8409

File tree

7 files changed

+279
-0
lines changed

7 files changed

+279
-0
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// Package quotas provides information and interaction with Quotas
2+
// extension for the OpenStack Compute service.
3+
package quotas
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// +build fixtures
2+
3+
package quotas
4+
5+
import (
6+
"fmt"
7+
"net/http"
8+
"testing"
9+
10+
th "github.com/rackspace/gophercloud/testhelper"
11+
"github.com/rackspace/gophercloud/testhelper/client"
12+
)
13+
14+
// GetOutput is a sample response to a Get call.
15+
const GetOutput = `
16+
{
17+
"quota_set" : {
18+
"instances" : 25,
19+
"security_groups" : 10,
20+
"security_group_rules" : 20,
21+
"cores" : 200,
22+
"injected_file_content_bytes" : 10240,
23+
"injected_files" : 5,
24+
"metadata_items" : 128,
25+
"ram" : 200000,
26+
"keypairs" : 10,
27+
"injected_file_path_bytes" : 255
28+
}
29+
}
30+
`
31+
32+
const FirstTenantID = "555544443333222211110000ffffeeee"
33+
34+
// FirstQuota is the first result in ListOutput.
35+
var FirstQuota = Quota{
36+
FixedIps: 0,
37+
FloatingIps: 0,
38+
InjectedFileContentBytes: 10240,
39+
InjectedFilePathBytes: 255,
40+
InjectedFiles: 5,
41+
KeyPairs: 10,
42+
MetadataItems: 128,
43+
Ram: 200000,
44+
SecurityGroupRules: 20,
45+
SecurityGroups: 10,
46+
Cores: 200,
47+
Instances: 25,
48+
}
49+
50+
// HandleGetSuccessfully configures the test server to respond to a Get request for sample tenant
51+
func HandleGetSuccessfully(t *testing.T) {
52+
th.Mux.HandleFunc("/os-quota-sets/"+FirstTenantID, func(w http.ResponseWriter, r *http.Request) {
53+
th.TestMethod(t, r, "GET")
54+
th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
55+
56+
w.Header().Add("Content-Type", "application/json")
57+
fmt.Fprintf(w, GetOutput)
58+
})
59+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package quotas
2+
3+
import (
4+
"github.com/rackspace/gophercloud"
5+
"github.com/rackspace/gophercloud/pagination"
6+
)
7+
8+
func List(client *gophercloud.ServiceClient) pagination.Pager {
9+
return pagination.NewPager(client, listURL(client), func(r pagination.PageResult) pagination.Page {
10+
return QuotaPage{pagination.SinglePageBase(r)}
11+
})
12+
}
13+
14+
// Get returns public data about a previously created Quota.
15+
func Get(client *gophercloud.ServiceClient, name string) GetResult {
16+
var res GetResult
17+
_, res.Err = client.Get(getURL(client, name), &res.Body, nil)
18+
return res
19+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package quotas
2+
3+
import (
4+
th "github.com/rackspace/gophercloud/testhelper"
5+
"github.com/rackspace/gophercloud/testhelper/client"
6+
"testing"
7+
)
8+
9+
func TestGet(t *testing.T) {
10+
th.SetupHTTP()
11+
defer th.TeardownHTTP()
12+
HandleGetSuccessfully(t)
13+
actual, err := Get(client.ServiceClient(), FirstTenantID).Extract()
14+
th.AssertNoErr(t, err)
15+
th.CheckDeepEquals(t, &FirstQuota, actual)
16+
}
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
package quotas
2+
3+
import (
4+
"github.com/mitchellh/mapstructure"
5+
"github.com/rackspace/gophercloud"
6+
"github.com/rackspace/gophercloud/pagination"
7+
)
8+
9+
// Quota is a set of operational limits that allow for control of compute usage.
10+
const sample = `
11+
{
12+
"quota_set" : {
13+
"fixed_ips" : -1,
14+
"security_groups" : 10,
15+
"id" : "56b6c3eb639e48c691052919e5a60dc3",
16+
"injected_files" : 5,
17+
"injected_file_path_bytes" : 255,
18+
"cores" : 108,
19+
"security_group_rules" : 20,
20+
"keypairs" : 10,
21+
"instances" : 25,
22+
"ram" : 204800,
23+
"metadata_items" : 128,
24+
"injected_file_content_bytes" : 10240
25+
}
26+
}
27+
`
28+
29+
type Quota struct {
30+
//ID is tenant associated with this quota_set
31+
ID string `mapstructure:"id"`
32+
//FixedIps is number of fixed ips alloted this quota_set
33+
FixedIps int `mapstructure:"fixed_ips"`
34+
// FloatingIps is number of floatinh ips alloted this quota_set
35+
FloatingIps int `mapstructure:"floating_ips"`
36+
// InjectedFileContentBytes is content bytes allowed for each injected file
37+
InjectedFileContentBytes int `mapstructure:"injected_file_content_bytes"`
38+
// InjectedFilePathBytes is allowed bytes for each injected file path
39+
InjectedFilePathBytes int `mapstructure:"injected_file_path_bytes"`
40+
// InjectedFiles is injected files allowed for each project
41+
InjectedFiles int `mapstructure:"injected_files"`
42+
// KeyPairs is number of ssh keypairs
43+
KeyPairs int `mapstructure:"keypairs"`
44+
// MetadataItems is number of metadata items allowed for each instance
45+
MetadataItems int `mapstructure:"metadata_items"`
46+
// Ram is megabytes allowed for each instance
47+
Ram int `mapstructure:"ram"`
48+
// SecurityGroupRules is rules allowed for each security group
49+
SecurityGroupRules int `mapstructure:"security_group_rules"`
50+
// SecurityGroups security groups allowed for each project
51+
SecurityGroups int `mapstructure:"security_groups"`
52+
// Cores is number of instance cores allowed for each project
53+
Cores int `mapstructure:"cores"`
54+
// Instances is number of instances allowed for each project
55+
Instances int `mapstructure:"instances"`
56+
}
57+
58+
// QuotaPage stores a single, only page of Quota results from a List call.
59+
type QuotaPage struct {
60+
pagination.SinglePageBase
61+
}
62+
63+
// IsEmpty determines whether or not a QuotaPage is empty.
64+
func (page QuotaPage) IsEmpty() (bool, error) {
65+
ks, err := ExtractQuotas(page)
66+
return len(ks) == 0, err
67+
}
68+
69+
// ExtractQuotas interprets a page of results as a slice of Quotas.
70+
func ExtractQuotas(page pagination.Page) ([]Quota, error) {
71+
var resp struct {
72+
Quotas []Quota `mapstructure:"quotas"`
73+
}
74+
75+
err := mapstructure.Decode(page.(QuotaPage).Body, &resp)
76+
results := make([]Quota, len(resp.Quotas))
77+
for i, q := range resp.Quotas {
78+
results[i] = q
79+
}
80+
return results, err
81+
}
82+
83+
type quotaResult struct {
84+
gophercloud.Result
85+
}
86+
87+
// Extract is a method that attempts to interpret any Quota resource response as a Quota struct.
88+
func (r quotaResult) Extract() (*Quota, error) {
89+
if r.Err != nil {
90+
return nil, r.Err
91+
}
92+
93+
var res struct {
94+
Quota *Quota `json:"quota_set" mapstructure:"quota_set"`
95+
}
96+
97+
err := mapstructure.Decode(r.Body, &res)
98+
return res.Quota, err
99+
}
100+
101+
// CreateResult is the response from a Create operation. Call its Extract method to interpret it
102+
// as a Quota.
103+
type CreateResult struct {
104+
quotaResult
105+
}
106+
107+
// GetResult is the response from a Get operation. Call its Extract method to interpret it
108+
// as a Quota.
109+
type GetResult struct {
110+
quotaResult
111+
}
112+
113+
// DeleteResult is the response from a Delete operation. Call its Extract method to determine if
114+
// the call succeeded or failed.
115+
type DeleteResult struct {
116+
gophercloud.ErrResult
117+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package quotas
2+
3+
import "github.com/rackspace/gophercloud"
4+
5+
const resourcePath = "os-quota-sets"
6+
7+
func resourceURL(c *gophercloud.ServiceClient) string {
8+
return c.ServiceURL(resourcePath)
9+
}
10+
11+
func listURL(c *gophercloud.ServiceClient) string {
12+
return resourceURL(c)
13+
}
14+
15+
func createURL(c *gophercloud.ServiceClient) string {
16+
return resourceURL(c)
17+
}
18+
19+
func getURL(c *gophercloud.ServiceClient, name string) string {
20+
return c.ServiceURL(resourcePath, name)
21+
}
22+
23+
func deleteURL(c *gophercloud.ServiceClient, name string) string {
24+
return getURL(c, name)
25+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package quotas
2+
3+
import (
4+
"testing"
5+
6+
th "github.com/rackspace/gophercloud/testhelper"
7+
"github.com/rackspace/gophercloud/testhelper/client"
8+
)
9+
10+
func TestListURL(t *testing.T) {
11+
th.SetupHTTP()
12+
defer th.TeardownHTTP()
13+
c := client.ServiceClient()
14+
15+
th.CheckEquals(t, c.Endpoint+"os-quota-sets", listURL(c))
16+
}
17+
18+
func TestCreateURL(t *testing.T) {
19+
th.SetupHTTP()
20+
defer th.TeardownHTTP()
21+
c := client.ServiceClient()
22+
23+
th.CheckEquals(t, c.Endpoint+"os-quota-sets", createURL(c))
24+
}
25+
26+
func TestGetURL(t *testing.T) {
27+
th.SetupHTTP()
28+
defer th.TeardownHTTP()
29+
c := client.ServiceClient()
30+
31+
th.CheckEquals(t, c.Endpoint+"os-quota-sets/wat", getURL(c, "wat"))
32+
}
33+
34+
func TestDeleteURL(t *testing.T) {
35+
th.SetupHTTP()
36+
defer th.TeardownHTTP()
37+
c := client.ServiceClient()
38+
39+
th.CheckEquals(t, c.Endpoint+"os-quota-sets/wat", deleteURL(c, "wat"))
40+
}

0 commit comments

Comments
 (0)