Skip to content

Commit

Permalink
Flattens facts
Browse files Browse the repository at this point in the history
  • Loading branch information
bomoko committed Nov 15, 2023
1 parent b816734 commit aa04080
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 33 deletions.
2 changes: 1 addition & 1 deletion internal/defs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
29 changes: 20 additions & 9 deletions internal/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package service

import (
"fmt"
"io/ioutil"
"net/http"
"strconv"

Expand Down Expand Up @@ -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(),
Expand All @@ -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"
Expand Down Expand Up @@ -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
Expand Down
81 changes: 58 additions & 23 deletions internal/service/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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",
})
Expand Down Expand Up @@ -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)
}

0 comments on commit aa04080

Please sign in to comment.