feat(inventory): use ownership filtering

This commit is contained in:
Denis Gukov 2024-12-17 20:43:41 +05:00
parent dc347442c4
commit aae2b572f5
No known key found for this signature in database
GPG Key ID: 044381366A5D4731
4 changed files with 52 additions and 8 deletions

View File

@ -130,3 +130,30 @@ func QueryParams(url *url.URL) db.RetrieveQueryParams {
SortInverted: url.Query().Get("order") == "desc",
}
}
func QueryParamsWithOwner(url *url.URL, props db.ObjectProps) db.RetrieveQueryParams {
res := QueryParams(url)
hasOwnerFilter := false
for _, ownership := range props.Ownerships {
s := url.Query().Get(ownership.ReferringColumnSuffix)
if s == "" {
continue
}
id, err := strconv.Atoi(s)
if err != nil {
continue
}
res.Ownership.SetOwnerID(*ownership, id)
hasOwnerFilter = true
}
if !hasOwnerFilter {
res.Ownership.WithoutOwnerOnly = true
}
return res
}

View File

@ -56,8 +56,8 @@ func GetInventory(w http.ResponseWriter, r *http.Request) {
project := context.Get(r, "project").(db.Project)
params := helpers.QueryParams(r.URL)
params.Ownership.WithoutOwnerOnly = true
params := helpers.QueryParamsWithOwner(r.URL, db.InventoryProps)
inventories, err := helpers.Store(r).GetInventories(project.ID, params)
if err != nil {

View File

@ -75,7 +75,7 @@ type IntegrationExtractorChildReferrers struct {
Integrations []ObjectReferrer `json:"integrations"`
}
func (f OwnershipFilter) GetOwnerID(ownership ObjectProps) *int {
func (f *OwnershipFilter) GetOwnerID(ownership ObjectProps) *int {
switch ownership.ReferringColumnSuffix {
case "template_id":
return f.TemplateID
@ -86,6 +86,15 @@ func (f OwnershipFilter) GetOwnerID(ownership ObjectProps) *int {
}
}
func (f *OwnershipFilter) SetOwnerID(ownership ObjectProps, ownerID int) {
switch ownership.ReferringColumnSuffix {
case "template_id":
f.TemplateID = &ownerID
case "environment_id":
f.EnvironmentID = &ownerID
}
}
// ObjectProps describe database entities.
// It mainly used for NoSQL implementations (currently BoltDB) to preserve same
// data structure of different implementations and easy change it if required.

View File

@ -452,11 +452,19 @@ export default {
responseType: 'json',
})).data;
this.inventory = (await axios({
keys: 'get',
url: `/api/project/${this.projectId}/inventory`,
responseType: 'json',
})).data;
this.inventory = [
...(await axios({
keys: 'get',
url: `/api/project/${this.projectId}/inventory?app=${this.app}&template_id=${this.itemId}`,
responseType: 'json',
})).data,
...(await axios({
keys: 'get',
url: `/api/project/${this.projectId}/inventory?app=${this.app}`,
responseType: 'json',
})).data,
];
this.environment = (await axios({
keys: 'get',