Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0c7b9d1

Browse files
authored
Networking V2: add quotas package (#1742)
* Networking V2: add quotas package Add openstack/networking/v2/extensions/quotas.Get. Add unit and acceptance tests. * Networking V2 acc tests: fix +build tag for quotas Add a blank line.
1 parent cf0980d commit 0c7b9d1

File tree

9 files changed

+171
-0
lines changed

9 files changed

+171
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// +build acceptance networking quotas
2+
3+
package quotas
4+
5+
import (
6+
"os"
7+
"testing"
8+
9+
"github.com/gophercloud/gophercloud/acceptance/clients"
10+
"github.com/gophercloud/gophercloud/acceptance/tools"
11+
"github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/quotas"
12+
th "github.com/gophercloud/gophercloud/testhelper"
13+
)
14+
15+
func TestQuotasGet(t *testing.T) {
16+
clients.RequireAdmin(t)
17+
18+
client, err := clients.NewNetworkV2Client()
19+
th.AssertNoErr(t, err)
20+
21+
quotasInfo, err := quotas.Get(client, os.Getenv("OS_PROJECT_NAME")).Extract()
22+
th.AssertNoErr(t, err)
23+
24+
tools.PrintResource(t, quotasInfo)
25+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/*
2+
Package quotas provides the ability to retrieve and manage Networking quotas through the Neutron API.
3+
4+
Example to Get project quotas
5+
6+
projectID = "23d5d3f79dfa4f73b72b8b0b0063ec55"
7+
quotasInfo, err := quotas.Get(networkClient, projectID).Extract()
8+
if err != nil {
9+
log.Fatal(err)
10+
}
11+
*/
12+
package quotas
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package quotas
2+
3+
import "github.com/gophercloud/gophercloud"
4+
5+
// Get returns Networking Quotas for a project.
6+
func Get(client *gophercloud.ServiceClient, projectID string) (r GetResult) {
7+
_, r.Err = client.Get(getURL(client, projectID), &r.Body, nil)
8+
return
9+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package quotas
2+
3+
import "github.com/gophercloud/gophercloud"
4+
5+
type commonResult struct {
6+
gophercloud.Result
7+
}
8+
9+
// Extract is a function that accepts a result and extracts a Quota resource.
10+
func (r commonResult) Extract() (*Quota, error) {
11+
var s struct {
12+
Quota *Quota `json:"quota"`
13+
}
14+
err := r.ExtractInto(&s)
15+
return s.Quota, err
16+
}
17+
18+
// GetResult represents the result of a get operation. Call its Extract
19+
// method to interpret it as a Quota.
20+
type GetResult struct {
21+
commonResult
22+
}
23+
24+
// Quota contains Networking quotas for a project.
25+
type Quota struct {
26+
// FloatingIP represents a number of floating IPs. A "-1" value means no limit.
27+
FloatingIP int `json:"floatingip"`
28+
29+
// Network represents a number of networks. A "-1" value means no limit.
30+
Network int `json:"network"`
31+
32+
// Port represents a number of ports. A "-1" value means no limit.
33+
Port int `json:"port"`
34+
35+
// RBACPolicy represents a number of RBAC policies. A "-1" value means no limit.
36+
RBACPolicy int `json:"rbac_policy"`
37+
38+
// Router represents a number of routers. A "-1" value means no limit.
39+
Router int `json:"router"`
40+
41+
// SecurityGroup represents a number of security groups. A "-1" value means no limit.
42+
SecurityGroup int `json:"security_group"`
43+
44+
// SecurityGroupRule represents a number of security group rules. A "-1" value means no limit.
45+
SecurityGroupRule int `json:"security_group_rule"`
46+
47+
// Subnet represents a number of subnets. A "-1" value means no limit.
48+
Subnet int `json:"subnet"`
49+
50+
// SubnetPool represents a number of subnet pools. A "-1" value means no limit.
51+
SubnetPool int `json:"subnetpool"`
52+
}
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: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package testing
2+
3+
import "github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/quotas"
4+
5+
const GetResponseRaw = `
6+
{
7+
"quota": {
8+
"floatingip": 15,
9+
"network": 20,
10+
"port": 25,
11+
"rbac_policy": -1,
12+
"router": 30,
13+
"security_group": 35,
14+
"security_group_rule": 40,
15+
"subnet": 45,
16+
"subnetpool": -1
17+
}
18+
}
19+
`
20+
21+
var GetResponse = quotas.Quota{
22+
FloatingIP: 15,
23+
Network: 20,
24+
Port: 25,
25+
RBACPolicy: -1,
26+
Router: 30,
27+
SecurityGroup: 35,
28+
SecurityGroupRule: 40,
29+
Subnet: 45,
30+
SubnetPool: -1,
31+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package testing
2+
3+
import (
4+
"fmt"
5+
"net/http"
6+
"testing"
7+
8+
fake "github.com/gophercloud/gophercloud/openstack/networking/v2/common"
9+
"github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/quotas"
10+
th "github.com/gophercloud/gophercloud/testhelper"
11+
)
12+
13+
func TestGet(t *testing.T) {
14+
th.SetupHTTP()
15+
defer th.TeardownHTTP()
16+
17+
th.Mux.HandleFunc("/v2.0/quotas/0a73845280574ad389c292f6a74afa76", func(w http.ResponseWriter, r *http.Request) {
18+
th.TestMethod(t, r, "GET")
19+
th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
20+
21+
w.Header().Add("Content-Type", "application/json")
22+
w.WriteHeader(http.StatusOK)
23+
24+
fmt.Fprintf(w, GetResponseRaw)
25+
})
26+
27+
q, err := quotas.Get(fake.ServiceClient(), "0a73845280574ad389c292f6a74afa76").Extract()
28+
th.AssertNoErr(t, err)
29+
th.AssertDeepEquals(t, q, &GetResponse)
30+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package quotas
2+
3+
import "github.com/gophercloud/gophercloud"
4+
5+
const resourcePath = "quotas"
6+
7+
func getURL(c *gophercloud.ServiceClient, projectID string) string {
8+
return c.ServiceURL(resourcePath, projectID)
9+
}

script/acceptancetest

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ acceptance/openstack/networking/v2/extensions/portsbinding
6666
acceptance/openstack/networking/v2/extensions/qos/policies
6767
acceptance/openstack/networking/v2/extensions/qos/rules
6868
acceptance/openstack/networking/v2/extensions/qos/ruletypes
69+
acceptance/openstack/networking/v2/extensions/quotas
6970
acceptance/openstack/networking/v2/extensions/rbacpolicies
7071
acceptance/openstack/networking/v2/extensions/subnetpools
7172
acceptance/openstack/networking/v2/extensions/trunks

0 commit comments

Comments
 (0)