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

     1  package model
     2  
     3  import (
     4  	"encoding/json"
     5  	"regexp"
     6  	"strconv"
     7  
     8  	"github.com/kyma-incubator/compass/components/director/pkg/accessstrategy"
     9  
    10  	"github.com/kyma-incubator/compass/components/director/pkg/str"
    11  
    12  	validation "github.com/go-ozzo/ozzo-validation/v4"
    13  	"github.com/go-ozzo/ozzo-validation/v4/is"
    14  	"github.com/kyma-incubator/compass/components/director/pkg/pagination"
    15  	"github.com/kyma-incubator/compass/components/director/pkg/resource"
    16  )
    17  
    18  // EventDefinition missing godoc
    19  type EventDefinition struct {
    20  	ApplicationID                *string
    21  	ApplicationTemplateVersionID *string
    22  	PackageID                    *string
    23  	Name                         string
    24  	Description                  *string
    25  	Group                        *string
    26  	OrdID                        *string
    27  	LocalTenantID                *string
    28  	ShortDescription             *string
    29  	SystemInstanceAware          *bool
    30  	PolicyLevel                  *string
    31  	CustomPolicyLevel            *string
    32  	ChangeLogEntries             json.RawMessage
    33  	Links                        json.RawMessage
    34  	Tags                         json.RawMessage
    35  	Countries                    json.RawMessage
    36  	ReleaseStatus                *string
    37  	SunsetDate                   *string
    38  	Successors                   json.RawMessage
    39  	Labels                       json.RawMessage
    40  	Visibility                   *string
    41  	Disabled                     *bool
    42  	PartOfProducts               json.RawMessage
    43  	LineOfBusiness               json.RawMessage
    44  	Industry                     json.RawMessage
    45  	Extensible                   json.RawMessage
    46  	ResourceHash                 *string
    47  	Version                      *Version
    48  	Hierarchy                    json.RawMessage
    49  	DocumentationLabels          json.RawMessage
    50  	*BaseEntity
    51  }
    52  
    53  // GetType missing godoc
    54  func (*EventDefinition) GetType() resource.Type {
    55  	return resource.EventDefinition
    56  }
    57  
    58  // EventDefinitionPage missing godoc
    59  type EventDefinitionPage struct {
    60  	Data       []*EventDefinition
    61  	PageInfo   *pagination.Page
    62  	TotalCount int
    63  }
    64  
    65  // IsPageable missing godoc
    66  func (EventDefinitionPage) IsPageable() {}
    67  
    68  // EventDefinitionInput missing godoc
    69  type EventDefinitionInput struct {
    70  	OrdPackageID             *string                       `json:"partOfPackage"`
    71  	Name                     string                        `json:"title"`
    72  	Description              *string                       `json:"description"`
    73  	Group                    *string                       `json:",omitempty"`
    74  	OrdID                    *string                       `json:"ordId"`
    75  	LocalTenantID            *string                       `json:"localTenantId"`
    76  	ShortDescription         *string                       `json:"shortDescription"`
    77  	SystemInstanceAware      *bool                         `json:"systemInstanceAware"`
    78  	PolicyLevel              *string                       `json:"policyLevel"`
    79  	CustomPolicyLevel        *string                       `json:"customPolicyLevel"`
    80  	ChangeLogEntries         json.RawMessage               `json:"changelogEntries"`
    81  	Links                    json.RawMessage               `json:"links"`
    82  	Tags                     json.RawMessage               `json:"tags"`
    83  	Countries                json.RawMessage               `json:"countries"`
    84  	ReleaseStatus            *string                       `json:"releaseStatus"`
    85  	SunsetDate               *string                       `json:"sunsetDate"`
    86  	Successors               json.RawMessage               `json:"successors,omitempty"`
    87  	Labels                   json.RawMessage               `json:"labels"`
    88  	Visibility               *string                       `json:"visibility"`
    89  	Disabled                 *bool                         `json:"disabled"`
    90  	PartOfProducts           json.RawMessage               `json:"partOfProducts"`
    91  	LineOfBusiness           json.RawMessage               `json:"lineOfBusiness"`
    92  	Industry                 json.RawMessage               `json:"industry"`
    93  	Extensible               json.RawMessage               `json:"extensible"`
    94  	ResourceDefinitions      []*EventResourceDefinition    `json:"resourceDefinitions"`
    95  	PartOfConsumptionBundles []*ConsumptionBundleReference `json:"partOfConsumptionBundles"`
    96  	DefaultConsumptionBundle *string                       `json:"defaultConsumptionBundle"`
    97  	Hierarchy                json.RawMessage               `json:"hierarchy"`
    98  	DocumentationLabels      json.RawMessage               `json:"documentationLabels"`
    99  
   100  	*VersionInput `hash:"ignore"`
   101  }
   102  
   103  // EventResourceDefinition missing godoc
   104  type EventResourceDefinition struct { // This is the place from where the specification for this API is fetched
   105  	Type           EventSpecType                   `json:"type"`
   106  	CustomType     string                          `json:"customType"`
   107  	MediaType      SpecFormat                      `json:"mediaType"`
   108  	URL            string                          `json:"url"`
   109  	AccessStrategy accessstrategy.AccessStrategies `json:"accessStrategies"`
   110  }
   111  
   112  // Validate missing godoc
   113  func (rd *EventResourceDefinition) Validate() error {
   114  	const CustomTypeRegex = "^([a-z0-9-]+(?:[.][a-z0-9-]+)*):([a-zA-Z0-9._\\-]+):v([0-9]+)$"
   115  	return validation.ValidateStruct(rd,
   116  		validation.Field(&rd.Type, validation.Required, validation.In(EventSpecTypeAsyncAPIV2, EventSpecTypeCustom), validation.When(rd.CustomType != "", validation.In(EventSpecTypeCustom))),
   117  		validation.Field(&rd.CustomType, validation.When(rd.CustomType != "", validation.Match(regexp.MustCompile(CustomTypeRegex)))),
   118  		validation.Field(&rd.MediaType, validation.Required, validation.In(SpecFormatApplicationJSON, SpecFormatTextYAML, SpecFormatApplicationXML, SpecFormatPlainText, SpecFormatOctetStream)),
   119  		validation.Field(&rd.URL, validation.Required, is.RequestURI),
   120  		validation.Field(&rd.AccessStrategy, validation.Required),
   121  	)
   122  }
   123  
   124  // ToSpec missing godoc
   125  func (rd *EventResourceDefinition) ToSpec() *SpecInput {
   126  	var auth *AuthInput
   127  	if as, ok := rd.AccessStrategy.GetSupported(); ok {
   128  		asString := string(as)
   129  		auth = &AuthInput{
   130  			AccessStrategy: &asString,
   131  		}
   132  	}
   133  
   134  	specType := rd.Type
   135  	return &SpecInput{
   136  		Format:     rd.MediaType,
   137  		EventType:  &specType,
   138  		CustomType: &rd.CustomType,
   139  		FetchRequest: &FetchRequestInput{
   140  			URL:  rd.URL,
   141  			Auth: auth,
   142  		},
   143  	}
   144  }
   145  
   146  // ToEventDefinition missing godoc
   147  func (e *EventDefinitionInput) ToEventDefinition(id string, resourceType resource.Type, resourceID string, packageID *string, eventHash uint64) *EventDefinition {
   148  	if e == nil {
   149  		return nil
   150  	}
   151  
   152  	var hash *string
   153  	if eventHash != 0 {
   154  		hash = str.Ptr(strconv.FormatUint(eventHash, 10))
   155  	}
   156  
   157  	event := &EventDefinition{
   158  		PackageID:           packageID,
   159  		Name:                e.Name,
   160  		Description:         e.Description,
   161  		Group:               e.Group,
   162  		OrdID:               e.OrdID,
   163  		LocalTenantID:       e.LocalTenantID,
   164  		ShortDescription:    e.ShortDescription,
   165  		SystemInstanceAware: e.SystemInstanceAware,
   166  		PolicyLevel:         e.PolicyLevel,
   167  		CustomPolicyLevel:   e.CustomPolicyLevel,
   168  		Tags:                e.Tags,
   169  		Countries:           e.Countries,
   170  		Links:               e.Links,
   171  		ReleaseStatus:       e.ReleaseStatus,
   172  		SunsetDate:          e.SunsetDate,
   173  		Successors:          e.Successors,
   174  		ChangeLogEntries:    e.ChangeLogEntries,
   175  		Labels:              e.Labels,
   176  		Visibility:          e.Visibility,
   177  		Disabled:            e.Disabled,
   178  		PartOfProducts:      e.PartOfProducts,
   179  		LineOfBusiness:      e.LineOfBusiness,
   180  		Industry:            e.Industry,
   181  		Version:             e.VersionInput.ToVersion(),
   182  		Extensible:          e.Extensible,
   183  		Hierarchy:           e.Hierarchy,
   184  		DocumentationLabels: e.DocumentationLabels,
   185  		ResourceHash:        hash,
   186  		BaseEntity: &BaseEntity{
   187  			ID:    id,
   188  			Ready: true,
   189  		},
   190  	}
   191  
   192  	if resourceType.IsTenantIgnorable() {
   193  		event.ApplicationTemplateVersionID = &resourceID
   194  	} else if resourceType == resource.Application {
   195  		event.ApplicationID = &resourceID
   196  	}
   197  
   198  	return event
   199  }