Skip to content

Commit

Permalink
have YAMLLinter use JMESPath to find resourceID
Browse files Browse the repository at this point in the history
  • Loading branch information
Larry Hitchon committed Mar 31, 2018
1 parent 562913d commit 833a967
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
11 changes: 10 additions & 1 deletion cli/yaml-linter.go
Expand Up @@ -15,6 +15,15 @@ type YAMLResourceLoader struct {
Resources []assertion.ResourceConfig
}

func extractResourceID(expression string, properties interface{}) string {
resourceID := "None"
result, err := assertion.SearchData(expression, properties)
if err == nil {
resourceID, _ = result.(string)
}
return resourceID
}

// Load converts a text file into a collection of Resource objects
func (l YAMLResourceLoader) Load(filename string) ([]assertion.Resource, error) {
resources := make([]assertion.Resource, 0)
Expand All @@ -32,7 +41,7 @@ func (l YAMLResourceLoader) Load(filename string) ([]assertion.Resource, error)
for _, element := range sliceOfProperties {
properties := element.(map[string]interface{})
resource := assertion.Resource{
ID: properties[resourceConfig.ID].(string), // FIXME should use JMESPath to ID, and make sure it handles numbers
ID: extractResourceID(resourceConfig.ID, properties),
Type: resourceConfig.Type,
Properties: properties,
Filename: filename,
Expand Down
12 changes: 9 additions & 3 deletions example-files/config/generic.config
Expand Up @@ -19,16 +19,22 @@ gadgets:

other_stuff:
contraptions:
- serial_number: S1000
- ids:
serial_number: S1000
sku: S1234
size: 10
locations:
- city: Seattle
- city: San Francisco
- serial_number: S2000
- ids:
serial_number: S2000
sku: S5678
size: 20
locations:
- city: New York
- serial_number: S3000
- ids:
serial_number: S3000
sku: S0101
size: 4000
locations:
- city: Paris
Expand Down
2 changes: 1 addition & 1 deletion example-files/rules/generic.yml
Expand Up @@ -16,7 +16,7 @@ resources:
id: name
- type: contraption
key: other_stuff.contraptions[]
id: serial_number
id: ids.serial_number

rules:

Expand Down

0 comments on commit 833a967

Please sign in to comment.