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

Skip to content

Commit b8eb8c0

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

File tree

8 files changed

+264
-0
lines changed

8 files changed

+264
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
//go:build acceptance || dns || quotas
2+
3+
package v2
4+
5+
import (
6+
"context"
7+
"testing"
8+
9+
"github.com/gophercloud/gophercloud/v2/internal/acceptance/clients"
10+
identity "github.com/gophercloud/gophercloud/v2/internal/acceptance/openstack/identity/v3"
11+
"github.com/gophercloud/gophercloud/v2/internal/acceptance/tools"
12+
"github.com/gophercloud/gophercloud/v2/openstack/dns/v2/quotas"
13+
th "github.com/gophercloud/gophercloud/v2/testhelper"
14+
)
15+
16+
func TestQuotaGetUpdate(t *testing.T) {
17+
clients.RequireAdmin(t)
18+
19+
client, err := clients.NewDNSV2Client()
20+
th.AssertNoErr(t, err)
21+
22+
identityClient, err := clients.NewIdentityV3Client()
23+
th.AssertNoErr(t, err)
24+
25+
project, err := identity.CreateProject(t, identityClient, nil)
26+
th.AssertNoErr(t, err)
27+
defer identity.DeleteProject(t, identityClient, project.ID)
28+
29+
// use DNS specific header to set the project ID
30+
client.MoreHeaders = map[string]string{
31+
"X-Auth-Sudo-Tenant-ID": project.ID,
32+
}
33+
34+
// test Get Quota
35+
quota, err := quotas.Get(context.TODO(), client, project.ID).Extract()
36+
th.AssertNoErr(t, err)
37+
38+
tools.PrintResource(t, quota)
39+
40+
// test Update Quota
41+
zones := 9
42+
updateOpts := quotas.UpdateOpts{
43+
Zones: &zones,
44+
}
45+
res, err := quotas.Update(context.TODO(), client, project.ID, updateOpts).Extract()
46+
th.AssertNoErr(t, err)
47+
48+
tools.PrintResource(t, res)
49+
50+
quota.Zones = zones
51+
th.AssertDeepEquals(t, *quota, *res)
52+
}

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

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)