Skip to content

Commit 96c819d

Browse files
authored
feat(obs-paas): add ListPlans and SelectPlan endpoints (#1621)
1 parent 1432d92 commit 96c819d

File tree

1 file changed

+190
-0
lines changed

1 file changed

+190
-0
lines changed

api/cockpit/v1beta1/cockpit_sdk.go

Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,36 @@ func (enum *ListGrafanaUsersRequestOrderBy) UnmarshalJSON(data []byte) error {
147147
return nil
148148
}
149149

150+
type ListPlansRequestOrderBy string
151+
152+
const (
153+
ListPlansRequestOrderByNameAsc = ListPlansRequestOrderBy("name_asc")
154+
ListPlansRequestOrderByNameDesc = ListPlansRequestOrderBy("name_desc")
155+
)
156+
157+
func (enum ListPlansRequestOrderBy) String() string {
158+
if enum == "" {
159+
// return default value if empty
160+
return "name_asc"
161+
}
162+
return string(enum)
163+
}
164+
165+
func (enum ListPlansRequestOrderBy) MarshalJSON() ([]byte, error) {
166+
return []byte(fmt.Sprintf(`"%s"`, enum)), nil
167+
}
168+
169+
func (enum *ListPlansRequestOrderBy) UnmarshalJSON(data []byte) error {
170+
tmp := ""
171+
172+
if err := json.Unmarshal(data, &tmp); err != nil {
173+
return err
174+
}
175+
176+
*enum = ListPlansRequestOrderBy(ListPlansRequestOrderBy(tmp).String())
177+
return nil
178+
}
179+
150180
type ListTokensRequestOrderBy string
151181

152182
const (
@@ -179,6 +209,38 @@ func (enum *ListTokensRequestOrderBy) UnmarshalJSON(data []byte) error {
179209
return nil
180210
}
181211

212+
type PlanName string
213+
214+
const (
215+
PlanNameUnknownName = PlanName("unknown_name")
216+
PlanNameFree = PlanName("free")
217+
PlanNamePremium = PlanName("premium")
218+
PlanNameCustom = PlanName("custom")
219+
)
220+
221+
func (enum PlanName) String() string {
222+
if enum == "" {
223+
// return default value if empty
224+
return "unknown_name"
225+
}
226+
return string(enum)
227+
}
228+
229+
func (enum PlanName) MarshalJSON() ([]byte, error) {
230+
return []byte(fmt.Sprintf(`"%s"`, enum)), nil
231+
}
232+
233+
func (enum *PlanName) UnmarshalJSON(data []byte) error {
234+
tmp := ""
235+
236+
if err := json.Unmarshal(data, &tmp); err != nil {
237+
return err
238+
}
239+
240+
*enum = PlanName(PlanName(tmp).String())
241+
return nil
242+
}
243+
182244
// Cockpit: cockpit.
183245
type Cockpit struct {
184246
// ProjectID: project ID.
@@ -194,6 +256,8 @@ type Cockpit struct {
194256
Status CockpitStatus `json:"status"`
195257
// ManagedAlertsEnabled: managed alerts enabled.
196258
ManagedAlertsEnabled bool `json:"managed_alerts_enabled"`
259+
// Plan: pricing plan.
260+
Plan *Plan `json:"plan"`
197261
}
198262

199263
// CockpitEndpoints: cockpit. endpoints.
@@ -255,13 +319,45 @@ type ListGrafanaUsersResponse struct {
255319
GrafanaUsers []*GrafanaUser `json:"grafana_users"`
256320
}
257321

322+
// ListPlansResponse: list all pricing plans response.
323+
// List plans response.
324+
type ListPlansResponse struct {
325+
TotalCount uint64 `json:"total_count"`
326+
327+
Plans []*Plan `json:"plans"`
328+
}
329+
258330
// ListTokensResponse: list tokens response.
259331
type ListTokensResponse struct {
260332
TotalCount uint32 `json:"total_count"`
261333

262334
Tokens []*Token `json:"tokens"`
263335
}
264336

337+
// Plan: plan.
338+
type Plan struct {
339+
// ID: plan id.
340+
ID string `json:"id"`
341+
// Name: plan name.
342+
// Default value: unknown_name
343+
Name PlanName `json:"name"`
344+
// RetentionMetricsInterval: retention for metrics.
345+
RetentionMetricsInterval *scw.Duration `json:"retention_metrics_interval"`
346+
// RetentionLogsInterval: retention for logs.
347+
RetentionLogsInterval *scw.Duration `json:"retention_logs_interval"`
348+
// SampleIngestionPrice: ingestion price for 1million samples in cents.
349+
SampleIngestionPrice uint32 `json:"sample_ingestion_price"`
350+
// LogsIngestionPrice: ingestion price in cents for 1 Go of logs.
351+
LogsIngestionPrice uint32 `json:"logs_ingestion_price"`
352+
// RetentionPrice: retention price in euros per month.
353+
RetentionPrice uint32 `json:"retention_price"`
354+
}
355+
356+
// SelectPlanResponse: select pricing plan response.
357+
// Select plan response.
358+
type SelectPlanResponse struct {
359+
}
360+
265361
// Token: token.
266362
type Token struct {
267363
ID string `json:"id"`
@@ -992,6 +1088,81 @@ func (s *API) ResetGrafanaUserPassword(req *ResetGrafanaUserPasswordRequest, opt
9921088
return &resp, nil
9931089
}
9941090

1091+
type ListPlansRequest struct {
1092+
Page *int32 `json:"-"`
1093+
1094+
PageSize *uint32 `json:"-"`
1095+
// OrderBy: default value: name_asc
1096+
OrderBy ListPlansRequestOrderBy `json:"-"`
1097+
}
1098+
1099+
// ListPlans: list plans.
1100+
// List all pricing plans.
1101+
func (s *API) ListPlans(req *ListPlansRequest, opts ...scw.RequestOption) (*ListPlansResponse, error) {
1102+
var err error
1103+
1104+
defaultPageSize, exist := s.client.GetDefaultPageSize()
1105+
if (req.PageSize == nil || *req.PageSize == 0) && exist {
1106+
req.PageSize = &defaultPageSize
1107+
}
1108+
1109+
query := url.Values{}
1110+
parameter.AddToQuery(query, "page", req.Page)
1111+
parameter.AddToQuery(query, "page_size", req.PageSize)
1112+
parameter.AddToQuery(query, "order_by", req.OrderBy)
1113+
1114+
scwReq := &scw.ScalewayRequest{
1115+
Method: "GET",
1116+
Path: "/cockpit/v1beta1/plans",
1117+
Query: query,
1118+
Headers: http.Header{},
1119+
}
1120+
1121+
var resp ListPlansResponse
1122+
1123+
err = s.client.Do(scwReq, &resp, opts...)
1124+
if err != nil {
1125+
return nil, err
1126+
}
1127+
return &resp, nil
1128+
}
1129+
1130+
type SelectPlanRequest struct {
1131+
ProjectID string `json:"project_id"`
1132+
1133+
PlanID string `json:"plan_id"`
1134+
}
1135+
1136+
// SelectPlan: select pricing plan.
1137+
// Select the wanted pricing plan.
1138+
func (s *API) SelectPlan(req *SelectPlanRequest, opts ...scw.RequestOption) (*SelectPlanResponse, error) {
1139+
var err error
1140+
1141+
if req.ProjectID == "" {
1142+
defaultProjectID, _ := s.client.GetDefaultProjectID()
1143+
req.ProjectID = defaultProjectID
1144+
}
1145+
1146+
scwReq := &scw.ScalewayRequest{
1147+
Method: "POST",
1148+
Path: "/cockpit/v1beta1/select-plan",
1149+
Headers: http.Header{},
1150+
}
1151+
1152+
err = scwReq.SetBody(req)
1153+
if err != nil {
1154+
return nil, err
1155+
}
1156+
1157+
var resp SelectPlanResponse
1158+
1159+
err = s.client.Do(scwReq, &resp, opts...)
1160+
if err != nil {
1161+
return nil, err
1162+
}
1163+
return &resp, nil
1164+
}
1165+
9951166
// UnsafeGetTotalCount should not be used
9961167
// Internal usage only
9971168
func (r *ListTokensResponse) UnsafeGetTotalCount() uint32 {
@@ -1048,3 +1219,22 @@ func (r *ListGrafanaUsersResponse) UnsafeAppend(res interface{}) (uint32, error)
10481219
r.TotalCount += uint32(len(results.GrafanaUsers))
10491220
return uint32(len(results.GrafanaUsers)), nil
10501221
}
1222+
1223+
// UnsafeGetTotalCount should not be used
1224+
// Internal usage only
1225+
func (r *ListPlansResponse) UnsafeGetTotalCount() uint64 {
1226+
return r.TotalCount
1227+
}
1228+
1229+
// UnsafeAppend should not be used
1230+
// Internal usage only
1231+
func (r *ListPlansResponse) UnsafeAppend(res interface{}) (uint64, error) {
1232+
results, ok := res.(*ListPlansResponse)
1233+
if !ok {
1234+
return 0, errors.New("%T type cannot be appended to type %T", res, r)
1235+
}
1236+
1237+
r.Plans = append(r.Plans, results.Plans...)
1238+
r.TotalCount += uint64(len(results.Plans))
1239+
return uint64(len(results.Plans)), nil
1240+
}

0 commit comments

Comments
 (0)