webhooks

package
v0.0.0-...-3511abf Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 2, 2023 License: Apache-2.0 Imports: 32 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

AuthZProvider is the authz registry for experiments.

Functions

func AddWebhook

func AddWebhook(ctx context.Context, w *Webhook) error

AddWebhook adds a Webhook and its Triggers to the DB.

func AuthorizeRequest

func AuthorizeRequest(ctx context.Context) error

AuthorizeRequest checks if the user has CanEditWebhooks permissions. TODO remove this eventually since authz replaces this We can't yet since we use it else where.

func Deinit

func Deinit()

Deinit closes a shipper.

func DeleteWebhook

func DeleteWebhook(ctx context.Context, id WebhookID) error

DeleteWebhook deletes a Webhook and its Triggers from the DB.

func Init

func Init()

Init creates a shipper singleton.

func ReportExperimentStateChanged

func ReportExperimentStateChanged(
	ctx context.Context, e model.Experiment, activeConfig expconf.ExperimentConfig,
) error

ReportExperimentStateChanged adds webhook events to the queue. TODO(DET-8577): Remove unnecessary active config usage (remove the activeConfig parameter).

func ScanLogs

func ScanLogs(ctx context.Context, logs []*model.TaskLog) error

ScanLogs sends webhooks for task logs. This should be called wherever we add task logs.

func SetDefault

func SetDefault(w *WebhookManager)

SetDefault sets the default webhook manager singleton.

Types

type Condition

type Condition struct {
	State model.State `json:"state,omitempty"`
	Regex string      `json:"regex,omitempty"`
}

Condition represents a trigger condition.

type Event

type Event struct {
	bun.BaseModel `bun:"table:webhook_events_queue"`

	ID      WebhookEventID `bun:"id,pk,autoincrement"`
	URL     string         `bun:"url,notnull"`
	Payload []byte         `bun:"payload,notnull"`
}

Event corresponds to a row in the "webhook_events" DB table.

type EventData

type EventData struct {
	TestData   *string            `json:"data,omitempty"`
	Experiment *ExperimentPayload `json:"experiment,omitempty"`
	TaskLog    *TaskLogPayload    `json:"task_log,omitempty"`
}

EventData represents the event_data for a webhook event.

type EventPayload

type EventPayload struct {
	ID        uuid.UUID   `json:"event_id"`
	Type      TriggerType `json:"event_type"`
	Timestamp int64       `json:"timestamp"`
	Condition Condition   `json:"condition"`
	Data      EventData   `json:"event_data"`
}

EventPayload respresents a webhook event.

type ExperimentPayload

type ExperimentPayload struct {
	ID            int          `json:"id"`
	State         model.State  `json:"state"`
	Name          expconf.Name `json:"name"`
	Duration      int          `json:"duration"`
	ResourcePool  string       `json:"resource_pool"`
	SlotsPerTrial int          `json:"slots_per_trial"`
	WorkspaceName string       `json:"workspace"`
	ProjectName   string       `json:"project"`
}

ExperimentPayload is the webhook request representation of an experiment.

type SlackAttachment

type SlackAttachment struct {
	Color  string       `json:"color,omitempty"`
	Blocks []SlackBlock `json:"blocks,omitempty"`
}

SlackAttachment corresponds to an Attachment Slack Block element.

type SlackBlock

type SlackBlock struct {
	Type   string        `json:"type,omitempty"`
	Text   SlackField    `json:"text,omitempty"`
	Fields *[]SlackField `json:"fields,omitempty"`
}

SlackBlock corresponds to a Slack Block element.

type SlackField

type SlackField struct {
	Type string `json:"type"`
	Text string `json:"text"`
}

SlackField corresponds to a field in a Slack Block element.

type SlackMessageBody

type SlackMessageBody struct {
	Blocks      []SlackBlock       `json:"blocks,omitempty"`
	Attachments *[]SlackAttachment `json:"attachments,omitempty"`
}

SlackMessageBody corresponds to an entire message as a Slack Block.

type TaskLogPayload

type TaskLogPayload struct {
	TaskID        model.TaskID `json:"task_id"`
	NodeName      string       `json:"node_name"`
	TriggeringLog string       `json:"triggering_log"`
}

TaskLogPayload is the webhook request representation of a trigger of a task log.

type Trigger

type Trigger struct {
	bun.BaseModel `bun:"table:webhook_triggers"`

	ID          TriggerID              `bun:"id,pk,autoincrement"`
	TriggerType TriggerType            `bun:"trigger_type,notnull"`
	Condition   map[string]interface{} `bun:"condition,notnull"`
	WebhookID   WebhookID              `bun:"webhook_id,notnull"`

	Webhook *Webhook `bun:"rel:belongs-to,join:webhook_id=id"`
}

Trigger corresponds to a row in the "webhook_triggers" DB table.

func TriggerFromProto

func TriggerFromProto(t *webhookv1.Trigger) *Trigger

TriggerFromProto returns a Trigger from a proto definition.

func (*Trigger) Proto

func (t *Trigger) Proto() *webhookv1.Trigger

Proto converts a Trigger to its protobuf representation.

type TriggerID

type TriggerID int

TriggerID is the type for Trigger IDs.

type TriggerType

type TriggerType string

TriggerType is type for the TriggerType enum.

const (
	// TriggerTypeStateChange represents a change in experiment state.
	TriggerTypeStateChange TriggerType = "EXPERIMENT_STATE_CHANGE"

	// TriggerTypeMetricThresholdExceeded represents a threshold for a training metric value.
	TriggerTypeMetricThresholdExceeded TriggerType = "METRIC_THRESHOLD_EXCEEDED"

	// TriggerTypeTaskLog represents a trigger for a task logs.
	TriggerTypeTaskLog TriggerType = "TASK_LOG"
)

func TriggerTypeFromProto

func TriggerTypeFromProto(t webhookv1.TriggerType) TriggerType

TriggerTypeFromProto returns a TriggerType from a proto.

func (TriggerType) Proto

func (t TriggerType) Proto() webhookv1.TriggerType

Proto returns a proto from a TriggerType.

type Triggers

type Triggers []*Trigger

Triggers is a slice of Trigger objects—primarily useful for its methods.

func TriggersFromProto

func TriggersFromProto(ts []*webhookv1.Trigger) Triggers

TriggersFromProto returns a slice of model Triggers from a proto definition.

func (Triggers) Proto

func (ts Triggers) Proto() []*webhookv1.Trigger

Proto converts a slice of triggers to its protobuf representation.

type Webhook

type Webhook struct {
	bun.BaseModel `bun:"table:webhooks"`

	ID          WebhookID   `bun:"id,pk,autoincrement"`
	WebhookType WebhookType `bun:"webhook_type,notnull"`
	URL         string      `bun:"url,notnull"`

	Triggers Triggers `bun:"rel:has-many,join:id=webhook_id"`
}

Webhook corresponds to a row in the "webhooks" DB table.

func GetWebhook

func GetWebhook(ctx context.Context, webhookID int) (*Webhook, error)

GetWebhook returns a single Webhooks from the DB.

func WebhookFromProto

func WebhookFromProto(w *webhookv1.Webhook) Webhook

WebhookFromProto returns a model Webhook from a proto definition.

func (*Webhook) Proto

func (w *Webhook) Proto() *webhookv1.Webhook

Proto converts a webhook to its protobuf representation.

type WebhookAuthZ

type WebhookAuthZ interface {
	// GET /api/v1/webhooks
	// POST /api/v1/webhooks
	// DELETE /api/v1/webhooks/:webhook_id
	// POST /api/v1/webhooks/test/:webhook_id
	CanEditWebhooks(ctx context.Context, curUser *model.User) (serverError error)
}

WebhookAuthZ describes authz methods for experiments.

type WebhookAuthZBasic

type WebhookAuthZBasic struct{}

WebhookAuthZBasic is basic OSS controls.

func (*WebhookAuthZBasic) CanEditWebhooks

func (a *WebhookAuthZBasic) CanEditWebhooks(
	ctx context.Context, curUser *model.User,
) (serverError error)

CanEditWebhooks always returns true and a nil error.

type WebhookEventID

type WebhookEventID int

WebhookEventID is the type for Trigger IDs.

type WebhookID

type WebhookID int

WebhookID is the type for Webhook IDs.

type WebhookManager

type WebhookManager struct {
	// contains filtered or unexported fields
}

WebhookManager manages webhooks.

func New

func New(ctx context.Context) (*WebhookManager, error)

New creates a new webhook manager.

type WebhookType

type WebhookType string

WebhookType is type for the WebhookType enum.

const (
	// WebhookTypeDefault represents a default webhook.
	WebhookTypeDefault WebhookType = "DEFAULT"

	// WebhookTypeSlack represents a slack webhook.
	WebhookTypeSlack WebhookType = "SLACK"
)

func WebhookTypeFromProto

func WebhookTypeFromProto(w webhookv1.WebhookType) WebhookType

WebhookTypeFromProto returns a WebhookType from a proto.

func (WebhookType) Proto

func (w WebhookType) Proto() webhookv1.WebhookType

Proto returns a proto from a WebhookType.

type Webhooks

type Webhooks []Webhook

Webhooks is a slice of Webhook objects.

func GetWebhooks

func GetWebhooks(ctx context.Context) (Webhooks, error)

GetWebhooks returns all Webhooks from the DB.

func (Webhooks) Proto

func (ws Webhooks) Proto() []*webhookv1.Webhook

Proto converts a slice of webhooks to its protobuf representation.

type WebhooksAPIServer

type WebhooksAPIServer struct{}

WebhooksAPIServer is an embedded api server struct.

func (*WebhooksAPIServer) DeleteWebhook

DeleteWebhook deletes a Webhook.

func (*WebhooksAPIServer) GetWebhooks

GetWebhooks returns all Webhooks.

func (*WebhooksAPIServer) PostWebhook

PostWebhook creates a new Webhook.

func (*WebhooksAPIServer) TestWebhook

TestWebhook sends a test event for a Webhook.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL