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