Skip to content

Commit

Permalink
🧹 Add function to load the asset url schema for all known providers (#…
Browse files Browse the repository at this point in the history
…3633)

* 🧹 Add function to load the asset url schema for all known providers

* make it safe to create schema multiple times
  • Loading branch information
jaym authored Apr 2, 2024
1 parent 48f7f43 commit 316689b
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
26 changes: 25 additions & 1 deletion providers-sdk/v1/inventory/asset_url.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,10 +149,34 @@ func (a *AssetUrlSchema) Add(branch *AssetUrlBranch) error {
}

branch.setDepth(found.Depth + 1)
found.Values[lastKey] = branch
found.Values[lastKey] = copyBranch(branch)
return nil
}

func copyBranch(b *AssetUrlBranch) *AssetUrlBranch {
if b == nil {
return nil
}

res := &AssetUrlBranch{
PathSegments: make([]string, len(b.PathSegments)),
Key: b.Key,
Values: make(map[string]*AssetUrlBranch, len(b.Values)),
Title: b.Title,
References: make([]string, len(b.References)),
Depth: b.Depth,
}

copy(res.PathSegments, b.PathSegments)
copy(res.References, b.References)

for k, v := range b.Values {
res.Values[k] = copyBranch(v)
}

return res
}

func (a *AssetUrlBranch) setDepth(i uint32) {
a.Depth = i
next := i + 1
Expand Down
27 changes: 27 additions & 0 deletions providers/providers.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/spf13/afero"
"github.com/ulikunitz/xz"
"go.mondoo.com/cnquery/v10/cli/config"
"go.mondoo.com/cnquery/v10/providers-sdk/v1/inventory"
"go.mondoo.com/cnquery/v10/providers-sdk/v1/plugin"
"go.mondoo.com/cnquery/v10/providers-sdk/v1/resources"
"go.mondoo.com/cnquery/v10/providers/core/resources/versions/semver"
Expand Down Expand Up @@ -942,3 +943,29 @@ func convertToFields(keysAndValues ...interface{}) map[string]interface{} {
}
return fields
}

func LoadAssetUrlSchema() (*inventory.AssetUrlSchema, error) {
providers, err := ListAll()
if err != nil {
return nil, err
}

s, err := inventory.NewAssetUrlSchema("technology")
if err != nil {
return nil, err
}

for _, provider := range providers {
for _, b := range provider.AssetUrlTrees {
if err := s.Add(b); err != nil {
return nil, err
}
}
}

if err := s.RefreshCache(); err != nil {
return nil, err
}

return s, nil
}

0 comments on commit 316689b

Please sign in to comment.