github.com/kyma-incubator/compass/components/director@v0.0.0-20230623144113-d764f56ff805/internal/model/api.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  
    15  	"github.com/kyma-incubator/compass/components/director/pkg/resource"
    16  
    17  	"github.com/kyma-incubator/compass/components/director/pkg/pagination"
    18  )
    19  
    20  // APIDefinition missing godoc
    21  type APIDefinition struct {
    22  	ApplicationID                           *string
    23  	ApplicationTemplateVersionID            *string
    24  	PackageID                               *string
    25  	Name                                    string
    26  	Description                             *string
    27  	TargetURLs                              json.RawMessage
    28  	Group                                   *string //  group allows you to find the same API but in different version
    29  	OrdID                                   *string
    30  	LocalTenantID                           *string
    31  	ShortDescription                        *string
    32  	SystemInstanceAware                     *bool
    33  	PolicyLevel                             *string
    34  	CustomPolicyLevel                       *string
    35  	APIProtocol                             *string
    36  	Tags                                    json.RawMessage
    37  	Countries                               json.RawMessage
    38  	Links                                   json.RawMessage
    39  	APIResourceLinks                        json.RawMessage
    40  	ReleaseStatus                           *string
    41  	SunsetDate                              *string
    42  	Successors                              json.RawMessage
    43  	ChangeLogEntries                        json.RawMessage
    44  	Labels                                  json.RawMessage
    45  	Visibility                              *string
    46  	Disabled                                *bool
    47  	PartOfProducts                          json.RawMessage
    48  	LineOfBusiness                          json.RawMessage
    49  	Industry                                json.RawMessage
    50  	ImplementationStandard                  *string
    51  	CustomImplementationStandard            *string
    52  	CustomImplementationStandardDescription *string
    53  	Version                                 *Version
    54  	Extensible                              json.RawMessage
    55  	ResourceHash                            *string
    56  	Hierarchy                               json.RawMessage
    57  	SupportedUseCases                       json.RawMessage
    58  	DocumentationLabels                     json.RawMessage
    59  	*BaseEntity
    60  }
    61  
    62  // GetType missing godoc
    63  func (*APIDefinition) GetType() resource.Type {
    64  	return resource.API
    65  }
    66  
    67  // APIDefinitionInput missing godoc
    68  type APIDefinitionInput struct {
    69  	OrdPackageID                            *string                       `json:"partOfPackage"`
    70  	Tenant                                  string                        `json:",omitempty"`
    71  	Name                                    string                        `json:"title"`
    72  	Description                             *string                       `json:"description"`
    73  	TargetURLs                              json.RawMessage               `json:"entryPoints"`
    74  	Group                                   *string                       `json:",omitempty"` //  group allows you to find the same API but in different version
    75  	OrdID                                   *string                       `json:"ordId"`
    76  	LocalTenantID                           *string                       `json:"localTenantId"`
    77  	ShortDescription                        *string                       `json:"shortDescription"`
    78  	SystemInstanceAware                     *bool                         `json:"systemInstanceAware"`
    79  	PolicyLevel                             *string                       `json:"policyLevel"`
    80  	CustomPolicyLevel                       *string                       `json:"customPolicyLevel"`
    81  	APIProtocol                             *string                       `json:"apiProtocol"`
    82  	Tags                                    json.RawMessage               `json:"tags"`
    83  	Countries                               json.RawMessage               `json:"countries"`
    84  	Links                                   json.RawMessage               `json:"links"`
    85  	APIResourceLinks                        json.RawMessage               `json:"apiResourceLinks"`
    86  	ReleaseStatus                           *string                       `json:"releaseStatus"`
    87  	SunsetDate                              *string                       `json:"sunsetDate"`
    88  	Successors                              json.RawMessage               `json:"successors,omitempty"`
    89  	ChangeLogEntries                        json.RawMessage               `json:"changelogEntries"`
    90  	Labels                                  json.RawMessage               `json:"labels"`
    91  	Visibility                              *string                       `json:"visibility"`
    92  	Disabled                                *bool                         `json:"disabled"`
    93  	PartOfProducts                          json.RawMessage               `json:"partOfProducts"`
    94  	LineOfBusiness                          json.RawMessage               `json:"lineOfBusiness"`
    95  	Industry                                json.RawMessage               `json:"industry"`
    96  	ImplementationStandard                  *string                       `json:"implementationStandard"`
    97  	CustomImplementationStandard            *string                       `json:"customImplementationStandard"`
    98  	CustomImplementationStandardDescription *string                       `json:"customImplementationStandardDescription"`
    99  	Extensible                              json.RawMessage               `json:"extensible"`
   100  	ResourceDefinitions                     []*APIResourceDefinition      `json:"resourceDefinitions"`
   101  	PartOfConsumptionBundles                []*ConsumptionBundleReference `json:"partOfConsumptionBundles"`
   102  	DefaultConsumptionBundle                *string                       `json:"defaultConsumptionBundle"`
   103  	Hierarchy                               json.RawMessage               `json:"hierarchy"`
   104  	SupportedUseCases                       json.RawMessage               `json:"supported_use_cases"`
   105  	DocumentationLabels                     json.RawMessage               `json:"documentationLabels"`
   106  
   107  	*VersionInput `hash:"ignore"`
   108  }
   109  
   110  // APIResourceDefinition missing godoc
   111  type APIResourceDefinition struct { // This is the place from where the specification for this API is fetched
   112  	Type           APISpecType                     `json:"type"`
   113  	CustomType     string                          `json:"customType"`
   114  	MediaType      SpecFormat                      `json:"mediaType"`
   115  	URL            string                          `json:"url"`
   116  	AccessStrategy accessstrategy.AccessStrategies `json:"accessStrategies"`
   117  }
   118  
   119  // Validate missing godoc
   120  func (rd *APIResourceDefinition) Validate() error {
   121  	const CustomTypeRegex = "^([a-z0-9-]+(?:[.][a-z0-9-]+)*):([a-zA-Z0-9._\\-]+):v([0-9]+)$"
   122  	return validation.ValidateStruct(rd,
   123  		validation.Field(&rd.Type, validation.Required, validation.In(APISpecTypeOpenAPIV2, APISpecTypeOpenAPIV3, APISpecTypeRaml, APISpecTypeEDMX,
   124  			APISpecTypeCsdl, APISpecTypeWsdlV1, APISpecTypeWsdlV2, APISpecTypeRfcMetadata, APISpecTypeCustom, APISpecTypeSQLAPIDefinitionV1), validation.When(rd.CustomType != "", validation.In(APISpecTypeCustom))),
   125  		validation.Field(&rd.CustomType, validation.When(rd.CustomType != "", validation.Match(regexp.MustCompile(CustomTypeRegex)))),
   126  		validation.Field(&rd.MediaType, validation.Required, validation.In(SpecFormatApplicationJSON, SpecFormatTextYAML, SpecFormatApplicationXML, SpecFormatPlainText, SpecFormatOctetStream),
   127  			validation.When(rd.Type == APISpecTypeOpenAPIV2 || rd.Type == APISpecTypeOpenAPIV3, validation.In(SpecFormatApplicationJSON, SpecFormatTextYAML)),
   128  			validation.When(rd.Type == APISpecTypeRaml, validation.In(SpecFormatTextYAML)),
   129  			validation.When(rd.Type == APISpecTypeEDMX, validation.In(SpecFormatApplicationXML)),
   130  			validation.When(rd.Type == APISpecTypeCsdl, validation.In(SpecFormatApplicationJSON)),
   131  			validation.When(rd.Type == APISpecTypeWsdlV1 || rd.Type == APISpecTypeWsdlV2, validation.In(SpecFormatApplicationXML)),
   132  			validation.When(rd.Type == APISpecTypeRfcMetadata, validation.In(SpecFormatApplicationXML)),
   133  			validation.When(rd.Type == APISpecTypeSQLAPIDefinitionV1, validation.In(SpecFormatApplicationJSON))),
   134  		validation.Field(&rd.URL, validation.Required, is.RequestURI),
   135  		validation.Field(&rd.AccessStrategy, validation.Required),
   136  	)
   137  }
   138  
   139  // ToSpec missing godoc
   140  func (rd *APIResourceDefinition) ToSpec() *SpecInput {
   141  	var auth *AuthInput
   142  	if as, ok := rd.AccessStrategy.GetSupported(); ok {
   143  		asString := string(as)
   144  		auth = &AuthInput{
   145  			AccessStrategy: &asString,
   146  		}
   147  	}
   148  
   149  	return &SpecInput{
   150  		Format:     rd.MediaType,
   151  		APIType:    &rd.Type,
   152  		CustomType: &rd.CustomType,
   153  		FetchRequest: &FetchRequestInput{
   154  			URL:  rd.URL,
   155  			Auth: auth,
   156  		},
   157  	}
   158  }
   159  
   160  // ConsumptionBundleReference missing godoc
   161  type ConsumptionBundleReference struct {
   162  	BundleOrdID      string `json:"ordId"`
   163  	DefaultTargetURL string `json:"defaultEntryPoint"`
   164  }
   165  
   166  // APIDefinitionPage missing godoc
   167  type APIDefinitionPage struct {
   168  	Data       []*APIDefinition
   169  	PageInfo   *pagination.Page
   170  	TotalCount int
   171  }
   172  
   173  // IsPageable missing godoc
   174  func (APIDefinitionPage) IsPageable() {}
   175  
   176  // ToAPIDefinition missing godoc
   177  func (a *APIDefinitionInput) ToAPIDefinition(id string, resourceType resource.Type, resourceID string, packageID *string, apiHash uint64) *APIDefinition {
   178  	if a == nil {
   179  		return nil
   180  	}
   181  
   182  	var hash *string
   183  	if apiHash != 0 {
   184  		hash = str.Ptr(strconv.FormatUint(apiHash, 10))
   185  	}
   186  
   187  	api := &APIDefinition{
   188  		PackageID:           packageID,
   189  		Name:                a.Name,
   190  		Description:         a.Description,
   191  		TargetURLs:          a.TargetURLs,
   192  		Group:               a.Group,
   193  		OrdID:               a.OrdID,
   194  		LocalTenantID:       a.LocalTenantID,
   195  		ShortDescription:    a.ShortDescription,
   196  		SystemInstanceAware: a.SystemInstanceAware,
   197  		PolicyLevel:         a.PolicyLevel,
   198  		CustomPolicyLevel:   a.CustomPolicyLevel,
   199  		APIProtocol:         a.APIProtocol,
   200  		Tags:                a.Tags,
   201  		Countries:           a.Countries,
   202  		Links:               a.Links,
   203  		APIResourceLinks:    a.APIResourceLinks,
   204  		ReleaseStatus:       a.ReleaseStatus,
   205  		SunsetDate:          a.SunsetDate,
   206  		Successors:          a.Successors,
   207  		ChangeLogEntries:    a.ChangeLogEntries,
   208  		Labels:              a.Labels,
   209  		Visibility:          a.Visibility,
   210  		Disabled:            a.Disabled,
   211  		PartOfProducts:      a.PartOfProducts,
   212  		LineOfBusiness:      a.LineOfBusiness,
   213  		Industry:            a.Industry,
   214  		Extensible:          a.Extensible,
   215  		Version:             a.VersionInput.ToVersion(),
   216  		Hierarchy:           a.Hierarchy,
   217  		SupportedUseCases:   a.SupportedUseCases,
   218  		DocumentationLabels: a.DocumentationLabels,
   219  		ResourceHash:        hash,
   220  		BaseEntity: &BaseEntity{
   221  			ID:    id,
   222  			Ready: true,
   223  		},
   224  	}
   225  
   226  	if resourceType.IsTenantIgnorable() {
   227  		api.ApplicationTemplateVersionID = &resourceID
   228  	} else if resourceType == resource.Application {
   229  		api.ApplicationID = &resourceID
   230  	}
   231  
   232  	return api
   233  }