Skip to content

Commit

Permalink
Merge pull request #4065 from mrueg/provider-public
Browse files Browse the repository at this point in the history
feat: make webhook httpapi reusable
  • Loading branch information
k8s-ci-robot committed Nov 26, 2023
2 parents 70eb2b7 + 8281e38 commit a60480f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 31 deletions.
24 changes: 12 additions & 12 deletions provider/webhook/httpapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ import (
)

type WebhookServer struct {
provider provider.Provider
Provider provider.Provider
}

func (p *WebhookServer) recordsHandler(w http.ResponseWriter, req *http.Request) {
func (p *WebhookServer) RecordsHandler(w http.ResponseWriter, req *http.Request) {
switch req.Method {
case http.MethodGet:
records, err := p.provider.Records(context.Background())
records, err := p.Provider.Records(context.Background())
if err != nil {
log.Errorf("Failed to get Records: %v", err)
w.WriteHeader(http.StatusInternalServerError)
Expand All @@ -56,7 +56,7 @@ func (p *WebhookServer) recordsHandler(w http.ResponseWriter, req *http.Request)
w.WriteHeader(http.StatusBadRequest)
return
}
err := p.provider.ApplyChanges(context.Background(), &changes)
err := p.Provider.ApplyChanges(context.Background(), &changes)
if err != nil {
log.Errorf("Failed to Apply Changes: %v", err)
w.WriteHeader(http.StatusInternalServerError)
Expand All @@ -70,7 +70,7 @@ func (p *WebhookServer) recordsHandler(w http.ResponseWriter, req *http.Request)
}
}

func (p *WebhookServer) adjustEndpointsHandler(w http.ResponseWriter, req *http.Request) {
func (p *WebhookServer) AdjustEndpointsHandler(w http.ResponseWriter, req *http.Request) {
if req.Method != http.MethodPost {
log.Errorf("Unsupported method %s", req.Method)
w.WriteHeader(http.StatusBadRequest)
Expand All @@ -84,7 +84,7 @@ func (p *WebhookServer) adjustEndpointsHandler(w http.ResponseWriter, req *http.
return
}
w.Header().Set(contentTypeHeader, mediaTypeFormatAndVersion)
pve, err := p.provider.AdjustEndpoints(pve)
pve, err := p.Provider.AdjustEndpoints(pve)
if err != nil {
log.Errorf("Failed to call adjust endpoints: %v", err)
w.WriteHeader(http.StatusInternalServerError)
Expand All @@ -96,9 +96,9 @@ func (p *WebhookServer) adjustEndpointsHandler(w http.ResponseWriter, req *http.
}
}

func (p *WebhookServer) negotiateHandler(w http.ResponseWriter, req *http.Request) {
func (p *WebhookServer) NegotiateHandler(w http.ResponseWriter, req *http.Request) {
w.Header().Set(contentTypeHeader, mediaTypeFormatAndVersion)
json.NewEncoder(w).Encode(p.provider.GetDomainFilter())
json.NewEncoder(w).Encode(p.Provider.GetDomainFilter())
}

// StartHTTPApi starts a HTTP server given any provider.
Expand All @@ -111,13 +111,13 @@ func (p *WebhookServer) negotiateHandler(w http.ResponseWriter, req *http.Reques
// - /adjustendpoints (POST): executes the AdjustEndpoints method
func StartHTTPApi(provider provider.Provider, startedChan chan struct{}, readTimeout, writeTimeout time.Duration, providerPort string) {
p := WebhookServer{
provider: provider,
Provider: provider,
}

m := http.NewServeMux()
m.HandleFunc("/", p.negotiateHandler)
m.HandleFunc("/records", p.recordsHandler)
m.HandleFunc("/adjustendpoints", p.adjustEndpointsHandler)
m.HandleFunc("/", p.NegotiateHandler)
m.HandleFunc("/records", p.RecordsHandler)
m.HandleFunc("/adjustendpoints", p.AdjustEndpointsHandler)

s := &http.Server{
Addr: providerPort,
Expand Down
38 changes: 19 additions & 19 deletions provider/webhook/httpapi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,11 @@ func TestRecordsHandlerRecords(t *testing.T) {
w := httptest.NewRecorder()

providerAPIServer := &WebhookServer{
provider: &FakeWebhookProvider{
Provider: &FakeWebhookProvider{
domainFilter: endpoint.NewDomainFilter([]string{"foo.bar.com"}),
},
}
providerAPIServer.recordsHandler(w, req)
providerAPIServer.RecordsHandler(w, req)
res := w.Result()
require.Equal(t, http.StatusOK, res.StatusCode)
// require that the res has the same endpoints as the records slice
Expand All @@ -103,11 +103,11 @@ func TestRecordsHandlerRecordsWithErrors(t *testing.T) {
w := httptest.NewRecorder()

providerAPIServer := &WebhookServer{
provider: &FakeWebhookProvider{
Provider: &FakeWebhookProvider{
err: fmt.Errorf("error"),
},
}
providerAPIServer.recordsHandler(w, req)
providerAPIServer.RecordsHandler(w, req)
res := w.Result()
require.Equal(t, http.StatusInternalServerError, res.StatusCode)
}
Expand All @@ -117,9 +117,9 @@ func TestRecordsHandlerApplyChangesWithBadRequest(t *testing.T) {
w := httptest.NewRecorder()

providerAPIServer := &WebhookServer{
provider: &FakeWebhookProvider{},
Provider: &FakeWebhookProvider{},
}
providerAPIServer.recordsHandler(w, req)
providerAPIServer.RecordsHandler(w, req)
res := w.Result()
require.Equal(t, http.StatusBadRequest, res.StatusCode)
}
Expand All @@ -143,9 +143,9 @@ func TestRecordsHandlerApplyChangesWithValidRequest(t *testing.T) {
w := httptest.NewRecorder()

providerAPIServer := &WebhookServer{
provider: &FakeWebhookProvider{},
Provider: &FakeWebhookProvider{},
}
providerAPIServer.recordsHandler(w, req)
providerAPIServer.RecordsHandler(w, req)
res := w.Result()
require.Equal(t, http.StatusNoContent, res.StatusCode)
}
Expand All @@ -169,11 +169,11 @@ func TestRecordsHandlerApplyChangesWithErrors(t *testing.T) {
w := httptest.NewRecorder()

providerAPIServer := &WebhookServer{
provider: &FakeWebhookProvider{
Provider: &FakeWebhookProvider{
err: fmt.Errorf("error"),
},
}
providerAPIServer.recordsHandler(w, req)
providerAPIServer.RecordsHandler(w, req)
res := w.Result()
require.Equal(t, http.StatusInternalServerError, res.StatusCode)
}
Expand All @@ -183,9 +183,9 @@ func TestRecordsHandlerWithWrongHTTPMethod(t *testing.T) {
w := httptest.NewRecorder()

providerAPIServer := &WebhookServer{
provider: &FakeWebhookProvider{},
Provider: &FakeWebhookProvider{},
}
providerAPIServer.recordsHandler(w, req)
providerAPIServer.RecordsHandler(w, req)
res := w.Result()
require.Equal(t, http.StatusBadRequest, res.StatusCode)
}
Expand All @@ -195,15 +195,15 @@ func TestAdjustEndpointsHandlerWithInvalidRequest(t *testing.T) {
w := httptest.NewRecorder()

providerAPIServer := &WebhookServer{
provider: &FakeWebhookProvider{},
Provider: &FakeWebhookProvider{},
}
providerAPIServer.adjustEndpointsHandler(w, req)
providerAPIServer.AdjustEndpointsHandler(w, req)
res := w.Result()
require.Equal(t, http.StatusBadRequest, res.StatusCode)

req = httptest.NewRequest(http.MethodGet, "/adjustendpoints", nil)

providerAPIServer.adjustEndpointsHandler(w, req)
providerAPIServer.AdjustEndpointsHandler(w, req)
res = w.Result()
require.Equal(t, http.StatusBadRequest, res.StatusCode)
}
Expand All @@ -226,9 +226,9 @@ func TestAdjustEndpointsHandlerWithValidRequest(t *testing.T) {
w := httptest.NewRecorder()

providerAPIServer := &WebhookServer{
provider: &FakeWebhookProvider{},
Provider: &FakeWebhookProvider{},
}
providerAPIServer.adjustEndpointsHandler(w, req)
providerAPIServer.AdjustEndpointsHandler(w, req)
res := w.Result()
require.Equal(t, http.StatusOK, res.StatusCode)
require.NotNil(t, res.Body)
Expand All @@ -252,11 +252,11 @@ func TestAdjustEndpointsHandlerWithError(t *testing.T) {
w := httptest.NewRecorder()

providerAPIServer := &WebhookServer{
provider: &FakeWebhookProvider{
Provider: &FakeWebhookProvider{
err: fmt.Errorf("error"),
},
}
providerAPIServer.adjustEndpointsHandler(w, req)
providerAPIServer.AdjustEndpointsHandler(w, req)
res := w.Result()
require.Equal(t, http.StatusInternalServerError, res.StatusCode)
require.NotNil(t, res.Body)
Expand Down

0 comments on commit a60480f

Please sign in to comment.