Skip to content

Commit 47916d4

Browse files
authored
Merge pull request #2053 from afumagalli98/2046
feat: add ListLocationsLicenses endpoint to APIService
2 parents 54015d4 + e30b0c5 commit 47916d4

File tree

7 files changed

+155
-0
lines changed

7 files changed

+155
-0
lines changed

api-service/controller/controller.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ type APIControllerInterface interface {
7171

7272
// ListLocations list locations using the filters in the request
7373
ListLocations(w http.ResponseWriter, r *http.Request)
74+
ListLocationsLicenses(w http.ResponseWriter, r *http.Request)
7475
// ListEnvironments list environments using the filters in the request
7576
ListEnvironments(w http.ResponseWriter, r *http.Request)
7677
// GetHostsCountStats return the number of the hosts using the filters in the request

api-service/controller/hosts.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,18 @@ func (ctrl *APIController) ListLocations(w http.ResponseWriter, r *http.Request)
248248
utils.WriteJSONResponse(w, http.StatusOK, locations)
249249
}
250250

251+
func (ctrl *APIController) ListLocationsLicenses(w http.ResponseWriter, r *http.Request) {
252+
user := context.Get(r, "user")
253+
254+
locations, err := ctrl.Service.ListLocationsLicenses(user)
255+
if err != nil {
256+
utils.WriteAndLogError(ctrl.Log, w, http.StatusInternalServerError, err)
257+
return
258+
}
259+
260+
utils.WriteJSONResponse(w, http.StatusOK, locations)
261+
}
262+
251263
// ListEnvironments list the environments using the filters in the request
252264
func (ctrl *APIController) ListEnvironments(w http.ResponseWriter, r *http.Request) {
253265
var location, environment string

api-service/controller/routing.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,8 @@ func (ctrl *APIController) setupProtectedRoutes(router *mux.Router) {
119119
router.HandleFunc("/hosts/technologies/all/databases/licenses-used-per-cluster", ctrl.GetUsedLicensesPerCluster).Methods("GET")
120120
router.HandleFunc("/hosts/technologies/all/databases/licenses-compliance", ctrl.GetDatabaseLicensesCompliance).Methods("GET")
121121

122+
router.HandleFunc("/hosts/technologies/all/databases/licenses/locations", ctrl.ListLocationsLicenses).Methods("GET")
123+
122124
router.HandleFunc("/hosts/technologies/all/databases/grant-dba", ctrl.ListOracleGrantDbaByHostname).Methods("GET")
123125

124126
// ORACLE

api-service/service/hosts.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,29 @@ func (as *APIService) ListLocations(user interface{}) ([]string, error) {
446446
return as.Database.GetUserLocations(u.Username)
447447
}
448448

449+
func (as *APIService) ListLocationsLicenses(user interface{}) ([]string, error) {
450+
locations, err := as.ListLocations(user)
451+
if err != nil {
452+
return nil, err
453+
}
454+
455+
if as.Config.APIService.ScopeAsLocation != "" {
456+
mergedLocations := make([]string, 0)
457+
mergedLocations = append(mergedLocations, as.Config.APIService.ScopeAsLocation)
458+
scopes := strings.Split(as.Config.APIService.ScopeAsLocation, ",")
459+
460+
for _, location := range locations {
461+
if !utils.ContainsI(scopes, location) {
462+
mergedLocations = append(mergedLocations, location)
463+
}
464+
}
465+
466+
return mergedLocations, nil
467+
}
468+
469+
return locations, nil
470+
}
471+
449472
// ListEnvironments list environments
450473
func (as *APIService) ListEnvironments(location string, environment string, olderThan time.Time) ([]string, error) {
451474
return as.Database.ListEnvironments(location, environment, olderThan)

api-service/service/hosts_test.go

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1114,3 +1114,100 @@ func TestGetVirtualHostWithoutCluster_Success(t *testing.T) {
11141114

11151115
assert.Equal(t, expected, actual)
11161116
}
1117+
1118+
func TestListLocationsLicenses(t *testing.T) {
1119+
testCases := []struct {
1120+
name string
1121+
config config.Configuration
1122+
user model.User
1123+
buildStubs func(db *MockMongoDatabaseInterface)
1124+
checkResponse func(t *testing.T, res []string, err error)
1125+
}{
1126+
{
1127+
name: "Ok user",
1128+
config: config.Configuration{
1129+
APIService: config.APIService{
1130+
ScopeAsLocation: "loc1,loc2,loc3",
1131+
},
1132+
},
1133+
user: model.User{
1134+
Username: "user01",
1135+
},
1136+
buildStubs: func(db *MockMongoDatabaseInterface) {
1137+
db.EXPECT().GetUserLocations("user01").
1138+
Return([]string{"loc1", "loc2", "loc3", "loc4"}, nil)
1139+
},
1140+
1141+
checkResponse: func(t *testing.T, res []string, err error) {
1142+
assert.NoError(t, err)
1143+
assert.NotEmpty(t, res)
1144+
expectedResult := []string{"loc1,loc2,loc3", "loc4"}
1145+
assert.Equal(t, expectedResult, res)
1146+
},
1147+
},
1148+
{
1149+
name: "Ok Admin",
1150+
config: config.Configuration{
1151+
APIService: config.APIService{
1152+
ScopeAsLocation: "loc1,loc2,loc3",
1153+
},
1154+
},
1155+
user: model.User{
1156+
Username: "user01",
1157+
Groups: []string{"admin"},
1158+
},
1159+
buildStubs: func(db *MockMongoDatabaseInterface) {
1160+
db.EXPECT().ListAllLocations("", "", utils.MAX_TIME).
1161+
Return([]string{"loc1", "loc2", "loc3", "loc4"}, nil)
1162+
},
1163+
1164+
checkResponse: func(t *testing.T, res []string, err error) {
1165+
assert.NoError(t, err)
1166+
assert.NotEmpty(t, res)
1167+
expectedResult := []string{"loc1,loc2,loc3", "loc4"}
1168+
assert.Equal(t, expectedResult, res)
1169+
},
1170+
},
1171+
{
1172+
name: "ScopeAsLocation empty",
1173+
config: config.Configuration{
1174+
APIService: config.APIService{
1175+
ScopeAsLocation: "",
1176+
},
1177+
},
1178+
user: model.User{
1179+
Username: "user01",
1180+
},
1181+
buildStubs: func(db *MockMongoDatabaseInterface) {
1182+
db.EXPECT().GetUserLocations("user01").
1183+
Return([]string{"loc1", "loc2", "loc3", "loc4"}, nil)
1184+
},
1185+
1186+
checkResponse: func(t *testing.T, res []string, err error) {
1187+
assert.NoError(t, err)
1188+
assert.NotEmpty(t, res)
1189+
expectedResult := []string{"loc1", "loc2", "loc3", "loc4"}
1190+
assert.Equal(t, expectedResult, res)
1191+
},
1192+
},
1193+
}
1194+
for _, tc := range testCases {
1195+
t.Run(tc.name, func(t *testing.T) {
1196+
1197+
mockCtrl := gomock.NewController(t)
1198+
defer mockCtrl.Finish()
1199+
db := NewMockMongoDatabaseInterface(mockCtrl)
1200+
1201+
as := APIService{
1202+
Database: db,
1203+
Config: tc.config,
1204+
}
1205+
1206+
tc.buildStubs(db)
1207+
1208+
res, err := as.ListLocationsLicenses(tc.user)
1209+
1210+
tc.checkResponse(t, res, err)
1211+
})
1212+
}
1213+
}

api-service/service/service.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ type APIServiceInterface interface {
8686
ListAllLocations(location string, environment string, olderThan time.Time) ([]string, error)
8787
// ListLocations list locations
8888
ListLocations(user interface{}) ([]string, error)
89+
ListLocationsLicenses(user interface{}) ([]string, error)
8990
// ListEnvironments list environments
9091
ListEnvironments(location string, environment string, olderThan time.Time) ([]string, error)
9192

swagger.yml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4129,6 +4129,25 @@ paths:
41294129
$ref: "#/components/responses/error"
41304130
"500":
41314131
$ref: "#/components/responses/error"
4132+
4133+
/hosts/technologies/all/databases/licenses/locations:
4134+
get:
4135+
tags:
4136+
- api-service
4137+
operationId: ListLocationsLicenses
4138+
summary: List locations using config.ScopeAsLocation
4139+
description: List locations using config.ScopeAsLocation
4140+
responses:
4141+
200:
4142+
description: OK
4143+
content:
4144+
application/json:
4145+
schema:
4146+
type: array
4147+
items:
4148+
type: string
4149+
uniqueItems: true
4150+
41324151
/hosts/environments:
41334152
get:
41344153
tags:

0 commit comments

Comments
 (0)