diff --git a/services/inventory/api/dto.go b/services/inventory/api/dto.go index 4c5dfdde6..360268f16 100644 --- a/services/inventory/api/dto.go +++ b/services/inventory/api/dto.go @@ -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"` diff --git a/services/inventory/db.go b/services/inventory/db.go index da702f984..22d9e4a74 100644 --- a/services/inventory/db.go +++ b/services/inventory/db.go @@ -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+"%") } diff --git a/services/inventory/http_routes.go b/services/inventory/http_routes.go index cd6df828d..717c56227 100644 --- a/services/inventory/http_routes.go +++ b/services/inventory/http_routes.go @@ -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) diff --git a/services/metadata/client/metadata.go b/services/metadata/client/metadata.go index 0a0041cdf..83093feb1 100644 --- a/services/metadata/client/metadata.go +++ b/services/metadata/client/metadata.go @@ -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()) } diff --git a/services/metadata/http_routes.go b/services/metadata/http_routes.go index cadef91b4..cab7fae19 100644 --- a/services/metadata/http_routes.go +++ b/services/metadata/http_routes.go @@ -21,6 +21,7 @@ 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" @@ -28,7 +29,6 @@ import ( 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" @@ -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)) @@ -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) @@ -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") @@ -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 { @@ -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) @@ -438,12 +432,12 @@ 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 { @@ -451,7 +445,7 @@ func (h HttpHandler) GetQueryParameter(ctx echo.Context) error { if p.Key == key { controlsList = append(controlsList, c) } - + } } for _, q := range namedQueries.Items { @@ -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