github.com/kyma-incubator/compass/components/director@v0.0.0-20230623144113-d764f56ff805/pkg/webhook/formation_configuration_change.go (about) 1 package webhook 2 3 import ( 4 "bytes" 5 "encoding/json" 6 "net/http" 7 8 "github.com/kyma-incubator/compass/components/director/internal/model" 9 ) 10 11 // ApplicationWithLabels represents an application with its corresponding labels 12 type ApplicationWithLabels struct { 13 *model.Application 14 Labels map[string]string 15 } 16 17 // ApplicationTemplateWithLabels represents an application template with its corresponding labels 18 type ApplicationTemplateWithLabels struct { 19 *model.ApplicationTemplate 20 Labels map[string]string 21 } 22 23 // RuntimeWithLabels represents a runtime with its corresponding labels 24 type RuntimeWithLabels struct { 25 *model.Runtime 26 Labels map[string]string 27 } 28 29 // RuntimeContextWithLabels represents runtime context with its corresponding labels 30 type RuntimeContextWithLabels struct { 31 *model.RuntimeContext 32 Labels map[string]string 33 } 34 35 // CustomerTenantContext represents the tenant hierarchy of the customer creating the formation. Both IDs are the external ones 36 type CustomerTenantContext struct { 37 CustomerID string 38 AccountID *string 39 Path *string 40 } 41 42 // FormationAssignment represents the FormationAssignment model, but with the value stored as a string 43 // Because otherwise the template later renders it as a stringified []byte rather than a string 44 type FormationAssignment struct { 45 ID string `json:"id"` 46 FormationID string `json:"formation_id"` 47 TenantID string `json:"tenant_id"` 48 Source string `json:"source"` 49 SourceType model.FormationAssignmentType `json:"source_type"` 50 Target string `json:"target"` 51 TargetType model.FormationAssignmentType `json:"target_type"` 52 State string `json:"state"` 53 Value string `json:"value"` 54 } 55 56 // FormationConfigurationChangeInput struct contains the input for a formation notification 57 type FormationConfigurationChangeInput struct { 58 Operation model.FormationOperation 59 FormationID string 60 ApplicationTemplate *ApplicationTemplateWithLabels 61 Application *ApplicationWithLabels 62 Runtime *RuntimeWithLabels 63 RuntimeContext *RuntimeContextWithLabels 64 CustomerTenantContext *CustomerTenantContext 65 Assignment *FormationAssignment 66 ReverseAssignment *FormationAssignment 67 } 68 69 // ParseURLTemplate missing godoc 70 func (rd *FormationConfigurationChangeInput) ParseURLTemplate(tmpl *string) (*URL, error) { 71 var url URL 72 return &url, parseTemplate(tmpl, *rd, &url) 73 } 74 75 // ParseInputTemplate missing godoc 76 func (rd *FormationConfigurationChangeInput) ParseInputTemplate(tmpl *string) ([]byte, error) { 77 res := json.RawMessage{} 78 if err := parseTemplate(tmpl, *rd, &res); err != nil { 79 return nil, err 80 } 81 res = bytes.ReplaceAll(res, []byte("<nil>"), nil) 82 return res, nil 83 } 84 85 // ParseHeadersTemplate missing godoc 86 func (rd *FormationConfigurationChangeInput) ParseHeadersTemplate(tmpl *string) (http.Header, error) { 87 var headers http.Header 88 return headers, parseTemplate(tmpl, *rd, &headers) 89 } 90 91 // GetParticipantsIDs returns the list of IDs part of the FormationConfigurationChangeInput 92 func (rd *FormationConfigurationChangeInput) GetParticipantsIDs() []string { 93 var participants []string 94 if rd.Application != nil { 95 participants = append(participants, rd.Application.ID) 96 } 97 if rd.Runtime != nil { 98 participants = append(participants, rd.Runtime.ID) 99 } 100 if rd.RuntimeContext != nil { 101 participants = append(participants, rd.RuntimeContext.ID) 102 } 103 104 return participants 105 } 106 107 // SetAssignment sets the assignment for the FormationConfigurationChangeInput to the provided one 108 func (rd *FormationConfigurationChangeInput) SetAssignment(assignment *model.FormationAssignment) { 109 config := string(assignment.Value) 110 if config == "" || assignment.State == string(model.CreateErrorAssignmentState) || assignment.State == string(model.DeleteErrorAssignmentState) { 111 config = "\"\"" 112 } 113 rd.Assignment = &FormationAssignment{ 114 ID: assignment.ID, 115 FormationID: assignment.FormationID, 116 TenantID: assignment.TenantID, 117 Source: assignment.Source, 118 SourceType: assignment.SourceType, 119 Target: assignment.Target, 120 TargetType: assignment.TargetType, 121 State: assignment.State, 122 Value: config, 123 } 124 } 125 126 // SetReverseAssignment sets the reverse assignment for the FormationConfigurationChangeInput to the provided one 127 func (rd *FormationConfigurationChangeInput) SetReverseAssignment(reverseAssignment *model.FormationAssignment) { 128 config := string(reverseAssignment.Value) 129 if config == "" || reverseAssignment.State == string(model.CreateErrorAssignmentState) || reverseAssignment.State == string(model.DeleteErrorAssignmentState) { 130 config = "\"\"" 131 } 132 rd.ReverseAssignment = &FormationAssignment{ 133 ID: reverseAssignment.ID, 134 FormationID: reverseAssignment.FormationID, 135 TenantID: reverseAssignment.TenantID, 136 Source: reverseAssignment.Source, 137 SourceType: reverseAssignment.SourceType, 138 Target: reverseAssignment.Target, 139 TargetType: reverseAssignment.TargetType, 140 State: reverseAssignment.State, 141 Value: config, 142 } 143 } 144 145 // Clone returns a copy of the FormationConfigurationChangeInput 146 func (rd *FormationConfigurationChangeInput) Clone() FormationAssignmentTemplateInput { 147 return &FormationConfigurationChangeInput{ 148 Operation: rd.Operation, 149 FormationID: rd.FormationID, 150 ApplicationTemplate: rd.ApplicationTemplate, 151 Application: rd.Application, 152 Runtime: rd.Runtime, 153 RuntimeContext: rd.RuntimeContext, 154 CustomerTenantContext: rd.CustomerTenantContext, 155 Assignment: rd.Assignment, 156 ReverseAssignment: rd.ReverseAssignment, 157 } 158 }