-
Notifications
You must be signed in to change notification settings - Fork 0
/
quotas.go
181 lines (143 loc) · 5.47 KB
/
quotas.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package edgecloud
import (
"context"
"fmt"
"net/http"
)
const (
quotasClientBasePathV2 = "/v2/quotas_client"
quotasGlobalBasePathV2 = "/v2/quotas_global"
quotasRegionalBasePathV2 = "/v2/quotas_regional"
)
const (
quotasNotificationThreshold = "notification_threshold"
)
// QuotasService is an interface for creating and managing Quotas with the EdgecenterCloud API.
// See: https://apidocs.edgecenter.ru/cloud#tag/quotas
type QuotasService interface {
ListCombined(context.Context, *ListCombinedOptions) (*CombinedQuota, *Response, error)
ListGlobal(context.Context, int) (*Quota, *Response, error)
ListRegional(context.Context, int, int) (*Quota, *Response, error)
DeleteNotificationThreshold(context.Context, int) (*Response, error)
GetNotificationThreshold(context.Context, int) (*QuotaNotificationThreshold, *Response, error)
UpdateNotificationThreshold(context.Context, int, *NotificationThresholdUpdateRequest) (*QuotaNotificationThreshold, *Response, error)
}
// QuotasServiceOp handles communication with Quotas methods of the EdgecenterCloud API.
type QuotasServiceOp struct {
client *Client
}
var _ QuotasService = &QuotasServiceOp{}
type Quota map[string]int
type CombinedQuota struct {
GlobalQuotas Quota `json:"global_quotas"`
RegionalQuotas []Quota `json:"regional_quotas"`
}
type QuotaNotificationThreshold struct {
LastMessage CombinedQuota `json:"last_message"`
LastSending string `json:"last_sending"`
Threshold int `json:"threshold"`
ClientID int `json:"client_id"`
}
type NotificationThresholdUpdateRequest struct {
LastMessage CombinedQuota `json:"last_message,omitempty" validate:"omitempty"`
LastSending string `json:"last_sending,omitempty" validate:"omitempty"`
Threshold int `json:"threshold" validate:"required"`
}
// ListCombinedOptions specifies the query parameters to ListCombined method.
type ListCombinedOptions struct {
ClientID int `url:"client_id,omitempty" validate:"omitempty"`
}
// ListCombined get combined client quotas, regional and global.
func (s *QuotasServiceOp) ListCombined(ctx context.Context, opts *ListCombinedOptions) (*CombinedQuota, *Response, error) {
path, err := addOptions(quotasClientBasePathV2, opts)
if err != nil {
return nil, nil, err
}
req, err := s.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
quota := new(CombinedQuota)
resp, err := s.client.Do(ctx, req, quota)
if err != nil {
return nil, resp, err
}
return quota, resp, err
}
// ListGlobal get a global quota.
func (s *QuotasServiceOp) ListGlobal(ctx context.Context, clientID int) (*Quota, *Response, error) {
if clientID == 0 {
return nil, nil, NewArgError("clientID", "no value specified")
}
path := fmt.Sprintf("%s/%d", quotasGlobalBasePathV2, clientID)
req, err := s.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
quota := new(Quota)
resp, err := s.client.Do(ctx, req, quota)
if err != nil {
return nil, resp, err
}
return quota, resp, err
}
// ListRegional get a quota by region.
func (s *QuotasServiceOp) ListRegional(ctx context.Context, clientID, regionID int) (*Quota, *Response, error) {
if clientID == 0 {
return nil, nil, NewArgError("clientID", "no value specified")
}
if regionID == 0 {
return nil, nil, NewArgError("regionID", "no value specified")
}
path := fmt.Sprintf("%s/%d/%d", quotasRegionalBasePathV2, clientID, regionID)
req, err := s.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
quota := new(Quota)
resp, err := s.client.Do(ctx, req, quota)
if err != nil {
return nil, resp, err
}
return quota, resp, err
}
// DeleteNotificationThreshold delete a client's quota notification threshold.
func (s *QuotasServiceOp) DeleteNotificationThreshold(ctx context.Context, clientID int) (*Response, error) {
path := fmt.Sprintf("%s/%d/%s", quotasClientBasePathV2, clientID, quotasNotificationThreshold)
req, err := s.client.NewRequest(ctx, http.MethodDelete, path, nil)
if err != nil {
return nil, err
}
return s.client.Do(ctx, req, nil)
}
// GetNotificationThreshold get a client's quota notification threshold.
func (s *QuotasServiceOp) GetNotificationThreshold(ctx context.Context, clientID int) (*QuotaNotificationThreshold, *Response, error) {
path := fmt.Sprintf("%s/%d/%s", quotasClientBasePathV2, clientID, quotasNotificationThreshold)
req, err := s.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
quotaNotificationThreshold := new(QuotaNotificationThreshold)
resp, err := s.client.Do(ctx, req, quotaNotificationThreshold)
if err != nil {
return nil, resp, err
}
return quotaNotificationThreshold, resp, err
}
// UpdateNotificationThreshold update or create a client's quota notification threshold.
func (s *QuotasServiceOp) UpdateNotificationThreshold(ctx context.Context, clientID int, reqBody *NotificationThresholdUpdateRequest) (*QuotaNotificationThreshold, *Response, error) {
if reqBody == nil {
return nil, nil, NewArgError("reqBody", "cannot be nil")
}
path := fmt.Sprintf("%s/%d/%s", quotasClientBasePathV2, clientID, quotasNotificationThreshold)
req, err := s.client.NewRequest(ctx, http.MethodPut, path, reqBody)
if err != nil {
return nil, nil, err
}
quotaNotificationThreshold := new(QuotaNotificationThreshold)
resp, err := s.client.Do(ctx, req, quotaNotificationThreshold)
if err != nil {
return nil, resp, err
}
return quotaNotificationThreshold, resp, err
}