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

Commit 8992d74

Browse files
committed
Merge pull request #542 from dangogh/quota-set-get
[rfr] os-quota-set GET api
2 parents 9b8e7f3 + ed3f5fd commit 8992d74

File tree

8 files changed

+265
-0
lines changed

8 files changed

+265
-0
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// +build acceptance compute
2+
3+
package v2
4+
5+
import (
6+
"testing"
7+
8+
"github.com/rackspace/gophercloud"
9+
"github.com/rackspace/gophercloud/openstack"
10+
"github.com/rackspace/gophercloud/openstack/compute/v2/extensions/quotasets"
11+
"github.com/rackspace/gophercloud/openstack/identity/v2/tenants"
12+
"github.com/rackspace/gophercloud/pagination"
13+
th "github.com/rackspace/gophercloud/testhelper"
14+
)
15+
16+
func TestGetQuotaset(t *testing.T) {
17+
client, err := newClient()
18+
if err != nil {
19+
t.Fatalf("Unable to create a compute client: %v", err)
20+
}
21+
22+
idclient := openstack.NewIdentityV2(client.ProviderClient)
23+
quotaset, err := quotasets.Get(client, findTenant(t, idclient)).Extract()
24+
if err != nil {
25+
t.Fatal(err)
26+
}
27+
28+
t.Logf("QuotaSet details:\n")
29+
t.Logf(" instances=[%d]\n", quotaset.Instances)
30+
t.Logf(" cores=[%d]\n", quotaset.Cores)
31+
t.Logf(" ram=[%d]\n", quotaset.Ram)
32+
t.Logf(" key_pairs=[%d]\n", quotaset.KeyPairs)
33+
t.Logf(" metadata_items=[%d]\n", quotaset.MetadataItems)
34+
t.Logf(" security_groups=[%d]\n", quotaset.SecurityGroups)
35+
t.Logf(" security_group_rules=[%d]\n", quotaset.SecurityGroupRules)
36+
t.Logf(" fixed_ips=[%d]\n", quotaset.FixedIps)
37+
t.Logf(" floating_ips=[%d]\n", quotaset.FloatingIps)
38+
t.Logf(" injected_file_content_bytes=[%d]\n", quotaset.InjectedFileContentBytes)
39+
t.Logf(" injected_file_path_bytes=[%d]\n", quotaset.InjectedFilePathBytes)
40+
t.Logf(" injected_files=[%d]\n", quotaset.InjectedFiles)
41+
42+
}
43+
44+
func findTenant(t *testing.T, client *gophercloud.ServiceClient) string {
45+
var tenantID string
46+
err := tenants.List(client, nil).EachPage(func(page pagination.Page) (bool, error) {
47+
tenantList, err := tenants.ExtractTenants(page)
48+
th.AssertNoErr(t, err)
49+
50+
for _, t := range tenantList {
51+
tenantID = t.ID
52+
break
53+
}
54+
55+
return true, nil
56+
})
57+
th.AssertNoErr(t, err)
58+
59+
return tenantID
60+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// Package quotasets provides information and interaction with QuotaSet
2+
// extension for the OpenStack Compute service.
3+
package quotasets
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// +build fixtures
2+
3+
package quotasets
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+
// FirstQuotaSet is the first result in ListOutput.
35+
var FirstQuotaSet = QuotaSet{
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: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package quotasets
2+
3+
import (
4+
"github.com/rackspace/gophercloud"
5+
)
6+
7+
// Get returns public data about a previously created QuotaSet.
8+
func Get(client *gophercloud.ServiceClient, tenantID string) GetResult {
9+
var res GetResult
10+
_, res.Err = client.Get(getURL(client, tenantID), &res.Body, nil)
11+
return res
12+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package quotasets
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, &FirstQuotaSet, actual)
16+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package quotasets
2+
3+
import (
4+
"github.com/mitchellh/mapstructure"
5+
"github.com/rackspace/gophercloud"
6+
"github.com/rackspace/gophercloud/pagination"
7+
)
8+
9+
// QuotaSet is a set of operational limits that allow for control of compute usage.
10+
type QuotaSet struct {
11+
//ID is tenant associated with this quota_set
12+
ID string `mapstructure:"id"`
13+
//FixedIps is number of fixed ips alloted this quota_set
14+
FixedIps int `mapstructure:"fixed_ips"`
15+
// FloatingIps is number of floating ips alloted this quota_set
16+
FloatingIps int `mapstructure:"floating_ips"`
17+
// InjectedFileContentBytes is content bytes allowed for each injected file
18+
InjectedFileContentBytes int `mapstructure:"injected_file_content_bytes"`
19+
// InjectedFilePathBytes is allowed bytes for each injected file path
20+
InjectedFilePathBytes int `mapstructure:"injected_file_path_bytes"`
21+
// InjectedFiles is injected files allowed for each project
22+
InjectedFiles int `mapstructure:"injected_files"`
23+
// KeyPairs is number of ssh keypairs
24+
KeyPairs int `mapstructure:"keypairs"`
25+
// MetadataItems is number of metadata items allowed for each instance
26+
MetadataItems int `mapstructure:"metadata_items"`
27+
// Ram is megabytes allowed for each instance
28+
Ram int `mapstructure:"ram"`
29+
// SecurityGroupRules is rules allowed for each security group
30+
SecurityGroupRules int `mapstructure:"security_group_rules"`
31+
// SecurityGroups security groups allowed for each project
32+
SecurityGroups int `mapstructure:"security_groups"`
33+
// Cores is number of instance cores allowed for each project
34+
Cores int `mapstructure:"cores"`
35+
// Instances is number of instances allowed for each project
36+
Instances int `mapstructure:"instances"`
37+
}
38+
39+
// QuotaSetPage stores a single, only page of QuotaSet results from a List call.
40+
type QuotaSetPage struct {
41+
pagination.SinglePageBase
42+
}
43+
44+
// IsEmpty determines whether or not a QuotaSetsetPage is empty.
45+
func (page QuotaSetPage) IsEmpty() (bool, error) {
46+
ks, err := ExtractQuotaSets(page)
47+
return len(ks) == 0, err
48+
}
49+
50+
// ExtractQuotaSets interprets a page of results as a slice of QuotaSets.
51+
func ExtractQuotaSets(page pagination.Page) ([]QuotaSet, error) {
52+
var resp struct {
53+
QuotaSets []QuotaSet `mapstructure:"quotas"`
54+
}
55+
56+
err := mapstructure.Decode(page.(QuotaSetPage).Body, &resp)
57+
results := make([]QuotaSet, len(resp.QuotaSets))
58+
for i, q := range resp.QuotaSets {
59+
results[i] = q
60+
}
61+
return results, err
62+
}
63+
64+
type quotaResult struct {
65+
gophercloud.Result
66+
}
67+
68+
// Extract is a method that attempts to interpret any QuotaSet resource response as a QuotaSet struct.
69+
func (r quotaResult) Extract() (*QuotaSet, error) {
70+
if r.Err != nil {
71+
return nil, r.Err
72+
}
73+
74+
var res struct {
75+
QuotaSet *QuotaSet `json:"quota_set" mapstructure:"quota_set"`
76+
}
77+
78+
err := mapstructure.Decode(r.Body, &res)
79+
return res.QuotaSet, err
80+
}
81+
82+
// GetResult is the response from a Get operation. Call its Extract method to interpret it
83+
// as a QuotaSet.
84+
type GetResult struct {
85+
quotaResult
86+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package quotasets
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 getURL(c *gophercloud.ServiceClient, tenantID string) string {
12+
return c.ServiceURL(resourcePath, tenantID)
13+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package quotasets
2+
3+
import (
4+
"testing"
5+
6+
th "github.com/rackspace/gophercloud/testhelper"
7+
"github.com/rackspace/gophercloud/testhelper/client"
8+
)
9+
10+
func TestGetURL(t *testing.T) {
11+
th.SetupHTTP()
12+
defer th.TeardownHTTP()
13+
c := client.ServiceClient()
14+
15+
th.CheckEquals(t, c.Endpoint+"os-quota-sets/wat", getURL(c, "wat"))
16+
}

0 commit comments

Comments
 (0)