Skip to content

Commit 5f0d193

Browse files
authored
chore(cockpit): migrate AnyAlertState to AlertState (scaleway#2425)
1 parent a762d31 commit 5f0d193

File tree

1 file changed

+20
-170
lines changed

1 file changed

+20
-170
lines changed

api/cockpit/v1/cockpit_sdk.go

Lines changed: 20 additions & 170 deletions
Original file line numberDiff line numberDiff line change
@@ -39,50 +39,47 @@ var (
3939
_ = namegenerator.GetRandomName
4040
)
4141

42-
type AnyAlertState string
42+
type AlertState string
4343

4444
const (
45-
AnyAlertStateUnknownState = AnyAlertState("unknown_state")
46-
// The alert is turned off and will never fire.
47-
AnyAlertStateDisabled = AnyAlertState("disabled")
48-
// The alert is active and may transition to `pending` or `firing` if its conditions are met.
49-
AnyAlertStateEnabled = AnyAlertState("enabled")
45+
AlertStateUnknownState = AlertState("unknown_state")
46+
// The alert is inactive and may transition to `pending` or `firing` if its conditions are met.
47+
AlertStateInactive = AlertState("inactive")
5048
// The alert's conditions are met. They must persist for the configured duration before transitioning to the `firing` state.
51-
AnyAlertStatePending = AnyAlertState("pending")
49+
AlertStatePending = AlertState("pending")
5250
// The alert's conditions, including the required duration, have been fully met.
53-
AnyAlertStateFiring = AnyAlertState("firing")
51+
AlertStateFiring = AlertState("firing")
5452
)
5553

56-
func (enum AnyAlertState) String() string {
54+
func (enum AlertState) String() string {
5755
if enum == "" {
5856
// return default value if empty
5957
return "unknown_state"
6058
}
6159
return string(enum)
6260
}
6361

64-
func (enum AnyAlertState) Values() []AnyAlertState {
65-
return []AnyAlertState{
62+
func (enum AlertState) Values() []AlertState {
63+
return []AlertState{
6664
"unknown_state",
67-
"disabled",
68-
"enabled",
65+
"inactive",
6966
"pending",
7067
"firing",
7168
}
7269
}
7370

74-
func (enum AnyAlertState) MarshalJSON() ([]byte, error) {
71+
func (enum AlertState) MarshalJSON() ([]byte, error) {
7572
return []byte(fmt.Sprintf(`"%s"`, enum)), nil
7673
}
7774

78-
func (enum *AnyAlertState) UnmarshalJSON(data []byte) error {
75+
func (enum *AlertState) UnmarshalJSON(data []byte) error {
7976
tmp := ""
8077

8178
if err := json.Unmarshal(data, &tmp); err != nil {
8279
return err
8380
}
8481

85-
*enum = AnyAlertState(AnyAlertState(tmp).String())
82+
*enum = AlertState(AlertState(tmp).String())
8683
return nil
8784
}
8885

@@ -300,51 +297,6 @@ func (enum *ListGrafanaUsersRequestOrderBy) UnmarshalJSON(data []byte) error {
300297
return nil
301298
}
302299

303-
type ListManagedAlertsRequestOrderBy string
304-
305-
const (
306-
ListManagedAlertsRequestOrderByCreatedAtAsc = ListManagedAlertsRequestOrderBy("created_at_asc")
307-
ListManagedAlertsRequestOrderByCreatedAtDesc = ListManagedAlertsRequestOrderBy("created_at_desc")
308-
ListManagedAlertsRequestOrderByNameAsc = ListManagedAlertsRequestOrderBy("name_asc")
309-
ListManagedAlertsRequestOrderByNameDesc = ListManagedAlertsRequestOrderBy("name_desc")
310-
ListManagedAlertsRequestOrderByTypeAsc = ListManagedAlertsRequestOrderBy("type_asc")
311-
ListManagedAlertsRequestOrderByTypeDesc = ListManagedAlertsRequestOrderBy("type_desc")
312-
)
313-
314-
func (enum ListManagedAlertsRequestOrderBy) String() string {
315-
if enum == "" {
316-
// return default value if empty
317-
return "created_at_asc"
318-
}
319-
return string(enum)
320-
}
321-
322-
func (enum ListManagedAlertsRequestOrderBy) Values() []ListManagedAlertsRequestOrderBy {
323-
return []ListManagedAlertsRequestOrderBy{
324-
"created_at_asc",
325-
"created_at_desc",
326-
"name_asc",
327-
"name_desc",
328-
"type_asc",
329-
"type_desc",
330-
}
331-
}
332-
333-
func (enum ListManagedAlertsRequestOrderBy) MarshalJSON() ([]byte, error) {
334-
return []byte(fmt.Sprintf(`"%s"`, enum)), nil
335-
}
336-
337-
func (enum *ListManagedAlertsRequestOrderBy) UnmarshalJSON(data []byte) error {
338-
tmp := ""
339-
340-
if err := json.Unmarshal(data, &tmp); err != nil {
341-
return err
342-
}
343-
344-
*enum = ListManagedAlertsRequestOrderBy(ListManagedAlertsRequestOrderBy(tmp).String())
345-
return nil
346-
}
347-
348300
type ListPlansRequestOrderBy string
349301

350302
const (
@@ -580,8 +532,8 @@ type GetConfigResponseRetention struct {
580532
DefaultDays uint32 `json:"default_days"`
581533
}
582534

583-
// AnyAlert: any alert.
584-
type AnyAlert struct {
535+
// Alert: alert.
536+
type Alert struct {
585537
// Region: region to target. If none is passed will use default region from the config.
586538
Region scw.Region `json:"region"`
587539

@@ -593,8 +545,10 @@ type AnyAlert struct {
593545

594546
Duration string `json:"duration"`
595547

548+
Enabled bool `json:"enabled"`
549+
596550
// State: default value: unknown_state
597-
State AnyAlertState `json:"state"`
551+
State *AlertState `json:"state"`
598552

599553
Annotations map[string]string `json:"annotations"`
600554
}
@@ -684,19 +638,6 @@ type GrafanaUser struct {
684638
Password *string `json:"password"`
685639
}
686640

687-
// Alert: alert.
688-
type Alert struct {
689-
ProductFamily string `json:"product_family"`
690-
691-
Product string `json:"product"`
692-
693-
Name string `json:"name"`
694-
695-
Rule string `json:"rule"`
696-
697-
Description string `json:"description"`
698-
}
699-
700641
// Plan: Type of pricing plan.
701642
type Plan struct {
702643
// Name: name of a given pricing plan.
@@ -938,7 +879,7 @@ type ListAlertsResponse struct {
938879
TotalCount uint64 `json:"total_count"`
939880

940881
// Alerts: list of alerts matching the applied filters.
941-
Alerts []*AnyAlert `json:"alerts"`
882+
Alerts []*Alert `json:"alerts"`
942883
}
943884

944885
// UnsafeGetTotalCount should not be used
@@ -1078,34 +1019,6 @@ func (r *ListGrafanaUsersResponse) UnsafeAppend(res interface{}) (uint64, error)
10781019
return uint64(len(results.GrafanaUsers)), nil
10791020
}
10801021

1081-
// ListManagedAlertsResponse: Response returned when listing data sources.
1082-
type ListManagedAlertsResponse struct {
1083-
// TotalCount: total count of data sources matching the request.
1084-
TotalCount uint64 `json:"total_count"`
1085-
1086-
// Alerts: alerts matching the request within the pagination.
1087-
Alerts []*Alert `json:"alerts"`
1088-
}
1089-
1090-
// UnsafeGetTotalCount should not be used
1091-
// Internal usage only
1092-
func (r *ListManagedAlertsResponse) UnsafeGetTotalCount() uint64 {
1093-
return r.TotalCount
1094-
}
1095-
1096-
// UnsafeAppend should not be used
1097-
// Internal usage only
1098-
func (r *ListManagedAlertsResponse) UnsafeAppend(res interface{}) (uint64, error) {
1099-
results, ok := res.(*ListManagedAlertsResponse)
1100-
if !ok {
1101-
return 0, errors.New("%T type cannot be appended to type %T", res, r)
1102-
}
1103-
1104-
r.Alerts = append(r.Alerts, results.Alerts...)
1105-
r.TotalCount += uint64(len(results.Alerts))
1106-
return uint64(len(results.Alerts)), nil
1107-
}
1108-
11091022
// ListPlansResponse: Output returned when listing pricing plans.
11101023
type ListPlansResponse struct {
11111024
// TotalCount: total count of available pricing plans.
@@ -1338,7 +1251,7 @@ type RegionalAPIListAlertsRequest struct {
13381251

13391252
// State: valid values to filter on are `disabled`, `enabled`, `pending` and `firing`. If omitted, no filtering is applied on alert states. Other filters may still apply.
13401253
// Default value: unknown_state
1341-
State *AnyAlertState `json:"-"`
1254+
State *AlertState `json:"-"`
13421255
}
13431256

13441257
// RegionalAPIListContactPointsRequest: List contact points.
@@ -1382,25 +1295,6 @@ type RegionalAPIListDataSourcesRequest struct {
13821295
Types []DataSourceType `json:"-"`
13831296
}
13841297

1385-
// RegionalAPIListManagedAlertsRequest: Enable the sending of managed alerts.
1386-
type RegionalAPIListManagedAlertsRequest struct {
1387-
// Region: region to target. If none is passed will use default region from the config.
1388-
Region scw.Region `json:"-"`
1389-
1390-
// Page: page number to return, from the paginated results.
1391-
Page *int32 `json:"-"`
1392-
1393-
// PageSize: number of data sources to return per page.
1394-
PageSize *uint32 `json:"-"`
1395-
1396-
// OrderBy: sort order for data sources in the response.
1397-
// Default value: created_at_asc
1398-
OrderBy ListManagedAlertsRequestOrderBy `json:"-"`
1399-
1400-
// ProjectID: project ID to filter for, only data sources from this Project will be returned.
1401-
ProjectID string `json:"-"`
1402-
}
1403-
14041298
// RegionalAPIListTokensRequest: List tokens.
14051299
type RegionalAPIListTokensRequest struct {
14061300
// Region: region to target. If none is passed will use default region from the config.
@@ -2494,50 +2388,6 @@ func (s *RegionalAPI) DeleteContactPoint(req *RegionalAPIDeleteContactPointReque
24942388
return nil
24952389
}
24962390

2497-
// ListManagedAlerts: List all managed alerts for the specified Project.
2498-
func (s *RegionalAPI) ListManagedAlerts(req *RegionalAPIListManagedAlertsRequest, opts ...scw.RequestOption) (*ListManagedAlertsResponse, error) {
2499-
var err error
2500-
2501-
if req.Region == "" {
2502-
defaultRegion, _ := s.client.GetDefaultRegion()
2503-
req.Region = defaultRegion
2504-
}
2505-
2506-
defaultPageSize, exist := s.client.GetDefaultPageSize()
2507-
if (req.PageSize == nil || *req.PageSize == 0) && exist {
2508-
req.PageSize = &defaultPageSize
2509-
}
2510-
2511-
if req.ProjectID == "" {
2512-
defaultProjectID, _ := s.client.GetDefaultProjectID()
2513-
req.ProjectID = defaultProjectID
2514-
}
2515-
2516-
query := url.Values{}
2517-
parameter.AddToQuery(query, "page", req.Page)
2518-
parameter.AddToQuery(query, "page_size", req.PageSize)
2519-
parameter.AddToQuery(query, "order_by", req.OrderBy)
2520-
parameter.AddToQuery(query, "project_id", req.ProjectID)
2521-
2522-
if fmt.Sprint(req.Region) == "" {
2523-
return nil, errors.New("field Region cannot be empty in request")
2524-
}
2525-
2526-
scwReq := &scw.ScalewayRequest{
2527-
Method: "GET",
2528-
Path: "/cockpit/v1/regions/" + fmt.Sprint(req.Region) + "/managed-alerts",
2529-
Query: query,
2530-
}
2531-
2532-
var resp ListManagedAlertsResponse
2533-
2534-
err = s.client.Do(scwReq, &resp, opts...)
2535-
if err != nil {
2536-
return nil, err
2537-
}
2538-
return &resp, nil
2539-
}
2540-
25412391
// ListAlerts: List preconfigured and/or custom alerts for the specified Project.
25422392
func (s *RegionalAPI) ListAlerts(req *RegionalAPIListAlertsRequest, opts ...scw.RequestOption) (*ListAlertsResponse, error) {
25432393
var err error

0 commit comments

Comments
 (0)