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

     1  package systemfetcher
     2  
     3  import (
     4  	"encoding/json"
     5  	"time"
     6  
     7  	"github.com/kyma-incubator/compass/components/director/internal/model"
     8  
     9  	"github.com/tidwall/gjson"
    10  )
    11  
    12  var (
    13  	// ApplicationTemplates global static configuration which is set after reading the configuration during startup, should only be used for the unmarshaling of system data
    14  	// It represents a model.ApplicationTemplate with its labels in the form of map[string]*model.Label
    15  	ApplicationTemplates []TemplateMapping
    16  	// ApplicationTemplateLabelFilter represent a label for the Application Templates which has a value that
    17  	// should match to the SystemSourceKey's value of the fetched systems
    18  	ApplicationTemplateLabelFilter string
    19  	// SelectFilter represents the select filter that determines which properties of a system will be fetched
    20  	SelectFilter []string
    21  	// SystemSourceKey represents a key for filtering systems
    22  	SystemSourceKey string
    23  	// SystemSynchronizationTimestamps represents the systems last synchronization timestamps for each tenant
    24  	SystemSynchronizationTimestamps map[string]map[string]SystemSynchronizationTimestamp
    25  )
    26  
    27  // TemplateMapping holds data for Application Templates and their Labels
    28  type TemplateMapping struct {
    29  	AppTemplate *model.ApplicationTemplate
    30  	Labels      map[string]*model.Label
    31  }
    32  
    33  // System missing godoc
    34  type System struct {
    35  	SystemPayload   map[string]interface{}
    36  	TemplateID      string                           `json:"-"`
    37  	StatusCondition model.ApplicationStatusCondition `json:"-"`
    38  }
    39  
    40  // SystemSynchronizationTimestamp represents the last synchronization time of a system
    41  type SystemSynchronizationTimestamp struct {
    42  	ID                string
    43  	LastSyncTimestamp time.Time
    44  }
    45  
    46  // UnmarshalJSON missing godoc
    47  func (s *System) UnmarshalJSON(data []byte) error {
    48  	if err := json.Unmarshal(data, &s.SystemPayload); err != nil {
    49  		return err
    50  	}
    51  
    52  	for _, tm := range ApplicationTemplates {
    53  		if matchProps(data, tm) {
    54  			s.TemplateID = tm.AppTemplate.ID
    55  			return nil
    56  		}
    57  	}
    58  
    59  	return nil
    60  }
    61  
    62  func matchProps(data []byte, tm TemplateMapping) bool {
    63  	lbl, ok := tm.Labels[ApplicationTemplateLabelFilter]
    64  	if !ok {
    65  		return false
    66  	}
    67  
    68  	templateMappingLabelValue, ok := lbl.Value.(string)
    69  	if !ok {
    70  		return false
    71  	}
    72  
    73  	if systemSourceKeyValue := gjson.GetBytes(data, SystemSourceKey).String(); systemSourceKeyValue != templateMappingLabelValue {
    74  		return false
    75  	}
    76  
    77  	return true
    78  }