diff --git a/main.go b/main.go index 544edbb..b922214 100644 --- a/main.go +++ b/main.go @@ -4,6 +4,7 @@ import ( "context" "crypto/tls" "crypto/x509" + "encoding/json" "errors" "fmt" "io/ioutil" @@ -242,14 +243,27 @@ func puppetNodeExists(client *http.Client, event *types.Event) (bool, error) { endpoint = fmt.Sprintf("%s/%s", endpoint, name) resp, err := client.Get(endpoint) if err != nil { + log.Printf("error getting puppet node: %s", err) return false, err } - _ = resp.Body.Close() + defer resp.Body.Close() // Determine if the node exists if resp.StatusCode == http.StatusOK { - log.Printf("puppet node %q exists", name) - return true, nil + var info map[string]interface{} + if err := json.NewDecoder(resp.Body).Decode(&info); err != nil { + log.Printf("puppet node returned invalid response: %s", err) + return false, err + } + nodeInfo := make(map[string]interface{}) + timeDeactivated := nodeInfo["deactivated"] + + log.Printf("puppet node %q exists, checking if deactivated", name) + if timeDeactivated != nil { + return false, nil + } else { + return true, nil + } } else if resp.StatusCode == http.StatusNotFound { log.Printf("puppet node %q does not exist", name) return false, nil diff --git a/main_test.go b/main_test.go index a2bfffe..5fc8729 100644 --- a/main_test.go +++ b/main_test.go @@ -1,9 +1,11 @@ package main import ( + "encoding/json" "net/http" "net/http/httptest" "testing" + "time" "github.com/sensu/sensu-go/types" ) @@ -161,6 +163,9 @@ func Test_puppetNodeExists(t *testing.T) { t.Run(tt.name, func(t *testing.T) { ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(tt.statusCode) + if tt.statusCode == http.StatusOK { + _ = json.NewEncoder(w).Encode(map[string]interface{}{"deactivated": time.Now().Unix()}) + } })) defer ts.Close() handler.endpoint = ts.URL