Skip to content

Commit

Permalink
Merge pull request #146 from hashicorp/ci/merge/main
Browse files Browse the repository at this point in the history
Merge upstream @ 1762763
  • Loading branch information
lornasong authored Oct 11, 2021
2 parents e346865 + 8cfda63 commit e108ce2
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 32 deletions.
71 changes: 40 additions & 31 deletions api/overall_status.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package api

import (
"fmt"
"net/http"

"github.com/hashicorp/consul-terraform-sync/driver"
Expand Down Expand Up @@ -59,42 +60,50 @@ func newOverallStatusHandler(store *event.Store, drivers *driver.Drivers, versio
func (h *overallStatusHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
logger := logging.FromContext(r.Context()).Named(overallStatusSubsystemName)
logger.Trace("requesting task status", "url_path", r.URL.Path)
switch r.Method {
case http.MethodGet:

data := h.store.Read("")
taskSummary := TaskSummary{}
for _, events := range data {
successes := make([]bool, len(events))
for i, event := range events {
successes[i] = event.Success
data := h.store.Read("")
taskSummary := TaskSummary{}
for _, events := range data {
successes := make([]bool, len(events))
for i, event := range events {
successes[i] = event.Success
}
status := successToStatus(successes)
switch status {
case StatusSuccessful:
taskSummary.Status.Successful++
case StatusErrored:
taskSummary.Status.Errored++
case StatusCritical:
taskSummary.Status.Critical++
}
}
status := successToStatus(successes)
switch status {
case StatusSuccessful:
taskSummary.Status.Successful++
case StatusErrored:
taskSummary.Status.Errored++
case StatusCritical:
taskSummary.Status.Critical++
}
}

for taskName, d := range h.drivers.Map() {
// look for any tasks that have a driver but no events
if _, ok := data[taskName]; !ok {
taskSummary.Status.Unknown++
}
for taskName, d := range h.drivers.Map() {
// look for any tasks that have a driver but no events
if _, ok := data[taskName]; !ok {
taskSummary.Status.Unknown++
}

if d.Task().IsEnabled() {
taskSummary.Enabled.True++
} else {
taskSummary.Enabled.False++
if d.Task().IsEnabled() {
taskSummary.Enabled.True++
} else {
taskSummary.Enabled.False++
}
}
}

err := jsonResponse(w, http.StatusOK, OverallStatus{
TaskSummary: taskSummary,
})
if err != nil {
logger.Error("error, could not generate json error response", "error", err)
err := jsonResponse(w, http.StatusOK, OverallStatus{
TaskSummary: taskSummary,
})
if err != nil {
logger.Error("error, could not generate json error response", "error", err)
}
default:
err := fmt.Errorf("'%s' in an unsupported method. The overallStatus API "+
"currently supports the method(s): '%s'", r.Method, http.MethodGet)
logger.Trace("unsupported method: %s", err)
jsonErrorResponse(r.Context(), w, http.StatusMethodNotAllowed, err)
}
}
11 changes: 10 additions & 1 deletion api/overall_status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,14 @@ func TestOverallStatus_ServeHTTP(t *testing.T) {
cases := []struct {
name string
path string
method string
statusCode int
expected OverallStatus
}{
{
"happy path",
"/v1/status",
http.MethodGet,
http.StatusOK,
OverallStatus{
TaskSummary: TaskSummary{
Expand All @@ -59,6 +61,13 @@ func TestOverallStatus_ServeHTTP(t *testing.T) {
},
},
},
{
"method not allowed",
"/v1/status",
http.MethodPatch,
http.StatusMethodNotAllowed,
OverallStatus{},
},
}

// set up store and handler
Expand All @@ -84,7 +93,7 @@ func TestOverallStatus_ServeHTTP(t *testing.T) {

for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
req, err := http.NewRequest("GET", tc.path, nil)
req, err := http.NewRequest(tc.method, tc.path, nil)
require.NoError(t, err)
resp := httptest.NewRecorder()

Expand Down

0 comments on commit e108ce2

Please sign in to comment.