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

Skip to content

Commit ab9616c

Browse files
SuperSandro2000pierreprinetti
authored andcommitted
dns: add support for /v2/quotas
Co-Authored-By: Pierre Prinetti <[email protected]>
1 parent 6e0ba67 commit ab9616c

File tree

8 files changed

+288
-0
lines changed

8 files changed

+288
-0
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
//go:build acceptance || dns || quotas
2+
3+
package v2
4+
5+
import (
6+
"context"
7+
"fmt"
8+
"testing"
9+
10+
"github.com/gophercloud/gophercloud/v2"
11+
"github.com/gophercloud/gophercloud/v2/internal/acceptance/clients"
12+
"github.com/gophercloud/gophercloud/v2/internal/acceptance/tools"
13+
"github.com/gophercloud/gophercloud/v2/openstack/dns/v2/quotas"
14+
"github.com/gophercloud/gophercloud/v2/openstack/identity/v3/projects"
15+
th "github.com/gophercloud/gophercloud/v2/testhelper"
16+
)
17+
18+
func TestQuotaGetUpdate(t *testing.T) {
19+
clients.RequireAdmin(t)
20+
21+
client, err := clients.NewDNSV2Client()
22+
th.AssertNoErr(t, err)
23+
24+
identityClient, err := clients.NewIdentityV3Client()
25+
th.AssertNoErr(t, err)
26+
27+
projectID, err := getProjectID(t, identityClient)
28+
th.AssertNoErr(t, err)
29+
30+
// test Get Quota
31+
quota, err := quotas.Get(context.TODO(), client, projectID).Extract()
32+
th.AssertNoErr(t, err)
33+
34+
tools.PrintResource(t, quota)
35+
36+
// test Update Quota
37+
zones := 10
38+
updateOpts := quotas.UpdateOpts{
39+
Zones: &zones,
40+
}
41+
res, err := quotas.Update(context.TODO(), client, projectID, updateOpts).Extract()
42+
th.AssertNoErr(t, err)
43+
44+
tools.PrintResource(t, res)
45+
46+
quota.Zones = zones
47+
th.AssertDeepEquals(t, *quota, *res)
48+
}
49+
50+
func getProjectID(t *testing.T, client *gophercloud.ServiceClient) (string, error) {
51+
allPages, err := projects.ListAvailable(client).AllPages(context.TODO())
52+
th.AssertNoErr(t, err)
53+
54+
allProjects, err := projects.ExtractProjects(allPages)
55+
th.AssertNoErr(t, err)
56+
57+
for _, project := range allProjects {
58+
return project.ID, nil
59+
}
60+
61+
return "", fmt.Errorf("Unable to get project ID")
62+
}

openstack/dns/v2/quotas/doc.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
Package quotas provides the ability to retrieve DNS quotas through the Designate API.
3+
4+
Example to Get a Quota Set
5+
6+
projectID = "23d5d3f79dfa4f73b72b8b0b0063ec55"
7+
quotasInfo, err := quotas.Get(context.TODO(), dnsClient, projectID).Extract()
8+
if err != nil {
9+
log.Fatal(err)
10+
}
11+
12+
fmt.Printf("quotas: %#v\n", quotasInfo)
13+
14+
Example to Update a Quota Set
15+
16+
projectID = "23d5d3f79dfa4f73b72b8b0b0063ec55"
17+
zones := 10
18+
quota := &quotas.UpdateOpts{
19+
Zones: &zones,
20+
}
21+
quotasInfo, err := quotas.Update(context.TODO(), dnsClient, projectID, quota).Extract()
22+
if err != nil {
23+
log.Fatal(err)
24+
}
25+
26+
fmt.Printf("quotas: %#v\n", quotasInfo)
27+
*/
28+
package quotas

openstack/dns/v2/quotas/requests.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package quotas
2+
3+
import (
4+
"context"
5+
6+
"github.com/gophercloud/gophercloud/v2"
7+
)
8+
9+
// ListOptsBuilder allows extensions to add parameters to the List request.
10+
type ListOptsBuilder interface {
11+
ToQuotasListQuery() (string, error)
12+
}
13+
14+
type ListOpts struct {
15+
}
16+
17+
// ToZoneListQuery formats a ListOpts into a query string.
18+
func (opts ListOpts) ToQuotasListQuery() (string, error) {
19+
q, err := gophercloud.BuildQueryString(opts)
20+
return q.String(), err
21+
}
22+
23+
// Get returns information about the quota, given its ID.
24+
func Get(ctx context.Context, client *gophercloud.ServiceClient, projectID string) (r Result) {
25+
resp, err := client.Get(ctx, URL(client, projectID), &r.Body, nil)
26+
_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
27+
return
28+
}
29+
30+
// UpdateOptsBuilder allows extensions to add additional parameters to the
31+
// Update request.
32+
type UpdateOptsBuilder interface {
33+
ToQuotaUpdateMap() (map[string]interface{}, error)
34+
}
35+
36+
// UpdateOpts represents options used to update the DNS Quotas.
37+
type UpdateOpts struct {
38+
APIExporterSize *int `json:"api_export_size,omitempty"`
39+
RecordsetRecords *int `json:"recordset_records,omitempty"`
40+
ZoneRecords *int `json:"zone_records,omitempty"`
41+
ZoneRecordsets *int `json:"zone_recordsets,omitempty"`
42+
Zones *int `json:"zones,omitempty"`
43+
}
44+
45+
// ToQuotaUpdateMap builds a request body from UpdateOpts.
46+
func (opts UpdateOpts) ToQuotaUpdateMap() (map[string]interface{}, error) {
47+
return gophercloud.BuildRequestBody(opts, "")
48+
}
49+
50+
// Update accepts a UpdateOpts struct and updates an existing DNS Quotas using the
51+
// values provided.
52+
func Update(ctx context.Context, c *gophercloud.ServiceClient, projectID string, opts UpdateOptsBuilder) (r Result) {
53+
b, err := opts.ToQuotaUpdateMap()
54+
if err != nil {
55+
r.Err = err
56+
return
57+
}
58+
resp, err := c.Patch(ctx, URL(c, projectID), b, &r.Body, &gophercloud.RequestOpts{
59+
OkCodes: []int{200},
60+
})
61+
_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
62+
return
63+
}

openstack/dns/v2/quotas/results.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package quotas
2+
3+
import (
4+
"github.com/gophercloud/gophercloud/v2"
5+
)
6+
7+
// Extract interprets a GetResult, CreateResult or UpdateResult as a Quota.
8+
// An error is returned if the original call or the extraction failed.
9+
func (r Result) Extract() (*Quota, error) {
10+
var s *Quota
11+
err := r.ExtractInto(&s)
12+
return s, err
13+
}
14+
15+
// ListResult is the result of a Create request. Call its Extract method
16+
// to interpret the result as a Zone.
17+
type Result struct {
18+
gophercloud.Result
19+
}
20+
21+
// Quota represents a quotas on the system.
22+
type Quota struct {
23+
APIExporterSize int `json:"api_export_size"`
24+
RecordsetRecords int `json:"recordset_records"`
25+
ZoneRecords int `json:"zone_records"`
26+
ZoneRecordsets int `json:"zone_recordsets"`
27+
Zones int `json:"zones"`
28+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// zones unit tests
2+
package testing
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package testing
2+
3+
import (
4+
"fmt"
5+
"net/http"
6+
"testing"
7+
8+
th "github.com/gophercloud/gophercloud/v2/testhelper"
9+
"github.com/gophercloud/gophercloud/v2/testhelper/client"
10+
11+
"github.com/gophercloud/gophercloud/v2/openstack/dns/v2/quotas"
12+
)
13+
14+
// List Output is a sample response to a List call.
15+
const QuotaOutput = `
16+
{
17+
"api_export_size": 1000,
18+
"recordset_records": 20,
19+
"zone_records": 500,
20+
"zone_recordsets": 500,
21+
"zones": 100
22+
}
23+
`
24+
25+
// UpdateQuotaRequest is a sample request body for updating quotas.
26+
const UpdateQuotaRequest = `
27+
{
28+
"zones": 100
29+
}
30+
`
31+
32+
var (
33+
Quota = &quotas.Quota{
34+
APIExporterSize: 1000,
35+
RecordsetRecords: 20,
36+
ZoneRecords: 500,
37+
ZoneRecordsets: 500,
38+
Zones: 100,
39+
}
40+
)
41+
42+
// HandleGetSuccessfully configures the test server to respond to a Get request.
43+
func HandleGetSuccessfully(t *testing.T, fakeServer th.FakeServer) {
44+
fakeServer.Mux.HandleFunc("/quotas/a86dba58-0043-4cc6-a1bb-69d5e86f3ca3", func(w http.ResponseWriter, r *http.Request) {
45+
th.TestMethod(t, r, "GET")
46+
th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
47+
48+
w.Header().Add("Content-Type", "application/json")
49+
fmt.Fprint(w, QuotaOutput)
50+
})
51+
}
52+
53+
// HandleUpdateSuccessfully configures the test server to respond to an Update request.
54+
func HandleUpdateSuccessfully(t *testing.T, fakeServer th.FakeServer) {
55+
fakeServer.Mux.HandleFunc("/quotas/a86dba58-0043-4cc6-a1bb-69d5e86f3ca3", func(w http.ResponseWriter, r *http.Request) {
56+
th.TestMethod(t, r, "PATCH")
57+
th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
58+
th.TestJSONRequest(t, r, UpdateQuotaRequest)
59+
60+
w.Header().Add("Content-Type", "application/json")
61+
fmt.Fprint(w, QuotaOutput)
62+
})
63+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package testing
2+
3+
import (
4+
"context"
5+
"testing"
6+
7+
"github.com/gophercloud/gophercloud/v2/openstack/dns/v2/quotas"
8+
th "github.com/gophercloud/gophercloud/v2/testhelper"
9+
"github.com/gophercloud/gophercloud/v2/testhelper/client"
10+
)
11+
12+
func TestGet(t *testing.T) {
13+
fakeServer := th.SetupHTTP()
14+
defer fakeServer.Teardown()
15+
HandleGetSuccessfully(t, fakeServer)
16+
17+
actual, err := quotas.Get(context.TODO(), client.ServiceClient(fakeServer), "a86dba58-0043-4cc6-a1bb-69d5e86f3ca3").Extract()
18+
th.AssertNoErr(t, err)
19+
th.CheckDeepEquals(t, Quota, actual)
20+
}
21+
22+
func TestUpdate(t *testing.T) {
23+
fakeServer := th.SetupHTTP()
24+
defer fakeServer.Teardown()
25+
HandleUpdateSuccessfully(t, fakeServer)
26+
27+
zones := 100
28+
updateOpts := quotas.UpdateOpts{
29+
Zones: &zones,
30+
}
31+
32+
actual, err := quotas.Update(context.TODO(), client.ServiceClient(fakeServer), "a86dba58-0043-4cc6-a1bb-69d5e86f3ca3", updateOpts).Extract()
33+
th.AssertNoErr(t, err)
34+
th.CheckDeepEquals(t, Quota, actual)
35+
}

openstack/dns/v2/quotas/urls.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package quotas
2+
3+
import "github.com/gophercloud/gophercloud/v2"
4+
5+
func URL(c *gophercloud.ServiceClient, projectID string) string {
6+
return c.ServiceURL("quotas", projectID)
7+
}

0 commit comments

Comments
 (0)