This repository has been archived by the owner on Dec 21, 2023. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(shipyard-controller): Add dbdump endpoint to the Debug-UI (#8618)
* debug ui db dump endpoint Signed-off-by: Andreas Samonik <andreas.samonik@dynatrace.com> * debug ui db dump endpoint Signed-off-by: Andreas Samonik <andreas.samonik@dynatrace.com> * bson Signed-off-by: Andreas Samonik <andreas.samonik@dynatrace.com> * removed commented out code Signed-off-by: Andreas Samonik <andreas.samonik@dynatrace.com> * additional repo test Signed-off-by: Andreas Samonik <andreas.samonik@dynatrace.com> * pr fixes Signed-off-by: Andreas Samonik <andreas.samonik@dynatrace.com> * frontend api change Signed-off-by: Andreas Samonik <andreas.samonik@dynatrace.com> * additional repo test case Signed-off-by: Andreas Samonik <andreas.samonik@dynatrace.com> * repo test Signed-off-by: Andreas Samonik <andreas.samonik@dynatrace.com> * additional filter Signed-off-by: Andreas Samonik <andreas.samonik@dynatrace.com> * html alignment fix Signed-off-by: Andreas Samonik <andreas.samonik@dynatrace.com> Signed-off-by: Andreas Samonik <andreas.samonik@dynatrace.com>
- Loading branch information
Showing
15 changed files
with
732 additions
and
8 deletions.
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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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,60 @@ | ||
package db | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
"go.mongodb.org/mongo-driver/bson" | ||
"go.mongodb.org/mongo-driver/mongo" | ||
) | ||
|
||
type MongoDBDumpRepo struct { | ||
DbConnection *MongoDBConnection | ||
} | ||
|
||
func NewMongoDBDumpRepo(dbConnection *MongoDBConnection) *MongoDBDumpRepo { | ||
return &MongoDBDumpRepo{DbConnection: dbConnection} | ||
} | ||
|
||
func (mdbrepo *MongoDBDumpRepo) GetDump(collectionName string) ([]bson.M, error) { | ||
collection, ctx, cancel, err := mdbrepo.getCollectionAndContext(collectionName) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer cancel() | ||
|
||
cursor, err := collection.Find(ctx, bson.D{}) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var result = []bson.M{} | ||
|
||
cursor.All(ctx, &result) | ||
|
||
return result, err | ||
} | ||
|
||
func (mdbrepo *MongoDBDumpRepo) ListAllCollections() ([]string, error) { | ||
err := mdbrepo.DbConnection.EnsureDBConnection() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
ctx, _ := context.WithTimeout(context.Background(), 10*time.Second) | ||
result, err := mdbrepo.DbConnection.Client.Database(getDatabaseName()).ListCollectionNames(ctx, bson.D{}) | ||
|
||
return result, err | ||
} | ||
|
||
func (mdbrepo *MongoDBDumpRepo) getCollectionAndContext(collectionName string) (*mongo.Collection, context.Context, context.CancelFunc, error) { | ||
err := mdbrepo.DbConnection.EnsureDBConnection() | ||
if err != nil { | ||
return nil, nil, nil, err | ||
} | ||
collection := mdbrepo.DbConnection.Client.Database(getDatabaseName()).Collection(collectionName) | ||
|
||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) | ||
return collection, ctx, cancel, nil | ||
} |
88 changes: 88 additions & 0 deletions
88
shipyard-controller/internal/db/mongodb_dbdump_repo_test.go
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,88 @@ | ||
package db | ||
|
||
import ( | ||
"encoding/json" | ||
"testing" | ||
|
||
apimodels "github.com/keptn/go-utils/pkg/api/models" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestMongoDBDumpRepo_ListAllCollections(t *testing.T) { | ||
dumpRepo := NewMongoDBDumpRepo(GetMongoDBConnectionInstance()) | ||
projectsRepo := NewMongoDBProjectsRepo(GetMongoDBConnectionInstance()) | ||
|
||
err := projectsRepo.CreateProject(&apimodels.ExpandedProject{ | ||
ProjectName: "my-project", | ||
}) | ||
require.Nil(t, err) | ||
|
||
projectNames, err := dumpRepo.ListAllCollections() | ||
|
||
require.Len(t, projectNames, 1) | ||
require.Nil(t, err) | ||
require.Contains(t, projectNames, "keptnProjectsMV") | ||
|
||
CleanupDB(t, projectsRepo) | ||
} | ||
|
||
func TestMongoDBDumpRepo_GetDump(t *testing.T) { | ||
dumpRepo := NewMongoDBDumpRepo(GetMongoDBConnectionInstance()) | ||
projectsRepo := NewMongoDBProjectsRepo(GetMongoDBConnectionInstance()) | ||
|
||
// get random collection dump | ||
_, err := dumpRepo.GetDump("test") | ||
require.Nil(t, err) | ||
|
||
// compare to project repo: 0 projects | ||
projects, err := projectsRepo.GetProjects() | ||
require.Nil(t, err) | ||
|
||
dump, err := dumpRepo.GetDump("keptnProjectsMV") | ||
require.Nil(t, err) | ||
|
||
var projects_dump []*apimodels.ExpandedProject | ||
bytes, err := json.Marshal(dump) | ||
err = json.Unmarshal(bytes, &projects_dump) | ||
|
||
require.Nil(t, err) | ||
require.Equal(t, projects_dump, projects) | ||
|
||
// compare to project repo: 1 project | ||
err = projectsRepo.CreateProject(&apimodels.ExpandedProject{ | ||
ProjectName: "my-project", | ||
}) | ||
require.Nil(t, err) | ||
|
||
projects, err = projectsRepo.GetProjects() | ||
require.Nil(t, err) | ||
|
||
dump, err = dumpRepo.GetDump("keptnProjectsMV") | ||
require.Nil(t, err) | ||
|
||
bytes, err = json.Marshal(dump) | ||
err = json.Unmarshal(bytes, &projects_dump) | ||
|
||
require.Nil(t, err) | ||
require.Equal(t, projects_dump, projects) | ||
|
||
// compare to project repo: 2 projects | ||
err = projectsRepo.CreateProject(&apimodels.ExpandedProject{ | ||
ProjectName: "my-project2", | ||
}) | ||
require.Nil(t, err) | ||
|
||
projects, err = projectsRepo.GetProjects() | ||
require.Nil(t, err) | ||
|
||
dump, err = dumpRepo.GetDump("keptnProjectsMV") | ||
require.Nil(t, err) | ||
|
||
bytes, err = json.Marshal(dump) | ||
err = json.Unmarshal(bytes, &projects_dump) | ||
|
||
require.Nil(t, err) | ||
require.Equal(t, projects_dump, projects) | ||
|
||
CleanupDB(t, projectsRepo) | ||
} |
Oops, something went wrong.