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

Introduce "cf_on_k8s": true into the / response #91

Merged
merged 1 commit into from
Sep 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions 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,16 +14,21 @@ const (
)

type RootHandler struct {
logger logr.Logger
serverURL string
}

func NewRootHandler(serverURL string) *RootHandler {
func NewRootHandler(logger logr.Logger, serverURL string) *RootHandler {
return &RootHandler{serverURL: serverURL}
}

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
38 changes: 35 additions & 3 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 @@ -29,7 +33,10 @@ func testRootAPI(t *testing.T, when spec.G, it spec.S) {
rr = httptest.NewRecorder()
router := mux.NewRouter()

apiHandler := apis.NewRootHandler(defaultServerURL)
apiHandler := apis.NewRootHandler(
logf.Log.WithName("TestRootHandler"),
defaultServerURL,
)
apiHandler.RegisterRoutes(router)

router.ServeHTTP(rr, req)
Expand All @@ -49,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{
Birdrock marked this conversation as resolved.
Show resolved Hide resolved
"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),
}))
})
}
5 changes: 4 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,10 @@ func main() {

handlers := []APIHandler{
apis.NewRootV3Handler(config.ServerURL),
apis.NewRootHandler(config.ServerURL),
apis.NewRootHandler(
ctrl.Log.WithName("RootHandler"),
config.ServerURL,
),
apis.NewResourceMatchesHandler(config.ServerURL),
apis.NewAppHandler(
ctrl.Log.WithName("AppHandler"),
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,
}
}