-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
6 changed files
with
11,526 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package service | ||
|
||
import ( | ||
"github.com/gin-gonic/gin" | ||
"github.com/uselagoon/lagoon/services/insights-handler/internal/lagoonclient" | ||
"gorm.io/driver/sqlite" | ||
"gorm.io/gorm" | ||
"net/http" | ||
"strconv" | ||
) | ||
|
||
func SetupRouter(db *gorm.DB) (*gin.Engine, error) { | ||
router := gin.Default() | ||
|
||
// Set up DB as middleware for injection | ||
router.Use(func(context *gin.Context) { | ||
context.Set("db", db) | ||
context.Next() | ||
}) | ||
|
||
router.GET("/environment/:id/facts", GetFactsByEnvironmentEndpoint) | ||
router.GET("/ping/{id}/facts", func(context *gin.Context) { | ||
context.String(200, "pong") | ||
}) | ||
return router, nil | ||
} | ||
|
||
type dboptions struct { | ||
Filename string | ||
} | ||
|
||
// setUpDatabase will connect to the selected DB and run pending migrations | ||
func setUpDatabase(opts dboptions) (*gorm.DB, error) { | ||
// TODO: currently we're only supporting sqlite for dev | ||
// going forward, this will run on mysql - but both should be selected | ||
|
||
db, err := gorm.Open(sqlite.Open(opts.Filename), &gorm.Config{}) | ||
if err != nil { | ||
return db, err | ||
} | ||
|
||
if err = db.AutoMigrate(&lagoonclient.Fact{}); err != nil { | ||
return db, err | ||
} | ||
|
||
if err = db.AutoMigrate(&lagoonclient.LagoonProblem{}); err != nil { | ||
return db, err | ||
} | ||
|
||
return db, nil | ||
} | ||
|
||
//func CreateProblem(c *gin.Context) { | ||
// | ||
//} | ||
// | ||
//func GetProblem(c *gin.Context) { | ||
// | ||
//} | ||
// | ||
//func GetProblemsForEnvironment(c *gin.Context) { | ||
// | ||
//} | ||
|
||
func GetFactsByEnvironmentEndpoint(c *gin.Context) { | ||
// Retrieve the environmentID parameter from the URL | ||
|
||
environmentIDStr := c.Param("id") | ||
environmentID, err := strconv.Atoi(environmentIDStr) | ||
if err != nil { | ||
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid environmentID"}) | ||
return | ||
} | ||
|
||
// Retrieve the GORM database instance from the Gin context | ||
db, exists := c.Get("db") | ||
if !exists { | ||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Database connection not found"}) | ||
return | ||
} | ||
|
||
// Cast the db interface to *gorm.DB | ||
gormDB, ok := db.(*gorm.DB) | ||
if !ok { | ||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Invalid database connection"}) | ||
return | ||
} | ||
|
||
// Query the database for facts by environmentID | ||
var facts []lagoonclient.Fact | ||
if err := gormDB.Where("environment = ?", environmentID).Find(&facts).Error; err != nil { | ||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to retrieve facts from the database"}) | ||
return | ||
} | ||
|
||
// Return the facts as JSON | ||
c.JSON(http.StatusOK, facts) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
package service | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"github.com/gin-gonic/gin" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/uselagoon/lagoon/services/insights-handler/internal/lagoonclient" | ||
"log/slog" | ||
"net/http" | ||
"net/http/httptest" | ||
"os" | ||
"testing" | ||
) | ||
|
||
func TestGetFactsByEnvironmentEndpoint(t *testing.T) { | ||
type args struct { | ||
c *gin.Context | ||
Facts []lagoonclient.Fact | ||
EnvironmentId int | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
}{ | ||
{ | ||
name: "Basic test", | ||
args: args{ | ||
c: nil, | ||
EnvironmentId: 1, | ||
Facts: []lagoonclient.Fact{ | ||
{ | ||
Environment: 1, | ||
Name: "TestFact1", | ||
Value: "TestValue1", | ||
}, | ||
{ | ||
Environment: 1, | ||
Name: "TestFact2", | ||
Value: "TestValue2", | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
db, err := setUpDatabase(dboptions{Filename: ":memory:"}) | ||
if err != nil { | ||
slog.Error(err.Error()) | ||
os.Exit(1) | ||
} | ||
|
||
r, err := SetupRouter(db) | ||
|
||
if err != nil { | ||
slog.Error(err.Error()) | ||
os.Exit(1) | ||
} | ||
|
||
for _, tt := range tests { | ||
|
||
for _, e := range tt.args.Facts { | ||
db.Create(&e) | ||
} | ||
|
||
t.Run(tt.name, func(t *testing.T) { | ||
|
||
// Create a mock HTTP request to the endpoint | ||
req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("/environment/%v/facts", tt.args.EnvironmentId), nil) | ||
assert.NoError(t, err) | ||
|
||
// Create a mock HTTP response recorder | ||
w := httptest.NewRecorder() | ||
|
||
// Call the handler function | ||
r.ServeHTTP(w, req) | ||
|
||
// Check the HTTP status code | ||
assert.Equal(t, http.StatusOK, w.Code) | ||
|
||
// Decode the response body | ||
var facts []lagoonclient.Fact | ||
err = json.Unmarshal(w.Body.Bytes(), &facts) | ||
assert.NoError(t, err) | ||
|
||
for _, e := range tt.args.Facts { | ||
appears := false | ||
//we test whether each of them appear in the result | ||
for _, testE := range facts { | ||
if e.Name == testE.Name { | ||
appears = true | ||
} | ||
} | ||
if appears == false { | ||
t.Errorf("Fact '%v' does not appear in results", e.Name) | ||
} | ||
} | ||
|
||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters