Skip to content

Commit

Permalink
Merge pull request #2292 from opengovern/fix-tasks
Browse files Browse the repository at this point in the history
Fix tasks
  • Loading branch information
artaasadi authored Dec 20, 2024
2 parents 4abc484 + 673723c commit b673f6b
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 25 deletions.
34 changes: 23 additions & 11 deletions pkg/cloudql/client/artifact_vulnerabilities.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,26 @@ type VulnerabilityFix struct {
State string `json:"state"`
}

type ArtifactVulnerabilitiesTaskResult struct {
PlatformID string `json:"platform_id"`
ResourceID string `json:"resource_id"`
ResourceName string `json:"resource_name"`
Description OciArtifactVulnerabilities `json:"description"`
TaskType string `json:"task_type"`
ResultType string `json:"result_type"`
Metadata map[string]string `json:"metadata"`
DescribedBy string `json:"described_by"`
DescribedAt int64 `json:"described_at"`
}

type OciArtifactVulnerabilitiesHit struct {
ID string `json:"_id"`
Score float64 `json:"_score"`
Index string `json:"_index"`
Type string `json:"_type"`
Version int64 `json:"_version,omitempty"`
Source OciArtifactVulnerabilities `json:"_source"`
Sort []any `json:"sort"`
ID string `json:"_id"`
Score float64 `json:"_score"`
Index string `json:"_index"`
Type string `json:"_type"`
Version int64 `json:"_version,omitempty"`
Source ArtifactVulnerabilitiesTaskResult `json:"_source"`
Sort []any `json:"sort"`
}

type OciArtifactVulnerabilitiesHits struct {
Expand Down Expand Up @@ -103,14 +115,14 @@ func (p OciArtifactVulnerabilitiesPaginator) Close(ctx context.Context) error {
return p.paginator.Deallocate(ctx)
}

func (p OciArtifactVulnerabilitiesPaginator) NextPage(ctx context.Context) ([]OciArtifactVulnerabilities, error) {
func (p OciArtifactVulnerabilitiesPaginator) NextPage(ctx context.Context) ([]ArtifactVulnerabilitiesTaskResult, error) {
var response OciArtifactVulnerabilitiesSearchResponse
err := p.paginator.SearchWithLog(ctx, &response, true)
if err != nil {
return nil, err
}

var values []OciArtifactVulnerabilities
var values []ArtifactVulnerabilitiesTaskResult
for _, hit := range response.Hits.Hits {
values = append(values, hit.Source)
}
Expand All @@ -126,8 +138,8 @@ func (p OciArtifactVulnerabilitiesPaginator) NextPage(ctx context.Context) ([]Oc
}

var artifactVulnerabilitiesMapping = map[string]string{
"image_url": "imageUrl",
"artifact_digest": "artifactDigest",
"image_url": "Description.imageUrl",
"artifact_digest": "Description.artifactDigest",
}

func ListArtifactVulnerabilities(ctx context.Context, d *plugin.QueryData, _ *plugin.HydrateData) (any, error) {
Expand Down
6 changes: 3 additions & 3 deletions pkg/cloudql/tables/table_platform_artifact_vulnerabilities.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,17 @@ func tablePlatformArtifactVulnerabilities(_ context.Context) *plugin.Table {
Columns: []*plugin.Column{
{
Name: "image_url",
Transform: transform.FromField("imageUrl"),
Transform: transform.FromField("Description.imageUrl"),
Type: proto.ColumnType_STRING,
},
{
Name: "artifact_digest",
Transform: transform.FromField("artifactDigest"),
Transform: transform.FromField("Description.artifactDigest"),
Type: proto.ColumnType_STRING,
},
{
Name: "vulnerabilities",
Transform: transform.FromField("Vulnerabilities"),
Transform: transform.FromField("Description.Vulnerabilities"),
Type: proto.ColumnType_JSON,
},
},
Expand Down
10 changes: 7 additions & 3 deletions services/describe/api/jobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,10 +179,14 @@ type RunBenchmarkResponse struct {
Jobs []RunBenchmarkItem `json:"jobs"`
}

type ResourceTypeRunDiscoveryRequest struct {
ResourceType string `json:"resource_type"`
Parameters map[string][]string `json:"parameters"`
}

type RunDiscoveryRequest struct {
ResourceTypes []string `json:"resource_types"`
IntegrationInfo []IntegrationInfoFilter `json:"integration_info"`
Parameters map[string][]string `json:"parameters"`
ResourceTypes []ResourceTypeRunDiscoveryRequest `json:"resource_types"`
IntegrationInfo []IntegrationInfoFilter `json:"integration_info"`
}

type RunDiscoveryJob struct {
Expand Down
24 changes: 16 additions & 8 deletions services/describe/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -1502,6 +1502,12 @@ func (h HttpServer) RunDiscovery(ctx echo.Context) error {
continue
}
rtToDescribe := request.ResourceTypes

var resourceTypesNames []string
for _, rt := range rtToDescribe {
resourceTypesNames = append(resourceTypesNames, rt.ResourceType)
}

discoveryType := model2.DiscoveryType_Fast
integrationDiscovery := &model2.IntegrationDiscovery{
TriggerID: uint(triggerId),
Expand All @@ -1510,7 +1516,7 @@ func (h HttpServer) RunDiscovery(ctx echo.Context) error {
TriggerType: enums.DescribeTriggerTypeManual,
TriggeredBy: userID,
DiscoveryType: discoveryType,
ResourceTypes: rtToDescribe,
ResourceTypes: resourceTypesNames,
}
err = h.DB.CreateIntegrationDiscovery(integrationDiscovery)
if err != nil {
Expand All @@ -1528,9 +1534,11 @@ func (h HttpServer) RunDiscovery(ctx echo.Context) error {
h.Scheduler.logger.Error("failed to get resource types by labels", zap.Error(err))
return echo.NewHTTPError(http.StatusInternalServerError, "failed to get resource types by labels")
}
var possibleRt []string
var possibleRt []api.ResourceTypeRunDiscoveryRequest
for rt, _ := range possibleRtMap {
possibleRt = append(possibleRt, rt)
possibleRt = append(possibleRt, api.ResourceTypeRunDiscoveryRequest{
ResourceType: rt,
})
}
if len(rtToDescribe) == 0 {
rtToDescribe = possibleRt
Expand All @@ -1539,20 +1547,20 @@ func (h HttpServer) RunDiscovery(ctx echo.Context) error {
for _, resourceType := range rtToDescribe {
isOK := false
for _, rt := range possibleRt {
if rt == resourceType {
if rt.ResourceType == resourceType.ResourceType {
isOK = true
}
}
if !isOK {
continue
}
var status, failureReason string
job, err := h.Scheduler.describe(integration, resourceType, false, false, false, &integrationDiscovery.ID, userID, request.Parameters)
job, err := h.Scheduler.describe(integration, resourceType.ResourceType, false, false, false, &integrationDiscovery.ID, userID, resourceType.Parameters)
if err != nil {
if err.Error() == "job already in progress" {
tmpJob, err := h.Scheduler.db.GetLastDescribeIntegrationJob(integration.IntegrationID, resourceType)
tmpJob, err := h.Scheduler.db.GetLastDescribeIntegrationJob(integration.IntegrationID, resourceType.ResourceType)
if err != nil {
h.Scheduler.logger.Error("failed to get last describe job", zap.String("resource_type", resourceType), zap.String("connection_id", integration.IntegrationID), zap.Error(err))
h.Scheduler.logger.Error("failed to get last describe job", zap.String("resource_type", resourceType.ResourceType), zap.String("connection_id", integration.IntegrationID), zap.Error(err))
}
h.Scheduler.logger.Error("failed to describe connection", zap.String("integration_id", integration.IntegrationID), zap.Error(err))
status = "FAILED"
Expand All @@ -1574,7 +1582,7 @@ func (h HttpServer) RunDiscovery(ctx echo.Context) error {
}
jobs = append(jobs, api.RunDiscoveryJob{
JobId: jobId,
ResourceType: resourceType,
ResourceType: resourceType.ResourceType,
Status: status,
FailureReason: failureReason,
IntegrationInfo: api.IntegrationInfo{
Expand Down

0 comments on commit b673f6b

Please sign in to comment.