Skip to content

Add policies_last_updated endpoint to internal policy server #231

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 26, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ func main() {

internalPoliciesHandlerV1 := handlers.NewPoliciesIndexInternal(logger, wrappedStore, policyMapperWriter, errorResponse)

internalPoliciesLastUpdatedHandlerV1 := handlers.NewPoliciesLastUpdatedInternal(logger, wrappedStore, errorResponse)

createTagsHandlerV1 := &handlers.TagsCreate{
Store: wrappedStore,
ErrorResponse: errorResponse,
Expand Down Expand Up @@ -144,13 +146,15 @@ func main() {
internalRoutes := rata.Routes{
{Name: "create_tags", Method: "PUT", Path: "/networking/v1/internal/tags"},
{Name: "internal_policies", Method: "GET", Path: "/networking/:version/internal/policies"},
{Name: "internal_policies_last_updated", Method: "GET", Path: "/networking/:version/internal/policies_last_updated"},
{Name: "internal_security_groups", Method: "GET", Path: "/networking/:version/internal/security_groups"},
}

internalHandlers := rata.Handlers{
"create_tags": metricsWrap("CreateTags", logWrap(createTagsHandlerV1)),
"internal_policies": metricsWrap("InternalPolicies", logWrap(internalPoliciesHandlerV1)),
"internal_security_groups": metricsWrap("InternalSecurityGroups", logWrap(securityGroupsHandlerV1)),
"create_tags": metricsWrap("CreateTags", logWrap(createTagsHandlerV1)),
"internal_policies": metricsWrap("InternalPolicies", logWrap(internalPoliciesHandlerV1)),
"internal_policies_last_updated": metricsWrap("InternalPoliciesLastUpdated", logWrap(internalPoliciesLastUpdatedHandlerV1)),
"internal_security_groups": metricsWrap("InternalSecurityGroups", logWrap(securityGroupsHandlerV1)),
}

for key, handler := range internalHandlers {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package handlers

import (
"net/http"
"strconv"

"code.cloudfoundry.org/lager/v3"
"code.cloudfoundry.org/policy-server/store"
)

type PoliciesLastUpdatedInternal struct {
Logger lager.Logger
Store store.Store
ErrorResponse errorResponse
}

func NewPoliciesLastUpdatedInternal(logger lager.Logger, store store.Store,
errorResponse errorResponse) *PoliciesLastUpdatedInternal {
return &PoliciesLastUpdatedInternal{
Logger: logger,
Store: store,
ErrorResponse: errorResponse,
}
}

func (h *PoliciesLastUpdatedInternal) ServeHTTP(w http.ResponseWriter, req *http.Request) {
logger := getLogger(req)
logger = logger.Session("policies-last-updated-internal")

lastUpdated, err := h.Store.LastUpdated()
if err != nil {
h.ErrorResponse.InternalServerError(logger, w, err, "database read failed")
return
}

w.WriteHeader(http.StatusOK)
w.Write([]byte(strconv.Itoa(lastUpdated)))
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package handlers_test

import (
"errors"
"net/http"
"net/http/httptest"

"code.cloudfoundry.org/lager/v3"
"code.cloudfoundry.org/lager/v3/lagertest"
"code.cloudfoundry.org/policy-server/handlers"
"code.cloudfoundry.org/policy-server/handlers/fakes"
storeFakes "code.cloudfoundry.org/policy-server/store/fakes"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

var _ = Describe("PoliciesLastUpdatedInternal", func() {
var (
handler *handlers.PoliciesLastUpdatedInternal
resp *httptest.ResponseRecorder
fakeStore *storeFakes.Store
fakeErrorResponse *fakes.ErrorResponse
logger *lagertest.TestLogger
expectedLogger lager.Logger
expectedResponseBody []byte
)

BeforeEach(func() {
expectedResponseBody = []byte("12345")

fakeStore = &storeFakes.Store{}
fakeStore.LastUpdatedReturns(12345, nil)
logger = lagertest.NewTestLogger("test")
expectedLogger = lager.NewLogger("test").Session("policies-last-updated-internal")

testSink := lagertest.NewTestSink()
expectedLogger.RegisterSink(testSink)
expectedLogger.RegisterSink(lager.NewWriterSink(GinkgoWriter, lager.DEBUG))
fakeErrorResponse = &fakes.ErrorResponse{}
handler = &handlers.PoliciesLastUpdatedInternal{
Logger: logger,
Store: fakeStore,
ErrorResponse: fakeErrorResponse,
}
resp = httptest.NewRecorder()
})

It("returns the last updated returned by LastUpdated", func() {
request, err := http.NewRequest("GET", "/networking/v0/internal/policies_last_updated", nil)
Expect(err).NotTo(HaveOccurred())
MakeRequestWithLogger(handler.ServeHTTP, resp, request, logger)

Expect(fakeStore.LastUpdatedCallCount()).To(Equal(1))
Expect(resp.Code).To(Equal(http.StatusOK))
Expect(resp.Body.Bytes()).To(Equal(expectedResponseBody))
})

Context("when the logger isn't on the request context", func() {
It("still works", func() {
request, err := http.NewRequest("GET", "/networking/v0/internal/policies_last_updated", nil)
Expect(err).NotTo(HaveOccurred())
handler.ServeHTTP(resp, request)

Expect(fakeStore.LastUpdatedCallCount()).To(Equal(1))
Expect(resp.Code).To(Equal(http.StatusOK))
Expect(resp.Body.Bytes()).To(Equal(expectedResponseBody))
})
})

Context("when store throws an error", func() {
BeforeEach(func() {
fakeStore.LastUpdatedReturns(0, errors.New("banana"))
})

It("calls the internal server error handler", func() {
request, err := http.NewRequest("GET", "/networking/v0/internal/policies_last_updated", nil)
Expect(err).NotTo(HaveOccurred())
MakeRequestWithLogger(handler.ServeHTTP, resp, request, logger)

Expect(fakeErrorResponse.InternalServerErrorCallCount()).To(Equal(1))

l, w, err, description := fakeErrorResponse.InternalServerErrorArgsForCall(0)
Expect(l).To(Equal(expectedLogger))
Expect(w).To(Equal(resp))
Expect(err).To(MatchError("banana"))
Expect(description).To(Equal("database read failed"))
})
})
})
70 changes: 70 additions & 0 deletions src/code.cloudfoundry.org/policy-server/store/fakes/store.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,19 @@ func (mw *MetricsWrapper) Delete(policies []Policy) error {
return err
}

func (mw *MetricsWrapper) LastUpdated() (int, error) {
startTime := time.Now()
timestamp, err := mw.Store.LastUpdated()
lastUpdatedTimeDuration := time.Now().Sub(startTime)
if err != nil {
mw.MetricsSender.IncrementCounter("StoreLastUpdatedError")
mw.MetricsSender.SendDuration("StoreLastUpdatedErrorTime", lastUpdatedTimeDuration)
} else {
mw.MetricsSender.SendDuration("StoreLastUpdatedSuccessTime", lastUpdatedTimeDuration)
}
return timestamp, err
}

func (mw *MetricsWrapper) Tags() ([]Tag, error) {
startTime := time.Now()
tags, err := mw.TagStore.Tags()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,46 @@ var _ = Describe("MetricsWrapper", func() {
})
})

Describe("LastUpdated", func() {
BeforeEach(func() {
fakeStore.LastUpdatedReturns(12345, nil)
})

It("calls LastUpdated on the Store", func() {
timestamp, err := metricsWrapper.LastUpdated()
Expect(err).NotTo(HaveOccurred())
Expect(timestamp).To(Equal(12345))

Expect(fakeStore.LastUpdatedCallCount()).To(Equal(1))
})

It("emits a metric", func() {
_, err := metricsWrapper.LastUpdated()
Expect(err).NotTo(HaveOccurred())

Expect(fakeMetricsSender.SendDurationCallCount()).To(Equal(1))
name, _ := fakeMetricsSender.SendDurationArgsForCall(0)
Expect(name).To(Equal("StoreLastUpdatedSuccessTime"))
})

Context("when there is an error", func() {
BeforeEach(func() {
fakeStore.LastUpdatedReturns(0, errors.New("banana"))
})
It("emits an error metric", func() {
_, err := metricsWrapper.LastUpdated()
Expect(err).To(MatchError("banana"))

Expect(fakeMetricsSender.IncrementCounterCallCount()).To(Equal(1))
Expect(fakeMetricsSender.IncrementCounterArgsForCall(0)).To(Equal("StoreLastUpdatedError"))

Expect(fakeMetricsSender.SendDurationCallCount()).To(Equal(1))
name, _ := fakeMetricsSender.SendDurationArgsForCall(0)
Expect(name).To(Equal("StoreLastUpdatedErrorTime"))
})
})
})

Describe("Tags", func() {
BeforeEach(func() {
fakeTagStore.TagsReturns(tags, nil)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -412,4 +412,12 @@ var MigrationsToPerform = PolicyServerMigrations{
Id: "76",
Up: migration_v0076,
},
PolicyServerMigration{
Id: "77",
Up: migration_v0077,
},
PolicyServerMigration{
Id: "78",
Up: migration_v0078,
},
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package migrations

// Adding policies information table to store the date
// when policies were last updated

var migration_v0077 = map[string][]string{
"mysql": {
`CREATE TABLE IF NOT EXISTS policies_info (
id int NOT NULL AUTO_INCREMENT,
PRIMARY KEY (id),
last_updated TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);`,
},
"postgres": {
`CREATE TABLE IF NOT EXISTS policies_info (
id SERIAL PRIMARY KEY,
last_updated TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);`,
},
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package migrations

// Adding first record of last updated field to policies
// set to current date

var migration_v0078 = map[string][]string{
"mysql": {
`INSERT INTO policies_info (last_updated) VALUES (CURRENT_TIMESTAMP);`,
},
"postgres": {
`INSERT INTO policies_info (last_updated) VALUES (CURRENT_TIMESTAMP);`,
},
}
4 changes: 3 additions & 1 deletion src/code.cloudfoundry.org/policy-server/store/policy.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package store

import "code.cloudfoundry.org/cf-networking-helpers/db"
import (
"code.cloudfoundry.org/cf-networking-helpers/db"
)

//counterfeiter:generate -o fakes/policy_repo.go --fake-name PolicyRepo . PolicyRepo
type PolicyRepo interface {
Expand Down
Loading