-
Notifications
You must be signed in to change notification settings - Fork 0
/
alert.go
478 lines (426 loc) · 20.3 KB
/
alert.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
package orb
import (
"context"
"errors"
"fmt"
"net/http"
"net/url"
"time"
"github.com/orbcorp/orb-go/internal/apijson"
"github.com/orbcorp/orb-go/internal/apiquery"
"github.com/orbcorp/orb-go/internal/param"
"github.com/orbcorp/orb-go/internal/requestconfig"
"github.com/orbcorp/orb-go/option"
"github.com/orbcorp/orb-go/packages/pagination"
)
// AlertService contains methods and other services that help with interacting with
// the orb API.
//
// Note, unlike clients, this service does not read variables from the environment
// automatically. You should not instantiate this service directly, and instead use
// the [NewAlertService] method instead.
type AlertService struct {
Options []option.RequestOption
}
// NewAlertService generates a new service that applies the given options to each
// request. These options are applied after the parent client's options (if there
// is one), and before any request-specific options.
func NewAlertService(opts ...option.RequestOption) (r *AlertService) {
r = &AlertService{}
r.Options = opts
return
}
// This endpoint retrieves an alert by its ID.
func (r *AlertService) Get(ctx context.Context, alertID string, opts ...option.RequestOption) (res *Alert, err error) {
opts = append(r.Options[:], opts...)
if alertID == "" {
err = errors.New("missing required alert_id parameter")
return
}
path := fmt.Sprintf("alerts/%s", alertID)
err = requestconfig.ExecuteNewRequest(ctx, http.MethodGet, path, nil, &res, opts...)
return
}
// This endpoint updates the thresholds of an alert.
func (r *AlertService) Update(ctx context.Context, alertConfigurationID string, body AlertUpdateParams, opts ...option.RequestOption) (res *Alert, err error) {
opts = append(r.Options[:], opts...)
if alertConfigurationID == "" {
err = errors.New("missing required alert_configuration_id parameter")
return
}
path := fmt.Sprintf("alerts/%s", alertConfigurationID)
err = requestconfig.ExecuteNewRequest(ctx, http.MethodPut, path, body, &res, opts...)
return
}
// This endpoint returns a list of alerts within Orb.
//
// The request must specify one of `customer_id`, `external_customer_id`, or
// `subscription_id`.
//
// If querying by subscripion_id, the endpoint will return the subscription level
// alerts as well as the plan level alerts associated with the subscription.
//
// The list of alerts is ordered starting from the most recently created alert.
// This endpoint follows Orb's
// [standardized pagination format](../reference/pagination).
func (r *AlertService) List(ctx context.Context, query AlertListParams, opts ...option.RequestOption) (res *pagination.Page[Alert], err error) {
var raw *http.Response
opts = append(r.Options[:], opts...)
opts = append([]option.RequestOption{option.WithResponseInto(&raw)}, opts...)
path := "alerts"
cfg, err := requestconfig.NewRequestConfig(ctx, http.MethodGet, path, query, &res, opts...)
if err != nil {
return nil, err
}
err = cfg.Execute()
if err != nil {
return nil, err
}
res.SetPageConfig(cfg, raw)
return res, nil
}
// This endpoint returns a list of alerts within Orb.
//
// The request must specify one of `customer_id`, `external_customer_id`, or
// `subscription_id`.
//
// If querying by subscripion_id, the endpoint will return the subscription level
// alerts as well as the plan level alerts associated with the subscription.
//
// The list of alerts is ordered starting from the most recently created alert.
// This endpoint follows Orb's
// [standardized pagination format](../reference/pagination).
func (r *AlertService) ListAutoPaging(ctx context.Context, query AlertListParams, opts ...option.RequestOption) *pagination.PageAutoPager[Alert] {
return pagination.NewPageAutoPager(r.List(ctx, query, opts...))
}
// This endpoint creates a new alert to monitor a customer's credit balance. There
// are three types of alerts that can be scoped to customers:
// `credit_balance_depleted`, `credit_balance_dropped`, and
// `credit_balance_recovered`. Customers can have a maximum of one of each type of
// alert per
// [credit balance currency](https://docs.withorb.com/guides/product-catalog/prepurchase).
// `credit_balance_dropped` alerts require a list of thresholds to be provided
// while `credit_balance_depleted` and `credit_balance_recovered` alerts do not
// require thresholds.
func (r *AlertService) NewForCustomer(ctx context.Context, customerID string, body AlertNewForCustomerParams, opts ...option.RequestOption) (res *Alert, err error) {
opts = append(r.Options[:], opts...)
if customerID == "" {
err = errors.New("missing required customer_id parameter")
return
}
path := fmt.Sprintf("alerts/customer_id/%s", customerID)
err = requestconfig.ExecuteNewRequest(ctx, http.MethodPost, path, body, &res, opts...)
return
}
// This endpoint creates a new alert to monitor a customer's credit balance. There
// are three types of alerts that can be scoped to customers:
// `credit_balance_depleted`, `credit_balance_dropped`, and
// `credit_balance_recovered`. Customers can have a maximum of one of each type of
// alert per
// [credit balance currency](https://docs.withorb.com/guides/product-catalog/prepurchase).
// `credit_balance_dropped` alerts require a list of thresholds to be provided
// while `credit_balance_depleted` and `credit_balance_recovered` alerts do not
// require thresholds.
func (r *AlertService) NewForExternalCustomer(ctx context.Context, externalCustomerID string, body AlertNewForExternalCustomerParams, opts ...option.RequestOption) (res *Alert, err error) {
opts = append(r.Options[:], opts...)
if externalCustomerID == "" {
err = errors.New("missing required external_customer_id parameter")
return
}
path := fmt.Sprintf("alerts/external_customer_id/%s", externalCustomerID)
err = requestconfig.ExecuteNewRequest(ctx, http.MethodPost, path, body, &res, opts...)
return
}
// This endpoint is used to create alerts at the subscription level.
//
// Subscription level alerts can be one of two types: `usage_exceeded` or
// `cost_exceeded`. A `usage_exceeded` alert is scoped to a particular metric and
// is triggered when the usage of that metric exceeds predefined thresholds during
// the current billing cycle. A `cost_exceeded` alert is triggered when the total
// amount due during the current billing cycle surpasses predefined thresholds.
// `cost_exceeded` alerts do not include burndown of pre-purchase credits. Each
// subscription can have one `cost_exceeded` alert and one `usage_exceeded` alert
// per metric that is a part of the subscription. Alerts are triggered based on
// usage or cost conditions met during the current billing cycle.
func (r *AlertService) NewForSubscription(ctx context.Context, subscriptionID string, body AlertNewForSubscriptionParams, opts ...option.RequestOption) (res *Alert, err error) {
opts = append(r.Options[:], opts...)
if subscriptionID == "" {
err = errors.New("missing required subscription_id parameter")
return
}
path := fmt.Sprintf("alerts/subscription_id/%s", subscriptionID)
err = requestconfig.ExecuteNewRequest(ctx, http.MethodPost, path, body, &res, opts...)
return
}
// This endpoint can be used to disable an alert.
func (r *AlertService) Disable(ctx context.Context, alertConfigurationID string, opts ...option.RequestOption) (res *Alert, err error) {
opts = append(r.Options[:], opts...)
if alertConfigurationID == "" {
err = errors.New("missing required alert_configuration_id parameter")
return
}
path := fmt.Sprintf("alerts/%s/disable", alertConfigurationID)
err = requestconfig.ExecuteNewRequest(ctx, http.MethodPost, path, nil, &res, opts...)
return
}
// This endpoint can be used to enable an alert.
func (r *AlertService) Enable(ctx context.Context, alertConfigurationID string, opts ...option.RequestOption) (res *Alert, err error) {
opts = append(r.Options[:], opts...)
if alertConfigurationID == "" {
err = errors.New("missing required alert_configuration_id parameter")
return
}
path := fmt.Sprintf("alerts/%s/enable", alertConfigurationID)
err = requestconfig.ExecuteNewRequest(ctx, http.MethodPost, path, nil, &res, opts...)
return
}
// [Alerts within Orb](https://docs.withorb.com/guides/product-catalog/configuring-alerts)
// monitor spending, usage, or credit balance and trigger webhooks when a threshold
// is exceeded.
//
// Alerts created through the API can be scoped to either customers or
// subscriptions.
//
// | Scope | Monitors | Vaild Alert Types |
// | ------------ | ------------------------------ | ----------------------------------------------------------------------------------- |
// | Customer | A customer's credit balance | `credit_balance_depleted`, `credit_balance_recovered`, and `credit_balance_dropped` |
// | Subscription | A subscription's usage or cost | `usage_exceeded` and `cost_exceeded` |
type Alert struct {
// Also referred to as alert_id in this documentation.
ID string `json:"id,required"`
// The creation time of the resource in Orb.
CreatedAt time.Time `json:"created_at,required" format:"date-time"`
// The name of the currency the credit balance or invoice cost is denominated in.
Currency string `json:"currency,required,nullable"`
// The customer the alert applies to.
Customer map[string]string `json:"customer,required,nullable"`
// Whether the alert is enabled or disabled.
Enabled bool `json:"enabled,required"`
// The metric the alert applies to.
Metric map[string]string `json:"metric,required,nullable"`
// The plan the alert applies to.
Plan map[string]string `json:"plan,required,nullable"`
// The subscription the alert applies to.
Subscription map[string]string `json:"subscription,required,nullable"`
// The thresholds that define the conditions under which the alert will be
// triggered.
Thresholds []AlertThreshold `json:"thresholds,required,nullable"`
// The type of alert. This must be a valid alert type.
Type AlertType `json:"type,required"`
JSON alertJSON `json:"-"`
}
// alertJSON contains the JSON metadata for the struct [Alert]
type alertJSON struct {
ID apijson.Field
CreatedAt apijson.Field
Currency apijson.Field
Customer apijson.Field
Enabled apijson.Field
Metric apijson.Field
Plan apijson.Field
Subscription apijson.Field
Thresholds apijson.Field
Type apijson.Field
raw string
ExtraFields map[string]apijson.Field
}
func (r *Alert) UnmarshalJSON(data []byte) (err error) {
return apijson.UnmarshalRoot(data, r)
}
func (r alertJSON) RawJSON() string {
return r.raw
}
// Thresholds are used to define the conditions under which an alert will be
// triggered.
type AlertThreshold struct {
// The value at which an alert will fire. For credit balance alerts, the alert will
// fire at or below this value. For usage and cost alerts, the alert will fire at
// or above this value.
Value float64 `json:"value,required"`
JSON alertThresholdJSON `json:"-"`
}
// alertThresholdJSON contains the JSON metadata for the struct [AlertThreshold]
type alertThresholdJSON struct {
Value apijson.Field
raw string
ExtraFields map[string]apijson.Field
}
func (r *AlertThreshold) UnmarshalJSON(data []byte) (err error) {
return apijson.UnmarshalRoot(data, r)
}
func (r alertThresholdJSON) RawJSON() string {
return r.raw
}
// The type of alert. This must be a valid alert type.
type AlertType string
const (
AlertTypeUsageExceeded AlertType = "usage_exceeded"
AlertTypeCostExceeded AlertType = "cost_exceeded"
AlertTypeCreditBalanceDepleted AlertType = "credit_balance_depleted"
AlertTypeCreditBalanceDropped AlertType = "credit_balance_dropped"
AlertTypeCreditBalanceRecovered AlertType = "credit_balance_recovered"
)
func (r AlertType) IsKnown() bool {
switch r {
case AlertTypeUsageExceeded, AlertTypeCostExceeded, AlertTypeCreditBalanceDepleted, AlertTypeCreditBalanceDropped, AlertTypeCreditBalanceRecovered:
return true
}
return false
}
type AlertUpdateParams struct {
// The thresholds that define the values at which the alert will be triggered.
Thresholds param.Field[[]AlertUpdateParamsThreshold] `json:"thresholds,required"`
}
func (r AlertUpdateParams) MarshalJSON() (data []byte, err error) {
return apijson.MarshalRoot(r)
}
// Thresholds are used to define the conditions under which an alert will be
// triggered.
type AlertUpdateParamsThreshold struct {
// The value at which an alert will fire. For credit balance alerts, the alert will
// fire at or below this value. For usage and cost alerts, the alert will fire at
// or above this value.
Value param.Field[float64] `json:"value,required"`
}
func (r AlertUpdateParamsThreshold) MarshalJSON() (data []byte, err error) {
return apijson.MarshalRoot(r)
}
type AlertListParams struct {
CreatedAtGt param.Field[time.Time] `query:"created_at[gt]" format:"date-time"`
CreatedAtGte param.Field[time.Time] `query:"created_at[gte]" format:"date-time"`
CreatedAtLt param.Field[time.Time] `query:"created_at[lt]" format:"date-time"`
CreatedAtLte param.Field[time.Time] `query:"created_at[lte]" format:"date-time"`
// Cursor for pagination. This can be populated by the `next_cursor` value returned
// from the initial request.
Cursor param.Field[string] `query:"cursor"`
// Fetch alerts scoped to this customer_id
CustomerID param.Field[string] `query:"customer_id"`
// Fetch alerts scoped to this external_customer_id
ExternalCustomerID param.Field[string] `query:"external_customer_id"`
// The number of items to fetch. Defaults to 20.
Limit param.Field[int64] `query:"limit"`
// Fetch alerts scoped to this subscription_id
SubscriptionID param.Field[string] `query:"subscription_id"`
}
// URLQuery serializes [AlertListParams]'s query parameters as `url.Values`.
func (r AlertListParams) URLQuery() (v url.Values) {
return apiquery.MarshalWithSettings(r, apiquery.QuerySettings{
ArrayFormat: apiquery.ArrayQueryFormatBrackets,
NestedFormat: apiquery.NestedQueryFormatBrackets,
})
}
type AlertNewForCustomerParams struct {
// The case sensitive currency or custom pricing unit to use for this alert.
Currency param.Field[string] `json:"currency,required"`
// The type of alert to create. This must be a valid alert type.
Type param.Field[AlertNewForCustomerParamsType] `json:"type,required"`
// The thresholds that define the values at which the alert will be triggered.
Thresholds param.Field[[]AlertNewForCustomerParamsThreshold] `json:"thresholds"`
}
func (r AlertNewForCustomerParams) MarshalJSON() (data []byte, err error) {
return apijson.MarshalRoot(r)
}
// The type of alert to create. This must be a valid alert type.
type AlertNewForCustomerParamsType string
const (
AlertNewForCustomerParamsTypeUsageExceeded AlertNewForCustomerParamsType = "usage_exceeded"
AlertNewForCustomerParamsTypeCostExceeded AlertNewForCustomerParamsType = "cost_exceeded"
AlertNewForCustomerParamsTypeCreditBalanceDepleted AlertNewForCustomerParamsType = "credit_balance_depleted"
AlertNewForCustomerParamsTypeCreditBalanceDropped AlertNewForCustomerParamsType = "credit_balance_dropped"
AlertNewForCustomerParamsTypeCreditBalanceRecovered AlertNewForCustomerParamsType = "credit_balance_recovered"
)
func (r AlertNewForCustomerParamsType) IsKnown() bool {
switch r {
case AlertNewForCustomerParamsTypeUsageExceeded, AlertNewForCustomerParamsTypeCostExceeded, AlertNewForCustomerParamsTypeCreditBalanceDepleted, AlertNewForCustomerParamsTypeCreditBalanceDropped, AlertNewForCustomerParamsTypeCreditBalanceRecovered:
return true
}
return false
}
// Thresholds are used to define the conditions under which an alert will be
// triggered.
type AlertNewForCustomerParamsThreshold struct {
// The value at which an alert will fire. For credit balance alerts, the alert will
// fire at or below this value. For usage and cost alerts, the alert will fire at
// or above this value.
Value param.Field[float64] `json:"value,required"`
}
func (r AlertNewForCustomerParamsThreshold) MarshalJSON() (data []byte, err error) {
return apijson.MarshalRoot(r)
}
type AlertNewForExternalCustomerParams struct {
// The case sensitive currency or custom pricing unit to use for this alert.
Currency param.Field[string] `json:"currency,required"`
// The type of alert to create. This must be a valid alert type.
Type param.Field[AlertNewForExternalCustomerParamsType] `json:"type,required"`
// The thresholds that define the values at which the alert will be triggered.
Thresholds param.Field[[]AlertNewForExternalCustomerParamsThreshold] `json:"thresholds"`
}
func (r AlertNewForExternalCustomerParams) MarshalJSON() (data []byte, err error) {
return apijson.MarshalRoot(r)
}
// The type of alert to create. This must be a valid alert type.
type AlertNewForExternalCustomerParamsType string
const (
AlertNewForExternalCustomerParamsTypeUsageExceeded AlertNewForExternalCustomerParamsType = "usage_exceeded"
AlertNewForExternalCustomerParamsTypeCostExceeded AlertNewForExternalCustomerParamsType = "cost_exceeded"
AlertNewForExternalCustomerParamsTypeCreditBalanceDepleted AlertNewForExternalCustomerParamsType = "credit_balance_depleted"
AlertNewForExternalCustomerParamsTypeCreditBalanceDropped AlertNewForExternalCustomerParamsType = "credit_balance_dropped"
AlertNewForExternalCustomerParamsTypeCreditBalanceRecovered AlertNewForExternalCustomerParamsType = "credit_balance_recovered"
)
func (r AlertNewForExternalCustomerParamsType) IsKnown() bool {
switch r {
case AlertNewForExternalCustomerParamsTypeUsageExceeded, AlertNewForExternalCustomerParamsTypeCostExceeded, AlertNewForExternalCustomerParamsTypeCreditBalanceDepleted, AlertNewForExternalCustomerParamsTypeCreditBalanceDropped, AlertNewForExternalCustomerParamsTypeCreditBalanceRecovered:
return true
}
return false
}
// Thresholds are used to define the conditions under which an alert will be
// triggered.
type AlertNewForExternalCustomerParamsThreshold struct {
// The value at which an alert will fire. For credit balance alerts, the alert will
// fire at or below this value. For usage and cost alerts, the alert will fire at
// or above this value.
Value param.Field[float64] `json:"value,required"`
}
func (r AlertNewForExternalCustomerParamsThreshold) MarshalJSON() (data []byte, err error) {
return apijson.MarshalRoot(r)
}
type AlertNewForSubscriptionParams struct {
// The thresholds that define the values at which the alert will be triggered.
Thresholds param.Field[[]AlertNewForSubscriptionParamsThreshold] `json:"thresholds,required"`
// The type of alert to create. This must be a valid alert type.
Type param.Field[AlertNewForSubscriptionParamsType] `json:"type,required"`
// The metric to track usage for.
MetricID param.Field[string] `json:"metric_id"`
}
func (r AlertNewForSubscriptionParams) MarshalJSON() (data []byte, err error) {
return apijson.MarshalRoot(r)
}
// Thresholds are used to define the conditions under which an alert will be
// triggered.
type AlertNewForSubscriptionParamsThreshold struct {
// The value at which an alert will fire. For credit balance alerts, the alert will
// fire at or below this value. For usage and cost alerts, the alert will fire at
// or above this value.
Value param.Field[float64] `json:"value,required"`
}
func (r AlertNewForSubscriptionParamsThreshold) MarshalJSON() (data []byte, err error) {
return apijson.MarshalRoot(r)
}
// The type of alert to create. This must be a valid alert type.
type AlertNewForSubscriptionParamsType string
const (
AlertNewForSubscriptionParamsTypeUsageExceeded AlertNewForSubscriptionParamsType = "usage_exceeded"
AlertNewForSubscriptionParamsTypeCostExceeded AlertNewForSubscriptionParamsType = "cost_exceeded"
AlertNewForSubscriptionParamsTypeCreditBalanceDepleted AlertNewForSubscriptionParamsType = "credit_balance_depleted"
AlertNewForSubscriptionParamsTypeCreditBalanceDropped AlertNewForSubscriptionParamsType = "credit_balance_dropped"
AlertNewForSubscriptionParamsTypeCreditBalanceRecovered AlertNewForSubscriptionParamsType = "credit_balance_recovered"
)
func (r AlertNewForSubscriptionParamsType) IsKnown() bool {
switch r {
case AlertNewForSubscriptionParamsTypeUsageExceeded, AlertNewForSubscriptionParamsTypeCostExceeded, AlertNewForSubscriptionParamsTypeCreditBalanceDepleted, AlertNewForSubscriptionParamsTypeCreditBalanceDropped, AlertNewForSubscriptionParamsTypeCreditBalanceRecovered:
return true
}
return false
}