Skip to content

Commit

Permalink
MergeFleetPolicy implementation using readConfigurationContent
Browse files Browse the repository at this point in the history
  • Loading branch information
dustmop committed Dec 10, 2024
1 parent 7ba5267 commit ec47cee
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 8 deletions.
15 changes: 12 additions & 3 deletions pkg/config/nodetreemodel/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -634,7 +634,7 @@ func (c *ntmConfig) MergeConfig(in io.Reader) error {
}

other := newInnerNode(nil)
if err = c.readConfigurationContent(other, content); err != nil {
if err = c.readConfigurationContent(other, model.SourceFile, content); err != nil {
return err
}

Expand Down Expand Up @@ -663,8 +663,17 @@ func (c *ntmConfig) MergeFleetPolicy(configPath string) error {
}
defer in.Close()

// TODO: Implement merging, merge in the policy that was read
return c.logErrorNotImplemented("MergeFleetPolicy")
content, err := io.ReadAll(in)
if err != nil {
return err
}

other := newInnerNode(nil)
if err = c.readConfigurationContent(other, model.SourceFleetPolicies, content); err != nil {
return err
}

return c.root.Merge(other)
}

// AllSettings returns all settings from the config
Expand Down
15 changes: 15 additions & 0 deletions pkg/config/nodetreemodel/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -575,3 +575,18 @@ func TestUnsetForSource(t *testing.T) {
val:4, source:default`
assert.Equal(t, expect, txt)
}

func TestMergeFleetPolicy(t *testing.T) {
config := NewConfig("test", "TEST", strings.NewReplacer(".", "_")) // nolint: forbidigo
config.SetConfigType("yaml")
config.Set("foo", "bar", model.SourceFile)

file, err := os.CreateTemp("", "datadog.yaml")
assert.NoError(t, err, "failed to create temporary file: %w", err)
file.Write([]byte("foo: baz"))
err = config.MergeFleetPolicy(file.Name())
assert.NoError(t, err)

assert.Equal(t, "baz", config.Get("foo"))
assert.Equal(t, model.SourceFleetPolicies, config.GetSource("foo"))
}
10 changes: 5 additions & 5 deletions pkg/config/nodetreemodel/read_config_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func (c *ntmConfig) ReadConfig(in io.Reader) error {
if err != nil {
return err
}
if err := c.readConfigurationContent(c.file, content); err != nil {
if err := c.readConfigurationContent(c.file, model.SourceFile, content); err != nil {
return err
}
return c.mergeAllLayers()
Expand All @@ -78,10 +78,10 @@ func (c *ntmConfig) readInConfig(filePath string) error {
if err != nil {
return err
}
return c.readConfigurationContent(c.file, content)
return c.readConfigurationContent(c.file, model.SourceFile, content)
}

func (c *ntmConfig) readConfigurationContent(target InnerNode, content []byte) error {
func (c *ntmConfig) readConfigurationContent(target InnerNode, source model.Source, content []byte) error {
var inData map[string]interface{}

if strictErr := yaml.UnmarshalStrict(content, &inData); strictErr != nil {
Expand All @@ -90,7 +90,7 @@ func (c *ntmConfig) readConfigurationContent(target InnerNode, content []byte) e
return err
}
}
c.warnings = append(c.warnings, loadYamlInto(target, model.SourceFile, inData, "", c.schema)...)
c.warnings = append(c.warnings, loadYamlInto(target, source, inData, "", c.schema)...)
return nil
}

Expand Down Expand Up @@ -142,7 +142,7 @@ func loadYamlInto(dest InnerNode, source model.Source, inData map[string]interfa
// Both default and dest have a child but they conflict in type. This should never happen.
warnings = append(warnings, "invalid tree: default and dest tree don't have the same layout")
} else {
dest.InsertChildNode(key, newLeafNode(value, model.SourceFile))
dest.InsertChildNode(key, newLeafNode(value, source))
}
continue
}
Expand Down

0 comments on commit ec47cee

Please sign in to comment.