Skip to content

Commit

Permalink
Move cc related types out of receptor
Browse files Browse the repository at this point in the history
[#86337946]

Signed-off-by: Ashvin Agrawal <[email protected]>
  • Loading branch information
sykesm authored and Ashvin Agrawal committed Feb 4, 2015
1 parent b11a7b8 commit d368139
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 134 deletions.
8 changes: 3 additions & 5 deletions cmd/receptor/desired_lrp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,9 @@ var _ = Describe("Desired LRP API", func() {
instances := 6
annotation := "update-annotation"
rawMessage := json.RawMessage([]byte(`[{"port":8080,"hostnames":["updated-route"]}]`))
routes := map[string]*json.RawMessage{
"cf-router": &rawMessage,
}

routingInfo := receptor.RoutingInfo{
CFRoutes: []receptor.CFRoute{{Port: 8080, Hostnames: []string{"updated-route"}}},
"cf-router": &rawMessage,
}

BeforeEach(func() {
Expand All @@ -114,8 +112,8 @@ var _ = Describe("Desired LRP API", func() {
desiredLRPs, err := bbs.DesiredLRPs()
Ω(err).ShouldNot(HaveOccurred())
Ω(desiredLRPs[0].Instances).To(Equal(instances))
Ω(desiredLRPs[0].Routes).To(Equal(routes))
Ω(desiredLRPs[0].Annotation).To(Equal(annotation))
Ω(desiredLRPs[0].Routes).To(Equal(map[string]*json.RawMessage(routingInfo)))
})
})

Expand Down
15 changes: 4 additions & 11 deletions cmd/receptor/event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,8 @@ var _ = Describe("Event", func() {
Describe("Desired LRPs", func() {
BeforeEach(func() {
routeMessage := json.RawMessage([]byte(`[{"port":8080,"hostnames":["original-route"]}]`))
routes := map[string]*json.RawMessage{
receptor.CFRouter: &routeMessage,
}
routes := map[string]*json.RawMessage{"cf-router": &routeMessage}

desiredLRP = models.DesiredLRP{
ProcessGuid: "some-guid",
Domain: "some-domain",
Expand All @@ -124,13 +123,7 @@ var _ = Describe("Event", func() {
By("updating an existing DesiredLRP")
routeMessage := json.RawMessage([]byte(`[{"port":8080,"hostnames":["new-route"]}]`))
newRoutes := map[string]*json.RawMessage{
receptor.CFRouter: &routeMessage,
}
expectedRoutingInfo := receptor.RoutingInfo{
CFRoutes: []receptor.CFRoute{{
Port: 8080,
Hostnames: []string{"new-route"},
}},
"cf-router": &routeMessage,
}
err = bbs.UpdateDesiredLRP(logger, desiredLRP.ProcessGuid, models.DesiredLRPUpdate{Routes: newRoutes})
Ω(err).ShouldNot(HaveOccurred())
Expand All @@ -139,7 +132,7 @@ var _ = Describe("Event", func() {

desiredLRPChangedEvent, ok := event.(receptor.DesiredLRPChangedEvent)
Ω(ok).Should(BeTrue())
Ω(desiredLRPChangedEvent.After.Routes).Should(Equal(&expectedRoutingInfo))
Ω(*desiredLRPChangedEvent.After.Routes).Should(Equal(receptor.RoutingInfo(newRoutes)))

By("removing the DesiredLRP")
err = bbs.RemoveDesiredLRPByProcessGuid(logger, desiredLRP.ProcessGuid)
Expand Down
13 changes: 4 additions & 9 deletions handlers/desired_lrp_handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,18 +278,13 @@ var _ = Describe("Desired LRP Handlers", func() {
expectedProcessGuid := "some-guid"
instances := 15
annotation := "new-annotation"
routingInfo := receptor.RoutingInfo{
CFRoutes: []receptor.CFRoute{
{
Port: 8080,
Hostnames: []string{"new-route-1", "new-route-2"},
},
},
}

routeMessage := json.RawMessage(`[{"port":8080,"hostnames":["new-route-1","new-route-2"]}]`)
routes := map[string]*json.RawMessage{
receptor.CFRouter: &routeMessage,
"cf-router": &routeMessage,
}
routingInfo := receptor.RoutingInfo{
"cf-router": &routeMessage,
}

validUpdateRequest := receptor.DesiredLRPUpdateRequest{
Expand Down
54 changes: 1 addition & 53 deletions resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,59 +154,7 @@ func (response *TaskResponse) UnmarshalJSON(payload []byte) error {
return nil
}

const CFRouter = "cf-router"

type CFRoute struct {
Port uint16 `json:"port"`
Hostnames []string `json:"hostnames"`
}

type CFRoutes []CFRoute

type RoutingInfo struct {
CFRoutes
Other map[string]*json.RawMessage
}

func (r RoutingInfo) MarshalJSON() ([]byte, error) {
out := make(map[string]*json.RawMessage)
for k, v := range r.Other {
out[k] = v
}

if len(r.CFRoutes) > 0 {
bytes, err := json.Marshal(r.CFRoutes)
if err != nil {
return nil, err
}
raw := json.RawMessage(bytes)
out[CFRouter] = &raw
}

return json.Marshal(out)
}

func (r *RoutingInfo) UnmarshalJSON(data []byte) error {
var out map[string]*json.RawMessage
err := json.Unmarshal(data, &out)
if err != nil {
return err
}

if cfroutes, ok := out[CFRouter]; ok {
delete(out, CFRouter)
err := json.Unmarshal(*cfroutes, &r.CFRoutes)
if err != nil {
return err
}
}

if len(out) > 0 {
r.Other = out
}

return nil
}
type RoutingInfo map[string]*json.RawMessage

type DesiredLRPCreateRequest struct {
ProcessGuid string `json:"process_guid"`
Expand Down
28 changes: 13 additions & 15 deletions resources_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"

"github.com/cloudfoundry-incubator/receptor"
"github.com/cloudfoundry-incubator/runtime-schema/cc_messages"
"github.com/cloudfoundry-incubator/runtime-schema/models"

. "github.com/onsi/ginkgo"
Expand Down Expand Up @@ -326,10 +327,10 @@ var _ = Describe("Resources", func() {
})

Describe("RoutingInfo", func() {
var r receptor.RoutingInfo
var routingInfo receptor.RoutingInfo

BeforeEach(func() {
r.Other = make(map[string]*json.RawMessage)
routingInfo = receptor.RoutingInfo{}
})

Context("Serialization", func() {
Expand All @@ -340,28 +341,25 @@ var _ = Describe("Resources", func() {

Context("MarshalJson", func() {
It("marshals routes when present", func() {
r.CFRoutes = append(r.CFRoutes, receptor.CFRoute{Port: 1, Hostnames: []string{"a", "b"}})
msg := json.RawMessage([]byte(`"bar"`))
r.Other["foo"] = &msg
bytes, err := json.Marshal(r)
routingInfo := *cc_messages.NewRoutingInfo([]string{"a", "b"}, 1)

otherRawMessage := json.RawMessage([]byte(`"bar"`))
routingInfo["foo"] = &otherRawMessage

bytes, err := json.Marshal(routingInfo)
Ω(err).ShouldNot(HaveOccurred())
Ω(bytes).Should(MatchJSON(jsonRoutes))
})
})

Context("Unmarshal", func() {
It("returns both cf-routes and other", func() {
err := json.Unmarshal([]byte(jsonRoutes), &r)
err := json.Unmarshal([]byte(jsonRoutes), &routingInfo)
Ω(err).ShouldNot(HaveOccurred())

Ω(r.CFRoutes).Should(HaveLen(1))
route := r.CFRoutes[0]
Ω(route.Port).Should(Equal(uint16(1)))
Ω(route.Hostnames).Should(ConsistOf("a", "b"))

Ω(r.Other).Should(HaveLen(1))
raw := r.Other["foo"]
Ω([]byte(*raw)).Should(Equal([]byte(`"bar"`)))
Ω(len(routingInfo)).Should(Equal(2))
Ω(string(*routingInfo["cf-router"])).Should(MatchJSON(`[{"port": 1,"hostnames":["a", "b"]}]`))
Ω(string(*routingInfo["foo"])).Should(MatchJSON(`"bar"`))
})
})
})
Expand Down
41 changes: 11 additions & 30 deletions serialization/desired_lrps.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,45 +91,26 @@ func DesiredLRPUpdateFromRequest(req receptor.DesiredLRPUpdateRequest) models.De
}

func RoutingInfoToRawMessages(r *receptor.RoutingInfo) map[string]*json.RawMessage {
if r == nil {
return nil
}
var messages map[string]*json.RawMessage

out := make(map[string]*json.RawMessage)
for k, v := range r.Other {
out[k] = v
}

if len(r.CFRoutes) > 0 {
bytes, err := json.Marshal(r.CFRoutes)
if err != nil {
panic(err)
if r != nil {
messages = map[string]*json.RawMessage{}
for key, value := range *r {
messages[key] = value
}
raw := json.RawMessage(bytes)
out[receptor.CFRouter] = &raw
}

return out
return messages
}

func RoutingInfoFromRawMessages(raw map[string]*json.RawMessage) *receptor.RoutingInfo {
if len(raw) == 0 {
if raw == nil {
return nil
}

var r receptor.RoutingInfo

if cfroutes, ok := raw[receptor.CFRouter]; ok {
delete(raw, receptor.CFRouter)
err := json.Unmarshal(*cfroutes, &r.CFRoutes)
if err != nil {
panic(err)
}
}

if len(raw) > 0 {
r.Other = raw
info := receptor.RoutingInfo{}
for key, value := range raw {
info[key] = value
}

return &r
return &info
}
14 changes: 3 additions & 11 deletions serialization/desired_lrps_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,11 @@ var _ = Describe("DesiredLRP Serialization", func() {
var routingInfo receptor.RoutingInfo

BeforeEach(func() {
routingInfo.CFRoutes = []receptor.CFRoute{
{
Port: 1,
Hostnames: []string{"route-1", "route-2"},
},
}

raw := json.RawMessage([]byte(`[{"port":1,"hostnames":["route-1","route-2"]}]`))
routes = map[string]*json.RawMessage{
receptor.CFRouter: &raw,
"cf-router": &raw,
}
routingInfo = receptor.RoutingInfo(routes)
})

Describe("DesiredLRPFromRequest", func() {
Expand Down Expand Up @@ -82,9 +76,7 @@ var _ = Describe("DesiredLRP Serialization", func() {
Ω(desiredLRP.EgressRules[0].PortRange).Should(Equal(securityRule.PortRange))
Ω(desiredLRP.EgressRules[0].Destinations).Should(Equal(securityRule.Destinations))
Ω(desiredLRP.Routes).Should(HaveLen(1))
cfroute, ok := desiredLRP.Routes[receptor.CFRouter]
Ω(ok).Should(BeTrue())
Ω([]byte(*cfroute)).Should(MatchJSON(`[{"port": 1,"hostnames": ["route-1", "route-2"]}]`))
Ω([]byte(*desiredLRP.Routes["cf-router"])).Should(MatchJSON(`[{"port": 1,"hostnames": ["route-1", "route-2"]}]`))
})
})

Expand Down

0 comments on commit d368139

Please sign in to comment.