github.com/influxdata/influxdb/v2@v2.7.6/notification_endpoint.go (about)

     1  package influxdb
     2  
     3  import (
     4  	"context"
     5  	"encoding/json"
     6  	"errors"
     7  
     8  	"github.com/influxdata/influxdb/v2/kit/platform"
     9  	errors2 "github.com/influxdata/influxdb/v2/kit/platform/errors"
    10  )
    11  
    12  var (
    13  	// ErrInvalidNotificationEndpointType denotes that the provided NotificationEndpoint is not a valid type
    14  	ErrInvalidNotificationEndpointType = errors.New("unknown notification endpoint type")
    15  )
    16  
    17  // NotificationEndpoint is the configuration describing
    18  // how to call a 3rd party service. E.g. Slack, Pagerduty
    19  type NotificationEndpoint interface {
    20  	Valid() error
    21  	Type() string
    22  	json.Marshaler
    23  	CRUDLogSetter
    24  	SetID(id platform.ID)
    25  	SetOrgID(id platform.ID)
    26  	SetName(name string)
    27  	SetDescription(description string)
    28  	SetStatus(status Status)
    29  
    30  	GetID() platform.ID
    31  	GetCRUDLog() CRUDLog
    32  	GetOrgID() platform.ID
    33  	GetName() string
    34  	GetDescription() string
    35  	GetStatus() Status
    36  	// SecretFields return available secret fields.
    37  	SecretFields() []SecretField
    38  	// BackfillSecretKeys fill back fill the secret field key during the unmarshalling
    39  	// if value of that secret field is not nil.
    40  	BackfillSecretKeys()
    41  }
    42  
    43  // ops for checks error
    44  var (
    45  	OpFindNotificationEndpointByID = "FindNotificationEndpointByID"
    46  	OpFindNotificationEndpoint     = "FindNotificationEndpoint"
    47  	OpFindNotificationEndpoints    = "FindNotificationEndpoints"
    48  	OpCreateNotificationEndpoint   = "CreateNotificationEndpoint"
    49  	OpUpdateNotificationEndpoint   = "UpdateNotificationEndpoint"
    50  	OpDeleteNotificationEndpoint   = "DeleteNotificationEndpoint"
    51  )
    52  
    53  // NotificationEndpointFilter represents a set of filter that restrict the returned notification endpoints.
    54  type NotificationEndpointFilter struct {
    55  	ID    *platform.ID
    56  	OrgID *platform.ID
    57  	Org   *string
    58  	UserResourceMappingFilter
    59  }
    60  
    61  // QueryParams Converts NotificationEndpointFilter fields to url query params.
    62  func (f NotificationEndpointFilter) QueryParams() map[string][]string {
    63  	qp := map[string][]string{}
    64  
    65  	if f.OrgID != nil {
    66  		qp["orgID"] = []string{f.OrgID.String()}
    67  	}
    68  
    69  	if f.Org != nil {
    70  		qp["org"] = []string{*f.Org}
    71  	}
    72  
    73  	return qp
    74  }
    75  
    76  // NotificationEndpointUpdate is the set of upgrade fields for patch request.
    77  type NotificationEndpointUpdate struct {
    78  	Name        *string `json:"name,omitempty"`
    79  	Description *string `json:"description,omitempty"`
    80  	Status      *Status `json:"status,omitempty"`
    81  }
    82  
    83  // Valid will verify if the NotificationEndpointUpdate is valid.
    84  func (n *NotificationEndpointUpdate) Valid() error {
    85  	if n.Name != nil && *n.Name == "" {
    86  		return &errors2.Error{
    87  			Code: errors2.EInvalid,
    88  			Msg:  "Notification Endpoint Name can't be empty",
    89  		}
    90  	}
    91  
    92  	if n.Description != nil && *n.Description == "" {
    93  		return &errors2.Error{
    94  			Code: errors2.EInvalid,
    95  			Msg:  "Notification Endpoint Description can't be empty",
    96  		}
    97  	}
    98  
    99  	if n.Status != nil {
   100  		if err := n.Status.Valid(); err != nil {
   101  			return err
   102  		}
   103  	}
   104  
   105  	return nil
   106  }
   107  
   108  // NotificationEndpointService represents a service for managing notification endpoints.
   109  type NotificationEndpointService interface {
   110  	// FindNotificationEndpointByID returns a single notification endpoint by ID.
   111  	FindNotificationEndpointByID(ctx context.Context, id platform.ID) (NotificationEndpoint, error)
   112  
   113  	// FindNotificationEndpoints returns a list of notification endpoints that match filter and the total count of matching notification endpoints.
   114  	// Additional options provide pagination & sorting.
   115  	FindNotificationEndpoints(ctx context.Context, filter NotificationEndpointFilter, opt ...FindOptions) ([]NotificationEndpoint, int, error)
   116  
   117  	// CreateNotificationEndpoint creates a new notification endpoint and sets b.ID with the new identifier.
   118  	CreateNotificationEndpoint(ctx context.Context, ne NotificationEndpoint, userID platform.ID) error
   119  
   120  	// UpdateNotificationEndpoint updates a single notification endpoint.
   121  	// Returns the new notification endpoint after update.
   122  	UpdateNotificationEndpoint(ctx context.Context, id platform.ID, nr NotificationEndpoint, userID platform.ID) (NotificationEndpoint, error)
   123  
   124  	// PatchNotificationEndpoint updates a single  notification endpoint with changeset.
   125  	// Returns the new notification endpoint state after update.
   126  	PatchNotificationEndpoint(ctx context.Context, id platform.ID, upd NotificationEndpointUpdate) (NotificationEndpoint, error)
   127  
   128  	// DeleteNotificationEndpoint removes a notification endpoint by ID, returns secret fields, orgID for further deletion.
   129  	DeleteNotificationEndpoint(ctx context.Context, id platform.ID) (flds []SecretField, orgID platform.ID, err error)
   130  }