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

Skip to content

Networking V2: add quotas package #1742

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// +build acceptance networking quotas

package quotas

import (
"os"
"testing"

"github.com/gophercloud/gophercloud/acceptance/clients"
"github.com/gophercloud/gophercloud/acceptance/tools"
"github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/quotas"
th "github.com/gophercloud/gophercloud/testhelper"
)

func TestQuotasGet(t *testing.T) {
clients.RequireAdmin(t)

client, err := clients.NewNetworkV2Client()
th.AssertNoErr(t, err)

quotasInfo, err := quotas.Get(client, os.Getenv("OS_PROJECT_NAME")).Extract()
th.AssertNoErr(t, err)

tools.PrintResource(t, quotasInfo)
}
12 changes: 12 additions & 0 deletions openstack/networking/v2/extensions/quotas/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*
Package quotas provides the ability to retrieve and manage Networking quotas through the Neutron API.

Example to Get project quotas

projectID = "23d5d3f79dfa4f73b72b8b0b0063ec55"
quotasInfo, err := quotas.Get(networkClient, projectID).Extract()
if err != nil {
log.Fatal(err)
}
*/
package quotas
9 changes: 9 additions & 0 deletions openstack/networking/v2/extensions/quotas/requests.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package quotas

import "github.com/gophercloud/gophercloud"

// Get returns Networking Quotas for a project.
func Get(client *gophercloud.ServiceClient, projectID string) (r GetResult) {
_, r.Err = client.Get(getURL(client, projectID), &r.Body, nil)
return
}
52 changes: 52 additions & 0 deletions openstack/networking/v2/extensions/quotas/results.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package quotas

import "github.com/gophercloud/gophercloud"

type commonResult struct {
gophercloud.Result
}

// Extract is a function that accepts a result and extracts a Quota resource.
func (r commonResult) Extract() (*Quota, error) {
var s struct {
Quota *Quota `json:"quota"`
}
err := r.ExtractInto(&s)
return s.Quota, err
}

// GetResult represents the result of a get operation. Call its Extract
// method to interpret it as a Quota.
type GetResult struct {
commonResult
}

// Quota contains Networking quotas for a project.
type Quota struct {
// FloatingIP represents a number of floating IPs. A "-1" value means no limit.
FloatingIP int `json:"floatingip"`

// Network represents a number of networks. A "-1" value means no limit.
Network int `json:"network"`

// Port represents a number of ports. A "-1" value means no limit.
Port int `json:"port"`

// RBACPolicy represents a number of RBAC policies. A "-1" value means no limit.
RBACPolicy int `json:"rbac_policy"`

// Router represents a number of routers. A "-1" value means no limit.
Router int `json:"router"`

// SecurityGroup represents a number of security groups. A "-1" value means no limit.
SecurityGroup int `json:"security_group"`

// SecurityGroupRule represents a number of security group rules. A "-1" value means no limit.
SecurityGroupRule int `json:"security_group_rule"`

// Subnet represents a number of subnets. A "-1" value means no limit.
Subnet int `json:"subnet"`

// SubnetPool represents a number of subnet pools. A "-1" value means no limit.
SubnetPool int `json:"subnetpool"`
}
2 changes: 2 additions & 0 deletions openstack/networking/v2/extensions/quotas/testing/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// quotas unit tests
package testing
31 changes: 31 additions & 0 deletions openstack/networking/v2/extensions/quotas/testing/fixtures.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package testing

import "github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/quotas"

const GetResponseRaw = `
{
"quota": {
"floatingip": 15,
"network": 20,
"port": 25,
"rbac_policy": -1,
"router": 30,
"security_group": 35,
"security_group_rule": 40,
"subnet": 45,
"subnetpool": -1
}
}
`

var GetResponse = quotas.Quota{
FloatingIP: 15,
Network: 20,
Port: 25,
RBACPolicy: -1,
Router: 30,
SecurityGroup: 35,
SecurityGroupRule: 40,
Subnet: 45,
SubnetPool: -1,
}
30 changes: 30 additions & 0 deletions openstack/networking/v2/extensions/quotas/testing/requests_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package testing

import (
"fmt"
"net/http"
"testing"

fake "github.com/gophercloud/gophercloud/openstack/networking/v2/common"
"github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/quotas"
th "github.com/gophercloud/gophercloud/testhelper"
)

func TestGet(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()

th.Mux.HandleFunc("/v2.0/quotas/0a73845280574ad389c292f6a74afa76", func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "GET")
th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)

w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)

fmt.Fprintf(w, GetResponseRaw)
})

q, err := quotas.Get(fake.ServiceClient(), "0a73845280574ad389c292f6a74afa76").Extract()
th.AssertNoErr(t, err)
th.AssertDeepEquals(t, q, &GetResponse)
}
9 changes: 9 additions & 0 deletions openstack/networking/v2/extensions/quotas/urls.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package quotas

import "github.com/gophercloud/gophercloud"

const resourcePath = "quotas"

func getURL(c *gophercloud.ServiceClient, projectID string) string {
return c.ServiceURL(resourcePath, projectID)
}
1 change: 1 addition & 0 deletions script/acceptancetest
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ acceptance/openstack/networking/v2/extensions/portsbinding
acceptance/openstack/networking/v2/extensions/qos/policies
acceptance/openstack/networking/v2/extensions/qos/rules
acceptance/openstack/networking/v2/extensions/qos/ruletypes
acceptance/openstack/networking/v2/extensions/quotas
acceptance/openstack/networking/v2/extensions/rbacpolicies
acceptance/openstack/networking/v2/extensions/subnetpools
acceptance/openstack/networking/v2/extensions/trunks
Expand Down