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

     1  package nsmodel
     2  
     3  import (
     4  	"encoding/json"
     5  
     6  	validation "github.com/go-ozzo/ozzo-validation/v4"
     7  	"github.com/kyma-incubator/compass/components/director/internal/model"
     8  	"github.com/kyma-incubator/compass/components/director/pkg/str"
     9  	"github.com/tidwall/gjson"
    10  )
    11  
    12  // Mappings stores mappings between system values and ApplicationTemplates
    13  var Mappings []TemplateMapping
    14  
    15  // TemplateMapping mapping for application templates
    16  type TemplateMapping struct {
    17  	Name        string
    18  	Region      string
    19  	ID          string
    20  	SourceKey   []string
    21  	SourceValue []string
    22  }
    23  
    24  // SystemBase represents on-premise system
    25  type SystemBase struct {
    26  	Protocol     string `json:"protocol"`
    27  	Host         string `json:"host"`
    28  	SystemType   string `json:"type"`
    29  	Description  string `json:"description"`
    30  	Status       string `json:"status"`
    31  	SystemNumber string `json:"systemNumber"`
    32  }
    33  
    34  // System represents on-premise system with ApplicationTemplate ID
    35  type System struct {
    36  	SystemBase
    37  	TemplateID string `json:"-"`
    38  }
    39  
    40  // Validate validates System fields
    41  func (s System) Validate() error {
    42  	return validation.ValidateStruct(&s,
    43  		validation.Field(&s.Protocol, validation.Required),
    44  		validation.Field(&s.Host, validation.Required),
    45  		validation.Field(&s.SystemType, validation.Required),
    46  		validation.Field(&s.Description, validation.NotNil),
    47  		validation.Field(&s.Status, validation.Required),
    48  		validation.Field(&s.SystemNumber, validation.NotNil),
    49  	)
    50  }
    51  
    52  // UnmarshalJSON unmarshal the provided data into System
    53  func (s *System) UnmarshalJSON(data []byte) error {
    54  	if err := json.Unmarshal(data, &s.SystemBase); err != nil {
    55  		return err
    56  	}
    57  
    58  	for _, mapping := range Mappings {
    59  		if matchProps(data, mapping) {
    60  			s.TemplateID = mapping.ID
    61  			return nil
    62  		}
    63  	}
    64  
    65  	return nil
    66  }
    67  
    68  func matchProps(data []byte, tm TemplateMapping) bool {
    69  	for i, sk := range tm.SourceKey {
    70  		v := gjson.GetBytes(data, sk).String()
    71  		if v != tm.SourceValue[i] {
    72  			return false
    73  		}
    74  	}
    75  	return true
    76  }
    77  
    78  // SCC represents SAP Cloud Connector
    79  type SCC struct {
    80  	ExternalSubaccountID string   `json:"subaccount"`
    81  	InternalSubaccountID string   `json:"-"`
    82  	LocationID           string   `json:"locationID"`
    83  	ExposedSystems       []System `json:"exposedSystems"`
    84  }
    85  
    86  // Validate validates SCC fields
    87  func (s SCC) Validate() error {
    88  	return validation.ValidateStruct(&s,
    89  		validation.Field(&s.ExternalSubaccountID, validation.Required),
    90  		validation.Field(&s.LocationID, validation.NotNil),
    91  		validation.Field(&s.ExposedSystems, validation.NotNil, validation.By(validateSystems)),
    92  	)
    93  }
    94  
    95  func validateSystems(value interface{}) error {
    96  	if systems, ok := value.([]System); ok {
    97  		for _, s := range systems {
    98  			if err := s.Validate(); err != nil {
    99  				return err
   100  			}
   101  		}
   102  	}
   103  	return nil
   104  }
   105  
   106  // Report represents Notification Service reports to CMP
   107  type Report struct {
   108  	ReportType string `json:"type"`
   109  	Value      []SCC  `json:"value"`
   110  }
   111  
   112  // Validate validates Report fields
   113  func (r Report) Validate() error {
   114  	return validation.ValidateStruct(&r,
   115  		validation.Field(&r.ReportType, validation.Required),
   116  		validation.Field(&r.Value, validation.NotNil, validation.By(validateSCCs)),
   117  	)
   118  }
   119  
   120  func validateSCCs(value interface{}) error {
   121  	if scc, ok := value.([]SCC); ok {
   122  		for _, s := range scc {
   123  			if err := s.Validate(); err != nil {
   124  				return err
   125  			}
   126  		}
   127  	}
   128  	return nil
   129  }
   130  
   131  // ToAppUpdateInput converts System to model.ApplicationUpdateInput
   132  func ToAppUpdateInput(system System) model.ApplicationUpdateInput {
   133  	return model.ApplicationUpdateInput{
   134  		Description:  str.Ptr(system.Description),
   135  		SystemStatus: str.Ptr(system.Status),
   136  	}
   137  }