Skip to content

Commit aae05d6

Browse files
authored
Add ability to set filters to GetProjectQuotas method (#176)
1 parent 4760293 commit aae05d6

File tree

4 files changed

+147
-9
lines changed

4 files changed

+147
-9
lines changed

.golangci.yml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
run:
2-
deadline: 5m
2+
timeout: 5m
33

44
linters:
55
fast: false
@@ -51,4 +51,7 @@ linters-settings:
5151
goimports:
5252
local-prefixes: github.com/selectel/go-selvpcclient
5353
gci:
54-
local-prefixes: github.com/selectel/go-selvpcclient
54+
sections:
55+
- standard
56+
- default
57+
- prefix(github.com/selectel/go-selvpcclient)

selvpcclient/quotamanager/quotas/requests.go

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package quotas
33
import (
44
"fmt"
55
"net/http"
6+
"net/url"
67
"strings"
78

89
"github.com/selectel/go-selvpcclient/v4/selvpcclient"
@@ -11,6 +12,17 @@ import (
1112

1213
const resourcePrefix = "projects"
1314

15+
func WithResourceFilter(name string) func(url.Values) {
16+
return func(query url.Values) {
17+
if query == nil {
18+
query = make(url.Values)
19+
}
20+
if name != "" {
21+
query.Add("resource", name)
22+
}
23+
}
24+
}
25+
1426
// GetLimits returns limits for a single project referenced by id in specific region.
1527
func GetLimits(client *selvpcclient.Client, projectID, region string,
1628
) ([]*Quota, *clientservices.ResponseResult, error) {
@@ -19,9 +31,9 @@ func GetLimits(client *selvpcclient.Client, projectID, region string,
1931
return nil, nil, fmt.Errorf("failed to get endpoint, err: %w", err)
2032
}
2133

22-
url := strings.Join([]string{endpoint, resourcePrefix, projectID, "limits"}, "/")
34+
fullURL := strings.Join([]string{endpoint, resourcePrefix, projectID, "limits"}, "/")
2335

24-
responseResult, err := client.QuotaManager.Requests.Do(http.MethodGet, url, &clientservices.RequestOptions{
36+
responseResult, err := client.QuotaManager.Requests.Do(http.MethodGet, fullURL, &clientservices.RequestOptions{
2537
OkCodes: []int{200},
2638
})
2739
if err != nil {
@@ -43,16 +55,21 @@ func GetLimits(client *selvpcclient.Client, projectID, region string,
4355
}
4456

4557
// GetProjectQuotas returns the quotas info for a single project referenced by id in specific region.
46-
func GetProjectQuotas(client *selvpcclient.Client, projectID, region string,
58+
func GetProjectQuotas(client *selvpcclient.Client, projectID, region string, options ...func(url.Values),
4759
) ([]*Quota, *clientservices.ResponseResult, error) {
60+
resourceFilters := url.Values{}
61+
for _, opts := range options {
62+
opts(resourceFilters)
63+
}
4864
endpoint, err := client.QuotaManager.GetEndpoint(region)
4965
if err != nil {
5066
return nil, nil, fmt.Errorf("failed to get endpoint, err: %w", err)
5167
}
5268

53-
url := strings.Join([]string{endpoint, resourcePrefix, projectID, "quotas"}, "/")
69+
baseURL := strings.Join([]string{endpoint, resourcePrefix, projectID, "quotas"}, "/")
70+
fullURL := baseURL + "?" + resourceFilters.Encode()
5471

55-
responseResult, err := client.QuotaManager.Requests.Do(http.MethodGet, url, &clientservices.RequestOptions{
72+
responseResult, err := client.QuotaManager.Requests.Do(http.MethodGet, fullURL, &clientservices.RequestOptions{
5673
OkCodes: []int{200},
5774
})
5875
if err != nil {
@@ -82,9 +99,9 @@ func UpdateProjectQuotas(client *selvpcclient.Client, projectID, region string,
8299
return nil, nil, fmt.Errorf("failed to get endpoint, err: %w", err)
83100
}
84101

85-
url := strings.Join([]string{endpoint, resourcePrefix, projectID, "quotas"}, "/")
102+
fullURL := strings.Join([]string{endpoint, resourcePrefix, projectID, "quotas"}, "/")
86103

87-
responseResult, err := client.QuotaManager.Requests.Do(http.MethodPatch, url, &clientservices.RequestOptions{
104+
responseResult, err := client.QuotaManager.Requests.Do(http.MethodPatch, fullURL, &clientservices.RequestOptions{
88105
JSONBody: &updateOpts,
89106
OkCodes: []int{200},
90107
})

selvpcclient/quotamanager/quotas/testing/fixtures.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,26 @@ const TestGetProjectQuotasResponseSingleRaw = `
6262
}
6363
`
6464

65+
// TestGetProjectQuotasResponseSingleRaw represents a raw response with a single quota from the GetProject request.
66+
const TestGetProjectQuotasResponseFilteredRaw = `
67+
{
68+
"quotas": {
69+
"compute_ram": [
70+
{
71+
"value": 51200,
72+
"zone": "ru-1a"
73+
}
74+
],
75+
"compute_cores": [
76+
{
77+
"value": 300,
78+
"zone": "ru-1a"
79+
}
80+
]
81+
}
82+
}
83+
`
84+
6585
// TestGetProjectQuotasResponseSingle represents the unmarshalled TestGetProjectQuotasResponseSingleRaw response.
6686
var TestGetProjectQuotasResponseSingle = []*quotas.Quota{
6787
{
@@ -76,6 +96,30 @@ var TestGetProjectQuotasResponseSingle = []*quotas.Quota{
7696
},
7797
}
7898

99+
// TestGetProjectQuotasResponseFiltered represents the unmarshalled TestGetProjectQuotasResponseFilteredRaw response.
100+
var TestGetProjectQuotasResponseFiltered = []*quotas.Quota{
101+
{
102+
Name: "compute_ram",
103+
ResourceQuotasEntities: []quotas.ResourceQuotaEntity{
104+
{
105+
Zone: "ru-1a",
106+
Value: 51200,
107+
},
108+
},
109+
ResourceQuotasErrors: []quotas.ResourceError{},
110+
},
111+
{
112+
Name: "compute_cores",
113+
ResourceQuotasEntities: []quotas.ResourceQuotaEntity{
114+
{
115+
Zone: "ru-1a",
116+
Value: 300,
117+
},
118+
},
119+
ResourceQuotasErrors: []quotas.ResourceError{},
120+
},
121+
}
122+
79123
var (
80124
ramQuotaZone = "ru-1a"
81125
ramQuotaValue = 64000

selvpcclient/quotamanager/quotas/testing/requests_test.go

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,80 @@ func TestGetProjectQuotasSingle(t *testing.T) {
202202
}
203203
}
204204

205+
func TestGetProjectQuotasFiltered(t *testing.T) {
206+
endpointCalled := false
207+
208+
testEnv := testutils.SetupTestEnv()
209+
defer testEnv.TearDownTestEnv()
210+
testEnv.NewSelVPCClient()
211+
testutils.HandleReqWithoutBody(t, &testutils.HandleReqOpts{
212+
Mux: testEnv.Mux,
213+
URL: strings.Join([]string{testBaseURL, "quotas"}, "/"),
214+
RawResponse: TestGetProjectQuotasResponseFilteredRaw,
215+
Method: http.MethodGet,
216+
Status: http.StatusOK,
217+
CallFlag: &endpointCalled,
218+
})
219+
220+
actual, _, err := quotas.GetProjectQuotas(
221+
testEnv.Client,
222+
testProjectID,
223+
testRegion,
224+
quotas.WithResourceFilter("compute_ram"),
225+
quotas.WithResourceFilter("compute_cores"),
226+
)
227+
if err != nil {
228+
t.Fatal(err)
229+
}
230+
231+
expected := TestGetProjectQuotasResponseFiltered
232+
233+
if !endpointCalled {
234+
t.Fatal("endpoint wasn't called")
235+
}
236+
if !reflect.DeepEqual(actual, expected) {
237+
t.Fatalf("expected %#v, but got %#v", expected, actual)
238+
}
239+
}
240+
241+
func TestGetProjectQuotasFilteredHTTPError(t *testing.T) {
242+
endpointCalled := false
243+
244+
testEnv := testutils.SetupTestEnv()
245+
defer testEnv.TearDownTestEnv()
246+
testEnv.NewSelVPCClient()
247+
testutils.HandleReqWithoutBody(t, &testutils.HandleReqOpts{
248+
Mux: testEnv.Mux,
249+
URL: strings.Join([]string{testBaseURL, "quotas"}, "/"),
250+
RawResponse: TestGetProjectQuotasResponseRaw,
251+
Method: http.MethodGet,
252+
Status: http.StatusBadGateway,
253+
CallFlag: &endpointCalled,
254+
})
255+
256+
allQuotas, httpResponse, err := quotas.GetProjectQuotas(
257+
testEnv.Client,
258+
testProjectID,
259+
testRegion,
260+
quotas.WithResourceFilter("compute_ram"),
261+
quotas.WithResourceFilter("compute_cores"),
262+
)
263+
264+
if !endpointCalled {
265+
t.Fatal("endpoint wasn't called")
266+
}
267+
if allQuotas != nil {
268+
t.Fatal("expected no quotas from the GetProjectQuotas method")
269+
}
270+
if err == nil {
271+
t.Fatal("expected error from the GetProjectQuotas method")
272+
}
273+
if httpResponse.StatusCode != http.StatusBadGateway {
274+
t.Fatalf("expected %d status in the HTTP response, but got %d",
275+
http.StatusBadGateway, httpResponse.StatusCode)
276+
}
277+
}
278+
205279
func TestGetProjectQuotasHTTPError(t *testing.T) {
206280
endpointCalled := false
207281

0 commit comments

Comments
 (0)