-
Notifications
You must be signed in to change notification settings - Fork 55
/
route.go
181 lines (155 loc) · 4.91 KB
/
route.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package payloads
import (
"net/url"
"code.cloudfoundry.org/korifi/api/payloads/parse"
"code.cloudfoundry.org/korifi/api/payloads/validation"
"code.cloudfoundry.org/korifi/api/repositories"
korifiv1alpha1 "code.cloudfoundry.org/korifi/controllers/api/v1alpha1"
jellidation "github.com/jellydator/validation"
)
type RouteCreate struct {
Host string `json:"host"`
Path string `json:"path"`
Relationships *RouteRelationships `json:"relationships"`
Metadata Metadata `json:"metadata"`
}
func (p RouteCreate) Validate() error {
return jellidation.ValidateStruct(&p,
jellidation.Field(&p.Host, jellidation.Required),
jellidation.Field(&p.Relationships, jellidation.NotNil),
jellidation.Field(&p.Metadata),
)
}
func (p RouteCreate) ToMessage(domainNamespace, domainName string) repositories.CreateRouteMessage {
return repositories.CreateRouteMessage{
Host: p.Host,
Path: p.Path,
SpaceGUID: p.Relationships.Space.Data.GUID,
DomainGUID: p.Relationships.Domain.Data.GUID,
DomainNamespace: domainNamespace,
DomainName: domainName,
Labels: p.Metadata.Labels,
Annotations: p.Metadata.Annotations,
}
}
type RouteRelationships struct {
Domain Relationship `json:"domain"`
Space Relationship `json:"space"`
}
func (r RouteRelationships) Validate() error {
return jellidation.ValidateStruct(&r,
jellidation.Field(&r.Domain, validation.StrictlyRequired),
jellidation.Field(&r.Space, validation.StrictlyRequired),
)
}
type RouteList struct {
AppGUIDs string
SpaceGUIDs string
DomainGUIDs string
Hosts string
Paths string
}
func (p RouteList) ToMessage() repositories.ListRoutesMessage {
return repositories.ListRoutesMessage{
AppGUIDs: parse.ArrayParam(p.AppGUIDs),
SpaceGUIDs: parse.ArrayParam(p.SpaceGUIDs),
DomainGUIDs: parse.ArrayParam(p.DomainGUIDs),
Hosts: parse.ArrayParam(p.Hosts),
Paths: parse.ArrayParam(p.Paths),
}
}
func (p RouteList) SupportedKeys() []string {
return []string{"app_guids", "space_guids", "domain_guids", "hosts", "paths", "per_page", "page"}
}
func (p *RouteList) DecodeFromURLValues(values url.Values) error {
p.AppGUIDs = values.Get("app_guids")
p.SpaceGUIDs = values.Get("space_guids")
p.DomainGUIDs = values.Get("domain_guids")
p.Hosts = values.Get("hosts")
p.Paths = values.Get("paths")
return nil
}
type RoutePatch struct {
Metadata MetadataPatch `json:"metadata"`
}
func (p RoutePatch) Validate() error {
return jellidation.ValidateStruct(&p,
jellidation.Field(&p.Metadata),
)
}
func (p RoutePatch) ToMessage(routeGUID, spaceGUID string) repositories.PatchRouteMetadataMessage {
return repositories.PatchRouteMetadataMessage{
RouteGUID: routeGUID,
SpaceGUID: spaceGUID,
MetadataPatch: repositories.MetadataPatch{
Annotations: p.Metadata.Annotations,
Labels: p.Metadata.Labels,
},
}
}
type RouteDestinationCreate struct {
Destinations []RouteDestination `json:"destinations"`
}
func (r RouteDestinationCreate) Validate() error {
return jellidation.ValidateStruct(&r,
jellidation.Field(&r.Destinations),
)
}
type RouteDestination struct {
App AppResource `json:"app"`
Port *int `json:"port"`
Protocol *string `json:"protocol"`
}
func (r RouteDestination) Validate() error {
return jellidation.ValidateStruct(&r,
jellidation.Field(&r.App),
jellidation.Field(&r.Protocol, validation.OneOf("http1")),
)
}
type AppResource struct {
GUID string `json:"guid"`
Process *DestinationAppProcess `json:"process"`
}
func (a AppResource) Validate() error {
return jellidation.ValidateStruct(&a,
jellidation.Field(&a.GUID, jellidation.Required),
jellidation.Field(&a.Process),
)
}
type DestinationAppProcess struct {
Type string `json:"type"`
}
func (p DestinationAppProcess) Validate() error {
return jellidation.ValidateStruct(&p,
jellidation.Field(&p.Type, jellidation.Required),
)
}
func (dc RouteDestinationCreate) ToMessage(routeRecord repositories.RouteRecord) repositories.AddDestinationsToRouteMessage {
addDestinations := make([]repositories.DestinationMessage, 0, len(dc.Destinations))
for _, destination := range dc.Destinations {
processType := korifiv1alpha1.ProcessTypeWeb
if destination.App.Process != nil {
processType = destination.App.Process.Type
}
port := 8080
if destination.Port != nil {
port = *destination.Port
}
protocol := "http1"
if destination.Protocol != nil {
protocol = *destination.Protocol
}
addDestinations = append(addDestinations, repositories.DestinationMessage{
AppGUID: destination.App.GUID,
ProcessType: processType,
Port: port,
Protocol: protocol,
})
}
return repositories.AddDestinationsToRouteMessage{
RouteGUID: routeRecord.GUID,
SpaceGUID: routeRecord.SpaceGUID,
ExistingDestinations: routeRecord.Destinations,
NewDestinations: addDestinations,
}
}