Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added missing databases to hosts API #1692

Merged
merged 1 commit into from
Nov 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions api-service/database/hosts.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ import (
)

const hostCollection = "hosts"
const usersCollection = "users"

func (md *MongoDatabase) SearchHosts(mode string, filters dto.SearchHostsFilters) ([]map[string]interface{}, error) {
out := make([]map[string]interface{}, 0)
Expand Down Expand Up @@ -907,9 +906,9 @@ func (md *MongoDatabase) getHostTechnology(hostname string, olderThan time.Time)
func (md *MongoDatabase) FindUnlistedRunningDatabases(hostname string) ([]string, error) {
ctx := context.TODO()

result := make([]string, 0)
out := make([]string, 0)

cur, err := md.Client.Database(md.Config.Mongodb.DBName).Collection(usersCollection).
cur, err := md.Client.Database(md.Config.Mongodb.DBName).Collection(hostCollection).
Aggregate(ctx, bson.A{
bson.D{
{Key: "$match",
Expand All @@ -927,17 +926,23 @@ func (md *MongoDatabase) FindUnlistedRunningDatabases(hostname string) ([]string
},
},
},
bson.D{{Key: "$project", Value: bson.D{{Key: "dbs", Value: "$features.oracle.database.unlistedRunningDatabases"}}}},
bson.D{{Key: "$project", Value: bson.D{{Key: "db", Value: "$features.oracle.database.unlistedRunningDatabases"}}}},
bson.D{{Key: "$group", Value: bson.D{{Key: "_id", Value: "$db"}}}},
})
if err != nil {
return nil, err
}

if err := cur.All(ctx, &result); err != nil {
return nil, err
for cur.Next(ctx) {
var item map[string]string
if cur.Decode(&item) != nil {
return nil, utils.NewError(err, "Decode ERROR")
}

out = append(out, item["_id"])
}

return result, nil
return out, nil
}

func (md *MongoDatabase) SearchHostMysqlLMS(filter dto.SearchHostsAsLMS) ([]dto.MySqlHostLMS, error) {
Expand Down
1 change: 1 addition & 0 deletions api-service/dto/hosts.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,4 +188,5 @@ type HostDataSummary struct {
VirtualizationNode string `json:"virtualizationNode" bson:"virtualizationNode"`
Cluster string `json:"cluster" bson:"cluster"`
Databases map[string][]string `json:"databases" bson:"databases"` // map[Technology] []database names
IsMissingDB []string `json:"isMissingDB,omitempty"`
}
16 changes: 15 additions & 1 deletion api-service/service/hosts.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,21 @@ func (as *APIService) getCSIsByHostname() (res map[string][]string, err error) {
}

func (as *APIService) GetHostDataSummaries(filters dto.SearchHostsFilters) ([]dto.HostDataSummary, error) {
return as.Database.GetHostDataSummaries(filters)
hosts, err := as.Database.GetHostDataSummaries(filters)
if err != nil {
return nil, err
}

for i := range hosts {
ismissingdb, err := as.IsMissingDB(hosts[i].Hostname)
if err != nil {
return nil, err
}

hosts[i].IsMissingDB = ismissingdb
}

return hosts, nil
}

// GetHost return the host specified in the hostname param
Expand Down
6 changes: 5 additions & 1 deletion api-service/service/hosts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -696,7 +696,7 @@ func TestGetHostDataSummaries(t *testing.T) {
},
{
filters: dto.SearchHostsFilters{},
res: []dto.HostDataSummary{},
res: nil,
err: aerrMock,
},
}
Expand All @@ -711,6 +711,10 @@ func TestGetHostDataSummaries(t *testing.T) {

db.EXPECT().GetHostDataSummaries(tc.filters).Return(tc.res, tc.err).Times(1)

if tc.res != nil {
db.EXPECT().FindUnlistedRunningDatabases("pluto").Return(nil, nil).Times(1)
}

res, err := as.GetHostDataSummaries(tc.filters)
if tc.err == nil {
assert.Nil(t, err)
Expand Down
4 changes: 4 additions & 0 deletions swagger.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1256,6 +1256,10 @@ components:
format: date-time
cpuAvg:
type: number
isMissingDB:
type: array
items:
type: string
required:
- hostname
- environment
Expand Down