Skip to content

Commit

Permalink
feat: add rego cloudql function
Browse files Browse the repository at this point in the history
  • Loading branch information
Mahanmmi committed Jan 21, 2025
1 parent 5fbb00a commit d456c38
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions services/rego/service/rego.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,47 @@ func (r *RegoEngine) getRegoFunctionForTables(ctx context.Context) ([]func(*rego
results = append(results, f)
}

// Add raw query function
f := rego.FunctionDyn(&rego.Function{
Name: "opencomply.cloudql",
Description: "",
Decl: types.NewFunction([]types.Type{types.S}, types.NewArray(nil, types.A)),
Memoize: true,
Nondeterministic: true,
}, func(bctx rego.BuiltinContext, terms []*ast.Term) (*ast.Term, error) {
var query string
if len(terms) == 0 {
return nil, fmt.Errorf("invalid query")
}
query = terms[0].Value.String()

rows, err := r.steampipe.Conn().Query(bctx.Context, query)
if err != nil {
r.logger.Error("Unable to query database", zap.Error(err))
r.logger.Sync()
return nil, err
}
defer rows.Close()

results, err := pgx.CollectRows(rows, pgx.RowToMap)
if err != nil {
r.logger.Error("Unable to scan row", zap.Error(err))
r.logger.Sync()
return nil, err
}

value, err := ast.InterfaceToValue(results)
if err != nil {
r.logger.Error("Unable to convert to value", zap.Error(err))
r.logger.Sync()
return nil, err
}

return ast.NewTerm(value), nil
})

results = append(results, f)

return results, nil
}

Expand Down

0 comments on commit d456c38

Please sign in to comment.