Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 21 additions & 26 deletions internal/elasticsearch/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,31 +324,31 @@ func (client *Client) redHealthCause(ctx context.Context) (string, error) {
return strings.Join(causes, ", "), nil
}

func (c *Client) SimulateIndexTemplate(ctx context.Context, indexTemplateName string) (json.RawMessage, json.RawMessage, error) {
type Mappings struct {
Properties json.RawMessage `json:"properties"`
DynamicTemplates json.RawMessage `json:"dynamic_templates"`
}

func (c *Client) SimulateIndexTemplate(ctx context.Context, indexTemplateName string) (*Mappings, error) {
resp, err := c.Indices.SimulateTemplate(
c.Indices.SimulateTemplate.WithContext(ctx),
c.Indices.SimulateTemplate.WithName(indexTemplateName),
)
if err != nil {
return nil, nil, fmt.Errorf("failed to get field mapping for data stream %q: %w", indexTemplateName, err)
return nil, fmt.Errorf("failed to get field mapping for data stream %q: %w", indexTemplateName, err)
}
defer resp.Body.Close()
if resp.IsError() {
return nil, nil, fmt.Errorf("error getting mapping: %s", resp)
return nil, fmt.Errorf("error getting mapping: %s", resp)
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, nil, fmt.Errorf("error reading mapping body: %w", err)
}

type mappingsIndexTemplate struct {
DynamicTemplates json.RawMessage `json:"dynamic_templates"`
Properties json.RawMessage `json:"properties"`
return nil, fmt.Errorf("error reading mapping body: %w", err)
}

type indexTemplateSimulated struct {
// Settings json.RawMessage `json:"settings"`
Mappings mappingsIndexTemplate `json:"mappings"`
Mappings Mappings `json:"mappings"`
}

type previewTemplate struct {
Expand All @@ -358,7 +358,7 @@ func (c *Client) SimulateIndexTemplate(ctx context.Context, indexTemplateName st
var preview previewTemplate

if err := json.Unmarshal(body, &preview); err != nil {
return nil, nil, fmt.Errorf("error unmarshaling mappings: %w", err)
return nil, fmt.Errorf("error unmarshaling mappings: %w", err)
}

// In case there are no dynamic templates, set an empty array
Expand All @@ -371,44 +371,39 @@ func (c *Client) SimulateIndexTemplate(ctx context.Context, indexTemplateName st
preview.Template.Mappings.Properties = []byte("{}")
}

return preview.Template.Mappings.DynamicTemplates, preview.Template.Mappings.Properties, nil
return &preview.Template.Mappings, nil
}

func (c *Client) DataStreamMappings(ctx context.Context, dataStreamName string) (json.RawMessage, json.RawMessage, error) {
func (c *Client) DataStreamMappings(ctx context.Context, dataStreamName string) (*Mappings, error) {
mappingResp, err := c.Indices.GetMapping(
c.Indices.GetMapping.WithContext(ctx),
c.Indices.GetMapping.WithIndex(dataStreamName),
)
if err != nil {
return nil, nil, fmt.Errorf("failed to get field mapping for data stream %q: %w", dataStreamName, err)
return nil, fmt.Errorf("failed to get field mapping for data stream %q: %w", dataStreamName, err)
}
defer mappingResp.Body.Close()
if mappingResp.IsError() {
return nil, nil, fmt.Errorf("error getting mapping: %s", mappingResp)
return nil, fmt.Errorf("error getting mapping: %s", mappingResp)
}
body, err := io.ReadAll(mappingResp.Body)
if err != nil {
return nil, nil, fmt.Errorf("error reading mapping body: %w", err)
}

type mappings struct {
DynamicTemplates json.RawMessage `json:"dynamic_templates"`
Properties json.RawMessage `json:"properties"`
return nil, fmt.Errorf("error reading mapping body: %w", err)
}

mappingsRaw := map[string]struct {
Mappings mappings `json:"mappings"`
Mappings Mappings `json:"mappings"`
}{}

if err := json.Unmarshal(body, &mappingsRaw); err != nil {
return nil, nil, fmt.Errorf("error unmarshaling mappings: %w", err)
return nil, fmt.Errorf("error unmarshaling mappings: %w", err)
}

if len(mappingsRaw) != 1 {
return nil, nil, fmt.Errorf("exactly 1 mapping was expected, got %d", len(mappingsRaw))
return nil, fmt.Errorf("exactly 1 mapping was expected, got %d", len(mappingsRaw))
}

var mappingsDefinition mappings
var mappingsDefinition Mappings
for _, v := range mappingsRaw {
mappingsDefinition = v.Mappings
}
Expand All @@ -423,5 +418,5 @@ func (c *Client) DataStreamMappings(ctx context.Context, dataStreamName string)
mappingsDefinition.Properties = []byte("{}")
}

return mappingsDefinition.DynamicTemplates, mappingsDefinition.Properties, nil
return &mappingsDefinition, nil
}
1 change: 0 additions & 1 deletion internal/fields/dynamic_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ func (d *dynamicTemplate) Matches(currentPath string, definition map[string]any)
}

if len(d.pathMatch) > 0 {
// logger.Debugf("path_match -> Comparing %s to %q", strings.Join(d.pathMatch, ";"), currentPath)
matches, err := stringMatchesPatterns(d.pathMatch, currentPath, fullRegex)
if err != nil {
return false, fmt.Errorf("failed to parse dynamic template %s: %w", d.name, err)
Expand Down
14 changes: 7 additions & 7 deletions internal/fields/mappings.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,14 @@ func createValidatorForMappingsAndPackageRoot(opts ...MappingValidatorOption) (v
func (v *MappingValidator) ValidateIndexMappings(ctx context.Context) multierror.Error {
var errs multierror.Error
logger.Debugf("Get Mappings from data stream (%s)", v.dataStreamName)
actualDynamicTemplates, actualMappings, err := v.esClient.DataStreamMappings(ctx, v.dataStreamName)
actualMappings, err := v.esClient.DataStreamMappings(ctx, v.dataStreamName)
if err != nil {
errs = append(errs, fmt.Errorf("failed to load mappings from ES (data stream %s): %w", v.dataStreamName, err))
return errs
}

logger.Debugf("Simulate Index Template (%s)", v.indexTemplateName)
previewDynamicTemplates, previewMappings, err := v.esClient.SimulateIndexTemplate(ctx, v.indexTemplateName)
previewMappings, err := v.esClient.SimulateIndexTemplate(ctx, v.indexTemplateName)
if err != nil {
errs = append(errs, fmt.Errorf("failed to load mappings from index template preview (%s): %w", v.indexTemplateName, err))
return errs
Expand All @@ -124,7 +124,7 @@ func (v *MappingValidator) ValidateIndexMappings(ctx context.Context) multierror
}))

// Compare dynamic templates, this should always be the same in preview and after ingesting documents
if diff := cmp.Diff(previewDynamicTemplates, actualDynamicTemplates, transformJSON); diff != "" {
if diff := cmp.Diff(previewMappings.DynamicTemplates, actualMappings.DynamicTemplates, transformJSON); diff != "" {
errs = append(errs, fmt.Errorf("dynamic templates are different (data stream %s):\n%s", v.dataStreamName, diff))
}

Expand All @@ -138,26 +138,26 @@ func (v *MappingValidator) ValidateIndexMappings(ctx context.Context) multierror
// - if it does not match, there should be some issue and it should be reported
// - If the mapping is a constant_keyword type (e.g. data_stream.dataset), how to check the value?
// - if the constant_keyword is defined in the preview, it should be the same
if diff := cmp.Diff(actualMappings, previewMappings, transformJSON); diff == "" {
if diff := cmp.Diff(actualMappings.Properties, previewMappings.Properties, transformJSON); diff == "" {
logger.Debug("No changes found in mappings")
return errs.Unique()
}

var rawPreview map[string]any
err = json.Unmarshal(previewMappings, &rawPreview)
err = json.Unmarshal(previewMappings.Properties, &rawPreview)
if err != nil {
errs = append(errs, fmt.Errorf("failed to unmarshal preview mappings (index template %s): %w", v.indexTemplateName, err))
return errs.Unique()
}
var rawActual map[string]any
err = json.Unmarshal(actualMappings, &rawActual)
err = json.Unmarshal(actualMappings.Properties, &rawActual)
if err != nil {
errs = append(errs, fmt.Errorf("failed to unmarshal actual mappings (data stream %s): %w", v.dataStreamName, err))
return errs.Unique()
}

var rawDynamicTemplates []map[string]any
err = json.Unmarshal(actualDynamicTemplates, &rawDynamicTemplates)
err = json.Unmarshal(actualMappings.DynamicTemplates, &rawDynamicTemplates)
if err != nil {
errs = append(errs, fmt.Errorf("failed to unmarshal actual dynamic templates (data stream %s): %w", v.dataStreamName, err))
return errs.Unique()
Expand Down