diff --git a/internal/defs.go b/internal/defs.go index ea508a3..a8c8c58 100644 --- a/internal/defs.go +++ b/internal/defs.go @@ -24,7 +24,7 @@ type Facts struct { type ProblemSeverityRating string type Problem struct { - EnvironmentId string `json:"environment"` + EnvironmentId int `json:"environment"` Identifier string `json:"identifier"` Version string `json:"version,omitempty"` FixedVersion string `json:"fixedVersion,omitempty"` diff --git a/internal/service/service.go b/internal/service/service.go index 73d6b03..b3046cf 100644 --- a/internal/service/service.go +++ b/internal/service/service.go @@ -2,6 +2,7 @@ package service import ( "fmt" + "io/ioutil" "net/http" "strconv" @@ -54,8 +55,9 @@ func (r *routerInstance) writeProblems(c *gin.Context) { //TODO: drop "InsightsType" for Type of the form "direct.fact"/"direct.problem" //details := &internal.Facts{Type: "direct.problems"} details := &internal.Problems{Type: "direct.problems"} + problemList := *new([]internal.Problem) - if err = c.ShouldBindJSON(details); err != nil { + if err = c.ShouldBindJSON(&problemList); err != nil { c.JSON(http.StatusBadRequest, gin.H{ "status": "Unable to parse incoming data", "message": err.Error(), @@ -78,8 +80,9 @@ func (r *routerInstance) writeProblems(c *gin.Context) { details.EnvironmentId = int(lid) details.ProjectName = namespace.ProjectName details.EnvironmentName = namespace.EnvironmentName + details.Problems = problemList for i := range details.Problems { - details.Problems[i].EnvironmentId = namespace.EnvironmentId + details.Problems[i].EnvironmentId = int(lid) if details.Problems[i].Source == "" { details.Problems[i].Source = "InsightsRemoteWebService" @@ -131,13 +134,21 @@ func (r *routerInstance) writeFacts(c *gin.Context) { //TODO: drop "InsightsType" for Type of the form "direct.fact"/"direct.problem" details := &internal.Facts{Type: "direct.facts"} - if err = c.ShouldBindJSON(details); err != nil { - c.JSON(http.StatusBadRequest, gin.H{ - "status": "Unable to parse incoming data", - "message": err.Error(), - }) - fmt.Println(err) - return + // we try two different ways of parsing incoming facts - first as a simple list of facts + ByteBody, _ := ioutil.ReadAll(c.Request.Body) + + factList := *new([]internal.Fact) + if err = json.Unmarshal(ByteBody, &factList); err != nil { // it might just be they're passing the "big" version with all details + if err = json.Unmarshal(ByteBody, details); err != nil { + c.JSON(http.StatusBadRequest, gin.H{ + "status": "Unable to parse incoming data", + "message": err.Error(), + }) + fmt.Println(err) + return + } + } else { + details.Facts = factList } //let's force our facts to get pushed to the right place diff --git a/internal/service/service_test.go b/internal/service/service_test.go index 438b238..2c25425 100644 --- a/internal/service/service_test.go +++ b/internal/service/service_test.go @@ -16,11 +16,11 @@ import ( const secretTestTokenSecret = "secret" const secretTestNamespace = "testNS" +const testEnvironmentId = "777" var queueWriterOutput string func messageQueueWriter(data []byte) error { - //fmt.Println(string(data)) queueWriterOutput = string(data) return nil } @@ -36,7 +36,7 @@ func TestWriteFactsRoute(t *testing.T) { token, err := tokens.GenerateTokenForNamespace(secretTestTokenSecret, tokens.NamespaceDetails{ Namespace: secretTestNamespace, - EnvironmentId: "1", + EnvironmentId: testEnvironmentId, ProjectName: "Test", EnvironmentName: "Test", }) @@ -66,44 +66,79 @@ func TestWriteFactsRoute(t *testing.T) { assert.Contains(t, queueWriterOutput, testFacts.Facts[0].Name) } -func TestWriteProblemsRoute(t *testing.T) { +func TestWriteFactsRouteNoProjectData(t *testing.T) { defer resetWriterOutput() router := SetupRouter(secretTestTokenSecret, messageQueueWriter, true) w := httptest.NewRecorder() token, err := tokens.GenerateTokenForNamespace(secretTestTokenSecret, tokens.NamespaceDetails{ Namespace: secretTestNamespace, - EnvironmentId: "1", + EnvironmentId: testEnvironmentId, ProjectName: "Test", EnvironmentName: "Test", }) require.NoError(t, err) - testFacts := internal.Problems{ - Problems: []internal.Problem{ - { - EnvironmentId: "1", - Identifier: "123", - Version: "1", - FixedVersion: "2", - Source: "a unique sources", - Service: "test", - Data: "test", - Severity: "1", - SeverityScore: 1, - AssociatedPackage: "test", - Description: "test", - Links: "test", - }, - }} + testFacts := []internal.Fact{ + { + Name: "testfact1", + Value: "testvalue1", + Source: "testsource1", + Description: "testdescription1", + Type: "testtype1", + Category: "testcategory1", + Service: "testservice1", + }, + } bodyString, _ := json.Marshal(testFacts) - req, _ := http.NewRequest(http.MethodPost, "/problems", bytes.NewBuffer(bodyString)) + req, _ := http.NewRequest(http.MethodPost, "/facts", bytes.NewBuffer(bodyString)) req.Header.Set("Authorization", token) req.Header.Set("Content-Type", "application/json") router.ServeHTTP(w, req) assert.Equal(t, http.StatusOK, w.Code) assert.Contains(t, w.Body.String(), w.Body.String()) - assert.Contains(t, queueWriterOutput, testFacts.Problems[0].Source) + assert.Contains(t, queueWriterOutput, testFacts[0].Name) +} + +func TestWriteProblemsRoute(t *testing.T) { + defer resetWriterOutput() + router := SetupRouter(secretTestTokenSecret, messageQueueWriter, true) + w := httptest.NewRecorder() + + token, err := tokens.GenerateTokenForNamespace(secretTestTokenSecret, tokens.NamespaceDetails{ + Namespace: secretTestNamespace, + EnvironmentId: testEnvironmentId, + ProjectName: "Test", + EnvironmentName: "Test", + }) + + require.NoError(t, err) + + testProblems := []internal.Problem{ + { + EnvironmentId: 4, + Identifier: "123", + Version: "1", + FixedVersion: "2", + Source: "a unique sources", + Service: "test", + Data: "test", + Severity: "1", + SeverityScore: 1, + AssociatedPackage: "test", + Description: "test", + Links: "test", + }, + } + bodyString, _ := json.Marshal(testProblems) + req, _ := http.NewRequest(http.MethodPost, "/problems", bytes.NewBuffer(bodyString)) + req.Header.Set("Authorization", token) + req.Header.Set("Content-Type", "application/json") + router.ServeHTTP(w, req) + + assert.Equal(t, http.StatusOK, w.Code) + + assert.Contains(t, queueWriterOutput, testProblems[0].Source) }