Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Interpolate facts in datadir setting #157 #160

Merged
merged 1 commit into from
Jun 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 4 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,7 @@ Naming/PredicateName:
- "have_"
Rails/I18nLocaleTexts:
Enabled: false

Metrics/ClassLength:
Exclude:
- "test/**/*.rb"
10 changes: 0 additions & 10 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ Layout/CaseIndentation:
Layout/EmptyLineAfterGuardClause:
Exclude:
- 'app/models/hiera_data/config.rb'
- 'app/models/hiera_data/hierarchy.rb'
- 'app/models/hiera_data/yaml_file.rb'

# Offense count: 2
Expand All @@ -48,13 +47,6 @@ Layout/EmptyLineAfterGuardClause:
Layout/EmptyLineBetweenDefs:
Exclude:
- 'test/models/hiera_data/config_test.rb'
- 'test/models/hiera_data/hierarchy_test.rb'

# Offense count: 1
# This cop supports safe auto-correction (--auto-correct).
Layout/EmptyLines:
Exclude:
- 'test/models/hiera_data/hierarchy_test.rb'

# Offense count: 2
# This cop supports safe auto-correction (--auto-correct).
Expand Down Expand Up @@ -146,7 +138,6 @@ Layout/MultilineArrayBraceLayout:
Layout/MultilineMethodCallIndentation:
Exclude:
- 'app/models/hiera_data/data_file.rb'
- 'app/models/hiera_data/hierarchy.rb'
- 'app/models/key.rb'

# Offense count: 13
Expand Down Expand Up @@ -175,7 +166,6 @@ Layout/SpaceInsideHashLiteralBraces:
Exclude:
- 'config/routes.rb'
- 'test/models/hiera_data/data_file_test.rb'
- 'test/models/hiera_data/hierarchy_test.rb'
- 'test/models/hiera_data/interpolation_test.rb'
- 'test/models/hiera_data/yaml_file_test.rb'
- 'test/models/hiera_data_test.rb'
Expand Down
2 changes: 1 addition & 1 deletion app/models/data_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def initialize(attributes = {})
end

def keys
@keys ||= hiera_data.keys_in_file(hierarchy.name, path).map do |key_name|
@keys ||= hiera_data.keys_in_file(hierarchy.name, path, facts: node&.facts).map do |key_name|
Key.new(environment:, name: key_name)
end
end
Expand Down
18 changes: 9 additions & 9 deletions app/models/hiera_data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def all_keys(facts)
keys = []
config.hierarchies.each do |hierarchy|
hierarchy.resolved_paths(facts:).each do |path|
file = DataFile.new(path: hierarchy.datadir.join(path), facts:)
file = DataFile.new(path: hierarchy.datadir(facts:).join(path), facts:)
keys.concat(file.keys)
end
end
Expand All @@ -41,22 +41,22 @@ def files_for(hierarchy_name, facts: {})

def file_attributes(hierarchy_name, path, facts: nil)
hierarchy = find_hierarchy(hierarchy_name)
file = DataFile.new(path: hierarchy.datadir.join(path), facts:)
file = DataFile.new(path: hierarchy.datadir(facts:).join(path), facts:)
{
exist: file.exist?,
writable: file.writable?,
replaced_from_git: file.replaced_from_git?
}
end

def keys_in_file(hierarchy_name, path)
def keys_in_file(hierarchy_name, path, facts: nil)
hierarchy = find_hierarchy(hierarchy_name)
DataFile.new(path: hierarchy.datadir.join(path)).keys
DataFile.new(path: hierarchy.datadir(facts:).join(path)).keys
end

def value_in_file(hierarchy_name, path, key, facts: {})
hierarchy = find_hierarchy(hierarchy_name)
file = DataFile.new(path: hierarchy.datadir.join(path), facts:)
file = DataFile.new(path: hierarchy.datadir(facts:).join(path), facts:)
file.content_for_key(key)
end

Expand All @@ -65,7 +65,7 @@ def search_key(hierarchy_name, key, facts: nil)
files = facts ? hierarchy.resolved_paths(facts:) : hierarchy.candidate_files
search_results = {}
files.each do |path|
file = DataFile.new(path: hierarchy.datadir.join(path), facts:)
file = DataFile.new(path: hierarchy.datadir(facts:).join(path), facts:)
search_results[path] = {
file_present: file.exist?,
file_writable: file.writable?,
Expand Down Expand Up @@ -95,13 +95,13 @@ def files_including(key)

def write_key(hierarchy_name, path, key, value, facts: {})
hierarchy = find_hierarchy(hierarchy_name)
read_file = DataFile.new(path: hierarchy.datadir.join(path), facts:)
read_file = DataFile.new(path: hierarchy.datadir(facts:).join(path), facts:)
read_file.write_key(key, value)
end

def remove_key(hierarchy_name, path, key, facts: {})
hierarchy = find_hierarchy(hierarchy_name)
read_file = DataFile.new(path: hierarchy.datadir.join(path), facts:)
read_file = DataFile.new(path: hierarchy.datadir(facts:).join(path), facts:)
read_file.remove_key(key)
end

Expand All @@ -128,7 +128,7 @@ def lookup_options(facts)
result = {}
config.hierarchies.each do |hierarchy|
hierarchy.resolved_paths(facts:).each do |path|
file = DataFile.new(path: hierarchy.datadir.join(path))
file = DataFile.new(path: hierarchy.datadir(facts:).join(path))
result = (file["lookup_options"] || {}).merge(result)
end
end
Expand Down
9 changes: 6 additions & 3 deletions app/models/hiera_data/hierarchy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,12 @@ def backend
end
end

def datadir
def datadir(facts: nil)
return @datadir if @datadir
path = Pathname.new(raw_hash["datadir"])

raw_datadir = raw_hash["datadir"]
raw_datadir = Interpolation.interpolate_facts(path: raw_datadir, facts:) if facts
path = Pathname.new(raw_datadir)
if path.absolute?
path
else
Expand Down Expand Up @@ -71,7 +74,7 @@ def resolved_paths(facts:)
resolved_path = Interpolation.interpolate_facts(path:, facts:)
if uses_globs?
resolved_path = Interpolation
.interpolate_globs(path: resolved_path, datadir:)
.interpolate_globs(path: resolved_path, datadir:)
end
resolved_path
end
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
hdm::float: 0.1
hdm::integer: 1
foobar::enable_firstrun: false
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
hdm::float: 0.3
hdm::integer: 4
foobar::enable_firstrun: true
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
version: 5
defaults:
data_hash: yaml_data

hierarchy:
- name: "dynamic hierarchy"
datadir: "%{facts.custom.datadir}"
paths:
- "nodes/%{::facts.fqdn}.yaml"
- "common.yaml"
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
fqdn: 'caegfj58.betadots.training'
role: 'hdm_test'
environment: 'dynamic_datadir'
zone: 'internal'
custom:
datadir: 'data1'
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
fqdn: 'kq8l8lbw.betadots.training'
role: 'hdm_test'
environment: 'dynamic_datadir'
zone: 'internal'
custom:
datadir: 'data2'
1 change: 1 addition & 0 deletions test/models/environment_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ class EnvironmentTest < ActiveSupport::TestCase
test "::all lists the environments" do
expected_environments = %w(
development
dynamic_datadir
eyaml
globs
hdm
Expand Down
27 changes: 19 additions & 8 deletions test/models/hiera_data/hierarchy_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@
class HieraData::HierarchyTest < ActiveSupport::TestCase
test "#uses_globs? returns true if `glob` key present" do
glob = "/*/singular/glob"
raw_hash = {"name" => "Singular path", "glob" => glob}
raw_hash = { "name" => "Singular path", "glob" => glob }
hierarchy = HieraData::Hierarchy.new(raw_hash:, base_path: ".")
assert hierarchy.uses_globs?
end

test "#uses_globs? returns true if `globs` key present" do
globs = ["/*/array/globs", "/test/**/globs"]
raw_hash = {"name" => "Singular path", "globs" => globs}
raw_hash = { "name" => "Singular path", "globs" => globs }
hierarchy = HieraData::Hierarchy.new(raw_hash:, base_path: ".")
assert hierarchy.uses_globs?
end

test "#paths supports the singular `path` setting" do
path = "/test/singular/path"
raw_hash = {"name" => "Singular path", "path" => path}
raw_hash = { "name" => "Singular path", "path" => path }
hierarchy = HieraData::Hierarchy.new(raw_hash:, base_path: ".")
assert_equal [path], hierarchy.paths
end
Expand All @@ -41,14 +41,14 @@ class HieraData::HierarchyTest < ActiveSupport::TestCase

test "#paths supports the singular `glob` setting" do
glob = "/*/singular/glob"
raw_hash = {"name" => "Singular path", "glob" => glob}
raw_hash = { "name" => "Singular path", "glob" => glob }
hierarchy = HieraData::Hierarchy.new(raw_hash:, base_path: ".")
assert_equal [glob], hierarchy.paths
end

test "#paths supports the `globs` array setting" do
globs = ["/*/array/globs", "/test/**/globs"]
raw_hash = {"name" => "Singular path", "globs" => globs}
raw_hash = { "name" => "Singular path", "globs" => globs }
hierarchy = HieraData::Hierarchy.new(raw_hash:, base_path: ".")
assert_equal globs, hierarchy.paths
end
Expand All @@ -74,9 +74,9 @@ class HieraData::HierarchyTest < ActiveSupport::TestCase
test "#resolved_paths resolves globs" do
base_path = Rails.root.join("test/fixtures/files/puppet/environments/globs")
globs = ["common/*.yaml"]
raw_hash = {"name" => "Common", "datadir" => "data", "globs" => globs}
raw_hash = { "name" => "Common", "datadir" => "data", "globs" => globs }
hierarchy = HieraData::Hierarchy.new(raw_hash:, base_path:)
facts = {"fqdn" => "testhost"}
facts = { "fqdn" => "testhost" }
expected_resolved_paths = [
"common/foobar.yaml",
"common/hdm.yaml"
Expand Down Expand Up @@ -106,6 +106,18 @@ class HieraData::HierarchyTest < ActiveSupport::TestCase
assert_equal expected_candidate_files, hierarchy.candidate_files
end

test "#datadir uses facts to resolve datadir" do
raw_hash = {
"name" => "dynamic datadir",
"datadir" => "%{facts.datadir}"
}
facts = { "datadir" => "data1" }
base_path = Rails.root.join("test/fixtures/files/puppet/environments/dynamic_datadir")
hierarchy = HieraData::Hierarchy.new(raw_hash:, base_path:)

assert_equal base_path.join("data1"), hierarchy.datadir(facts:)
end

def raw_hash
{
"name" => "Yaml hierarchy",
Expand Down Expand Up @@ -147,7 +159,6 @@ def raw_hash
end
end


class HieraData::HierarchyForEyamlDataTest < ActiveSupport::TestCase
setup do
@tmpdir = Dir.mktmpdir
Expand Down