Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
2c3f76e
Add dynamic templates as parameter
mrodm Dec 12, 2024
41d7b88
Add validation for each dynamic template depending on the parameters
mrodm Dec 16, 2024
ff5298e
Fixes for dynamic templates
mrodm Dec 16, 2024
af04d66
Compare fields with dynamic templates
mrodm Dec 17, 2024
f21c712
Remove multi_fields from flattened fields
mrodm Dec 17, 2024
aa28db6
Test without filtering dynamic templates
mrodm Dec 17, 2024
3767111
Ensure properties subfields are validated accordingly
mrodm Dec 18, 2024
9fb80b4
Restore filtering and add tests
mrodm Dec 18, 2024
2ad28ac
Disable unmatch_mapping_type and match_mapping_type and continue look…
mrodm Dec 19, 2024
8bdad65
Merge upstream/main into validate-dynamic-mappings
mrodm Dec 19, 2024
151e59e
Refactors and remove log statements
mrodm Dec 19, 2024
d0ff6b9
Fix function naming
mrodm Dec 19, 2024
b474b4b
Add test with match_pattern regex
mrodm Dec 19, 2024
066b5a0
Add comment in tests
mrodm Dec 19, 2024
a7eb191
Remove loading schema from create validator for mappings method
mrodm Jan 7, 2025
7666ac2
Separate parsing and validation of dynamic templates
mrodm Jan 7, 2025
a718796
Support match_pattern for the other settings
mrodm Jan 7, 2025
0a5e775
fix test
mrodm Jan 8, 2025
131cc9c
Update tests for multi-fields
mrodm Jan 8, 2025
38db8df
Review multi-fields logic
mrodm Jan 8, 2025
9ff3d0c
Revisit multi-fields logic in ECS
mrodm Jan 8, 2025
5165612
Report all errors related to multi-fields comparing with ECS
mrodm Jan 8, 2025
ce557c9
Ignored validation of multi-fields with ECS
mrodm Jan 8, 2025
ebda859
Fix multi-field test
mrodm Jan 8, 2025
019ffd4
Rephrase errors
mrodm Jan 9, 2025
8f36c18
Validate fully dynamic objects (preview) with dynamic templates
mrodm Jan 9, 2025
d96ffbf
Rephrase debug message
mrodm Jan 9, 2025
dbca4fe
Add logging - to be removed
mrodm Jan 9, 2025
8fce0ec
Merge remote-tracking branch 'upstream/main' into validate-dynamic-ma…
mrodm Jan 14, 2025
c333707
Revisited log messages
mrodm Jan 20, 2025
0569cfb
Remove comments
mrodm Jan 20, 2025
1aea303
Remove more debug statements
mrodm Jan 20, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Remove loading schema from create validator for mappings method
  • Loading branch information
mrodm committed Jan 7, 2025
commit a7eb191bdddda72817af9c833ff40ccbb662320f
92 changes: 3 additions & 89 deletions internal/fields/mappings.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,11 @@ package fields
import (
"context"
"encoding/json"
"errors"
"fmt"
"path/filepath"
"regexp"
"slices"
"strings"

"github.com/Masterminds/semver/v3"
"github.com/google/go-cmp/cmp"

"github.com/elastic/elastic-package/internal/elasticsearch"
Expand All @@ -27,17 +24,6 @@ type MappingValidator struct {
// Schema contains definition records.
Schema []FieldDefinition

// SpecVersion contains the version of the spec used by the package.
specVersion semver.Version

disabledDependencyManagement bool

enabledImportAllECSSchema bool

disabledNormalization bool

injectFieldsOptions InjectFieldsOptions

esClient *elasticsearch.Client

indexTemplateName string
Expand All @@ -50,50 +36,6 @@ type MappingValidator struct {
// MappingValidatorOption represents an optional flag that can be passed to CreateValidatorForMappings.
type MappingValidatorOption func(*MappingValidator) error

// WithMappingValidatorSpecVersion enables validation dependant of the spec version used by the package.
func WithMappingValidatorSpecVersion(version string) MappingValidatorOption {
return func(v *MappingValidator) error {
sv, err := semver.NewVersion(version)
if err != nil {
return fmt.Errorf("invalid version %q: %v", version, err)
}
v.specVersion = *sv
return nil
}
}

// WithMappingValidatorDisabledDependencyManagement configures the validator to ignore external fields and won't follow dependencies.
func WithMappingValidatorDisabledDependencyManagement() MappingValidatorOption {
return func(v *MappingValidator) error {
v.disabledDependencyManagement = true
return nil
}
}

// WithMappingValidatorEnabledImportAllECSSchema configures the validator to check or not the fields with the complete ECS schema.
func WithMappingValidatorEnabledImportAllECSSChema(importSchema bool) MappingValidatorOption {
return func(v *MappingValidator) error {
v.enabledImportAllECSSchema = importSchema
return nil
}
}

// WithMappingValidatorDisableNormalization configures the validator to disable normalization.
func WithMappingValidatorDisableNormalization(disabledNormalization bool) MappingValidatorOption {
return func(v *MappingValidator) error {
v.disabledNormalization = disabledNormalization
return nil
}
}

// WithMappingValidatorInjectFieldsOptions configures fields injection.
func WithMappingValidatorInjectFieldsOptions(options InjectFieldsOptions) MappingValidatorOption {
return func(v *MappingValidator) error {
v.injectFieldsOptions = options
return nil
}
}

// WithMappingValidatorElasticsearchClient configures the Elasticsearch client.
func WithMappingValidatorElasticsearchClient(esClient *elasticsearch.Client) MappingValidatorOption {
return func(v *MappingValidator) error {
Expand Down Expand Up @@ -135,47 +77,19 @@ func WithMappingValidatorExceptionFields(fields []string) MappingValidatorOption
}

// CreateValidatorForMappings function creates a validator for the mappings.
func CreateValidatorForMappings(fieldsParentDir string, esClient *elasticsearch.Client, opts ...MappingValidatorOption) (v *MappingValidator, err error) {
p := packageRoot{}
func CreateValidatorForMappings(esClient *elasticsearch.Client, opts ...MappingValidatorOption) (v *MappingValidator, err error) {
opts = append(opts, WithMappingValidatorElasticsearchClient(esClient))
return createValidatorForMappingsAndPackageRoot(fieldsParentDir, p, opts...)
return createValidatorForMappingsAndPackageRoot(opts...)
}

func createValidatorForMappingsAndPackageRoot(fieldsParentDir string, finder packageRootFinder, opts ...MappingValidatorOption) (v *MappingValidator, err error) {
func createValidatorForMappingsAndPackageRoot(opts ...MappingValidatorOption) (v *MappingValidator, err error) {
v = new(MappingValidator)
for _, opt := range opts {
if err := opt(v); err != nil {
return nil, err
}
}

if len(v.Schema) > 0 {
return v, nil
}

// TODO: Should we remove this code to load external and local fields?
fieldsDir := filepath.Join(fieldsParentDir, "fields")

var fdm *DependencyManager
if !v.disabledDependencyManagement {
packageRoot, found, err := finder.FindPackageRoot()
if err != nil {
return nil, fmt.Errorf("can't find package root: %w", err)
}
if !found {
return nil, errors.New("package root not found and dependency management is enabled")
}
fdm, v.Schema, err = initDependencyManagement(packageRoot, v.specVersion, v.enabledImportAllECSSchema)
if err != nil {
return nil, fmt.Errorf("failed to initialize dependency management: %w", err)
}
}
fields, err := loadFieldsFromDir(fieldsDir, fdm, v.injectFieldsOptions)
if err != nil {
return nil, fmt.Errorf("can't load fields from directory (path: %s): %w", fieldsDir, err)
}

v.Schema = append(fields, v.Schema...)
return v, nil
}

Expand Down
14 changes: 1 addition & 13 deletions internal/fields/mappings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,12 @@ import (
)

func TestComparingMappings(t *testing.T) {
defaultSpecVersion := "3.3.0"
cases := []struct {
title string
preview map[string]any
actual map[string]any
schema []FieldDefinition
dynamicTemplates []map[string]any
spec string
exceptionFields []string
expectedErrors []string
}{
Expand Down Expand Up @@ -517,7 +515,6 @@ func TestComparingMappings(t *testing.T) {
},
},
exceptionFields: []string{"access.field"},
spec: "1.0.0",
expectedErrors: []string{
`field "error.field" is undefined: missing definition for path`,
// should status.field return error ? or should it be ignored?
Expand Down Expand Up @@ -679,7 +676,6 @@ func TestComparingMappings(t *testing.T) {
},
// foo is added to the exception list because it is type nested
exceptionFields: []string{"foo"},
spec: "3.0.0",
schema: []FieldDefinition{},
expectedErrors: []string{},
},
Expand All @@ -701,7 +697,6 @@ func TestComparingMappings(t *testing.T) {
},
},
exceptionFields: []string{},
spec: "3.0.1",
schema: []FieldDefinition{},
expectedErrors: []string{
`not found properties in preview mappings for path: "foo"`,
Expand Down Expand Up @@ -790,7 +785,6 @@ func TestComparingMappings(t *testing.T) {
},
},
exceptionFields: []string{},
spec: "3.0.0",
schema: []FieldDefinition{},
expectedErrors: []string{
// Should it be considered this error in "foa" "missing time_series_metric bar"?
Expand All @@ -803,14 +797,8 @@ func TestComparingMappings(t *testing.T) {
for _, c := range cases {
t.Run(c.title, func(t *testing.T) {
logger.EnableDebugMode()
specVersion := defaultSpecVersion
if c.spec != "" {
specVersion = c.spec
}
v, err := CreateValidatorForMappings("", nil,
WithMappingValidatorSpecVersion(specVersion),
v, err := CreateValidatorForMappings(nil,
WithMappingValidatorFallbackSchema(c.schema),
WithMappingValidatorDisabledDependencyManagement(),
WithMappingValidatorExceptionFields(c.exceptionFields),
)
require.NoError(t, err)
Expand Down
4 changes: 1 addition & 3 deletions internal/testrunner/runners/system/tester.go
Original file line number Diff line number Diff line change
Expand Up @@ -1528,12 +1528,10 @@ func (r *tester) validateTestScenario(ctx context.Context, result *testrunner.Re
logger.Warn("Validate mappings found (technical preview)")
exceptionFields := listExceptionFields(scenario.docs, fieldsValidator)

mappingsValidator, err := fields.CreateValidatorForMappings(r.dataStreamPath, r.esClient,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As the process to load the fields from the package and ECS has been removed, these parameters are not needed anymore.

The schema to be used to validate fields with ECS is the one set (optionally) via the method WithMappingValidatorFallbackSchema

mappingsValidator, err := fields.CreateValidatorForMappings(r.esClient,
fields.WithMappingValidatorFallbackSchema(fieldsValidator.Schema),
fields.WithMappingValidatorIndexTemplate(scenario.indexTemplateName),
fields.WithMappingValidatorDataStream(scenario.dataStream),
fields.WithMappingValidatorSpecVersion(r.pkgManifest.SpecVersion),
fields.WithMappingValidatorEnabledImportAllECSSChema(true),
fields.WithMappingValidatorExceptionFields(exceptionFields),
)
if err != nil {
Expand Down