Skip to content
This repository has been archived by the owner on Nov 3, 2021. It is now read-only.

Commit

Permalink
Introduce "cf_on_k8s": true into the / response
Browse files Browse the repository at this point in the history
Also introduce root response presenter and response type

Issue: #10

Co-authored-by: Georgi Sabev <georgethebeatle@gmail.com>
  • Loading branch information
danail-branekov and georgethebeatle committed Sep 24, 2021
1 parent e665429 commit 50b7e62
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 3 deletions.
11 changes: 10 additions & 1 deletion apis/root_handler.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package apis

import (
"encoding/json"
"net/http"

"code.cloudfoundry.org/cf-k8s-api/presenter"
"github.com/go-logr/logr"
"github.com/gorilla/mux"
)

Expand All @@ -11,11 +14,17 @@ const (
)

type RootHandler struct {
Logger logr.Logger
ServerURL string
}

func (h *RootHandler) RootGetHandler(w http.ResponseWriter, r *http.Request) {
body := `{"links":{"self":{"href":"` + h.ServerURL + `"},"bits_service":null,"cloud_controller_v2":null,"cloud_controller_v3":{"href":"` + h.ServerURL + `/v3","meta":{"version":"3.90.0"}},"network_policy_v0":null,"network_policy_v1":null,"login":null,"uaa":null,"credhub":null,"routing":null,"logging":null,"log_cache":null,"log_stream":null,"app_ssh":null}}`
body, err := json.Marshal(presenter.GetRootResponse(h.ServerURL))
if err != nil {
h.Logger.Error(err, "Failed to render response")
writeUnknownErrorResponse(w)
return
}

w.Header().Set("Content-Type", "application/json")
w.Write([]byte(body))
Expand Down
34 changes: 32 additions & 2 deletions apis/root_handler_test.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
package apis_test

import (
"encoding/json"
"net/http"
"net/http/httptest"
"testing"

"github.com/gorilla/mux"
logf "sigs.k8s.io/controller-runtime/pkg/log"

"code.cloudfoundry.org/cf-k8s-api/apis"
"code.cloudfoundry.org/cf-k8s-api/presenter"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/gstruct"
"github.com/sclevine/spec"
"github.com/sclevine/spec/report"
)
Expand All @@ -30,6 +34,7 @@ func testRootAPI(t *testing.T, when spec.G, it spec.S) {
router := mux.NewRouter()

apiHandler := apis.RootHandler{
Logger: logf.Log.WithName("TestRootHandler"),
ServerURL: defaultServerURL,
}
apiHandler.RegisterRoutes(router)
Expand All @@ -51,7 +56,32 @@ func testRootAPI(t *testing.T, when spec.G, it spec.S) {
})

it("matches the expected response body format", func() {
expectedBody := `{"links":{"self":{"href":"` + defaultServerURL + `"},"bits_service":null,"cloud_controller_v2":null,"cloud_controller_v3":{"href":"` + defaultServerURL + `/v3","meta":{"version":"3.90.0"}},"network_policy_v0":null,"network_policy_v1":null,"login":null,"uaa":null,"credhub":null,"routing":null,"logging":null,"log_cache":null,"log_stream":null,"app_ssh":null}}`
g.Expect(rr.Body.String()).To(Equal(expectedBody), "Response body matches RootV3GetHandler response:")
var resp presenter.RootResponse
g.Expect(json.Unmarshal(rr.Body.Bytes(), &resp)).To(Succeed())

g.Expect(resp).To(gstruct.MatchAllFields(gstruct.Fields{
"Links": Equal(map[string]*presenter.APILink{
"self": {
Link: presenter.Link{HREF: defaultServerURL},
},
"bits_service": nil,
"cloud_controller_v2": nil,
"cloud_controller_v3": {
Link: presenter.Link{HREF: defaultServerURL + "/v3"},
Meta: presenter.APILinkMeta{Version: "3.90.0"},
},
"network_policy_v0": nil,
"network_policy_v1": nil,
"login": nil,
"uaa": nil,
"credhub": nil,
"routing": nil,
"logging": nil,
"log_cache": nil,
"log_stream": nil,
"app_ssh": nil,
}),
"CFOnK8s": Equal(true),
}))
})
}
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ func main() {
ServerURL: config.ServerURL,
},
&apis.RootHandler{
Logger: ctrl.Log.WithName("RootHandler"),
ServerURL: config.ServerURL,
},
&apis.ResourceMatchesHandler{
Expand Down
37 changes: 37 additions & 0 deletions presenter/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package presenter

type APILink struct {
Link
Meta APILinkMeta `json:"meta"`
}

type APILinkMeta struct {
Version string `json:"version"`
}

type RootResponse struct {
Links map[string]*APILink `json:"links"`
CFOnK8s bool `json:"cf_on_k8s"`
}

func GetRootResponse(serverURL string) RootResponse {
return RootResponse{
Links: map[string]*APILink{
"self": {Link: Link{HREF: serverURL}},
"bits_service": nil,
"cloud_controller_v2": nil,
"cloud_controller_v3": {Link: Link{HREF: serverURL + "/v3"}, Meta: APILinkMeta{Version: "3.90.0"}},
"network_policy_v0": nil,
"network_policy_v1": nil,
"login": nil,
"uaa": nil,
"credhub": nil,
"routing": nil,
"logging": nil,
"log_cache": nil,
"log_stream": nil,
"app_ssh": nil,
},
CFOnK8s: true,
}
}

0 comments on commit 50b7e62

Please sign in to comment.