Skip to content

Commit e626aef

Browse files
authored
feat(cockpit): add support for ListAlerts (scaleway#2421)
1 parent 140c142 commit e626aef

File tree

1 file changed

+152
-0
lines changed

1 file changed

+152
-0
lines changed

api/cockpit/v1/cockpit_sdk.go

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,53 @@ var (
3939
_ = namegenerator.GetRandomName
4040
)
4141

42+
type AnyAlertState string
43+
44+
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")
50+
// The alert's conditions are met. They must persist for the configured duration before transitioning to the `firing` state.
51+
AnyAlertStatePending = AnyAlertState("pending")
52+
// The alert's conditions, including the required duration, have been fully met.
53+
AnyAlertStateFiring = AnyAlertState("firing")
54+
)
55+
56+
func (enum AnyAlertState) String() string {
57+
if enum == "" {
58+
// return default value if empty
59+
return "unknown_state"
60+
}
61+
return string(enum)
62+
}
63+
64+
func (enum AnyAlertState) Values() []AnyAlertState {
65+
return []AnyAlertState{
66+
"unknown_state",
67+
"disabled",
68+
"enabled",
69+
"pending",
70+
"firing",
71+
}
72+
}
73+
74+
func (enum AnyAlertState) MarshalJSON() ([]byte, error) {
75+
return []byte(fmt.Sprintf(`"%s"`, enum)), nil
76+
}
77+
78+
func (enum *AnyAlertState) UnmarshalJSON(data []byte) error {
79+
tmp := ""
80+
81+
if err := json.Unmarshal(data, &tmp); err != nil {
82+
return err
83+
}
84+
85+
*enum = AnyAlertState(AnyAlertState(tmp).String())
86+
return nil
87+
}
88+
4289
type DataSourceOrigin string
4390

4491
const (
@@ -533,6 +580,25 @@ type GetConfigResponseRetention struct {
533580
DefaultDays uint32 `json:"default_days"`
534581
}
535582

583+
// AnyAlert: any alert.
584+
type AnyAlert struct {
585+
// Region: region to target. If none is passed will use default region from the config.
586+
Region scw.Region `json:"region"`
587+
588+
Preconfigured bool `json:"preconfigured"`
589+
590+
Name string `json:"name"`
591+
592+
Rule string `json:"rule"`
593+
594+
Duration string `json:"duration"`
595+
596+
// State: default value: unknown_state
597+
State AnyAlertState `json:"state"`
598+
599+
Annotations map[string]string `json:"annotations"`
600+
}
601+
536602
// ContactPoint: Contact point.
537603
type ContactPoint struct {
538604
// Email: email address to send alerts to.
@@ -866,6 +932,34 @@ type Grafana struct {
866932
GrafanaURL string `json:"grafana_url"`
867933
}
868934

935+
// ListAlertsResponse: Retrieve a list of alerts matching the request.
936+
type ListAlertsResponse struct {
937+
// TotalCount: total count of alerts matching the request.
938+
TotalCount uint64 `json:"total_count"`
939+
940+
// Alerts: list of alerts matching the applied filters.
941+
Alerts []*AnyAlert `json:"alerts"`
942+
}
943+
944+
// UnsafeGetTotalCount should not be used
945+
// Internal usage only
946+
func (r *ListAlertsResponse) UnsafeGetTotalCount() uint64 {
947+
return r.TotalCount
948+
}
949+
950+
// UnsafeAppend should not be used
951+
// Internal usage only
952+
func (r *ListAlertsResponse) UnsafeAppend(res interface{}) (uint64, error) {
953+
results, ok := res.(*ListAlertsResponse)
954+
if !ok {
955+
return 0, errors.New("%T type cannot be appended to type %T", res, r)
956+
}
957+
958+
r.Alerts = append(r.Alerts, results.Alerts...)
959+
r.TotalCount += uint64(len(results.Alerts))
960+
return uint64(len(results.Alerts)), nil
961+
}
962+
869963
// ListContactPointsResponse: Response returned when listing contact points.
870964
type ListContactPointsResponse struct {
871965
// TotalCount: total count of contact points associated with the default receiver.
@@ -1228,6 +1322,25 @@ type RegionalAPIGetUsageOverviewRequest struct {
12281322
Interval *scw.Duration `json:"-"`
12291323
}
12301324

1325+
// RegionalAPIListAlertsRequest: Retrieve a list of alerts.
1326+
type RegionalAPIListAlertsRequest struct {
1327+
// Region: region to target. If none is passed will use default region from the config.
1328+
Region scw.Region `json:"-"`
1329+
1330+
// ProjectID: project ID to filter for, only alerts from this Project will be returned.
1331+
ProjectID string `json:"-"`
1332+
1333+
// IsEnabled: true returns only enabled alerts. False returns only disabled alerts. If omitted, no alert filtering is applied. Other filters may still apply.
1334+
IsEnabled *bool `json:"-"`
1335+
1336+
// IsPreconfigured: true returns only preconfigured alerts. False returns only custom alerts. If omitted, no filtering is applied on alert types. Other filters may still apply.
1337+
IsPreconfigured *bool `json:"-"`
1338+
1339+
// 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.
1340+
// Default value: unknown_state
1341+
State *AnyAlertState `json:"-"`
1342+
}
1343+
12311344
// RegionalAPIListContactPointsRequest: List contact points.
12321345
type RegionalAPIListContactPointsRequest struct {
12331346
// Region: region to target. If none is passed will use default region from the config.
@@ -2425,6 +2538,45 @@ func (s *RegionalAPI) ListManagedAlerts(req *RegionalAPIListManagedAlertsRequest
24252538
return &resp, nil
24262539
}
24272540

2541+
// ListAlerts: List preconfigured and/or custom alerts for the specified Project.
2542+
func (s *RegionalAPI) ListAlerts(req *RegionalAPIListAlertsRequest, opts ...scw.RequestOption) (*ListAlertsResponse, error) {
2543+
var err error
2544+
2545+
if req.Region == "" {
2546+
defaultRegion, _ := s.client.GetDefaultRegion()
2547+
req.Region = defaultRegion
2548+
}
2549+
2550+
if req.ProjectID == "" {
2551+
defaultProjectID, _ := s.client.GetDefaultProjectID()
2552+
req.ProjectID = defaultProjectID
2553+
}
2554+
2555+
query := url.Values{}
2556+
parameter.AddToQuery(query, "project_id", req.ProjectID)
2557+
parameter.AddToQuery(query, "is_enabled", req.IsEnabled)
2558+
parameter.AddToQuery(query, "is_preconfigured", req.IsPreconfigured)
2559+
parameter.AddToQuery(query, "state", req.State)
2560+
2561+
if fmt.Sprint(req.Region) == "" {
2562+
return nil, errors.New("field Region cannot be empty in request")
2563+
}
2564+
2565+
scwReq := &scw.ScalewayRequest{
2566+
Method: "GET",
2567+
Path: "/cockpit/v1/regions/" + fmt.Sprint(req.Region) + "/alerts",
2568+
Query: query,
2569+
}
2570+
2571+
var resp ListAlertsResponse
2572+
2573+
err = s.client.Do(scwReq, &resp, opts...)
2574+
if err != nil {
2575+
return nil, err
2576+
}
2577+
return &resp, nil
2578+
}
2579+
24282580
// EnableManagedAlerts: Enable the sending of managed alerts for the specified Project. Managed alerts are predefined alerts that apply to Scaleway recources integrated with Cockpit by default.
24292581
func (s *RegionalAPI) EnableManagedAlerts(req *RegionalAPIEnableManagedAlertsRequest, opts ...scw.RequestOption) (*AlertManager, error) {
24302582
var err error

0 commit comments

Comments
 (0)