Skip to content

Commit 0c7d4ab

Browse files
committed
feat: include route_policy_scope and route_policies in /routes endpoint output
1 parent 36868de commit 0c7d4ab

2 files changed

Lines changed: 51 additions & 0 deletions

File tree

src/code.cloudfoundry.org/gorouter/route/pool.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -809,6 +809,8 @@ func (e *Endpoint) MarshalJSON() ([]byte, error) {
809809
LoadBalancingAlgorithm string `json:"load_balancing_algorithm,omitempty"`
810810
HashHeader string `json:"hash_header,omitempty"`
811811
HashBalance *float64 `json:"hash_balance,omitempty"` // omitempty on a float64 field will omit the field when the value is 0.0, to keep 0 use pointer of float64
812+
RoutePolicyScope string `json:"route_policy_scope,omitempty"`
813+
RoutePolicies []string `json:"route_policies,omitempty"`
812814
}
813815

814816
jsonObj.Address = e.addr
@@ -823,6 +825,8 @@ func (e *Endpoint) MarshalJSON() ([]byte, error) {
823825
jsonObj.ServerCertDomainSAN = e.ServerCertDomainSAN
824826
jsonObj.LoadBalancingAlgorithm = e.LoadBalancingAlgorithm
825827
jsonObj.HashHeader = e.HashHeaderName
828+
jsonObj.RoutePolicyScope = e.RoutePolicyScope
829+
jsonObj.RoutePolicies = e.RoutePolicies
826830

827831
// marshal balance factor only if load balancing algorithm is hash-based
828832
if e.LoadBalancingAlgorithm == config.LOAD_BALANCE_HB {

src/code.cloudfoundry.org/gorouter/route/pool_test.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1082,6 +1082,53 @@ var _ = Describe("EndpointPool", func() {
10821082
})
10831083
})
10841084

1085+
Context("when endpoints have route policy fields", func() {
1086+
It("marshals json with route_policy_scope and route_policies", func() {
1087+
e := route.NewEndpoint(&route.EndpointOpts{
1088+
Host: "1.2.3.4",
1089+
Port: 5678,
1090+
Protocol: "http2",
1091+
StaleThresholdInSeconds: -1,
1092+
RoutePolicyScope: route.RoutePolicyScopeOrg,
1093+
RoutePolicies: []string{"cf:org:org-guid-1", "cf:app:app-guid-1"},
1094+
})
1095+
pool.Put(e)
1096+
1097+
json, err := pool.MarshalJSON()
1098+
Expect(err).ToNot(HaveOccurred())
1099+
Expect(string(json)).To(Equal(`[{"address":"1.2.3.4:5678","availability_zone":"","protocol":"http2","tls":false,"ttl":-1,"tags":null,"route_policy_scope":"org","route_policies":["cf:org:org-guid-1","cf:app:app-guid-1"]}]`))
1100+
})
1101+
1102+
It("marshals json with route_policy_scope only", func() {
1103+
e := route.NewEndpoint(&route.EndpointOpts{
1104+
Host: "1.2.3.4",
1105+
Port: 5678,
1106+
Protocol: "http2",
1107+
StaleThresholdInSeconds: -1,
1108+
RoutePolicyScope: route.RoutePolicyScopeSpace,
1109+
})
1110+
pool.Put(e)
1111+
1112+
json, err := pool.MarshalJSON()
1113+
Expect(err).ToNot(HaveOccurred())
1114+
Expect(string(json)).To(Equal(`[{"address":"1.2.3.4:5678","availability_zone":"","protocol":"http2","tls":false,"ttl":-1,"tags":null,"route_policy_scope":"space"}]`))
1115+
})
1116+
1117+
It("omits route policy fields when empty", func() {
1118+
e := route.NewEndpoint(&route.EndpointOpts{
1119+
Host: "1.2.3.4",
1120+
Port: 5678,
1121+
Protocol: "http2",
1122+
StaleThresholdInSeconds: -1,
1123+
})
1124+
pool.Put(e)
1125+
1126+
json, err := pool.MarshalJSON()
1127+
Expect(err).ToNot(HaveOccurred())
1128+
Expect(string(json)).To(Equal(`[{"address":"1.2.3.4:5678","availability_zone":"","protocol":"http2","tls":false,"ttl":-1,"tags":null}]`))
1129+
})
1130+
})
1131+
10851132
Describe("ProcessId", func() {
10861133
Context("when there are no tags", func() {
10871134
It("returns an empty string", func() {

0 commit comments

Comments
 (0)