github.com/kyma-incubator/compass/components/director@v0.0.0-20230623144113-d764f56ff805/internal/model/webhook.go (about)

     1  package model
     2  
     3  import (
     4  	"time"
     5  
     6  	"github.com/kyma-incubator/compass/components/director/pkg/resource"
     7  )
     8  
     9  // Webhook represents a webhook that is called by Compass.
    10  type Webhook struct {
    11  	ID               string
    12  	ObjectID         string
    13  	ObjectType       WebhookReferenceObjectType
    14  	CorrelationIDKey *string
    15  	Type             WebhookType
    16  	URL              *string
    17  	Auth             *Auth
    18  	Mode             *WebhookMode
    19  	RetryInterval    *int
    20  	Timeout          *int
    21  	URLTemplate      *string
    22  	InputTemplate    *string
    23  	HeaderTemplate   *string
    24  	OutputTemplate   *string
    25  	StatusTemplate   *string
    26  	CreatedAt        *time.Time
    27  }
    28  
    29  // WebhookInput represents a webhook input for creating/updating webhooks.
    30  type WebhookInput struct {
    31  	ID               string
    32  	CorrelationIDKey *string
    33  	Type             WebhookType
    34  	URL              *string
    35  	Auth             *AuthInput
    36  	Mode             *WebhookMode
    37  	RetryInterval    *int
    38  	Timeout          *int
    39  	URLTemplate      *string
    40  	InputTemplate    *string
    41  	HeaderTemplate   *string
    42  	OutputTemplate   *string
    43  	StatusTemplate   *string
    44  }
    45  
    46  // WebhookType represents the type of the webhook.
    47  type WebhookType string
    48  
    49  const (
    50  	// WebhookTypeConfigurationChanged represents a webhook that is called when a configuration is changed.
    51  	WebhookTypeConfigurationChanged WebhookType = "CONFIGURATION_CHANGED"
    52  	// WebhookTypeApplicationTenantMapping represents a webhook that is called for app to app notifications for formation changes.
    53  	WebhookTypeApplicationTenantMapping WebhookType = "APPLICATION_TENANT_MAPPING"
    54  	// WebhookTypeRegisterApplication represents a webhook that is called when an application is registered.
    55  	WebhookTypeRegisterApplication WebhookType = "REGISTER_APPLICATION"
    56  	// WebhookTypeDeleteApplication represents a webhook that is called when an application is deleted.
    57  	WebhookTypeDeleteApplication WebhookType = "UNREGISTER_APPLICATION"
    58  	// WebhookTypeOpenResourceDiscovery represents a webhook that is called to aggregate ORD information of a system.
    59  	WebhookTypeOpenResourceDiscovery WebhookType = "OPEN_RESOURCE_DISCOVERY"
    60  	// WebhookTypeFormationLifecycle represents a webhook that is called when lifecycle event (creation/deletion) of formation occurs
    61  	WebhookTypeFormationLifecycle WebhookType = "FORMATION_LIFECYCLE"
    62  )
    63  
    64  // WebhookMode represents the mode of the webhook.
    65  type WebhookMode string
    66  
    67  const (
    68  	// WebhookModeSync represents a webhook that is called synchronously.
    69  	WebhookModeSync WebhookMode = "SYNC"
    70  	// WebhookModeAsync represents a webhook that is called asynchronously.
    71  	WebhookModeAsync WebhookMode = "ASYNC"
    72  	// WebhookModeAsyncCallback represents a webhook that is called asynchronously and status is expected via callback on a status API.
    73  	WebhookModeAsyncCallback WebhookMode = "ASYNC_CALLBACK"
    74  )
    75  
    76  // WebhookReferenceObjectType represents the type of the object that is referenced by the webhook.
    77  type WebhookReferenceObjectType string
    78  
    79  const (
    80  	// UnknownWebhookReference is used when the webhook's reference entity cannot be determined.
    81  	// For example in case of update we have only the target's webhook ID and the input, we cannot determine the reference entity.
    82  	// In those cases an aggregated view with all the webhook ref entity tenant access views unioned together is used for tenant isolation.
    83  	UnknownWebhookReference WebhookReferenceObjectType = "Unknown"
    84  	// ApplicationWebhookReference is used when the webhook's reference entity is an application.
    85  	ApplicationWebhookReference WebhookReferenceObjectType = "ApplicationWebhook"
    86  	// RuntimeWebhookReference is used when the webhook's reference entity is a runtime.
    87  	RuntimeWebhookReference WebhookReferenceObjectType = "RuntimeWebhook"
    88  	// ApplicationTemplateWebhookReference is used when the webhook's reference entity is an application template.
    89  	ApplicationTemplateWebhookReference WebhookReferenceObjectType = "ApplicationTemplateWebhook"
    90  	// IntegrationSystemWebhookReference is used when the webhook's reference entity is an integration system.
    91  	IntegrationSystemWebhookReference WebhookReferenceObjectType = "IntegrationSystemWebhook"
    92  	// FormationTemplateWebhookReference is used when the webhook's reference entity is a formation template.
    93  	FormationTemplateWebhookReference WebhookReferenceObjectType = "FormationTemplateWebhook"
    94  )
    95  
    96  // GetResourceType returns the resource type of the webhook based on the referenced entity.
    97  func (obj WebhookReferenceObjectType) GetResourceType() resource.Type {
    98  	switch obj {
    99  	case UnknownWebhookReference:
   100  		return resource.Webhook
   101  	case ApplicationWebhookReference:
   102  		return resource.AppWebhook
   103  	case RuntimeWebhookReference:
   104  		return resource.RuntimeWebhook
   105  	case ApplicationTemplateWebhookReference:
   106  		return resource.Webhook
   107  	case IntegrationSystemWebhookReference:
   108  		return resource.Webhook
   109  	case FormationTemplateWebhookReference:
   110  		return resource.FormationTemplateWebhook
   111  	}
   112  	return ""
   113  }
   114  
   115  // ToWebhook converts the given input to a webhook.
   116  func (i *WebhookInput) ToWebhook(id, objID string, objectType WebhookReferenceObjectType) *Webhook {
   117  	return &Webhook{
   118  		ID:               id,
   119  		ObjectID:         objID,
   120  		ObjectType:       objectType,
   121  		CorrelationIDKey: i.CorrelationIDKey,
   122  		Type:             i.Type,
   123  		URL:              i.URL,
   124  		Auth:             i.Auth.ToAuth(),
   125  		Mode:             i.Mode,
   126  		RetryInterval:    i.RetryInterval,
   127  		Timeout:          i.Timeout,
   128  		URLTemplate:      i.URLTemplate,
   129  		InputTemplate:    i.InputTemplate,
   130  		HeaderTemplate:   i.HeaderTemplate,
   131  		OutputTemplate:   i.OutputTemplate,
   132  		StatusTemplate:   i.StatusTemplate,
   133  	}
   134  }