From d368139ac47a1ce4764ff146663c9028f96d2a88 Mon Sep 17 00:00:00 2001 From: Matthew Sykes Date: Wed, 4 Feb 2015 17:26:43 -0500 Subject: [PATCH] Move cc related types out of receptor [#86337946] Signed-off-by: Ashvin Agrawal --- cmd/receptor/desired_lrp_test.go | 8 ++-- cmd/receptor/event_test.go | 15 ++------ handlers/desired_lrp_handlers_test.go | 13 ++----- resources.go | 54 +-------------------------- resources_test.go | 28 +++++++------- serialization/desired_lrps.go | 41 ++++++-------------- serialization/desired_lrps_test.go | 14 ++----- 7 files changed, 39 insertions(+), 134 deletions(-) diff --git a/cmd/receptor/desired_lrp_test.go b/cmd/receptor/desired_lrp_test.go index 187bb98..9a11376 100644 --- a/cmd/receptor/desired_lrp_test.go +++ b/cmd/receptor/desired_lrp_test.go @@ -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() { @@ -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))) }) }) diff --git a/cmd/receptor/event_test.go b/cmd/receptor/event_test.go index df18b95..383c3c0 100644 --- a/cmd/receptor/event_test.go +++ b/cmd/receptor/event_test.go @@ -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", @@ -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()) @@ -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) diff --git a/handlers/desired_lrp_handlers_test.go b/handlers/desired_lrp_handlers_test.go index bfaa395..9b510a1 100644 --- a/handlers/desired_lrp_handlers_test.go +++ b/handlers/desired_lrp_handlers_test.go @@ -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{ diff --git a/resources.go b/resources.go index b13577e..913b500 100644 --- a/resources.go +++ b/resources.go @@ -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"` diff --git a/resources_test.go b/resources_test.go index 0296fc5..2df4f69 100644 --- a/resources_test.go +++ b/resources_test.go @@ -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" @@ -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() { @@ -340,10 +341,12 @@ 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)) }) @@ -351,17 +354,12 @@ var _ = Describe("Resources", func() { 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"`)) }) }) }) diff --git a/serialization/desired_lrps.go b/serialization/desired_lrps.go index b5559a5..2b5f7c9 100644 --- a/serialization/desired_lrps.go +++ b/serialization/desired_lrps.go @@ -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 } diff --git a/serialization/desired_lrps_test.go b/serialization/desired_lrps_test.go index 9df70aa..4170187 100644 --- a/serialization/desired_lrps_test.go +++ b/serialization/desired_lrps_test.go @@ -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() { @@ -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"]}]`)) }) })