Skip to content

Commit

Permalink
Merge pull request #2343 from opengovern/fix-tasks
Browse files Browse the repository at this point in the history
fix: fix query api
  • Loading branch information
artaasadi authored Jan 2, 2025
2 parents fe2e40f + 25a4a00 commit d9a4ec6
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 33 deletions.
1 change: 1 addition & 0 deletions services/inventory/api/dto.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ type Query struct {
}

type ListQueryV2Request struct {
QueryIDs []string `json:"query_ids"`
TitleFilter string `json:"title_filter"`
IntegrationTypes []string `json:"integration_types"`
HasParameters *bool `json:"has_parameters"`
Expand Down
6 changes: 5 additions & 1 deletion services/inventory/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,12 +178,16 @@ func (db Database) GetQuery(id string) (*NamedQuery, error) {
return &s, nil
}

func (db Database) ListQueriesByFilters(search *string, tagFilters map[string][]string, integrationTypes []string,
func (db Database) ListQueriesByFilters(queryIds []string, search *string, tagFilters map[string][]string, integrationTypes []string,
hasParameters *bool, primaryTable []string, listOfTables []string, params []string) ([]NamedQuery, error) {
var s []NamedQuery

m := db.orm.Model(&NamedQuery{}).Distinct("named_queries.*").Preload(clause.Associations).Preload("Query.Parameters").Preload("Tags")

if len(queryIds) > 0 {
m = m.Where("id IN ?", queryIds)
}

if search != nil {
m = m.Where("title LIKE ?", "%"+*search+"%")
}
Expand Down
2 changes: 1 addition & 1 deletion services/inventory/http_routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ func (h *HttpHandler) ListQueriesV2(ctx echo.Context) error {
tablesFilter = req.ListOfTables
}

queries, err := h.db.ListQueriesByFilters(search, req.Tags, req.IntegrationTypes, req.HasParameters, req.PrimaryTable,
queries, err := h.db.ListQueriesByFilters(req.QueryIDs, search, req.Tags, req.IntegrationTypes, req.HasParameters, req.PrimaryTable,
tablesFilter, nil)
if err != nil {
span.RecordError(err)
Expand Down
2 changes: 1 addition & 1 deletion services/metadata/client/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func (s *metadataClient) SetConfigMetadata(ctx *httpclient.Context, key models.M
func (s *metadataClient) ListQueryParameters(ctx *httpclient.Context) (api.ListQueryParametersResponse, error) {
url := fmt.Sprintf("%s/api/v1/query_parameter", s.baseURL)
var resp api.ListQueryParametersResponse
if statusCode, err := httpclient.DoRequest(ctx.Ctx, http.MethodGet, url, ctx.ToHeaders(), nil, &resp); err != nil {
if statusCode, err := httpclient.DoRequest(ctx.Ctx, http.MethodPost, url, ctx.ToHeaders(), nil, &resp); err != nil {
if 400 <= statusCode && statusCode < 500 {
return resp, echo.NewHTTPError(statusCode, err.Error())
}
Expand Down
53 changes: 23 additions & 30 deletions services/metadata/http_routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ import (
"github.com/opengovern/og-util/pkg/httpserver"
model2 "github.com/opengovern/opencomply/jobs/demo-importer-job/db/model"
"github.com/opengovern/opencomply/jobs/post-install-job/db/model"
complianceapi "github.com/opengovern/opencomply/services/compliance/api"
complianceClient "github.com/opengovern/opencomply/services/compliance/client"
schedulerClient "github.com/opengovern/opencomply/services/describe/client"
integrationApi "github.com/opengovern/opencomply/services/integration/api/models"
integrationClient "github.com/opengovern/opencomply/services/integration/client"
inventoryApi "github.com/opengovern/opencomply/services/inventory/api"
client2 "github.com/opengovern/opencomply/services/inventory/client"
inventoryClient "github.com/opengovern/opencomply/services/inventory/client"
complianceapi "github.com/opengovern/opencomply/services/compliance/api"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes"
Expand Down Expand Up @@ -60,7 +60,7 @@ func (h HttpHandler) Register(r *echo.Echo) {
metadata.POST("", httpserver.AuthorizeHandler(h.SetConfigMetadata, api3.AdminRole))

queryParameter := v1.Group("/query_parameter")
queryParameter.POST("", httpserver.AuthorizeHandler(h.SetQueryParameter, api3.AdminRole))
queryParameter.POST("/set", httpserver.AuthorizeHandler(h.SetQueryParameter, api3.AdminRole))
queryParameter.POST("", httpserver.AuthorizeHandler(h.ListQueryParameters, api3.ViewerRole))
queryParameter.GET("/:key", httpserver.AuthorizeHandler(h.GetQueryParameter, api3.ViewerRole))

Expand Down Expand Up @@ -296,10 +296,8 @@ func (h HttpHandler) ListQueryParameters(ctx echo.Context) error {
cursor = request.Cursor
perPage = request.PerPage



Queries := request.Queries
Controls := request.Controls
queryIDs := request.Queries
controlIDs := request.Controls

complianceURL := strings.ReplaceAll(h.cfg.Compliance.BaseURL, "%NAMESPACE%", h.cfg.OpengovernanceNamespace)
complianceClient := complianceClient.NewComplianceClient(complianceURL)
Expand All @@ -318,8 +316,8 @@ func (h HttpHandler) ListQueryParameters(ctx echo.Context) error {
}

var filteredQueryParams []string
if Controls !=nil {
all_control, err := complianceClient.ListControl(clientCtx, Controls,nil)
if controlIDs != nil {
all_control, err := complianceClient.ListControl(clientCtx, controlIDs, nil)
if err != nil {
h.logger.Error("error getting control", zap.Error(err))
return echo.NewHTTPError(http.StatusInternalServerError, "error getting control")
Expand All @@ -331,23 +329,20 @@ func (h HttpHandler) ListQueryParameters(ctx echo.Context) error {
for _, param := range control.Query.Parameters {
filteredQueryParams = append(filteredQueryParams, param.Key)
}
}
} else if Queries != nil {
}
} else if queryIDs != nil {
// TODO: Fix this part and write new client on inventory
for _, query := range Queries {
query, err := inventoryClient.GetQuery(clientCtx, query)
queries, err := inventoryClient.ListQueriesV2(clientCtx, &inventoryApi.ListQueryV2Request{QueryIDs: queryIDs})
if err != nil {
h.logger.Error("error getting query", zap.Error(err))
return echo.NewHTTPError(http.StatusInternalServerError, "error getting query")
}
if query == nil {
return echo.NewHTTPError(http.StatusNotFound, "query not found")
}
for _, param := range query.Query.Parameters {
filteredQueryParams = append(filteredQueryParams, param.Key)
for _, q := range queries.Items {
for _, param := range q.Query.Parameters {
filteredQueryParams = append(filteredQueryParams, param.Key)
}
}
}
}

var queryParams []models.QueryParameterValues
if len(filteredQueryParams) > 0 {
Expand Down Expand Up @@ -421,8 +416,7 @@ func (h HttpHandler) ListQueryParameters(ctx echo.Context) error {
func (h HttpHandler) GetQueryParameter(ctx echo.Context) error {
key := ctx.Param("key")
clientCtx := &httpclient.Context{UserRole: api3.AdminRole}



complianceURL := strings.ReplaceAll(h.cfg.Compliance.BaseURL, "%NAMESPACE%", h.cfg.OpengovernanceNamespace)
complianceClient := complianceClient.NewComplianceClient(complianceURL)
inventoryURL := strings.ReplaceAll(h.cfg.Inventory.BaseURL, "%NAMESPACE%", h.cfg.OpengovernanceNamespace)
Expand All @@ -438,20 +432,20 @@ func (h HttpHandler) GetQueryParameter(ctx echo.Context) error {
h.logger.Error("error listing queries", zap.Error(err))
return echo.NewHTTPError(http.StatusInternalServerError, "error listing queries")
}

queryParam, err := h.db.GetQueryParameter(key)
if err != nil {
h.logger.Error("error getting query parameters", zap.Error(err))
return err
}
if err != nil {
h.logger.Error("error getting query parameters", zap.Error(err))
return err
}
var controlsList []complianceapi.Control
var queriesList []inventoryApi.NamedQueryItemV2
for _, c := range controls {
for _, p := range c.Query.Parameters {
if p.Key == key {
controlsList = append(controlsList, c)
}

}
}
for _, q := range namedQueries.Items {
Expand All @@ -462,14 +456,13 @@ func (h HttpHandler) GetQueryParameter(ctx echo.Context) error {
}
}
return ctx.JSON(http.StatusOK, api.GetQueryParamDetailsResponse{
Key: key,
Value: queryParam.Value,
Key: key,
Value: queryParam.Value,
Controls: controlsList,
Queries: queriesList,
Queries: queriesList,
})
}


// PurgeSampleData godoc
//
// @Summary List all workspaces with owner id
Expand Down

0 comments on commit d9a4ec6

Please sign in to comment.