Skip to content

Commit

Permalink
✨ Allow loading asset by mrn (#4485)
Browse files Browse the repository at this point in the history
  • Loading branch information
jaym authored Aug 5, 2024
1 parent ae90ce7 commit d7f5fc4
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 1 deletion.
2 changes: 1 addition & 1 deletion providers/mondoo/resources/mondoo.lr.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

64 changes: 64 additions & 0 deletions providers/mondoo/resources/mondoo_assets.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,78 @@ import (

"go.mondoo.com/cnquery/v11/explorer/resources"
"go.mondoo.com/cnquery/v11/llx"
"go.mondoo.com/cnquery/v11/providers-sdk/v1/plugin"
"go.mondoo.com/cnquery/v11/providers/mondoo/connection"
"go.mondoo.com/cnquery/v11/types"
"go.mondoo.com/cnquery/v11/utils/multierr"
mondoogql "go.mondoo.com/mondoo-go"
)

func (m *mqlMondooAsset) id() (string, error) {
return m.Mrn.Data, nil
}

type gqlAsset struct {
ID string
Mrn string
State string
Name string
AssetType string `graphql:"asset_type"`
UpdatedAt *string
// Annotations map[string]string
Annotations []keyValue
Labels []keyValue
Score struct {
Grade string
Value int
}
}

func initMondooAsset(runtime *plugin.Runtime, args map[string]*llx.RawData) (map[string]*llx.RawData, plugin.Resource, error) {
if len(args) != 1 {
return args, nil, nil
}

// if asset is only initialized with a mrn, we will try to look it up
rawMrn, mrnOk := args["mrn"]
if !mrnOk {
return args, nil, nil
}

// fetch the asset from mondoo
conn := runtime.Connection.(*connection.Connection)
asset, err := fetchAssetByMrn(context.Background(), conn, rawMrn.Value.(string))
if err != nil {
return nil, nil, err
}

args["name"] = llx.StringData(asset.Name)
args["mrn"] = llx.StringData(asset.Mrn)
args["platform"] = llx.StringData(asset.AssetType)
args["annotations"] = llx.MapData(keyvals2map(asset.Annotations), types.Map(types.String, types.String))
args["labels"] = llx.MapData(keyvals2map(asset.Labels), types.Map(types.String, types.String))
args["updatedAt"] = llx.TimeDataPtr(string2time(asset.UpdatedAt))
args["scoreGrade"] = llx.StringData(asset.Score.Grade)
args["scoreValue"] = llx.IntData(asset.Score.Value)

return args, nil, nil
}

func fetchAssetByMrn(ctx context.Context, conn *connection.Connection, mrn string) (*gqlAsset, error) {
var q struct {
Asset gqlAsset `graphql:"asset(mrn: $mrn)"`
}
vars := map[string]any{
"mrn": mondoogql.String(mrn),
}

if err := conn.Client.Query(ctx, &q, vars); err != nil {
return nil, err
}

return &q.Asset, nil
}

func (m *mqlMondooAsset) resources() ([]any, error) {
conn := m.MqlRuntime.Connection.(*connection.Connection)
upstream := conn.Upstream
Expand Down

0 comments on commit d7f5fc4

Please sign in to comment.