alert_channels.go raw

   1  package linodego
   2  
   3  import (
   4  	"context"
   5  	"encoding/json"
   6  	"time"
   7  
   8  	"github.com/linode/linodego/internal/parseabletime"
   9  )
  10  
  11  type AlertNotificationType string
  12  
  13  const (
  14  	EmailAlertNotification AlertNotificationType = "email"
  15  )
  16  
  17  type AlertChannelType string
  18  
  19  const (
  20  	SystemAlertChannel AlertChannelType = "system"
  21  	UserAlertChannel   AlertChannelType = "user"
  22  )
  23  
  24  // AlertChannelEnvelope represents a single alert channel entry returned inside alert definition
  25  type AlertChannelEnvelope struct {
  26  	ID    int    `json:"id"`
  27  	Label string `json:"label"`
  28  	Type  string `json:"type"`
  29  	URL   string `json:"url"`
  30  }
  31  
  32  // AlertChannel represents a Monitor Channel object.
  33  type AlertChannel struct {
  34  	Alerts      []AlertChannelEnvelope `json:"alerts"`
  35  	ChannelType AlertNotificationType  `json:"channel_type"`
  36  	Content     ChannelContent         `json:"content"`
  37  	Created     *time.Time             `json:"-"`
  38  	CreatedBy   string                 `json:"created_by"`
  39  	Updated     *time.Time             `json:"-"`
  40  	UpdatedBy   string                 `json:"updated_by"`
  41  	ID          int                    `json:"id"`
  42  	Label       string                 `json:"label"`
  43  	Type        AlertChannelType       `json:"type"`
  44  }
  45  
  46  type EmailChannelContent struct {
  47  	EmailAddresses []string `json:"email_addresses"`
  48  }
  49  
  50  // ChannelContent represents the content block for an AlertChannel, which varies by channel type.
  51  type ChannelContent struct {
  52  	Email *EmailChannelContent `json:"email"`
  53  	// Other channel types like 'webhook', 'slack' could be added here as optional fields.
  54  }
  55  
  56  // UnmarshalJSON implements the json.Unmarshaler interface
  57  func (a *AlertChannel) UnmarshalJSON(b []byte) error {
  58  	type Mask AlertChannel
  59  
  60  	p := struct {
  61  		*Mask
  62  
  63  		Updated *parseabletime.ParseableTime `json:"updated"`
  64  		Created *parseabletime.ParseableTime `json:"created"`
  65  	}{
  66  		Mask: (*Mask)(a),
  67  	}
  68  
  69  	if err := json.Unmarshal(b, &p); err != nil {
  70  		return err
  71  	}
  72  
  73  	a.Updated = (*time.Time)(p.Updated)
  74  	a.Created = (*time.Time)(p.Created)
  75  
  76  	return nil
  77  }
  78  
  79  // ListAlertChannels gets a paginated list of Alert Channels.
  80  func (c *Client) ListAlertChannels(ctx context.Context, opts *ListOptions) ([]AlertChannel, error) {
  81  	endpoint := formatAPIPath("monitor/alert-channels")
  82  	return getPaginatedResults[AlertChannel](ctx, c, endpoint, opts)
  83  }
  84